1. Sylvain Rouquette
  2. pyGAP

Commits

Kévin Delbrayelle  committed acc45ab

input: added commands (x11)
application: minor changes

  • Participants
  • Parent commits ec707d1
  • Branches input

Comments (0)

Files changed (42)

File game/configure

View file
  • Ignore whitespace
         echo Using $CXX
     fi
     export WAFLOCK=.lock-waf$TARGET
-    ./waf configure $@ --target=$TARGET
+    echo ./waf configure $* --target=$TARGET
+    ./waf configure $* --target=$TARGET
 else
     echo Using default configuration
-    ./waf configure $@
+    echo ./waf configure $*
+    ./waf configure $*
 fi

File game/inc/game/game_loop_sample.h

View file
  • Ignore whitespace
 #ifndef _GAME_GAME_LOOP_SAMPLE_
 #define _GAME_GAME_LOOP_SAMPLE_
+
 #include "platform/game_loop.h"
 
 
 namespace platform {
 class Renderer;
 class Clock;
+class CommandManager;
 }
 
 
 namespace game {
+class CommandSample;
 
 
 class GameLoopSample : public platform::GameLoop
     ~GameLoopSample();
 
     void init();
-    int loop(platform::Renderer* renderer, platform::Clock* clock);
+    int loop(platform::Renderer* renderer, platform::Clock* clock, platform::CommandManager* commands);
     void finish();
 
 private:
-    int counter_;
-    float color_;
+    //CommandSample* commands_;
 };
 
 

File game/inc/game/game_x11.h

View file
  • Ignore whitespace
     GameX11(platform::GameLoop* game_loop);
     ~GameX11();
 
+    int init_commands();
+
 private:
     GameX11(const GameX11&);
     GameX11& operator=(const GameX11&);

File game/inc/game/input/command_sample.h

View file
  • Ignore whitespace
+#ifndef _SAMPLE_COMMAND_SAMPLE_
+#define _SAMPLE_COMMAND_SAMPLE_
+
+#include "input/command_manager.h"
+#include "common/singleton.h"
+
+
+namespace game {
+
+
+class CommandSample: public input::CommandManager
+{
+public:
+    enum command_sample_e {
+        QUIT,
+        PAUSE,
+        SWITCH,
+        MOVE_X,
+        MOVE_Y,
+        MAX
+    };
+
+/*
+    CommandSample(): input::CommandManager() {}
+    ~CommandSample() {}
+*/
+};
+
+
+} // namespace input
+
+
+#endif // _SAMPLE_COMMAND_SAMPLE_
+

File game/inc/game/state/state_factory.h

View file
  • Ignore whitespace
 class StateFactory: public state::Factory
 {
 public:
+    StateFactory();
+
     typedef state::State* (*creator_t)();
     typedef std::map< std::string, creator_t > creator_map_t;
 

File game/inc/game/state/state_game.h

View file
  • Ignore whitespace
     StateGame();
 
     void enter();
+    int translate(input::CommandManager* command);
     void update(unsigned int);
     void render(renderer::Renderer* renderer) const;
 
 private:
     float color_;
+    bool pause_;
 };
 
 

File game/inc/game/state/state_intro.h

View file
  • Ignore whitespace
 }
 
 
+namespace input {
+class CommandManager;
+}
+
+
 namespace game {
 
 
     StateIntro();
 
     void enter();
+    int translate(input::CommandManager* command);
     void update(unsigned int);
     void render(renderer::Renderer* renderer) const;
 
 private:
     float color_;
+    bool pause_;
 };
 
 

File game/inc/input/command.h

View file
  • Ignore whitespace
+#ifndef _INPUT_COMMAND_
+#define _INPUT_COMMAND_
+
+
+#include "input/input.h"
+
+namespace input {
+
+
+//template <class T, int max_inputs>
+class Command
+{
+public:
+    Command(Input* input): input_(input) {}
+    Command(): input_(0) {}
+    ~Command() {}
+
+    void update_input() { if (input_) input_->update(); }
+    void set_input(Input* input) { input_ = input; }
+    int get_value() { return input_ ? input_->get_value() : 0; }
+    bool is_triggered() { return input_ ? input_->is_triggered() : false; }
+    bool has_changed() { return input_ ? input_->has_changed() : false; }
+
+private:
+    Input* input_; // Input* inputs_[max_inputs];
+};
+
+
+} // namespace input
+
+
+#endif // _INPUT_COMMAND_

File game/inc/input/command_manager.h

View file
  • Ignore whitespace
+#ifndef _INPUT_COMMAND_MANAGER_
+#define _INPUT_COMMAND_MANAGER_
+
+
+#include "platform/command_manager.h"
+
+
+namespace input {
+class Command;
+class Input;
+
+
+class CommandManager: public platform::CommandManager
+{
+public:
+    CommandManager();
+    ~CommandManager();
+
+    void set_num_commands(int num_commands);
+    void update_inputs();
+
+    Command& operator [] (int index);
+    Command& operator [] (int index) const;
+
+private:
+    int num_commands_;
+    Command* commands_;
+};
+
+
+} // namespace input
+
+
+#endif // _INPUT_COMMAND_MANAGER_
+

File game/inc/input/input.h

View file
  • Ignore whitespace
+#ifndef _INPUT_INPUT_
+#define _INPUT_INPUT_
+
+#include "input/input_device.h"
+namespace input {
+class InputDevice;
+
+
+class Input
+{
+public:
+    Input(InputDevice* device, int index): device_(device), index_(index), last_value_(0), value_(0) {}
+    ~Input() {}
+
+    void update() { last_value_ = value_; value_ = device_->get_value(index_); }
+    int get_value() { return value_; }
+    int get_last_value() { return last_value_; }
+    bool is_triggered() { return value_ ? true : false; }
+    bool has_changed() { return (value_ != last_value_) ? true : false; }
+
+private:
+    InputDevice* device_;
+    int index_;
+
+    int last_value_;
+    int value_;
+};
+
+
+} // namespace input
+
+
+#endif // _INPUT_INPUT_

File game/inc/input/input_device.h

View file
  • Ignore whitespace
 #include "platform/input_device.h"
 
 
+namespace common {
+class Log;
+}
+
+
 namespace input {
-class Input;
 
 
 class InputDevice: public platform::InputDevice
     InputDevice() {}
     virtual ~InputDevice() {}
 
+    virtual int get_value(int index) = 0;
+    virtual void update() = 0;
+
 protected:
-    Input* input;
+#if defined(DEBUG)
+    static common::Log*    log_;
+#endif
 };
 
 

File game/inc/input/x11/keyboard.h

View file
  • Ignore whitespace
 
 #include <X11/Xlib.h>
 
+#include <cstdio>
+
 
 namespace input {
 namespace x11 {
 
     bool is_pressed(int key);
 
+    void update() {}
+    int get_value(int index);
+
     int get_pressed_key();
     int get_key_code(int key);
     void get_key_symbol(int key, char* name);

File game/inc/input/x11/mouse.h

View file
  • Ignore whitespace
     Mouse(::Display* display);
     ~Mouse();
 
+    int get_value(int index) {};
+
     bool is_pressed(int button);
 
-    void update(::Window window);
+    void update();
     int get_x();
     int get_y();
 
+    //TODO: move to private
+    ::Window window_;
+
 private:
     ::Display* display_;
 
+
     int x_;
     int y_;
 };

File game/inc/platform/application.h

View file
  • Ignore whitespace
 }
 
 
-namespace state {
-class Manager;
-}
-
-
 namespace platform {
 class Platform;
 class GameLoop;
+class Renderer;
+class Clock;
+class CommandManager;
 class InputDevice;
 
 
 
     void run();
 
+    Renderer* get_renderer() { return renderer_; }
+    Clock* get_clock() { return clock_; }
+
 protected:
     void request_exit();
     Platform* get_platform();
 
-    InputDevice* mouse_;
+    int num_input_devices_;
+    InputDevice** input_devices_; //TODO: move in Platform or GameLoop?
+    CommandManager* commands_; //TODO: move in GameLoop?
+
+    Renderer* renderer_;
+    Clock* clock_;
+
+#if defined(DEBUG)
+    static common::Log*    log_;
+#endif
 
 private:
     Application(const Application&);
     Application& operator=(const Application&);
 
     virtual int init() { return 0; }
-    virtual void update() {}
+    virtual void update();
     virtual void display() {}
     virtual void finish() {}
 
     GameLoop* game_loop_;
 
     bool exit_requested_;
-
-#if defined(DEBUG)
-    static common::Log*    log_;
-#endif
 };
 
 

File game/inc/platform/display.h

View file
  • Ignore whitespace
 #define _PLATFORM_DISPLAY_
 
 
+namespace common {
+class Log;
+}
+
+
 namespace platform {
 struct VideoMode;
 
     int cur_video_mode_;
     int num_video_modes_;
     VideoMode* video_modes_;
+
+#if defined(DEBUG)
+    static common::Log*    log_;
+#endif
 };
 
 

File game/inc/platform/game_loop.h

View file
  • Ignore whitespace
 #ifndef _PLATFORM_GAME_LOOP_
 #define _PLATFORM_GAME_LOOP_
 
-#include "platform/renderer.h"
-#include "platform/clock.h"
+//#include "platform/renderer.h"
+//#include "platform/clock.h"
 
 
 namespace platform {
+class Renderer;
+class Clock;
+class CommandManager;
 
 
 class GameLoop
     virtual ~GameLoop() {}
 
     virtual void init() {};
-    virtual int loop(Renderer* renderer, Clock* clock) = 0;
+    virtual int loop(Renderer* renderer, Clock* clock, CommandManager* commands) = 0;
     virtual void finish() {};
 
 private:

File game/inc/platform/input_device.h

View file
  • Ignore whitespace
 public:
     InputDevice() {}
     virtual ~InputDevice() {}
+
+    virtual void update() {}
 };
 
 

File game/inc/platform/window.h

View file
  • Ignore whitespace
 #define _PLATFORM_WINDOW_
 
 
+namespace common {
+class Log;
+}
+
+
 namespace platform {
 
 
     static const char* window_name_;
 
     bool deleted_;
+
+#if defined(DEBUG)
+    static common::Log*    log_;
+#endif
 };
 
 

File game/inc/platform/x11/application_x11.h

View file
  • Ignore whitespace
 class Clock;
 }
 
-//TODO: move this
-namespace input {
-namespace x11 {
-class Keyboard;
-class Mouse;
-}
-}
 
 namespace platform {
 namespace x11 {
     ~ApplicationX11();
 
     WindowX11* get_window();
-    Display* get_display();
-    Clock* get_clock();
+    //Display* get_display();
+    //Clock* get_clock();
 
 private:
     ApplicationX11(const ApplicationX11&);
     ApplicationX11& operator=(const ApplicationX11&);
 
+    int init_input_devices();
+    virtual int init_commands();
+    int init_display();
+    int init_renderer();
+    int init_clock();
+
     int init();
     void display();
     void update();
     void finish();
 
+    int init_window();
+
     WindowX11* window_;
     Display* display_;
-    Clock* clock_;
+    //Clock* clock_;
 
     io::SettingsDisplay display_settings_;
 

File game/inc/state/state_state.h

View file
  • Ignore whitespace
 class Log;
 }
 
+namespace input {
+class CommandManager;
+}
+
+
 namespace state {
 
 
     State();
     virtual ~State() {}
 
+    virtual void enter() {}
+    virtual void leave() {}
+
+    virtual int translate(input::CommandManager* command) {}
     virtual void update(unsigned int) = 0;
     virtual void render(renderer::Renderer* renderer) const = 0;
-    virtual void enter() {}
-    virtual void leave() {}
 
 protected:
 #if defined(DEBUG)
     common::Log*    log_;
 #endif
+
 };
 
 

File game/settings/game.cfg

View file
  • Ignore whitespace
 
 display:
 {
-    fullscreen = true;
-    size = { width = 1280; height = 720; };
+    fullscreen = false;
+    size = { width = 640; height = 480; };
     pixel_ratio = 1.0;
 };
 

File game/src/common/log.cpp

View file
  • Ignore whitespace
         context_length_ = LOG_BUFFER_SIZE / 2;
 
     char fmt[10];
-    sprintf(fmt, "[\%%ds] ", context_length_);
-    sprintf(buffer_, fmt, context);
+    sprintf(buffer_, "[%s] ", context);
     context_length_ = strlen(buffer_);
 }
 

File game/src/game/game_loop_sample.cpp

View file
  • Ignore whitespace
 #include "game/game_loop_sample.h"
 #include "game/state/state_manager.h"
+#include "game/input/command_sample.h"
 
 #include <cstdio>
 
 #include "text/language.h"
 #include "texts/test.h"
 
+#include "input/input.h"
 
 namespace game {
 
 
-GameLoopSample::GameLoopSample():
-    counter_(0),
-    color_(0.0)
+GameLoopSample::GameLoopSample()
 {}
 
 
 }
 
 
-int GameLoopSample::loop(platform::Renderer* renderer, platform::Clock* clock)
+int GameLoopSample::loop(platform::Renderer* renderer, platform::Clock* clock, platform::CommandManager* commands)
 {
     state::State* state = StateManager::instance()->get_current_state();
     time_t dt;
 
     //clock->usleep(500000);
 
+    static_cast< input::CommandManager* >(commands)->update_inputs();
     dt = clock->get_elapsed_time();
     clock->reset();
 
+    if (state->translate(static_cast< input::CommandManager* >(commands)))
+        return 1;
     state->update(dt);
     state->render(static_cast< renderer::Renderer* >(renderer));
 
 
 
 void GameLoopSample::finish()
-{}
+{
+    //delete commands_;
+}
 
 
 } // namespace game

File game/src/game/game_x11.cpp

View file
  • Ignore whitespace
 #include "platform/x11/platform_x11.h"
 
 
+#include "game/input/command_sample.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"
+
 namespace game {
 
 
 {}
 
 
+int GameX11::init_commands()
+{
+    //TODO: read config from file
+    input::CommandManager* commands = new input::CommandManager;
+    input::x11::Keyboard* keyboard = static_cast< input::x11::Keyboard* >(input_devices_[0]);
+    input::Input* i;
+
+    commands->set_num_commands(CommandSample::MAX);
+
+    i = new input::Input(keyboard, keyboard->get_key_code(input::Keyboard::KEY_ESC));
+    (*commands)[CommandSample::QUIT] = input::Command(i);
+
+    i = new input::Input(keyboard, keyboard->get_key_code(input::Keyboard::KEY_S));
+    (*commands)[CommandSample::SWITCH] = input::Command(i);
+
+    i = new input::Input(keyboard, keyboard->get_key_code(input::Keyboard::KEY_P));
+    (*commands)[CommandSample::PAUSE] = input::Command(i);
+
+    commands_ = commands;
+
+    return 0;
+}
+
+
 GameX11::~GameX11()
 {}
 

File game/src/game/state/state_factory.cpp

View file
  • Ignore whitespace
 #endif
 
 
-bool StateFactory::register_state(const char* name, creator_t creator)
+StateFactory::StateFactory()
 {
 #if defined(DEBUG)
     if (!log_)
-        log_ = common::Log::get("StateFactory");
+        log_ = common::Log::get(__FUNCTION__);
 #endif
+}
+
+
+bool StateFactory::register_state(const char* name, creator_t creator)
+{
+    LOG("Registering state %s\n", name);
 
     std::string str = std::string(name);
-
-    LOG("Registering state %s\n", name);
-
     return creators_.insert(creator_map_t::value_type(str, creator)).second;
 }
 

File game/src/game/state/state_game.cpp

View file
  • Ignore whitespace
 #include "game/state/state_game.h"
 #include "game/state/state_manager.h"
+#include "game/input/command_sample.h"
 
+#include "input/command_manager.h"
+#include "input/command.h"
 #include "renderer/renderer.h"
 
 #include "common/log.h"
 
 StateGame::StateGame() : state::State(), common::Singleton< StateGame >() {
 #if defined(DEBUG)
-    log_ = common::Log::get("StateGame");
+    log_ = common::Log::get(__FUNCTION__);
 #endif
 }
 
     LOG("Entering state game\n");
 
     color_ = 0.0;
+    pause_ = false;
+}
+
+
+int StateGame::translate(input::CommandManager* commands)
+{
+    input::Command* quit = &(*commands)[CommandSample::QUIT];
+    input::Command* sw = &(*commands)[CommandSample::SWITCH];
+    input::Command* pause = &(*commands)[CommandSample::PAUSE];
+
+    if (!quit->is_triggered() && quit->has_changed())
+        return 1;
+
+    if (!sw->is_triggered() && sw->has_changed())
+        StateManager::instance()->switch_to_state("intro");
+
+    if (pause->is_triggered() && pause->has_changed()) // is_triggered and has_changed means "has just been triggered"
+        pause_ = !pause_;
+
+    return 0;
 }
 
 
 void StateGame::update(unsigned int dt)
 {
+    if(pause_)
+        return;
+
     float dtf = (float(dt) / 1000000.0);
 
     color_ > 1.0 ? color_ = 0.0 : color_ += 1.0 * dtf;
-
 }
 
 

File game/src/game/state/state_intro.cpp

View file
  • Ignore whitespace
 #include "game/state/state_intro.h"
 #include "game/state/state_manager.h"
+#include "game/input/command_sample.h"
 
+#include "input/command_manager.h"
+#include "input/command.h"
 #include "renderer/renderer.h"
 
 #include "common/log.h"
 
+#include <cstdio>
 
 namespace game {
 
 
 StateIntro::StateIntro() : state::State(), common::Singleton< StateIntro >() {
 #if defined(DEBUG)
-    log_ = common::Log::get("StateIntro");
+    log_ = common::Log::get(__FUNCTION__);
 #endif
 }
 
     LOG("Entering state intro\n");
 
     color_ = 0.0;
+    pause_ = false;
+}
+
+
+int StateIntro::translate(input::CommandManager* commands)
+{
+    input::Command* sw = &(*commands)[CommandSample::SWITCH];
+    input::Command* pause = &(*commands)[CommandSample::PAUSE];
+
+    if (!sw->is_triggered()  && sw->has_changed())
+        StateManager::instance()->switch_to_state("game");
+
+    if (pause->is_triggered() && pause->has_changed()) // is_triggered and has_changed means "has just been triggered"
+        pause_ = !pause_;
+
+    return 0;
 }
 
 
 void StateIntro::update(unsigned int dt)
 {
-    if (color_ > 1.0) StateManager::instance()->switch_to_state("game");
+    //if (color_ > 1.0) StateManager::instance()->switch_to_state("game");
+    //StateManager::instance()->switch_to_state("game");
+    if (color_ == 0.0)
+        LOG("Update\n")
+
+    if(pause_)
+        return;
 
     float dtf = (float(dt) / 1000000.0);
     color_ > 1.0 ? color_ = 0.0 : color_ += 1.0 * dtf;

File game/src/input/command_manager.cpp

View file
  • Ignore whitespace
+#include "input/command_manager.h"
+#include "input/command.h"
+
+
+namespace input {
+
+
+CommandManager::CommandManager():
+    num_commands_(0),
+    commands_(0)
+{}
+
+
+CommandManager::~CommandManager()
+{
+    delete [] commands_;
+}
+
+
+void CommandManager::set_num_commands(int num_commands)
+{
+    num_commands_ = num_commands;
+    commands_ = new Command[num_commands];
+}
+
+
+void CommandManager::update_inputs()
+{
+    for (int i = 0; i < num_commands_; i++)
+    {
+        commands_[i].update_input();
+    }
+}
+
+
+Command& CommandManager::operator [] (int index)
+{
+    return commands_[index];
+}
+
+
+Command& CommandManager::operator [] (int index) const
+{
+    return commands_[index];
+}
+
+
+} // namespace input

File game/src/input/win32/keyboard.cpp

View file
  • Ignore whitespace
 {}
 
 
+void Keyboard::update()
+{
+    // nothing here, keymap is update by the application
+}
+
+
 int Keyboard::get_key_code(int key)
 {
     //int vkey = virtual_key_[key];

File game/src/input/x11/keyboard.cpp

View file
  • Ignore whitespace
 #include "input/x11/keyboard.h"
 
+#include "common/log.h"
 #include <X11/Xlib.h>
 #include <X11/keysym.h>
 
 #include <cstring>
-#include <cstdio>
 
 
 namespace input {
+
+#if defined(DEBUG)
+common::Log* InputDevice::log_ = NULL;
+#endif
+
 namespace x11 {
 
 
     display_(display),
     key_map_(key_map)
 {
-    printf("Keyboard created\n");
+#if defined(DEBUG)
+    if (!log_)
+        log_ = common::Log::get(__FUNCTION__);
+#endif
+    LOG("Keyboard created\n")
 }
 
 
 {}
 
 
+int Keyboard::get_value(int index)
+{
+    //TODO: define what is the range of value depending on various input devices
+    return is_pressed(index) ? 1 : 0;
+}
+
+
 int Keyboard::get_key_code(int key)
 {
 //TODO: precalculate all keycode/keysym at init so there's no need for having
 
     strcpy(name, key_symbol_string);
 
-    printf("Key code: 0x%02x\n", key_code);
-    printf("Key symbol: %s (0x%02x)(len: %i)\n", key_symbol_string, key_symbol, strlen(key_symbol_string));
+    LOG("Key code: 0x%02x\n", key_code)
+    LOG("Key symbol: %s (0x%02x)(len: %i)\n", key_symbol_string, key_symbol, strlen(key_symbol_string))
 }
 
 

File game/src/input/x11/mouse.cpp

View file
  • Ignore whitespace
 
 
 Mouse::Mouse(::Display* display):
-    display_(display)
+    display_(display),
+    window_(0)
 {}
 
 
 int Mouse::get_x() { return x_; }
 int Mouse::get_y() { return y_; }
 
-void Mouse::update(::Window window) {
+void Mouse::update() {
     ::Window root_window;
     ::Window child_window;
     int root_x, root_y;
     int win_x, win_y;
     unsigned int mask;
 
-    ::XQueryPointer(display_, window, &root_window, &child_window, &root_x, &root_y, &win_x, &win_y, &mask);
+    ::XQueryPointer(display_, window_, &root_window, &child_window, &root_x, &root_y, &win_x, &win_y, &mask);
     x_ = win_x - 160;
     y_ = win_y - 120;
 
-    ::XWarpPointer(display_, None, window, 0, 0, 0, 0, 160, 120);
+    ::XWarpPointer(display_, None, window_, 0, 0, 0, 0, 160, 120);
 }
 
 

File game/src/io/settings_display.cpp

View file
  • Ignore whitespace
 {
     config_setting_t* setting_p = 0;
 
-    FILE * stream = fopen (filename_, "r");
+    FILE * stream = fopen(filename_, "r");
     if (!stream)
         return 1;
 

File game/src/main/main_x11.cpp

View file
  • Ignore whitespace
 #include "game/game_x11.h"
 #include "game/game_loop_sample.h"
+#include "common/log.h"
 
+#include <cstdio>
 
 int main()
 {
 #if defined(DEBUG)
-    common::Log* log_ = common::Log::get("main");
+    common::Log* log_ = common::Log::get(__FUNCTION__);
 #endif
-    LOG("starting game...");
+    LOG("starting game...\n");
 
     game::GameLoopSample game_loop;
     game::GameX11 *game;

File game/src/platform/application.cpp

View file
  • Ignore whitespace
 Application::Application(Platform* platform, GameLoop* game_loop) :
     platform_(platform),
     game_loop_(game_loop),
-    exit_requested_(false)
+    exit_requested_(false),
+    num_input_devices_(0),
+    input_devices_(0),
+    renderer_(0),
+    clock_(0)
 {
 #if defined(DEBUG)
     if (!log_)
-        log_ = common::Log::get("Application");
+        log_ = common::Log::get(__FUNCTION__);
 #endif
 }
 
 }
 
 
+void Application::update()
+{
+    //Renderer* renderer = get_renderer();
+    //Clock* clock = get_clock();
+    LOG("Update\n")
+}
+
+
 void Application::run()
 {
     if (!init())
         LOG("Application succesfully initialized\n")
-        //printf("Application succesfully initialized\n");
     else
     {
-        LOG("Failed to initialize Application\n");
-        //printf("Failed to initialize Application\n");
+        LOG("Failed to initialize Application\n")
         return;
     }
 
-    //TODO: do not call create but get_xxx()
-    Renderer* renderer = get_platform()->create_renderer();
-    Clock* clock = get_platform()->create_clock();
-    input::Keyboard* keyboard =
-        static_cast< input::Keyboard* >(get_platform()->create_input_device(0));
-
-    int key_esc = keyboard->get_key_code(input::Keyboard::KEY_ESC);
-
-    input::Mouse* mouse = static_cast< input::Mouse* >(get_platform()->create_input_device(1));
-
-    mouse_ = mouse;
-
     game_loop_->init();
 
     while(!exit_requested_)
     {
         update();
+        for (int i = 0; i < num_input_devices_; i++)
+            input_devices_[i]->update();
 
-        int mouse_x = mouse->get_x();
-        int mouse_y = mouse->get_y();
+        if (game_loop_->loop(renderer_, clock_, commands_))
+            exit_requested_ = true;
 
-        if (mouse_x || mouse_y)
-            LOG("mouse: %3i %3i\n", mouse_x, mouse_y);
-
-        int key = keyboard->get_pressed_key();
-        if (key >= 0)
-        {
-            char key_symbol[32];
-            keyboard->get_key_symbol(key, key_symbol);
-            LOG("Key pressed: %s\n", key_symbol);
-        }
-
-        if (keyboard->is_pressed(key_esc)) exit_requested_ = true;
-
-        if (game_loop_->loop(renderer, clock))
-            exit_requested_ = true;
         display();
     }
 
     game_loop_->finish();
 
-    get_platform()->delete_renderer(renderer);
+    //get_platform()->delete_renderer(renderer);
 
     finish();
 }

File game/src/platform/window.cpp

View file
  • Ignore whitespace
 #include "platform/window.h"
 #include "platform/video_mode.h"
 
+#include "common/log.h"
+
 
 namespace platform {
+#if defined(DEBUG)
+common::Log* Window::log_ = NULL;
+#endif
 
 
 const char* Window::window_name_ = "Ca marche !(TM)";
     //video_modes_(0),
     deleted_(false)
 {
-
+#if defined(DEBUG)
+    if (!log_)
+        log_ = common::Log::get(__FUNCTION__);
+#endif
 }
 
 

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

View file
  • Ignore whitespace
 #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 {
 
 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),
-    clock_(0)
+    display_(0)
 {}
 
 
     int width(0), height(0);
     bool fullscreen(false);
 
-    window_ = new WindowGLX(get_display()); //TODO: Do not pass x11::Display but just ::Display and ::Window root_window
+    if (init_display()) {
+        LOG("Failed to init display\n");
+        return 1;
+    }
+
+    window_ = new WindowGLX(display_); //TODO: Do not pass x11::Display but just ::Display and ::Window root_window
     if (!window_)
         return 1;
 
     display_settings_.set_file(c_config_filename);
 
     if (display_settings_.read())
-        printf("Error reading file %s\n", c_config_filename);
+        LOG("Error reading file %s\n", c_config_filename)
     else {
         width = display_settings_.width;
         height = display_settings_.height;
         // 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");
+            LOG("Failed to switch to %i x %i video mode\n", width, height)
+            LOG("Fall back to windowed mode\n")
             fullscreen = false;
         }
         else
         {
-            printf("Switching to video mode %i...\n", mode);
+            LOG("Switching to video mode %i...\n", mode)
             display_->set_video_mode(mode);
         }
     }
     display_settings_.height = height;
     display_settings_.fullscreen = fullscreen;
 
-    return window_->init(width, height, 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()
+{
+    // vitual, 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;
 }
 
 
     PlatformX11* platform = reinterpret_cast< PlatformX11* >(get_platform());
     platform->update_key_map();
 
-    static_cast< input::x11::Mouse* >(mouse_)->update(window_->get_window());
+    //static_cast< input::x11::Mouse* >(mouse_)->update(window_->get_window());
 
     window_->manage_events();
 
     display_->set_video_mode(); // restore original mode
 }
 
-
+/*
 platform::Clock* ApplicationX11::get_clock() {
     if (clock_)
         return clock_;
 
     return display_;
 }
-
+*/
 
 } /* namespace x11 */ } /* namespace platform */

File game/src/platform/x11/display.cpp

View file
  • Ignore whitespace
 
 
 namespace platform {
+
+#if defined(DEBUG)
+common::Log* Display::log_ = NULL;
+#endif
+
 namespace x11 {
 
 
     root_window_(::XDefaultRootWindow(display_)),
     xrr_config_(::XRRGetScreenInfo(display_, root_window_))
 {
+#if defined(DEBUG)
+    if (!log_)
+        log_ = common::Log::get(__FUNCTION__);
+#endif
     if (!display_)
-        LOG("Failed to open X display\n");
+        LOG("Failed to open X display\n")
     else
         get_available_video_modes();
 }
 
     get_ratio(rw, rh);
     LOG("Screen original resolution is %4i x %4i (%i/%i)\n",
-        xrrs[original_mode].width, xrrs[original_mode].height, rw, rh);
+        xrrs[original_mode].width, xrrs[original_mode].height, rw, rh)
 
     // Register selected video modes from slot 1 to max
     for(int i = 0, slot = 1; (i < num_modes) && (slot < num_video_modes_); i++) {
     // Debug display of available resolutions
     for (int i = 0; i < num_video_modes_; i++) {
         LOG("%2i: mode %2i: %4i x %4i @ %3i Hz\n", i,
-        video_modes_[i].index,
-        video_modes_[i].width,
-        video_modes_[i].height,
-        video_modes_[i].rate);
+            video_modes_[i].index,
+            video_modes_[i].width,
+            video_modes_[i].height,
+            video_modes_[i].rate)
     }
 }
 

File game/src/platform/x11/platform_x11.cpp

View file
  • Ignore whitespace
 #include "platform/x11/platform_x11.h"
 
-#include <cstdio>
 #include <X11/Xlib.h>
 
 #include "platform/x11/clock.h"

File game/src/platform/x11/window_events.cpp

View file
  • Ignore whitespace
 #include "platform/x11/window_x11.h"
 #include "platform/x11/display.h"
 
+#include "common/log.h"
 #include <X11/Xlib.h>
-#include <cstdio>
 
 
 namespace platform {
             event.xclient.format == 32 &&
             (::Atom)(event.xclient.data.l[0]) == wm_delete_)
         {
-            printf("Window close requested...\n");
+            LOG("Window close requested...\n")
             deleted_ = true;
         }
     }
     while (::XCheckWindowEvent(display, window_, event_mask, &event)) {
         if ((event.type == KeyPress) || (event.type == KeyRelease)) {
             if (event.xkey.keycode < 256)
-                printf("Keycode: 0x%02x\n", event.xkey.keycode);
+                LOG("Keycode: 0x%02x\n", event.xkey.keycode)
         }
         else if (event.type == ButtonPress)
-            printf("Button pressed...\n");
+            LOG("Button pressed...\n")
         else if (event.type == Expose) {
             ::XExposeEvent *ev = reinterpret_cast<XExposeEvent *>(&event);
-            printf("Exposition...\n");
-            printf("New size is %i x %i\n",ev->width, ev->height);
+            LOG("Exposition...\n")
+            LOG("New size is %i x %i\n",ev->width, ev->height)
         }
         else if (event.type == FocusIn) {
             ::Cursor cursor = make_blank_cursor(); //TODO: move outside event loop
             ::XGrabPointer(display, window_, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, window_, cursor, CurrentTime);
-            printf("Focus in\n");
+            LOG("Focus in\n")
         }
         else if (event.type == FocusOut) {
             ::XUngrabPointer(display, CurrentTime);
-            printf("Focus out\n");
+            LOG("Focus out\n")
         }
     }
     return 0;

File game/src/platform/x11/window_glx.cpp

View file
  • Ignore whitespace
 #include "platform/x11/window_glx.h"
 #include "platform/x11/display.h"
 
-
+#include "common/log.h"
 #include <GL/glx.h>
 
 
-namespace platform { namespace x11 {
+namespace platform {
+namespace x11 {
 
 
 WindowGLX::WindowGLX(Display* display):
     {
         int glx_major, glx_minor;
         ::glXQueryVersion(disp, &glx_major, &glx_minor);
-        printf("GLX version %d.%d\n", glx_major, glx_minor);
+        LOG("GLX version %d.%d\n", glx_major, glx_minor)
     }
 }
 
     int x(0), y(0);
 
     if (!display) {
-        printf("No open X display\n");
+        LOG("No open X display\n")
         return 1;
     }
 
         &attributes);
 
     if (window_ == 0) {
-        printf("Failed to create window\n");
+        LOG("Failed to create window\n")
         return 1;
     }
 
     }
     else {
         if ((wm_protocol_ = ::XInternAtom(display, "WM_PROTOCOLS", False)) == None) {
-            printf("Failed to look up WM_PROTOCOLS\n");
+            LOG("Failed to look up WM_PROTOCOLS\n")
             return 1;
         }
 
             ::XSetWMProtocols(display, window_, &wm_delete_, 1);
     }
     //::XFlush(display_);
-    printf("Window created\n");
+    LOG("Window created\n")
 
     return 0;
 }
 
     gl_context_ = ::glXCreateContext(display, visual_info_, NULL, true);
     if (!gl_context_) {
-        printf( "Failed to create rendering context\n" );
+        LOG("Failed to create rendering context\n")
         return 1;
     }
-    printf( "Rendering context created\n" );
+    LOG("Rendering context created\n")
 
     if (::glXIsDirect(display, gl_context_))
-       printf("DRI enabled\n");
+       LOG("DRI enabled\n")
     else
-       printf("no DRI available\n");
+       LOG("No DRI available\n")
 
     return 0;
 }
     ::Display* display = display_->get_display();
 
     if (!::glXMakeCurrent(display, window_, gl_context_)) {
-        printf("Failed to make rendering context current\n");
+        LOG("Failed to make rendering context current\n")
         return 1;
     }
     return 0;
     ::Display* display = display_->get_display();
 
     ::glXDestroyContext(display, gl_context_);
-    printf("Rendering context destroyed\n");
+    LOG("Rendering context destroyed\n")
 }
 
 
     ::XDestroyWindow(display, window_);
     window_ = 0;
 
-    printf("Window destroyed\n");
+    LOG("Window destroyed\n")
     ::XFreeColormap(display, color_map_);
 }
 

File game/src/platform/x11/window_x11.cpp

View file
  • Ignore whitespace
 #include "platform/x11/display.h"
 #include "platform/video_mode.h"
 
-#include <cstdio>
+#include "common/log.h"
 #include <X11/Xlib.h>
 #include <X11/extensions/Xrandr.h>
 
 
     blank = ::XCreateBitmapFromData(display, root_window, data, 1, 1);
     if (blank == None)
-        printf("Failed to create blank cursor\n");
+        LOG("Failed to create blank cursor\n")
     cursor = ::XCreatePixmapCursor(display, blank, blank, &dummy, &dummy, 0, 0);
     ::XFreePixmap(display, blank);
 

File game/wscript

View file
  • Ignore whitespace
         conf.env.cxxflags = ['-m64' if conf.env.m64 else '-m32']
         conf.env.linkflags = conf.env.cxxflags
         if conf.options.debug:
-            conf.env.cxxflags.append('-g')
+            conf.env.cxxflags.extend(('-g', '-DDEBUG'))
 
     if conf.options.debug:
         conf.env.cxxflags.append('-DDEBUG')
                                          src/codec/**/*.cpp
                                          src/common/**/*.cpp
                                          src/game/state/*.cpp
+                                         src/game/input/*.cpp
                                          src/game/game_loop_sample.cpp
                                          src/game/game_%(platform)s.cpp
                                          src/input/*.cpp