src/Graphics/Display.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 23:07:22 +0200
branchnew_graphics
changeset 412 721c60072091
parent 411 106aaf6eadfe
child 413 7dddc163489a
permissions -rw-r--r--
new graphics code compiles... no, it doesn't work yet
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include "Display.hh"
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
     3
#include "../Error.hh"
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
namespace graphics
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
{
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
     8
Display::Display (const DisplayConfig &config) :
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
     9
    CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, config.resolution.width, config.resolution.height, config.fullscreen, true),
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    10
    config(config), 
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    11
    fullscreen_resolution(0, 0),
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    12
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS), 
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    13
    target(NULL)
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    14
{
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    15
    // connect timer signal
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    16
    slots.connect(update_timer.sig_tick(), this, &Display::on_update);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    17
    slots.connect(this->sig_resize(), this, &Display::on_window_resize);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    18
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    19
    // set correct fullscreen resolution
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    20
    if (config.fullscreen) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    21
        fullscreen_resolution = config.resolution;
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    22
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    23
    } else {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    24
        CL_DisplayMode best_mode = getBestMode();
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    25
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    26
        fullscreen_resolution = PixelDimensions(best_mode.get_resolution().width, best_mode.get_resolution().height);
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    27
    }
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    28
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    29
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    30
const std::vector<CL_DisplayMode> & Display::getDisplayModes (void) {
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    return CL_DisplayMode::get_display_modes();
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
}
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    34
const CL_DisplayMode Display::getBestMode (void) {
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    35
    const std::vector<CL_DisplayMode> &modes = getDisplayModes();
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    const CL_DisplayMode *best_mode = NULL;
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
    for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++)
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        if (best_mode == NULL || (
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
                it->get_resolution().width * it->get_resolution().height > 
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
                best_mode->get_resolution().width * best_mode->get_resolution().height    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        ))
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
            best_mode = &*it;
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    if (best_mode == NULL)
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        throw Error("No available video modes!");
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    return *best_mode;
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
}
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    52
void Display::toggle_fullscreen (void) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    53
    if (is_fullscreen()) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    54
        // enter windowed mode
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    55
        set_windowed();
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    56
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    57
    } else {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    58
        // enter fullscreen mode
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    59
        set_fullscreen(fullscreen_resolution.width, fullscreen_resolution.height);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    60
    }
410
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
}
41fd46cffc52 start working out the Graphics/* code, this is a long way from compiling, let alone working
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    63
void Display::on_update (TimeMS dt) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    64
    (void) dt;
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    65
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    66
    // do we have something to draw?
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    67
    if (target) {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    68
        // draw it
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    69
        target->draw(*this);
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    70
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    71
        // flip buffers, sync
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    72
        flip(1);
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    73
    }
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    74
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    75
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    76
void Display::on_window_resize (int new_x, int new_y) {
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    77
    (void) new_x;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    78
    (void) new_y;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    79
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    80
    // ignore resize in fullscreen mode
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    81
    if (is_fullscreen())
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    82
        return;
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    83
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    84
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    85
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    86
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    87