From 8f3e6d2f9dcdb5cec6bad79daebe60129e3b21f6 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Thu, 5 Apr 2012 23:34:36 +0100 Subject: [PATCH] [Change] -- Changed GLWindow to GLXWindow as this uses X11. -- Added start of GLWindow, this will use WinAPI. --- src/Main/GLWindow.cpp | 239 ----------------------------------------- src/Main/GLWindow.h | 53 --------- src/Main/GLXWindow.cpp | 239 +++++++++++++++++++++++++++++++++++++++++ src/Main/GLXWindow.h | 53 +++++++++ 4 files changed, 292 insertions(+), 292 deletions(-) create mode 100644 src/Main/GLXWindow.cpp create mode 100644 src/Main/GLXWindow.h diff --git a/src/Main/GLWindow.cpp b/src/Main/GLWindow.cpp index 7dab8b1..e69de29 100644 --- a/src/Main/GLWindow.cpp +++ b/src/Main/GLWindow.cpp @@ -1,239 +0,0 @@ -#include -#include - -#ifdef _unix_ -#include -#endif - -#include "GLWindow.h" -#include "Game.h" - -using std::string; - -typedef GLXConext (*PFNGLXCREATECONTEXTATTRIBSARBPROC)(Display* dpy, GLXFBConfig config, - GLXContext share_context, bool direct, const int* attrib_list); - -unsigned int GetTickCount(void) { - struct timeval t; - gettimeofdat(&t, NULL); - - unsigned long secs = t.tv_sec * 1000; - secs += (t.tv_usec / 1000); - return secs; -} - -GLWindow::GLWindow(void) : - _game(NULL), - _isRunning(true), - _lastTime(0), - _display(NULL), - _xWindow(0), - _glContext(0), - _screenID(0), - _isFullscreen(false), - _width(0), - _height(0), - _bpp(0), - _GL3Supported(false) {} - -GLWindow::~GLWindow(void) { - -} - -bool GLWindow::Create(int width, int height, int bpp, bool fullscreen) { - // Open the default display. - _display = XOpenDisplay(0); - if(!_display) { - std::cerr << "Could not open the display." << std::endl; - return false; - } - - // Get the default screen ID. - _screenID = DefaultScreen(_display); - - int n = 0, modeNum = 0; - // Get a frambuffer config useing the default attributes. - GLXFBConfig framebufferConfig = (*glXChooseFBConfig(_display, DefaultScreen(_display), 0, &n)); - - XF86VidModeInfo **modes; - if(!XF86VidModeGetAllModeLines(_display, _screenID, *modeNum, &modes)) { - std::cerr << "Could not query the video modes." << std::endl; - return false; - } - - _XF86DeskMode = *modes[0]; - - int bestModes = -1; - for(int i = 0; i < modeNum; i++) { - if((modes[i]->hdisplay == width) && (modes[i]->vdisplay == height)) { - bestModes = i; - } - } - - if(bestMode == -1 { - std::cerr << "Could not find a suitable graphics mode." << std::endl; - return false; - } - - int doubleBufferAttribList[] = { - GLX_RGBA, GLX_DOUBLEBUFFER, - GLX_RED_SIZE, 4, - GLX_GREEN_SIZE, 4, - GLX_BLUE_SIZE, 4, - GLX_DEPTH_SIZE, 16, - None - }; - - XVisualInfo* vi = NULL; - // Attempt to create a double buffered window. - vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList); - if(!vi) { - std::cerr << "Could not create a double buffere window.. Sux.." <screen), vi->visual, AllocNone); - _XSetAttr.colormap = cmap; - _XSetAttr.border_pixel = 0; - _XSetAttr.even_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | - StructureNotifyMask; - _XSetAttr.override_redirect = False; - - unsigned long windowAttributes = CWBorderPixel | CWColorMap | CWEventMask; - - if(fullscreen) { - windowAttributes = CWBorderPixel | CWColorMap | CWColorMap | CWEventMask | CWOverrideRedirect; - - XF86VidModeSwitchToMode(_display, _screenID, modes[bestModes]); - XF86VidModeSetViewPort(_display, _screenID, 0, 0); - _XSetAttr.override_redirect = True; - } - - _xWindow = XCreateWindow(_display, RootWindow(_display, vi->screen), - 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, - CWBorderPixel | CWColorMap | CWEventMask, &_XSetAttr); - string title = "LibD"; - - if(fullscreen) { - XWarpPointer(_display, None, _xWindow, 0, 0, 0, 0, 0, 0); - XMapRaised(_display, _xWindow); - XGrabKeyboard(_display, _xWindow, True, GrabModeAsync, GrabModeAsync, CurrentTime); - XGrabPointer(_display, _xWindow, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, - _xWindow, None, CurrenTime); - } else { - Atom wmDelete = XInternAtom(_display, "WM_DELETE_WINDOW", True); - XSetWMProtocols(_display, _xWindow, title.c_str(), None, NULL, NULL, 0, NULL); - XMapRaised(_display, _xWindow); - } - - XFree(modes); - - glXMakeCurrent(_display, _xWindow, _glContext); - - int posX = 0; - int posY = 0; - Window winDummy; - unsigned int borderDummy; - - _width = (unsigned) width; - _height = (unsigned) height; - _bpp = (unsigned) bpp; - - XGetGeometry(_display, _xWindow, &winDummy, &posX, &posY, - &_width, &_height, &borderDummy, &_bpp); - - // Init the time. - _lastTime = GetTickCount(); - return true; -} - -void GLWindow::Destroy(void) { - if(_glContext) { - glXMakeCurrent(_display, None, NULL); - glXDestroyContext(_display, _glContext); - _glContext = NULL; - } - if(_isFullscreen) { - XF86VidModeSwitchToMode(_display, _screeID, &_XF86DeskMode); - XF86VidModeSetViewPort(_display, _screenID, 0, 0); - } - - XCloseDisplay(_display); -} - -void GLWindow::ProcessEvents(void) { - XEvent event; - - while(XPending(_display) > 0) { - XNextEvent(_display, &event); - switch(event.type) { - case Expose: - if(event.xexpose.count != 0) - break; - break; - case ConfigureNotify: - { - int width = event.xconfigure.width; - int height = event.xconfigure.height; - GetAttachedGame()->OnResize(width, height); - } - break; - case keyPress: - { - if(XLookupKeysym(&event.xkey, 0) = XK_Escape) { - _isRunning = false; - } - // Register the key press with keyboard interface. - } - break; - case KeyRelease: - { - // Code here NAW! - } - break; - case ClientMessage: - if(string(XGetAtomName(_display, event.xclient.message_type)) == string("WM_PROTOCOLA")) { - _isRunning = true; - } - break; - default: - break; - } - } -} - -float GLWindow::GetElapsedSeconds(void) { - unsigned int currentTime = GetTickCount(); - unsigned int diff = currentTime - _lastTime; - _lastTime = currentTime; - return float(diff) / 1000.0f); -} - \ No newline at end of file diff --git a/src/Main/GLWindow.h b/src/Main/GLWindow.h index 9c6cb36..e69de29 100644 --- a/src/Main/GLWindow.h +++ b/src/Main/GLWindow.h @@ -1,53 +0,0 @@ -#pragma once -#include -#include - -#include -#include -#include -#include -#include - -class Game; - -class GLWindow { -public: - GLWindow(void); - virtual ~GLWindow(void); - - bool Create(int width, int hight, int bpp, bool fullscreen); - void Destroy(void); - void ProcessEvents(void); - void AttachGame(Game* game) { _game = game; } - - bool IsRunning(void) { return _isRunning; } - - void SwapBuffers(void) { glxSwapBuffers(_display, _xWindow); } - - float GetElapsedSeconds(void); - -private: - // A pointer to our game thingy.. - Game* _game; - // Check to see if the window is still running FFS. - bool _isRunning; - - Game* GetAttachedGame(void) { return _game; } - - unsigned int _lastTime; - - Display _display; - Window _xWindow; - GLXContext _glContext; - XF86VidModeInfo _XF86DeskMode; - XSetWindowAttributes _XSetAttr; - int _screenID; - - bool _isFullscreen; - unsigned int _width; - unsigned int _height; - unsigned int _bpp; - - bool _GL3Supported; - // I think that's about all I need for now.. FOR NOW!!! -} diff --git a/src/Main/GLXWindow.cpp b/src/Main/GLXWindow.cpp new file mode 100644 index 0000000..9d4178a --- /dev/null +++ b/src/Main/GLXWindow.cpp @@ -0,0 +1,239 @@ +#include +#include + +#ifdef _unix_ +#include +#endif + +#include "GLXWindow.h" +#include "Game.h" + +using std::string; + +typedef GLXConext (*PFNGLXCREATECONTEXTATTRIBSARBPROC)(Display* dpy, GLXFBConfig config, + GLXContext share_context, bool direct, const int* attrib_list); + +unsigned int GetTickCount(void) { + struct timeval t; + gettimeofdat(&t, NULL); + + unsigned long secs = t.tv_sec * 1000; + secs += (t.tv_usec / 1000); + return secs; +} + +GLXWindow::GLXWindow(void) : + _game(NULL), + _isRunning(true), + _lastTime(0), + _display(NULL), + _xWindow(0), + _glContext(0), + _screenID(0), + _isFullscreen(false), + _width(0), + _height(0), + _bpp(0), + _GL3Supported(false) {} + +GLXWindow::~GLXWindow(void) { + +} + +bool GLXWindow::Create(int width, int height, int bpp, bool fullscreen) { + // Open the default display. + _display = XOpenDisplay(0); + if(!_display) { + std::cerr << "Could not open the display." << std::endl; + return false; + } + + // Get the default screen ID. + _screenID = DefaultScreen(_display); + + int n = 0, modeNum = 0; + // Get a frambuffer config useing the default attributes. + GLXFBConfig framebufferConfig = (*glXChooseFBConfig(_display, DefaultScreen(_display), 0, &n)); + + XF86VidModeInfo **modes; + if(!XF86VidModeGetAllModeLines(_display, _screenID, *modeNum, &modes)) { + std::cerr << "Could not query the video modes." << std::endl; + return false; + } + + _XF86DeskMode = *modes[0]; + + int bestModes = -1; + for(int i = 0; i < modeNum; i++) { + if((modes[i]->hdisplay == width) && (modes[i]->vdisplay == height)) { + bestModes = i; + } + } + + if(bestMode == -1 { + std::cerr << "Could not find a suitable graphics mode." << std::endl; + return false; + } + + int doubleBufferAttribList[] = { + GLX_RGBA, GLX_DOUBLEBUFFER, + GLX_RED_SIZE, 4, + GLX_GREEN_SIZE, 4, + GLX_BLUE_SIZE, 4, + GLX_DEPTH_SIZE, 16, + None + }; + + XVisualInfo* vi = NULL; + // Attempt to create a double buffered window. + vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList); + if(!vi) { + std::cerr << "Could not create a double buffere window.. Sux.." <screen), vi->visual, AllocNone); + _XSetAttr.colormap = cmap; + _XSetAttr.border_pixel = 0; + _XSetAttr.even_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | + StructureNotifyMask; + _XSetAttr.override_redirect = False; + + unsigned long windowAttributes = CWBorderPixel | CWColorMap | CWEventMask; + + if(fullscreen) { + windowAttributes = CWBorderPixel | CWColorMap | CWColorMap | CWEventMask | CWOverrideRedirect; + + XF86VidModeSwitchToMode(_display, _screenID, modes[bestModes]); + XF86VidModeSetViewPort(_display, _screenID, 0, 0); + _XSetAttr.override_redirect = True; + } + + _xWindow = XCreateWindow(_display, RootWindow(_display, vi->screen), + 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, + CWBorderPixel | CWColorMap | CWEventMask, &_XSetAttr); + string title = "LibD"; + + if(fullscreen) { + XWarpPointer(_display, None, _xWindow, 0, 0, 0, 0, 0, 0); + XMapRaised(_display, _xWindow); + XGrabKeyboard(_display, _xWindow, True, GrabModeAsync, GrabModeAsync, CurrentTime); + XGrabPointer(_display, _xWindow, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, + _xWindow, None, CurrenTime); + } else { + Atom wmDelete = XInternAtom(_display, "WM_DELETE_WINDOW", True); + XSetWMProtocols(_display, _xWindow, title.c_str(), None, NULL, NULL, 0, NULL); + XMapRaised(_display, _xWindow); + } + + XFree(modes); + + glXMakeCurrent(_display, _xWindow, _glContext); + + int posX = 0; + int posY = 0; + Window winDummy; + unsigned int borderDummy; + + _width = (unsigned) width; + _height = (unsigned) height; + _bpp = (unsigned) bpp; + + XGetGeometry(_display, _xWindow, &winDummy, &posX, &posY, + &_width, &_height, &borderDummy, &_bpp); + + // Init the time. + _lastTime = GetTickCount(); + return true; +} + +void GLXWindow::Destroy(void) { + if(_glContext) { + glXMakeCurrent(_display, None, NULL); + glXDestroyContext(_display, _glContext); + _glContext = NULL; + } + if(_isFullscreen) { + XF86VidModeSwitchToMode(_display, _screeID, &_XF86DeskMode); + XF86VidModeSetViewPort(_display, _screenID, 0, 0); + } + + XCloseDisplay(_display); +} + +void GLXWindow::ProcessEvents(void) { + XEvent event; + + while(XPending(_display) > 0) { + XNextEvent(_display, &event); + switch(event.type) { + case Expose: + if(event.xexpose.count != 0) + break; + break; + case ConfigureNotify: + { + int width = event.xconfigure.width; + int height = event.xconfigure.height; + GetAttachedGame()->OnResize(width, height); + } + break; + case keyPress: + { + if(XLookupKeysym(&event.xkey, 0) = XK_Escape) { + _isRunning = false; + } + // Register the key press with keyboard interface. + } + break; + case KeyRelease: + { + // Code here NAW! + } + break; + case ClientMessage: + if(string(XGetAtomName(_display, event.xclient.message_type)) == string("WM_PROTOCOLA")) { + _isRunning = true; + } + break; + default: + break; + } + } +} + +float GLXWindow::GetElapsedSeconds(void) { + unsigned int currentTime = GetTickCount(); + unsigned int diff = currentTime - _lastTime; + _lastTime = currentTime; + return float(diff) / 1000.0f); +} + \ No newline at end of file diff --git a/src/Main/GLXWindow.h b/src/Main/GLXWindow.h new file mode 100644 index 0000000..4023a33 --- /dev/null +++ b/src/Main/GLXWindow.h @@ -0,0 +1,53 @@ +#pragma once +#include +#include + +#include +#include +#include +#include +#include + +class Game; + +class GLXWindow { +public: + GLXWindow(void); + virtual ~GLXWindow(void); + + bool Create(int width, int hight, int bpp, bool fullscreen); + void Destroy(void); + void ProcessEvents(void); + void AttachGame(Game* game) { _game = game; } + + bool IsRunning(void) { return _isRunning; } + + void SwapBuffers(void) { glxSwapBuffers(_display, _xWindow); } + + float GetElapsedSeconds(void); + +private: + // A pointer to our game thingy.. + Game* _game; + // Check to see if the window is still running FFS. + bool _isRunning; + + Game* GetAttachedGame(void) { return _game; } + + unsigned int _lastTime; + + Display _display; + Window _xWindow; + GLXContext _glContext; + XF86VidModeInfo _XF86DeskMode; + XSetWindowAttributes _XSetAttr; + int _screenID; + + bool _isFullscreen; + unsigned int _width; + unsigned int _height; + unsigned int _bpp; + + bool _GL3Supported; + // I think that's about all I need for now.. FOR NOW!!! +}