src/Graphics/Display.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 23:25:29 +0200
branchnew_graphics
changeset 413 7dddc163489a
parent 412 721c60072091
child 414 cede5463b845
permissions -rw-r--r--
drawing the GameView works
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), 
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    13
    current_view(NULL)
411
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
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    19
    // enable timer
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    20
    update_timer.start();
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    21
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    22
    // 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
    23
    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
    24
        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
    25
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    26
    } else {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    27
        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
    28
412
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    29
        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
    30
    }
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    31
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    32
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    33
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
    34
    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
    35
}
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
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    37
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
    38
    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
    39
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
    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
    41
    
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
    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
    43
        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
    44
                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
    45
                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
    46
        ))
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
            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
    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
    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
    50
        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
    51
    
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
    52
    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
    53
}
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
    54
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    55
void Display::setView (View *view) {
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    56
    this->current_view = view;
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    57
    
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    58
    // resize to fill display
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    59
    if (view)
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    60
        view->resize(PixelArea(0, 0, get_width(), get_height()));
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    61
}
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
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::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
    64
    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
    65
        // 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
    66
        set_windowed();
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    67
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    68
    } else {
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    69
        // 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
    70
        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
    71
    }
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
    72
}
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
    73
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    74
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
    75
    (void) dt;
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    76
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    77
    // do we have something to draw?
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    78
    if (current_view) {
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    79
        // draw it
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    80
        current_view->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
    81
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    82
        // 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
    83
        flip(1);
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
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
    88
    (void) new_x;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    89
    (void) new_y;
721c60072091 new graphics code compiles... no, it doesn't work yet
Tero Marttila <terom@fixme.fi>
parents: 411
diff changeset
    90
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    91
    // 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
    92
    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
    93
        return;
413
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    94
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    95
    // resize view
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    96
    if (current_view)
7dddc163489a drawing the GameView works
Tero Marttila <terom@fixme.fi>
parents: 412
diff changeset
    97
        current_view->resize(PixelArea(0, 0, new_x, new_y));
411
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    98
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
    99
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   100
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   101
}
106aaf6eadfe there's a grain of truth in the new graphics code now...
Tero Marttila <terom@fixme.fi>
parents: 410
diff changeset
   102