From ce6bcdd4395c9efec37a26f6c1caf8944d75f6d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Sat, 9 Apr 2011 16:09:31 +0100 Subject: [PATCH] Implement glws for WGL. --- glws.hpp | 11 +-- glws_glx.cpp | 31 +++----- glws_wgl.cpp | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+), 32 deletions(-) create mode 100644 glws_wgl.cpp diff --git a/glws.hpp b/glws.hpp index 8f4a845..7f4b52d 100644 --- a/glws.hpp +++ b/glws.hpp @@ -86,19 +86,10 @@ public: class WindowSystem { public: - Drawable *currentDrawable; - Context *currentContext; - - - inline WindowSystem() : - currentDrawable(NULL), - currentContext(NULL) - {} - virtual ~WindowSystem() {} virtual Visual * - createVisual(bool doubleBuffer=false) = 0; + createVisual(bool doubleBuffer = false) = 0; virtual Drawable * createDrawable(const Visual *visual) = 0; diff --git a/glws_glx.cpp b/glws_glx.cpp index be0a82b..3e25dc3 100644 --- a/glws_glx.cpp +++ b/glws_glx.cpp @@ -98,12 +98,9 @@ private: int screen; public: - Drawable *currentDrawable; - Context *currentContext; - XlibWindowSystem() { - display = XOpenDisplay(NULL); - screen = DefaultScreen(display); + display = XOpenDisplay(NULL); + screen = DefaultScreen(display); } ~XlibWindowSystem() { @@ -199,27 +196,17 @@ public: Window win = drawable ? dynamic_cast(drawable)->window : NULL; GLXContext ctx = context ? dynamic_cast(context)->context : NULL; - bool ret = glXMakeCurrent(display, win, ctx); - - if (drawable && context && ret) { - currentDrawable = drawable; - currentContext = context; - } else { - currentDrawable = NULL; - currentContext = NULL; - } - - return ret; + return glXMakeCurrent(display, win, ctx); } bool processEvents(void) { - while (XPending(display) > 0) { - XEvent event; - XNextEvent(display, &event); - // TODO - } - return true; + while (XPending(display) > 0) { + XEvent event; + XNextEvent(display, &event); + // TODO + } + return true; } }; diff --git a/glws_wgl.cpp b/glws_wgl.cpp new file mode 100644 index 0000000..7e58071 --- /dev/null +++ b/glws_wgl.cpp @@ -0,0 +1,205 @@ +/************************************************************************** + * + * Copyright 2011 Jose Fonseca + * All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + **************************************************************************/ + +#include "glimports.hpp" +#include "glws.hpp" + + +namespace glws { + + +class WglDrawable : public Drawable +{ +public: + DWORD dwExStyle; + DWORD dwStyle; + HWND hWnd; + HDC hDC; + PIXELFORMATDESCRIPTOR pfd; + int iPixelFormat; + + WglDrawable(const Visual *vis) : + Drawable(vis) + { + static bool first = TRUE; + RECT rect; + + if (first) { + WNDCLASS wc; + memset(&wc, 0, sizeof wc); + wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wc.lpfnWndProc = DefWindowProc; + wc.lpszClassName = "glretrace"; + wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; + RegisterClass(&wc); + first = FALSE; + } + + dwExStyle = 0; + dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_OVERLAPPEDWINDOW; + + int x = 0, y = 0, width = 256, height = 256; + + rect.left = x; + rect.top = y; + rect.right = rect.left + width; + rect.bottom = rect.top + height; + + AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle); + + hWnd = CreateWindowEx(dwExStyle, + "glretrace", /* wc.lpszClassName */ + NULL, + dwStyle, + CW_USEDEFAULT, /* x */ + CW_USEDEFAULT, /* y */ + rect.right - rect.left, /* width */ + rect.bottom - rect.top, /* height */ + NULL, + NULL, + NULL, + NULL); + hDC = GetDC(hWnd); + + memset(&pfd, 0, sizeof pfd); + pfd.cColorBits = 3; + pfd.cRedBits = 1; + pfd.cGreenBits = 1; + pfd.cBlueBits = 1; + pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL; + pfd.iLayerType = PFD_MAIN_PLANE; + pfd.iPixelType = PFD_TYPE_RGBA; + pfd.nSize = sizeof(pfd); + pfd.nVersion = 1; + + if (visual->doubleBuffer) { + pfd.dwFlags |= PFD_DOUBLEBUFFER; + } + + iPixelFormat = ChoosePixelFormat(hDC, &pfd); + + SetPixelFormat(hDC, iPixelFormat, &pfd); + } + + ~WglDrawable() { + ReleaseDC(hWnd, hDC); + DestroyWindow(hWnd); + } + + void + resize(unsigned w, unsigned h) { + Drawable::resize(w, h); + RECT rClient, rWindow; + GetClientRect(hWnd, &rClient); + GetWindowRect(hWnd, &rWindow); + w += (rWindow.right - rWindow.left) - rClient.right; + h += (rWindow.bottom - rWindow.top) - rClient.bottom; + MoveWindow(hWnd, rWindow.left, rWindow.top, w, h, TRUE); + } + + void swapBuffers(void) { + SwapBuffers(hDC); + } +}; + + +class WglContext : public Context +{ +public: + HGLRC hglrc; + + WglContext(const Visual *vis) : + Context(vis), + hglrc(0) + {} + + ~WglContext() { + if (hglrc) { + wglDeleteContext(hglrc); + } + } +}; + + +class WglWindowSystem : public WindowSystem +{ +public: + Visual * + createVisual(bool doubleBuffer) { + Visual *visual = new Visual(); + + visual->doubleBuffer = doubleBuffer; + + return visual; + } + + Drawable * + createDrawable(const Visual *visual) + { + return new WglDrawable(visual); + } + + Context * + createContext(const Visual *visual) + { + return new WglContext(visual); + } + + bool + makeCurrent(Drawable *drawable, Context *context) + { + if (!drawable || !context) { + return wglMakeCurrent(NULL, NULL); + } else { + WglDrawable *wglDrawable = dynamic_cast(drawable); + WglContext *wglContext = dynamic_cast(context); + + if (!wglContext->hglrc) { + wglContext->hglrc = wglCreateContext(wglDrawable->hDC); + if (!wglContext->hglrc) { + return false; + } + } + + return wglMakeCurrent(wglDrawable->hDC, wglContext->hglrc); + } + } + + bool + processEvents(void) { + // TODO + return true; + } +}; + + +WindowSystem *createNativeWindowSystem(void) { + return new WglWindowSystem(); +} + + +} /* namespace glretrace */ -- 2.45.2