Commits

Sylvain Rouquette  committed 7e4da82

move window code from application to window
styles and fixes

  • Participants
  • Parent commits 384f0f9

Comments (0)

Files changed (28)

File game/inc/platform/application.h

 #ifndef _PLATFORM_APPLICATION_
 #define _PLATFORM_APPLICATION_
 
-#include "platform/platform.h"
-
 
 namespace platform {
 class Platform;
 class Application
 {
 public:
-    Application(Platform *platform, GameLoop *game_loop);
+    Application(Platform* platform, GameLoop* game_loop);
     virtual ~Application();
 
     void run();
 
 protected:
     void request_exit();
-    Platform * get_platform();
+    Platform* get_platform();
 
 private:
     Application(const Application&);
     virtual void display() {}
     virtual void finish() {}
 
-    Platform *platform_;
-    GameLoop *game_loop_;
+    Platform* platform_;
+    GameLoop* game_loop_;
 
     bool exit_requested_;
 };

File game/inc/platform/game_loop.h

     virtual ~GameLoop() {}
 
     virtual void init() {};
-    virtual int loop(Renderer *renderer) {};
+    virtual int loop(Renderer* renderer) {};
     virtual void finish() {};
 
 private:

File game/inc/platform/platform.h

 #ifndef _PLATFORM_PLATFORM_
 #define _PLATFORM_PLATFORM_
 
-#include "platform/renderer.h"
-
 
 namespace platform {
+class Renderer;
 
 
 class Platform
 public:
     virtual ~Platform() {}
 
-    virtual Renderer *create_renderer() = 0;
+    virtual Renderer* create_renderer() = 0;
+    virtual void delete_renderer(Renderer* renderer) = 0;
 };
 
 

File game/inc/platform/win32/application_win32.h

 class WindowWin32;
 
 
-class ApplicationWin32 : public platform::Application
+class ApplicationWin32 : public Application
 {
 public:
-    ApplicationWin32(GameLoop *game_loop);
+    ApplicationWin32(GameLoop* game_loop);
     ~ApplicationWin32();
 
     WindowWin32* get_window();

File game/inc/platform/win32/game_win32.h

 #define _PLATFORM_WIN32_GAME_WIN32_
 
 #include "platform/win32/application_win32.h"
-#include "renderer/renderer.h"
+
+
+namespace renderer {
+class Renderer;
+}
 
 
 namespace platform {
 class GameLoop;
-}
 
 
-class GameWin32 : public platform::win32::ApplicationWin32
+namespace win32 {
+
+
+class GameWin32 : public ApplicationWin32
 {
 public:
-    GameWin32(platform::GameLoop *game_loop);
+    GameWin32(GameLoop* game_loop);
     ~GameWin32();
 
-    renderer::Renderer * get_renderer();
+    renderer::Renderer* get_renderer();
 
 private:
     GameWin32(const GameWin32&);
     GameWin32& operator=(const GameWin32&);
 
-    renderer::Renderer *renderer_;
+    renderer::Renderer* renderer_;
 };
 
 
+} /* namespace win32 */ } /* namespace platform */
+
+
 #endif // _PLATFORM_WIN32_GAME_WIN32_

File game/inc/platform/win32/platform_win32.h

 #include <windows.h>
 
 
-namespace renderer {
+namespace platform {
 class Renderer;
-}
+class Window;
 
 
-namespace platform {
-class Window;
-}
-
-
-namespace platform {
 namespace win32 {
 
 
-class PlatformWin32 : public platform::Platform
+class PlatformWin32 : public Platform
 {
 public:
     PlatformWin32();
-    ~PlatformWin32();
+    virtual ~PlatformWin32();
 
-    ::platform::Window * create_window();
-    ::platform::Renderer * create_renderer();
+    Window* create_window();
+    virtual Renderer* create_renderer();
+    virtual void delete_renderer(Renderer* renderer);
 
 private:
     PlatformWin32(const PlatformWin32&);
 };
 
 
-} // namespace win32
-} // namespace platform
+} /* namespace win32 */ } /* namespace platform */
 
 
 #endif // _PLATFORM_WIN32_PLATFORM_WIN32_

File game/inc/platform/win32/window_wgl.h

 #include "platform/win32/window_win32.h"
 
 
-namespace platform {
-namespace win32 {
+namespace platform { namespace win32 {
 
 
 class WindowWGL : public WindowWin32
 
 public:
     WindowWGL();
-    ~WindowWGL() {}
+    virtual ~WindowWGL();
 
+    virtual int init(int width, int height, bool fullscreen);
     int create(int width, int height, bool fullscreen);
     void destroy();
 
     int attach_context();
     void detach_context();
 
-    void display() const;
+    virtual void display() const;
 
 private:
     WindowWGL(const WindowWGL&);
 };
 
 
-} //namespace win32
-} //namespace platform
+} /* namespace win32 */ } /* namespace platform */
+
 
 #endif // _PLATFORM_WIN32_WINDOW_WGL_

File game/inc/platform/win32/window_win32.h

 namespace win32 {
 
 
-class WindowWin32 : public ::platform::Window
+class WindowWin32 : public Window
 {
 public:
     WindowWin32();

File game/inc/platform/window.h

     Window();
     virtual ~Window();
 
+    virtual int init(int width, int height, bool fullscreen) { return 0; }
+
     virtual void display() const {};
     int resize(int width, int height, bool fullscreen=false);
 
     void get_available_video_modes();
     int get_video_mode(int width, int height);
     int set_video_mode(int mode=0);
-    void get_ratio(int &w, int &h);
+    void get_ratio(int& w, int& h);
     bool is_deleted();
 
 private:
     int width_;
     int height_;
 
-    static const char *window_name_;
+    static const char* window_name_;
 
     int cur_video_mode_;
     int num_video_modes_;
-    VideoMode *video_modes_;
+    VideoMode* video_modes_;
 
     bool deleted_;
 };

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

 class WindowX11;
 
 
-class ApplicationX11 : public platform::Application
+class ApplicationX11 : public Application
 {
 public:
-    ApplicationX11(GameLoop *game_loop);
+    ApplicationX11(GameLoop* game_loop);
     ~ApplicationX11();
 
     WindowX11* get_window();

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

 #define _PLATFORM_X11_GAME_X11_
 
 #include "platform/x11/application_x11.h"
-#include "renderer/renderer.h"
 
 
 namespace platform {
 class GameLoop;
-}
 
 
-class GameX11 : public platform::x11::ApplicationX11
+namespace x11 {
+
+
+class GameX11 : public ApplicationX11
 {
 public:
-    GameX11(platform::GameLoop *game_loop);
+    GameX11(platform::GameLoop* game_loop);
     ~GameX11();
 
-    renderer::Renderer * get_renderer();
-
 private:
     GameX11(const GameX11&);
     GameX11& operator=(const GameX11&);
-
-    renderer::Renderer *renderer_;
 };
 
 
+} /* namespace x11 */ } /* namespace platform */
+
+
 #endif // _PLATFORM_X11_GAME_X11_

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

 #include <X11/Xutil.h>
 
 
-namespace renderer {
+namespace platform {
 class Renderer;
-}
+class Window;
 
 
-namespace platform {
-class Window;
-}
-
-
-namespace platform {
 namespace x11 {
 
 
 {
 public:
     PlatformX11();
-    ~PlatformX11();
+    virtual ~PlatformX11();
 
-    ::platform::Window* create_window();
-    ::platform::Renderer* create_renderer();
+    Window* create_window();
+    virtual Renderer* create_renderer();
+    virtual void delete_renderer(Renderer* renderer);
 
 private:
     PlatformX11(const PlatformX11&);
 };
 
 
-} // namespace x11
-} // namespace platform
+} /* namespace x11 */ } /* namespace platform */
 
 
 #endif // _PLATFORM_X11_PLATFORM_X11_

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

 #include "platform/x11/window_x11.h"
 
 
-namespace platform {
-namespace x11 {
+namespace platform { namespace x11 {
 
 
 class WindowGLX : public WindowX11
 
 public:
     WindowGLX();
-    ~WindowGLX() {}
+    virtual ~WindowGLX();
 
     int create(int width, int height, bool fullscreen);
     void destroy();
     int attach_context();
     void detach_context();
 
-    void display() const;
+    virtual void display() const;
 
 private:
     WindowGLX(const WindowGLX&);
 };
 
 
-} //namespace x11
-} //namespace platform
+} /* namespace x11 */ } /* namespace platform */
+
 
 #endif // _PLATFORM_X11_WINDOW_GLX_
-

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

 namespace x11 {
 
 
-class WindowX11 : public ::platform::Window
+class WindowX11 : public Window
 {
 public:
     WindowX11();

File game/src/main/main_win32.cpp

     LPSTR lpCmdLine, int nCmdShow)
 {
     GameLoopSample game_loop;
-    GameWin32 *game;
-    game = new GameWin32(&game_loop);
+    platform::win32::GameWin32 *game;
+    game = new platform::win32::GameWin32(&game_loop);
 
     game->run();
 

File game/src/main/main_x11.cpp

 int main()
 {
     GameLoopSample game_loop;
-    GameX11 *game;
-    game = new GameX11(&game_loop);
+    platform::x11::GameX11 *game;
+    game = new platform::x11::GameX11(&game_loop);
 
     game->run();
 

File game/src/platform/application.cpp

 {
 
 
-Application::Application(Platform *platform, GameLoop *game_loop) :
+Application::Application(Platform* platform, GameLoop* game_loop) :
     platform_(platform),
     game_loop_(game_loop),
     exit_requested_(false)
 }
 
 
-Platform * Application::get_platform()
+Platform* Application::get_platform()
 {
     return platform_;
 }
         return;
     }
 
-    Renderer *renderer = get_platform()->create_renderer();
+    Renderer* renderer = get_platform()->create_renderer();
 
     game_loop_->init();
 
 
     game_loop_->finish();
 
+    get_platform()->delete_renderer(renderer);
+
     finish();
 }
 

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

 #include "platform/win32/application_win32.h"
 
 #include "platform/win32/platform_win32.h"
-#include "platform/win32/window_wgl.h"
+#include "platform/win32/window_win32.h"
 
 
 namespace platform {
 }
 
 
-namespace platform {
-namespace win32 {
+namespace platform { namespace win32 {
 
 
-ApplicationWin32::ApplicationWin32(GameLoop *game_loop):
+ApplicationWin32::ApplicationWin32(GameLoop* game_loop):
     platform::Application(new PlatformWin32, game_loop)
 {}
 
 {}
 
 
-int ApplicationWin32::init()
-{
-    WindowWGL * window = reinterpret_cast< WindowWGL* >(get_window());
-
-    // Load config file or default configuration here?
-    bool fullscreen = false;
-    int width = 640;
-    int height = 360;
-
-    // Check if fullscreen video mode is available for the given resolution
-    if (fullscreen)
-    {
-        int mode = window->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);
-            window->set_video_mode(mode);
-        }
-    }
-
-    if (window->create(width, height, fullscreen)) return 1;
-    if (window->create_context()) return 1;
-    if (window->attach_context()) return 1;
-    // TODO: delete window if error
-
-    window_ = window;
-
-    return 0;
+int ApplicationWin32::init() {
+    return get_window()->init(640, 480, true);
 }
 
 
-void ApplicationWin32::update()
-{
+void ApplicationWin32::update() {
     window_->manage_events();
 
     if (window_->is_deleted())
 }
 
 
-void ApplicationWin32::display()
-{
+void ApplicationWin32::display() {
     window_->display();
 }
 
 
-void ApplicationWin32::finish()
-{
-    if (!window_) return;
-
-    WindowWGL * window =
-        reinterpret_cast< ::platform::win32::WindowWGL* >(window_);
-
-    window->detach_context();
-    window->destroy_context();
-    window->destroy();
-    window->set_video_mode(); // restore original mode
+void ApplicationWin32::finish() {
+    if (!window_)
+        return;
 
     delete window_;
+    window_ = NULL;
 }
 
 
-WindowWin32* ApplicationWin32::get_window()
-{
+WindowWin32* ApplicationWin32::get_window() {
     if (window_)
         return window_;
 
 }
 
 
-} // namespace win32
-} // namespace platform
+} /* namespace win32 */ } /* namespace platform */

File game/src/platform/win32/game_win32.cpp

 #include "platform/win32/window_wgl.h"
 
 
-GameWin32::GameWin32(platform::GameLoop *game_loop) :
-    platform::win32::ApplicationWin32(game_loop),
-    renderer_(0)
+namespace platform { namespace win32 {
+
+
+GameWin32::GameWin32(GameLoop* game_loop) :
+    ApplicationWin32(game_loop)
 {}
 
 
 GameWin32::~GameWin32()
-{
-    delete renderer_;
-}
+{}
 
-
-renderer::Renderer * GameWin32::get_renderer()
-{
-    if (renderer_)
-        return renderer_;
-
-    ::platform::win32::PlatformWin32 * platform = reinterpret_cast< platform::win32::PlatformWin32* >(get_platform());
-    renderer_ = reinterpret_cast< ::renderer::Renderer* >(platform->create_renderer());
-
-    return renderer_;
-}
+} /* namespace win32 */ } /* namespace platform */

File game/src/platform/win32/platform_win32.cpp

 #include "renderer/opengl/renderer_opengl.h"
 
 
-namespace platform {
-namespace win32 {
+namespace platform { namespace win32 {
 
 
 PlatformWin32::PlatformWin32() :
-    ::platform::Platform()
+    Platform()
 {}
 
 
 {}
 
 
-::platform::Window * PlatformWin32::create_window()
-{
-    return(reinterpret_cast< ::platform::Window* >(new ::platform::win32::WindowWGL));
+Window* PlatformWin32::create_window() {
+    return reinterpret_cast< Window* >(new WindowWGL);
 }
 
 
-::platform::Renderer * PlatformWin32::create_renderer()
-{
-    //return(new ::renderer::d3d9::RendererDirect3D9);
-    return(reinterpret_cast< ::platform::Renderer* >(new ::renderer::opengl::RendererOpenGL));
+Renderer* PlatformWin32::create_renderer() {
+    //return new ::renderer::d3d9::RendererDirect3D9;
+    return reinterpret_cast< Renderer* >(new ::renderer::opengl::RendererOpenGL);
 }
 
 
-} // namespace win32
-} // namespace platform
+void PlatformWin32::delete_renderer(Renderer* renderer) {
+    delete reinterpret_cast< ::renderer::opengl::RendererOpenGL* >(renderer);
+}
+
+
+} /* namespace win32 */ } /* namespace platform */

File game/src/platform/win32/window_wgl.cpp

 #include "platform/win32/window_wgl.h"
 
 
-namespace platform {
-namespace win32 {
+namespace platform { namespace win32 {
 
 
 WindowWGL::WindowWGL() :
 }
 
 
-int WindowWGL::create(int width, int height, bool fullscreen)
-{
+WindowWGL::~WindowWGL() {
+    detach_context();
+    destroy_context();
+    destroy();
+    set_video_mode(); // restore original mode
+}
+
+
+int WindowWGL::init(int width, int height, bool fullscreen) {
+    // Check if fullscreen video mode is available for the given resolution
+    if (fullscreen) {
+        int mode = 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);
+            set_video_mode(mode);
+        }
+    }
+
+    if (create(width, height, fullscreen))
+        return 1;
+    if (create_context())
+        return 1;
+    if (attach_context())
+        return 1;
+    // TODO: delete window if error
+
+    return 0;
+}
+
+
+int WindowWGL::create(int width, int height, bool fullscreen) {
     ::DWORD window_style, window_style_ex;
     ::MSG Msg;
 
 
     register_window_class();
 
-    if (fullscreen)
-    {
+    if (fullscreen) {
         x = y = 0;
         window_style_ex = WS_EX_TOPMOST;
         window_style = WS_POPUP|WS_VISIBLE|WS_SYSMENU;
     }
-    else
-    {
+    else {
         x = y = CW_USEDEFAULT;
         ::RECT r = {0, 0, width, height};
         window_style_ex = 0;
         x, y, width, height,
         0, 0, hinstance_,0);
 
-    if (!window_)
-    {
+    if (!window_) {
         printf("Failed to create window\n");
         return 1;
     }
     };
 
     device_context_ = GetDC(window_);
-    if(device_context_ == 0)
-    {
+    if(device_context_ == 0) {
         printf("Failed to get device context\n");
         return 1;
     }
 
-    if (!(pixel_format = ChoosePixelFormat(device_context_, &pfd)))
-    {
+    if (!(pixel_format = ChoosePixelFormat(device_context_, &pfd))) {
         printf("Failed to choose pixel format\n");
         return 1;
     }
 
-    if (!SetPixelFormat(device_context_, pixel_format, &pfd))
-    {
+    if (!SetPixelFormat(device_context_, pixel_format, &pfd)) {
         printf("Failed to set pixel format\n");
         return 1;
     }
 }
 
 
-int WindowWGL::create_context()
-{
+int WindowWGL::create_context() {
     gl_context_ = wglCreateContext(device_context_);
-    if (!gl_context_)
-    {
+    if (!gl_context_) {
         printf("Failed to create rendering context\n");
         return 1;
     }
 }
 
 
-int WindowWGL::attach_context()
-{
-    if (!wglMakeCurrent(device_context_, gl_context_))
-    {
+int WindowWGL::attach_context() {
+    if (!wglMakeCurrent(device_context_, gl_context_)) {
         printf("Failed to make rendering context current\n");
         return 1;
     }
 }
 
 
-void WindowWGL::display() const
-{
+void WindowWGL::display() const {
     ::SwapBuffers(device_context_);
 }
 
 
-void WindowWGL::detach_context()
-{
+void WindowWGL::detach_context() {
     ::wglMakeCurrent(0, 0);
 }
 
 
-void WindowWGL::destroy_context()
-{
+void WindowWGL::destroy_context() {
     ::wglDeleteContext(gl_context_);
 }
 
 
-void WindowWGL::destroy()
-{
+void WindowWGL::destroy() {
     ::ShowWindow(window_, SW_HIDE);
 
     if (window_ && !::DestroyWindow(window_))
 }
 
 
-} // namespace win32
-} // namespace platform
+} /* namespace win32 */ } /* namespace platform */

File game/src/platform/window.cpp

 namespace platform {
 
 
-const char *Window::window_name_ = "Ca marche !(TM)";
+const char* Window::window_name_ = "Ca marche !(TM)";
 
 
 Window::Window():
 }
 
 
-void Window::get_ratio(int &x, int &y)
+void Window::get_ratio(int& w, int& h)
 {
-    int a = x;
-    int b = y;
+    int a = w;
+    int b = h;
     int r, gcd = -1;
 
     while (gcd == -1)
         }
     }
 
-    x /= gcd;
-    y /= gcd;
+    w /= gcd;
+    h /= gcd;
 }
 
 

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

 
 #include "platform/x11/platform_x11.h"
 #include "platform/x11/window_x11.h"
-#include "platform/x11/window_glx.h"
 
 
 namespace platform {
 }
 
 
-namespace platform {
-namespace x11 {
+namespace platform { namespace x11 {
 
 
-ApplicationX11::ApplicationX11(GameLoop *game_loop):
+ApplicationX11::ApplicationX11(GameLoop* game_loop):
     platform::Application(new PlatformX11, game_loop)
 {}
 
 {}
 
 
-int ApplicationX11::init()
-{
-    WindowGLX* window = reinterpret_cast< WindowGLX* >(get_window());
-
-    // Load config file or default configuration here?
-    bool fullscreen = false;
-    int width = 1280/2;
-    int height = 720/2;
-
-    // Check if fullscreen video mode is available for the given resolution
-    if (fullscreen)
-    {
-        int mode = window->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);
-            window->set_video_mode(mode);
-        }
-    }
-
-    if (window->create(width, height, fullscreen)) return 1;
-    if (window->create_context()) return 1;
-    if (window->attach_context()) return 1;
-    // TODO: delete window if error
-
-    window_ = window;
-
-    return 0;
+int ApplicationWin32::init() {
+    return get_window()->init(1280 / 2, 720 / 2, false);
 }
 
 
 }
 
 
-void ApplicationX11::finish()
-{
-    if (!window_) return;
-
-    WindowGLX * window =
-        reinterpret_cast< WindowGLX* >(window_);
-
-    window->detach_context();
-    window->destroy_context();
-    window->destroy();
-    window->set_video_mode(); // restore original mode
+void ApplicationX11::finish() {
+    if (!window_)
+        return;
 
     delete window_;
-    window_ = 0;
+    window_ = NULL;
 }
 
 
 }
 
 
-} // namespace x11
-} // namespace platform
+} /* namespace x11 */ } /* namespace platform */

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

 #include "platform/x11/game_x11.h"
 #include "platform/x11/platform_x11.h"
-#include "renderer/renderer.h"
 
 
-GameX11::GameX11(::platform::GameLoop *game_loop):
-    platform::x11::ApplicationX11(game_loop),
-    renderer_(0)
+namespace platform { namespace x11 {
+
+
+GameX11::GameX11(GameLoop* game_loop):
+    ApplicationX11(game_loop)
 {}
 
 
 GameX11::~GameX11()
-{
-    delete renderer_;
-}
+{}
 
-
-renderer::Renderer * GameX11::get_renderer()
-{
-    if (renderer_)
-        return renderer_;
-
-    ::platform::x11::PlatformX11* platform = reinterpret_cast< platform::x11::PlatformX11* >(get_platform());
-    renderer_ = reinterpret_cast< ::renderer::Renderer* >(platform->create_renderer());
-
-    return renderer_;
-}
-
+} /* namespace x11 */ } /* namespace platform */

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

 #include "renderer/opengl/renderer_opengl.h"
 
 
-namespace platform {
-namespace x11 {
+namespace platform { namespace x11 {
 
 
 PlatformX11::PlatformX11():
-    ::platform::Platform()
+    Platform()
 {}
 
 
 {}
 
 
-::platform::Window * PlatformX11::create_window()
-{
-    return(reinterpret_cast< ::platform::Window* >(new ::platform::x11::WindowGLX));
+Window* PlatformX11::create_window() {
+    return reinterpret_cast< Window* >(new ::platform::x11::WindowGLX);
 }
 
 
-::platform::Renderer * PlatformX11::create_renderer()
-{
-    return(reinterpret_cast< ::platform::Renderer* >(new ::renderer::opengl::RendererOpenGL));
+Renderer* PlatformX11::create_renderer() {
+    return reinterpret_cast< Renderer* >(new ::renderer::opengl::RendererOpenGL);
 }
 
 
-} // namespace x11
-} // namespace platform
+void PlatformWinX11::delete_renderer(Renderer* renderer) {
+    delete reinterpret_cast< ::renderer::opengl::RendererOpenGL* >(renderer);
+}
+
+
+} /* namespace x11 */ } /* namespace platform */

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

 namespace x11 {
 
 
-int WindowX11::manage_events()
-{
+int WindowX11::manage_events() {
     ::XEvent event;
 
     // 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 &&
     ::XQueryKeymap(display_, reinterpret_cast< char* >(key_map_));
 
     // Check window events
-    while (::XCheckWindowEvent(display_, window_, event_mask, &event))
-    {
-        if ((event.type == KeyPress) || (event.type == KeyRelease))
-        {
+    while (::XCheckWindowEvent(display_, window_, event_mask, &event)) {
+        if ((event.type == KeyPress) || (event.type == KeyRelease)) {
             printf("Keymap: ");
-            for (int i = 0; i < 32; i++) printf("%02x", key_map_[i]);
+            for (int i = 0; i < 32; i++)
+                printf("%02x", key_map_[i]);
             printf("\n");
 
-            if (event.xkey.keycode < 256)
-            {
+            if (event.xkey.keycode < 256) {
                 printf("Keycode: 0x%02x\n", event.xkey.keycode);
                 // Get the keysym of the key that has been pressed
                 /*
             }
             if (event.xkey.keycode == 0x09) deleted_ = 1;
         }
-        else if (event.type == ButtonPress)
-        {
+        else if (event.type == ButtonPress) {
             printf("Button pressed...\n");
             deleted_ = true;
         }
-        else if (event.type == Expose)
-        {
+        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);

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

 #include <GL/glx.h>
 
 
-namespace platform {
-namespace x11 {
+namespace platform { namespace x11 {
 
 
 WindowGLX::WindowGLX() :
 }
 
 
-int WindowGLX::create(int width, int height, bool fullscreen)
-{
+WindowGLX::~WindowGLX() {
+    detach_context();
+    destroy_context();
+    destroy();
+    set_video_mode(); // restore original mode
+}
+
+
+int WindowGLX::create(int width, int height, bool fullscreen) {
     ::XSetWindowAttributes attributes;
     unsigned long          value_mask;
 
     int x(0), y(0);
 
-    if (!display_)
-    {
+    if (!display_) {
         printf("No open X display\n");
         return 1;
     }
     attributes.border_pixel = 0;
     attributes.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|ButtonPressMask|StructureNotifyMask;
 
-    if (fullscreen)
-    {
+    if (fullscreen) {
         attributes.override_redirect = True;
         value_mask = CWBorderPixel|CWColormap|CWEventMask|CWOverrideRedirect;
     }
-    else
-    {
+    else {
         attributes.override_redirect = False;
         value_mask = CWBorderPixel|CWColormap|CWEventMask;
     }
         value_mask,
         &attributes);
 
-    if (window_ == 0)
-    {
+    if (window_ == 0) {
         printf("Failed to create window\n");
         return 1;
     }
     ::XStoreName(display_, window_, window_name_);
     ::XMapRaised(display_, window_);
 
-    if (fullscreen)
-    {
+    if (fullscreen) {
         //::XMapRaised(display_, window_);
         ::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)
-        {
+    else {
+        if ((wm_protocol_ = ::XInternAtom(display_, "WM_PROTOCOLS", False)) == None) {
             printf("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);
-        }
     }
     //::XFlush(display_);
     printf("Window created\n");
 }
 
 
-int WindowGLX::create_context()
-{
+int WindowGLX::create_context() {
     gl_context_ = ::glXCreateContext(display_, visual_info_, NULL, true);
-    if (!gl_context_)
-    {
+    if (!gl_context_) {
         printf( "Failed to create rendering context\n" );
         return 1;
     }
 
 int WindowGLX::attach_context()
 {
-    if (!::glXMakeCurrent(display_, window_, gl_context_))
-    {
+    if (!::glXMakeCurrent(display_, window_, gl_context_)) {
         printf("Failed to make rendering context current\n");
         return 1;
     }
 void WindowGLX::display() const
 {
     if (window_ && gl_context_)
-    {
         ::glXSwapBuffers(display_, window_);
-    }
 }
 
 
 void WindowGLX::destroy()
 {
     if (!window_)
-        printf("Failed to destroy window\n");
+        return;
 
     ::XDestroyWindow(display_, window_);
     window_ = 0;
 }
 
 
-} // namespace x11
-} // namespace platform
+} /* namespace x11 */ } /* namespace platform */

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

 }
 
 
-WindowX11::~WindowX11()
-{
+WindowX11::~WindowX11() {
     ::XRRFreeScreenConfigInfo(xrr_config_);
     ::XCloseDisplay(display_);
 }
 
 
-void WindowX11::get_available_video_modes(const int min_height, const int max_modes)
-{
+void WindowX11::get_available_video_modes(const int min_height, const int max_modes) {
     int num_modes;
     ::XRRScreenSize *xrrs;
 
     xrrs = ::XRRSizes(display_, 0, &num_modes);
 
     // Select interesting video modes
-    for(int i = 0; (num_video_modes_ < (max_modes - 1)) && (i < num_modes); i++)
-    {
+    for(int i = 0; (num_video_modes_ < (max_modes - 1)) && (i < num_modes); i++) {
         if (xrrs[i].height < min_height) continue;
         num_video_modes_++;
     }
         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++)
-    {
-        if (xrrs[i].height < min_height) continue;
-        else
-        {
+    for(int i = 0, slot = 1; (i < num_modes) && (slot < num_video_modes_); i++) {
+        if (xrrs[i].height < min_height)
+            continue;
+        else {
             short *rates;
             int num_rates;
             rates = ::XRRRates(display_, 0, i, &num_rates);
     }
 
     // Debug display of available resolutions
-    for (int i = 0; i < num_video_modes_; i++)
-    {
-        printf("%2i: mode %2i: %4i x %4i @ %3i Hz\n",
-        i,
+    for (int i = 0; i < num_video_modes_; i++) {
+        printf("%2i: mode %2i: %4i x %4i @ %3i Hz\n", i,
         video_modes_[i].index,
         video_modes_[i].width,
         video_modes_[i].height,
 }
 
 
-int WindowX11::set_video_mode(int mode)
-{
-    if (mode == cur_video_mode_) return 0;
-    if (mode > num_video_modes_) return 1;
+int WindowX11::set_video_mode(int mode) {
+    if (mode == cur_video_mode_)
+        return 0;
+    if (mode > num_video_modes_)
+        return 1;
 
     int res = ::XRRSetScreenConfig(display_, xrr_config_, root_window_,
         video_modes_[mode].index, RR_Rotate_0, CurrentTime);
 
-    if (res) return 1;
+    if (res)
+        return 1;
 
     cur_video_mode_ = mode;