pyGAP / game / src / platform / x11 / application_x11.cpp

#include "platform/x11/application_x11.h"

#include "platform/x11/platform_x11.h"
#include "platform/x11/clock.h"
#include "platform/x11/display.h"
#include "platform/x11/window_glx.h"

#include "input/input_device.h"
#include "input/input.h"
#include "input/command.h"

#include "input/x11/mouse.h"
#include "input/x11/keyboard.h"
#include "input/command_manager.h"

#include "io/settings_display.h"
#include "common/log.h"


namespace platform {
class GameLoop;
}


namespace platform { namespace x11 {

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


ApplicationX11::ApplicationX11(GameLoop* game_loop):
    platform::Application(new PlatformX11, game_loop),
    window_(0),
    display_(0)
{}


ApplicationX11::~ApplicationX11()
{}


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

    if (init_display()) {
        LOG("Failed to init display\n");
        return 1;
    }

    window_ = new WindowGLX(display_->get_display(), display_->get_root_window());
    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())
        LOG("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) {
            LOG("Failed to switch to %i x %i video mode\n", width, height)
            LOG("Fall back to windowed mode\n")
            fullscreen = false;
        }
        else
        {
            LOG("Switching to video mode %i...\n", mode)
            display_->set_video_mode(mode);
        }
    }

    // Store current display settings (will be saved on quit)
    display_settings_.width = width;
    display_settings_.height = height;
    display_settings_.fullscreen = fullscreen;

    if (init_window()) {
        LOG("Failed to init window\n")
        return 1;
    }

    if (init_renderer()) {
        LOG("Failed to init renderer\n");
        return 1;
    }

    if (init_clock()) {
        LOG("Failed to init clock\n");
        return 1;
    }

    if (init_input_devices()) {
        LOG("Failed to init input devices\n")
        return 1;
    }

    if (init_commands()) {
        LOG("Failed to init commands\n")
        return 1;
    }
    return 0;
}


int ApplicationX11::init_display()
{
    display_ = static_cast< PlatformX11* >(get_platform())->create_display();
    if (!display_) {
        LOG("Failed to init display\n")
        return 1;
    }
    return 0;
}


int ApplicationX11::init_window()
{
    return window_->init(display_settings_.width,
        display_settings_.height,
        display_settings_.fullscreen);
}


int ApplicationX11::init_input_devices()
{
    //TODO: get all available devices
    num_input_devices_ = 1;
    input_devices_ = reinterpret_cast< platform::InputDevice** >(new input::InputDevice*[num_input_devices_]);
    input_devices_[0] = get_platform()->create_input_device(0);

    //input_devices_[1] = get_platform()->create_input_device(1);
    return 0;
}


int ApplicationX11::init_commands()
{
    // virtual, commands are game dependant and must be defined in GameX11
}


int ApplicationX11::init_renderer() {
    renderer_ = get_platform()->create_renderer();
    if (!renderer_) {
        LOG("Failed to init renderer\n")
        return 1;
    }
    return 0;
}


int ApplicationX11::init_clock() {
    clock_ = get_platform()->create_clock();
    if (!clock_) {
        LOG("Failed to init clock\n")
        return 1;
    }
    return 0;
}


void ApplicationX11::update() {

    PlatformX11* platform = reinterpret_cast< PlatformX11* >(get_platform());
    platform->update_key_map();

    //static_cast< input::x11::Mouse* >(mouse_)->update(window_->get_window());

    window_->manage_events();

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


void ApplicationX11::display() {
    static_cast< WindowGLX* >(window_)->display();
}


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

    delete window_;
    window_ = NULL;

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

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

/*
platform::Clock* ApplicationX11::get_clock() {
    if (clock_)
        return clock_;

    PlatformX11* platform = reinterpret_cast< PlatformX11* >(get_platform());
    clock_ = reinterpret_cast< Clock* >(platform->create_clock());

    return clock_;
}


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

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

    return display_;
}
*/

} /* namespace x11 */ } /* namespace platform */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.