Source

pyGAP / game / src / platform / win32 / application_win32.cpp

Full commit
#include "platform/win32/application_win32.h"

#include "platform/win32/platform_win32.h"
#include "platform/win32/window_win32.h"
#include "platform/win32/window_wgl.h"
#include "platform/win32/display.h"

#include <cstdio>


namespace platform {
class GameLoop;
}


namespace platform { namespace win32 {


const char* ApplicationWin32::c_config_filename = "../settings/game.cfg";
const unsigned short ApplicationWin32::c_display_width = 640;
const unsigned short ApplicationWin32::c_display_height = 480;


ApplicationWin32::ApplicationWin32(GameLoop* game_loop):
    platform::Application(new PlatformWin32, game_loop),
    window_(0),
    display_(0)
{}


ApplicationWin32::~ApplicationWin32()
{}


int ApplicationWin32::init()
{
    int width(0), height(0);
    bool fullscreen(false);

    get_display();

    PlatformWin32* platform = reinterpret_cast< PlatformWin32* >(get_platform());
    window_ = new WindowWGL(platform->get_hinstance());

    if (!window_)
        return 1;

    //TODO: other parts of the app will have to access display settings (when changing/saving resolution)
    display_settings_.set_file(c_config_filename);

    if (display_settings_.read())
        printf("Error reading file %s\n", c_config_filename);
    else {
        width = display_settings_.width;
        height = display_settings_.height;
        fullscreen = display_settings_.fullscreen;
    }

    width = width >= c_display_width ? width : c_display_width;
    height = height >= c_display_height ? height : c_display_height;

    if (fullscreen)
    {
        // Check if fullscreen video mode is available for the given resolution
        int mode = display_->get_video_mode(width, height);
        if (!mode) {
            printf("Failed to switch to %i x %i video mode\n", width, height);
            printf("Fall back to windowed mode\n");
            fullscreen = false;
        }
        else
        {
            printf("Switching to video mode %i...\n", mode);
            display_->set_video_mode(mode);
        }
    }

    if (reinterpret_cast< PlatformWin32* >(get_platform())->register_input_devices())
        return 1;

    return window_->init(width, height, fullscreen);
}


void ApplicationWin32::update() {
    reinterpret_cast< PlatformWin32* >(get_platform())->update_inputs();

    window_->manage_events();

    if (window_->is_deleted())
        request_exit();
}


void ApplicationWin32::display() {
    static_cast< WindowWGL* >(window_)->display();
}


void ApplicationWin32::finish() {
    if (!window_)
        return;

    delete window_;
    window_ = NULL;

    // Save current display settings
    display_settings_.write();

    display_->set_video_mode(); // restore original mode
}


Display* ApplicationWin32::get_display() {
    if (display_)
        return display_;

    PlatformWin32* platform = reinterpret_cast< PlatformWin32* >(get_platform());
    display_ = reinterpret_cast< Display* >(platform->create_display());

    return display_;
}


} /* namespace win32 */ } /* namespace platform */