Commits

Kévin Delbrayelle committed b6b159e

input (x11)

Comments (0)

Files changed (20)

game/inc/game/game_loop_sample.h

 #define _GAME_GAME_LOOP_SAMPLE_
 
 #include "platform/game_loop.h"
+//#include "game/input/command_sample.h"
+
+
+namespace input {
+class CommandManager;
+}
 
 
 namespace platform {
 
 
 namespace game {
-class CommandSample;
+//class CommandSample;
 
 
 class GameLoopSample : public platform::GameLoop
     GameLoopSample();
     ~GameLoopSample();
 
+    input::CommandManager* get_commands();
+
     void init();
-    int loop(platform::Renderer* renderer, platform::Clock* clock, platform::CommandManager* commands);
+    int loop(platform::Renderer* renderer, platform::Clock* clock);
     void finish();
 
 private:
-    //CommandSample* commands_;
+    input::CommandManager* commands_;
 };
 
 

game/inc/game/game_x11.h

 #define _GAME_GAME_X11_
 
 #include "platform/x11/application_x11.h"
+#include "game/input/settings_commands.h"
 
 
 namespace platform {
 private:
     GameX11(const GameX11&);
     GameX11& operator=(const GameX11&);
+
+    SettingsCommands commands_settings_;
 };
 
 

game/inc/game/input/command_sample.h

         MAX
     };
 
-/*
-    CommandSample(): input::CommandManager() {}
+    CommandSample(): input::CommandManager(MAX) {}
     ~CommandSample() {}
-*/
 };
 
 

game/inc/input/command_manager.h

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

game/inc/platform/application.h

 protected:
     void request_exit();
     Platform* get_platform();
+    GameLoop* get_game_loop();
 
     int num_input_devices_;
     InputDevice** input_devices_; //TODO: move in Platform or GameLoop?

game/inc/platform/game_loop.h

 #ifndef _PLATFORM_GAME_LOOP_
 #define _PLATFORM_GAME_LOOP_
 
-//#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, CommandManager* commands) = 0;
+    virtual int loop(Renderer* renderer, Clock* clock) = 0;
     virtual void finish() {};
 
 private:

game/inc/platform/x11/application_x11.h

     ~ApplicationX11();
 
     WindowX11* get_window();
-    //Display* get_display();
-    //Clock* get_clock();
 
 private:
     ApplicationX11(const ApplicationX11&);
 
     WindowX11* window_;
     Display* display_;
-    //Clock* clock_;
 
+    //TODO: move SettingsDisplay from io to platform::x11?
+    // because every platform does not necessary need the same settings
+    // information
     io::SettingsDisplay display_settings_;
 
     static const char* c_config_filename;

game/inc/platform/x11/window_glx.h

 #ifndef _PLATFORM_X11_WINDOW_GLX_
 #define _PLATFORM_X11_WINDOW_GLX_
 
-#include <cstdio>
+#include "platform/x11/window_x11.h"
+
+#include <X11/Xlib.h>
 #include <GL/glx.h>
 
 
-#include "platform/x11/window_x11.h"
-
-
 namespace platform { namespace x11 {
 
 
 friend class PlatformX11;
 
 public:
-    WindowGLX(Display* display);
+    WindowGLX(::Display* display, ::Window root_window);
     virtual ~WindowGLX();
 
     virtual int init(int width, int height, bool fullscreen);

game/inc/platform/x11/window_x11.h

 class WindowX11: public Window
 {
 public:
-    WindowX11(Display* display);
+    WindowX11(::Display* display, ::Window root_window);
     virtual ~WindowX11();
 
     int manage_events();
     void remove_cursor();
     void restore_cursor();
 
-    Display* display_;
-
     // X Window related members
-    //::Display       display_;
-    //::Window        root_window_;
+    ::Display*      display_;
+    ::Window        root_window_;
     ::Window        window_;
     ::Colormap      color_map_;
     ::XVisualInfo   *visual_info_;

game/src/game/game_loop_sample.cpp

 
 #include <cstdio>
 
+//#include "input/command_manager.h"
 #include "renderer/renderer.h"
 #include "platform/clock.h"
 #include "state/state_state.h"
 namespace game {
 
 
-GameLoopSample::GameLoopSample()
+GameLoopSample::GameLoopSample(): commands_(new CommandSample)
 {}
 
 
 {}
 
 
+input::CommandManager* GameLoopSample::get_commands()
+{
+    return commands_;
+}
+
+
 void GameLoopSample::init()
 {
     using namespace ::text;
 }
 
 
-int GameLoopSample::loop(platform::Renderer* renderer, platform::Clock* clock, platform::CommandManager* commands)
+int GameLoopSample::loop(platform::Renderer* renderer, platform::Clock* clock)
 {
     state::State* state = StateManager::instance()->get_current_state();
     time_t dt;
 
     //clock->usleep(500000);
 
-    static_cast< input::CommandManager* >(commands)->update_inputs();
+    commands_->update();
     dt = clock->get_elapsed_time();
     clock->reset();
 
-    if (state->translate(static_cast< input::CommandManager* >(commands)))
+    if (state->translate(commands_))
         return 1;
     state->update(dt);
     state->render(static_cast< renderer::Renderer* >(renderer));
 
 void GameLoopSample::finish()
 {
-    //delete commands_;
+    delete commands_;
 }
 
 

game/src/game/game_x11.cpp

 #include "game/game_x11.h"
 #include "platform/x11/platform_x11.h"
 
-
+#include "game/game_loop_sample.h"
 #include "game/input/command_sample.h"
 
-#include "input/input_device.h"
 #include "input/input.h"
 #include "input/command.h"
+#include "input/command_manager.h"
 
 #include "input/x11/mouse.h"
 #include "input/x11/keyboard.h"
-#include "input/command_manager.h"
+
 
 namespace game {
 
 
 int GameX11::init_commands()
 {
+    GameLoopSample* game_loop = static_cast< GameLoopSample* >(get_game_loop());
+    input::CommandManager* commands = game_loop->get_commands();
+
     //TODO: read config from file
-    input::CommandManager* commands = new input::CommandManager;
+    //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_P));
     (*commands)[CommandSample::PAUSE] = input::Command(i);
 
-    commands_ = commands;
-
     return 0;
 }
 

game/src/game/state/state_intro.cpp

     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"
+    if (pause->is_triggered() && pause->has_changed()) // is_triggered && 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");
-    //StateManager::instance()->switch_to_state("game");
-    if (color_ == 0.0)
-        LOG("Update\n")
-
     if(pause_)
         return;
 

game/src/input/command_manager.cpp

 namespace input {
 
 
-CommandManager::CommandManager():
-    num_commands_(0),
-    commands_(0)
+CommandManager::CommandManager(int num_commands):
+    num_commands_(num_commands),
+    commands_(new Command[num_commands_])
 {}
 
 
 CommandManager::~CommandManager()
 {
+    num_commands_ = 0;
     delete [] commands_;
 }
 
-
+/*
 void CommandManager::set_num_commands(int num_commands)
 {
     num_commands_ = num_commands;
     commands_ = new Command[num_commands];
 }
+*/
 
-
-void CommandManager::update_inputs()
+void CommandManager::update()
 {
     for (int i = 0; i < num_commands_; i++)
-    {
         commands_[i].update_input();
-    }
 }
 
 

game/src/io/settings_display.cpp

         }
     }
 
-    //config_destroy(&config);
-
     return 0;
 }
 

game/src/main/main_x11.cpp

     LOG("starting game...\n");
 
     game::GameLoopSample game_loop;
-    game::GameX11 *game;
+    game::GameX11* game;
     game = new game::GameX11(&game_loop);
 
     game->run();

game/src/platform/application.cpp

 }
 
 
+GameLoop* Application::get_game_loop()
+{
+    return game_loop_;
+}
+
+
 void Application::request_exit()
 {
     exit_requested_ = true;
 {
     //Renderer* renderer = get_renderer();
     //Clock* clock = get_clock();
-    LOG("Update\n")
 }
 
 
         for (int i = 0; i < num_input_devices_; i++)
             input_devices_[i]->update();
 
-        if (game_loop_->loop(renderer_, clock_, commands_))
+        if (game_loop_->loop(renderer_, clock_))
             exit_requested_ = true;
 
         display();

game/src/platform/x11/application_x11.cpp

         return 1;
     }
 
-    window_ = new WindowGLX(display_); //TODO: Do not pass x11::Display but just ::Display and ::Window root_window
+    window_ = new WindowGLX(display_->get_display(), display_->get_root_window());
     if (!window_)
         return 1;
 
     display_settings_.height = height;
     display_settings_.fullscreen = fullscreen;
 
-
-
     if (init_window()) {
         LOG("Failed to init window\n")
         return 1;
 
 int ApplicationX11::init_commands()
 {
-    // vitual, commands are game dependant and must be defined in GameX11
+    // virtual, commands are game dependant and must be defined in GameX11
 }
 
 

game/src/platform/x11/window_events.cpp

 int WindowX11::manage_events() {
     ::XEvent event;
 
-    ::Display* display = display_->get_display();
-
     // Check window manager events
-    while (::XCheckTypedEvent(display, ClientMessage, &event)) {
+    while (::XCheckTypedEvent(display_, ClientMessage, &event)) {
         if(event.xclient.window == window_ &&
             event.xclient.message_type == wm_protocol_ &&
             event.xclient.format == 32 &&
     long event_mask = KeyPressMask|KeyReleaseMask|ExposureMask|ButtonPressMask|FocusChangeMask;
 
     // Check window events
-    while (::XCheckWindowEvent(display, window_, event_mask, &event)) {
+    while (::XCheckWindowEvent(display_, window_, event_mask, &event)) {
         if ((event.type == KeyPress) || (event.type == KeyRelease)) {
             if (event.xkey.keycode < 256)
                 LOG("Keycode: 0x%02x\n", event.xkey.keycode)
         }
         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);
+            ::XGrabPointer(display_, window_, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, window_, cursor, CurrentTime);
             LOG("Focus in\n")
         }
         else if (event.type == FocusOut) {
-            ::XUngrabPointer(display, CurrentTime);
+            ::XUngrabPointer(display_, CurrentTime);
             LOG("Focus out\n")
         }
     }

game/src/platform/x11/window_glx.cpp

 #include "platform/x11/display.h"
 
 #include "common/log.h"
+
+#include <X11/Xlib.h>
 #include <GL/glx.h>
 
 
 namespace x11 {
 
 
-WindowGLX::WindowGLX(Display* display):
-    WindowX11(display),
+WindowGLX::WindowGLX(::Display* display, ::Window root_window):
+    WindowX11(display, root_window),
     gl_context_(0)
-{
-    ::Display* disp = display_->get_display();
-
-    if (disp)
-    {
-        int glx_major, glx_minor;
-        ::glXQueryVersion(disp, &glx_major, &glx_minor);
-        LOG("GLX version %d.%d\n", glx_major, glx_minor)
-    }
-}
+{}
 
 
 WindowGLX::~WindowGLX() {
             display_->set_video_mode(mode);
         }
     }*/
+    int glx_major, glx_minor;
+    ::glXQueryVersion(display_, &glx_major, &glx_minor);
+    LOG("GLX version %d.%d\n", glx_major, glx_minor)
 
     if (create(width, height, fullscreen))
         return 1;
     ::XSetWindowAttributes attributes;
     unsigned long          value_mask;
 
-    ::Display* display = display_->get_display();
-    ::Window root_window = display_->get_root_window();
-
     int x(0), y(0);
 
-    if (!display) {
+    if (!display_) {
         LOG("No open X display\n")
         return 1;
     }
 
     GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
 
-    visual_info_ = ::glXChooseVisual(display, 0, att);
-    color_map_ = ::XCreateColormap(display, root_window, visual_info_->visual, AllocNone);
+    visual_info_ = ::glXChooseVisual(display_, 0, att);
+    color_map_ = ::XCreateColormap(display_, root_window_, visual_info_->visual, AllocNone);
 
     attributes.colormap = color_map_;
     attributes.border_pixel = 0;
     }
 
     window_ = ::XCreateWindow(
-        display, root_window,
+        display_, root_window_,
         x, y, width, height, 0,
         visual_info_->depth,
         InputOutput,
     XSetWMNormalHints(display_, window_, &size_hint);
     */
 
-    ::XStoreName(display, window_, window_name_);
-    ::XMapRaised(display, window_);
+    ::XStoreName(display_, window_, window_name_);
+    ::XMapRaised(display_, window_);
 
     if (fullscreen) {
         //::XMapRaised(display_, window_);
-        ::XGrabKeyboard(display, window_, True, GrabModeAsync, GrabModeAsync, CurrentTime);
-        ::XGrabPointer(display, window_, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, window_, None, CurrentTime);
+        ::XGrabKeyboard(display_, window_, True, GrabModeAsync, GrabModeAsync, CurrentTime);
+        ::XGrabPointer(display_, window_, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, window_, None, CurrentTime);
     }
     else {
-        if ((wm_protocol_ = ::XInternAtom(display, "WM_PROTOCOLS", False)) == None) {
+        if ((wm_protocol_ = ::XInternAtom(display_, "WM_PROTOCOLS", False)) == None) {
             LOG("Failed to look up WM_PROTOCOLS\n")
             return 1;
         }
 
-        if ((wm_delete_ = ::XInternAtom(display, "WM_DELETE_WINDOW", False)) != None)
-            ::XSetWMProtocols(display, window_, &wm_delete_, 1);
+        if ((wm_delete_ = ::XInternAtom(display_, "WM_DELETE_WINDOW", False)) != None)
+            ::XSetWMProtocols(display_, window_, &wm_delete_, 1);
     }
     //::XFlush(display_);
     LOG("Window created\n")
 
 
 int WindowGLX::create_context() {
-    ::Display* display = display_->get_display();
-
-    gl_context_ = ::glXCreateContext(display, visual_info_, NULL, true);
+    gl_context_ = ::glXCreateContext(display_, visual_info_, NULL, true);
     if (!gl_context_) {
         LOG("Failed to create rendering context\n")
         return 1;
     }
     LOG("Rendering context created\n")
 
-    if (::glXIsDirect(display, gl_context_))
+    if (::glXIsDirect(display_, gl_context_))
        LOG("DRI enabled\n")
     else
        LOG("No DRI available\n")
 
 
 int WindowGLX::attach_context() {
-    ::Display* display = display_->get_display();
-
-    if (!::glXMakeCurrent(display, window_, gl_context_)) {
+    if (!::glXMakeCurrent(display_, window_, gl_context_)) {
         LOG("Failed to make rendering context current\n")
         return 1;
     }
 
 
 void WindowGLX::display() const {
-    ::Display* display = display_->get_display();
-
     if (window_ && gl_context_)
-        ::glXSwapBuffers(display, window_);
+        ::glXSwapBuffers(display_, window_);
 }
 
 
 void WindowGLX::detach_context() {
-    ::Display* display = display_->get_display();
+    //::Display* display = display_->get_display();
 
-    ::glXMakeCurrent(display, 0, 0);
+    ::glXMakeCurrent(display_, 0, 0);
 }
 
 
 void WindowGLX::destroy_context()
 {
-    ::Display* display = display_->get_display();
-
-    ::glXDestroyContext(display, gl_context_);
+    ::glXDestroyContext(display_, gl_context_);
     LOG("Rendering context destroyed\n")
 }
 
 
 void WindowGLX::destroy() {
-    ::Display* display = display_->get_display();
-
     if (!window_)
         return;
 
-    ::XDestroyWindow(display, window_);
+    ::XDestroyWindow(display_, window_);
     window_ = 0;
 
     LOG("Window destroyed\n")
-    ::XFreeColormap(display, color_map_);
+    ::XFreeColormap(display_, color_map_);
 }
 
 

game/src/platform/x11/window_x11.cpp

 namespace x11 {
 
 
-WindowX11::WindowX11(Display* display):
-    display_(display),
+WindowX11::WindowX11(::Display* display, ::Window root_window):
+    display_(display), root_window_(root_window),
     window_(0), color_map_(0), visual_info_(0),
     wm_delete_(0), wm_protocol_(0)
 {}
     ::Pixmap blank;
     ::XColor dummy;
 
-    ::Display* display = display_->get_display();
-    ::Window root_window = display_->get_root_window();
-
-    blank = ::XCreateBitmapFromData(display, root_window, data, 1, 1);
+    blank = ::XCreateBitmapFromData(display_, root_window_, data, 1, 1);
     if (blank == None)
         LOG("Failed to create blank cursor\n")
-    cursor = ::XCreatePixmapCursor(display, blank, blank, &dummy, &dummy, 0, 0);
-    ::XFreePixmap(display, blank);
+    cursor = ::XCreatePixmapCursor(display_, blank, blank, &dummy, &dummy, 0, 0);
+    ::XFreePixmap(display_, blank);
 
     return cursor;
 }