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

#include "Display.hh"
#include "../Error.hh"

namespace graphics
{

Display::Display (const DisplayConfig &config) :
    CL_DisplayWindow(GRAPHICS_WINDOW_TITLE, config.resolution.width, config.resolution.height, config.fullscreen, true),
    config(config), 
    fullscreen_resolution(0, 0),
    update_timer(GRAPHICS_UPDATE_INTERVAL_MS), 
    target(NULL)
{
    // connect timer signal
    slots.connect(update_timer.sig_tick(), this, &Display::on_update);
    slots.connect(this->sig_resize(), this, &Display::on_window_resize);

    // set correct fullscreen resolution
    if (config.fullscreen) {
        fullscreen_resolution = config.resolution;

    } else {
        CL_DisplayMode best_mode = getBestMode();

        fullscreen_resolution = PixelDimensions(best_mode.get_resolution().width, best_mode.get_resolution().height);
    }
}

const std::vector<CL_DisplayMode> & Display::getDisplayModes (void) {
    return CL_DisplayMode::get_display_modes();
}

const CL_DisplayMode Display::getBestMode (void) {
    const std::vector<CL_DisplayMode> &modes = getDisplayModes();

    const CL_DisplayMode *best_mode = NULL;
    
    for (std::vector<CL_DisplayMode>::const_iterator it = modes.begin(); it != modes.end(); it++)
        if (best_mode == NULL || (
                it->get_resolution().width * it->get_resolution().height > 
                best_mode->get_resolution().width * best_mode->get_resolution().height    
        ))
            best_mode = &*it;
    
    if (best_mode == NULL)
        throw Error("No available video modes!");
    
    return *best_mode;
}

void Display::toggle_fullscreen (void) {
    if (is_fullscreen()) {
        // enter windowed mode
        set_windowed();

    } else {
        // enter fullscreen mode
        set_fullscreen(fullscreen_resolution.width, fullscreen_resolution.height);
    }
}

void Display::on_update (TimeMS dt) {
    (void) dt;

    // do we have something to draw?
    if (target) {
        // draw it
        target->draw(*this);

        // flip buffers, sync
        flip(1);
    }
}

void Display::on_window_resize (int new_x, int new_y) {
    (void) new_x;
    (void) new_y;

    // ignore resize in fullscreen mode
    if (is_fullscreen())
        return;
}


}