Back to index

nux  3.0.0
GLWindowManager.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore/NuxCore.h"
00024 #include "GLWindowManager.h"
00025 
00026 namespace nux
00027 {
00028 
00029 #if defined(NUX_OS_WINDOWS)
00030   const char *WINDOW_CLASS_NAME = "InalogicWindowClass";
00031   HINSTANCE gInstance = 0;
00032 #endif
00033 
00034   DisplayAccessController *DisplayAccessController::m_pInstance = 0;
00035 
00036   DisplayAccessController::DisplayAccessController()
00037   {
00038 #if defined(NUX_OS_WINDOWS)
00039     // Register Windows Class
00040 
00041     // Get hInstance of current application.
00042     if (gInstance == 0)
00043       hInstance = GetModuleHandle(NULL);
00044     else
00045       hInstance = gInstance;
00046 
00047     WinClass.cbSize         = sizeof(WNDCLASSEX);
00048     //  CS_CLASSDC      Specifies that one device context is shared between all windows created with this class.
00049     //  CS_DBLCLKS      This is needed if you want to be able to detect double mouse clicks made on the window.
00050     //  CS_HREDRAW      The window is redrawn if there is a change in the window's width or if the window is moved horizontally.
00051     //  CS_NOCLOSE      Disables the close option on the window menu.
00052     //  CS_OWNDC        A unique device context is created for each window created. This is the opposite to CS_CLASSDC.
00053     //  CS_PARENTDC     This sets the clipping rectangle of the child window to that of the parent window. This allows the child window to be able to draw on the parent window.
00054     //  CS_VREDRAW      The window is redrawn if there is a change in the window's height or if the window is moved vertically.
00055     WinClass.style                 = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;      // Redraw On Size, And Own DC For Window.
00056     WinClass.lpfnWndProc    = (WNDPROC) WndProcManager;                      // WndProc Handles Messages
00057     WinClass.cbClsExtra     = 0;                                                           // No Extra Window Data
00058     WinClass.cbWndExtra     = 0;                                                           // No Extra Window Data
00059     WinClass.hInstance      = hInstance;                                            // Set The Instance
00060     WinClass.hIcon          = LoadIcon(hInstance, "IDI_INALOGIC"); //LoadIcon(NULL, IDI_WINLOGO);               // Load The Default Icon
00061     WinClass.hCursor        = LoadCursor(NULL, IDC_ARROW);                   // Class cursor: Load The Arrow Pointer
00062     WinClass.hbrBackground  = NULL; //(HBRUSH)GetStockObject(BLACK_BRUSH);  // No Background Required For GL
00063     WinClass.lpszMenuName   = NULL;                                                               // We Don't Want A Menu
00064     WinClass.lpszClassName  = WINDOW_CLASS_NAME; //gClassName;                                           // Set The Class Name
00065     WinClass.hIconSm        = LoadIcon(hInstance, "IDI_INALOGIC");
00066 
00067     if (!RegisterClassEx(&WinClass))                                                              // Attempt To Register The Window Class
00068     {
00069       nuxCriticalMsg("[DisplayAccessController::~DisplayAccessController] Failed to register window class name: %s.", WINDOW_CLASS_NAME);
00070     }
00071 
00072 #endif
00073 
00074   }
00075 
00076   DisplayAccessController::~DisplayAccessController()
00077   {
00078 #if defined(NUX_OS_WINDOWS)
00079 
00080     if (!UnregisterClass(WINDOW_CLASS_NAME, hInstance))               // Are We Able To Unregister Class
00081     {
00082       nuxDebugMsg("[DisplayAccessController::~DisplayAccessController] Failed to unregister window class name: %s.", WINDOW_CLASS_NAME);
00083       hInstance = NULL;                                                                   // Set hInstance To NULL
00084     }
00085 
00086 #endif
00087   }
00088 
00089   DisplayAccessController &DisplayAccessController::Instance()
00090   {
00091     if (m_pInstance == 0)
00092     {
00093       m_pInstance = new DisplayAccessController();
00094     }
00095 
00096     return *m_pInstance;
00097   }
00098 
00099   GraphicsDisplay *DisplayAccessController::CreateGLWindow(const char *WindowTitle, unsigned int WindowWidth, unsigned int WindowHeight,
00100                                                            WindowStyle Style,
00101                                                            GraphicsDisplay *GLWindow,
00102                                                            bool FullscreenFlag,
00103                                                            bool create_rendering_data)
00104   {
00105     if (GetGraphicsDisplay())
00106     {
00107       // A GlWindow already exist for this thread.
00108       nuxAssertMsg(0, "Only one GLWindow per thread is allowed");
00109       return 0;
00110     }
00111 
00112     GraphicsDisplay *glwindow = new GraphicsDisplay();
00113     glwindow->CreateOpenGLWindow(WindowTitle, WindowWidth, WindowHeight, Style, GLWindow, FullscreenFlag, create_rendering_data);
00114 
00115     return glwindow;
00116   }
00117 
00118 #if defined(NUX_OS_WINDOWS)
00119   GraphicsDisplay *DisplayAccessController::CreateFromForeignWindow(HWND WindowHandle, HDC WindowDCHandle, HGLRC OpenGLRenderingContext)
00120   {
00121     if (GetGraphicsDisplay())
00122     {
00123       // A GlWindow already exist for this thread.
00124       nuxAssertMsg(0, "Only one GLWindow per thread is allowed");
00125       return 0;
00126     }
00127 
00128     GraphicsDisplay *glwindow = new GraphicsDisplay();
00129     glwindow->CreateFromOpenGLWindow(WindowHandle, WindowDCHandle, OpenGLRenderingContext);
00130 
00131     return glwindow;
00132   }
00133 #elif defined(NUX_OS_LINUX)
00134 #ifdef NUX_OPENGLES_20
00135   GraphicsDisplay *DisplayAccessController::CreateFromForeignWindow(Display *X11Display, Window X11Window, EGLContext OpenGLContext)
00136 #else
00137   GraphicsDisplay *DisplayAccessController::CreateFromForeignWindow(Display *X11Display, Window X11Window, GLXContext OpenGLContext)
00138 #endif
00139   {
00140     if (GetGraphicsDisplay())
00141     {
00142       // A GlWindow already exist for this thread.
00143       nuxAssertMsg(0, "Only one GLWindow per thread is allowed");
00144       return 0;
00145     }
00146 
00147     GraphicsDisplay *glwindow = new GraphicsDisplay();
00148     glwindow->CreateFromOpenGLWindow(X11Display, X11Window, OpenGLContext);
00149 
00150     return glwindow;
00151   }
00152 #endif
00153 }
00154 
00155 #ifndef NUX_OPENGLES_20
00156 GLEWContext *glewGetContext()
00157 {
00158   return nux::GetGraphicsDisplay()->GetGLEWContext();
00159 }
00160 #endif
00161 
00162 #if defined(NUX_OS_WINDOWS)
00163 WGLEWContext *wglewGetContext()
00164 {
00165   return nux::GetGraphicsDisplay()->GetWGLEWContext();
00166 }
00167 #elif defined(NUX_OS_LINUX) && !defined(NUX_OPENGLES_20)
00168 GLXEWContext *glxewGetContext()
00169 {
00170   return nux::GetGraphicsDisplay()->GetGLXEWContext();
00171 }
00172 #endif