wgl_window_management.cpp

Go to the documentation of this file.
00001 /***********************************************
00002 *                                              *
00003 *    Jeff Molofee's Revised OpenGL Basecode    *
00004 *  Huge Thanks To Maxwell Sayles & Peter Puck  *
00005 *            http://nehe.gamedev.net           *
00006 *                     2001                     *
00007 *                                              *
00008 ***********************************************/
00009 
00010 #include <windows.h>                                                                                                    // Header File For The Windows Library
00011 #include <gl/gl.h>                                                                                                              // Header File For The OpenGL32 Library
00012 #include <gl/glu.h>                                                                                                             // Header File For The GLu32 Library
00013 
00014 #include "wgl_window_management.h"                                                                              // Header File For The NeHeGL Basecode
00015 
00016 #define WM_TOGGLEFULLSCREEN (WM_USER+1)                                                                 // Application Define Message For Toggling
00017 
00018 static BOOL g_isProgramLooping;                                                                                 // Window Creation Loop, For FullScreen/Windowed Toggle                                                                                                                                         // Between Fullscreen / Windowed Mode
00019 static BOOL g_createFullScreen;                                                                                 // If TRUE, Then Create Fullscreen
00020 
00021 void TerminateApplication (GL_Window* window)                                                   // Terminate The Application
00022 {
00023         PostMessage (window->hWnd, WM_QUIT, 0, 0);                                                      // Send A WM_QUIT Message
00024         g_isProgramLooping = FALSE;                                                                                     // Stop Looping Of The Program
00025 }
00026 
00027 void ToggleFullscreen (GL_Window* window)                                                               // Toggle Fullscreen/Windowed
00028 {
00029         PostMessage (window->hWnd, WM_TOGGLEFULLSCREEN, 0, 0);                          // Send A WM_TOGGLEFULLSCREEN Message
00030 }
00031 
00032 void ReshapeGL (int width, int height)                                                                  // Reshape The Window When It's Moved Or Resized
00033 {
00034         glViewport (0, 0, (GLsizei)(width), (GLsizei)(height));                         // Reset The Current Viewport
00035         glMatrixMode (GL_PROJECTION);                                                                           // Select The Projection Matrix
00036         glLoadIdentity ();                                                                                                      // Reset The Projection Matrix
00037         gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),                      // Calculate The Aspect Ratio Of The Window
00038                                         1.0f, 100.0f);          
00039         glMatrixMode (GL_MODELVIEW);                                                                            // Select The Modelview Matrix
00040         glLoadIdentity ();                                                                                                      // Reset The Modelview Matrix
00041 }
00042 
00043 BOOL ChangeScreenResolution (int width, int height, int bitsPerPixel)   // Change The Screen Resolution
00044 {
00045         DEVMODE dmScreenSettings;                                                                                       // Device Mode
00046         ZeroMemory (&dmScreenSettings, sizeof (DEVMODE));                                       // Make Sure Memory Is Cleared
00047         dmScreenSettings.dmSize                         = sizeof (DEVMODE);                             // Size Of The Devmode Structure
00048         dmScreenSettings.dmPelsWidth            = width;                                                // Select Screen Width
00049         dmScreenSettings.dmPelsHeight           = height;                                               // Select Screen Height
00050         dmScreenSettings.dmBitsPerPel           = bitsPerPixel;                                 // Select Bits Per Pixel
00051         dmScreenSettings.dmFields                       = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
00052         if (ChangeDisplaySettings (&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
00053         {
00054                 return FALSE;                                                                                                   // Display Change Failed, Return False
00055         }
00056         return TRUE;                                                                                                            // Display Change Was Successful, Return True
00057 }
00058 
00059 BOOL CreateWindowGL (GL_Window* window)                                                                 // This Code Creates Our OpenGL Window
00060 {
00061         
00062         DWORD windowStyle = WS_OVERLAPPEDWINDOW;                                                        // Define Our Window Style
00063         DWORD windowExtendedStyle = WS_EX_APPWINDOW;                                            // Define The Window's Extended Style
00064 
00065         PIXELFORMATDESCRIPTOR pfd =                                                                                     // pfd Tells Windows How We Want Things To Be
00066         {
00067                 sizeof (PIXELFORMATDESCRIPTOR),                                                                 // Size Of This Pixel Format Descriptor
00068                 1,                                                                                                                              // Version Number
00069                 PFD_DRAW_TO_WINDOW |                                                                                    // Format Must Support Window
00070                 PFD_SUPPORT_OPENGL |                                                                                    // Format Must Support OpenGL
00071                 PFD_DOUBLEBUFFER,                                                                                               // Must Support Double Buffering
00072                 PFD_TYPE_RGBA,                                                                                                  // Request An RGBA Format
00073                 window->init.bitsPerPixel,                                                                              // Select Our Color Depth
00074                 0, 0, 0, 0, 0, 0,                                                                                               // Color Bits Ignored
00075                 0,                                                                                                                              // No Alpha Buffer
00076                 0,                                                                                                                              // Shift Bit Ignored
00077                 0,                                                                                                                              // No Accumulation Buffer
00078                 0, 0, 0, 0,                                                                                                             // Accumulation Bits Ignored
00079                 16,                                                                                                                             // 16Bit Z-Buffer (Depth Buffer)  
00080                 0,                                                                                                                              // No Stencil Buffer
00081                 0,                                                                                                                              // No Auxiliary Buffer
00082                 PFD_MAIN_PLANE,                                                                                                 // Main Drawing Layer
00083                 0,                                                                                                                              // Reserved
00084                 0, 0, 0                                                                                                                 // Layer Masks Ignored
00085         };
00086 
00087         RECT windowRect = {0, 0, window->init.width, window->init.height};      // Define Our Window Coordinates
00088 
00089         GLuint PixelFormat;                                                                                                     // Will Hold The Selected Pixel Format
00090 
00091         if (window->init.isFullScreen == TRUE)                                                          // Fullscreen Requested, Try Changing Video Modes
00092         {
00093                 if (ChangeScreenResolution (window->init.width, window->init.height, window->init.bitsPerPixel) == FALSE)
00094                 {
00095                         // Fullscreen Mode Failed.  Run In Windowed Mode Instead
00096                         MessageBox (HWND_DESKTOP, "Mode Switch Failed.\nRunning In Windowed Mode.", "Error", MB_OK | MB_ICONEXCLAMATION);
00097                         window->init.isFullScreen = FALSE;                                                      // Set isFullscreen To False (Windowed Mode)
00098                 }
00099                 else                                                                                                                    // Otherwise (If Fullscreen Mode Was Successful)
00100                 {
00101                         ShowCursor (FALSE);                                                                                     // Turn Off The Cursor
00102                         windowStyle = WS_POPUP;                                                                         // Set The WindowStyle To WS_POPUP (Popup Window)
00103                         windowExtendedStyle |= WS_EX_TOPMOST;                                           // Set The Extended Window Style To WS_EX_TOPMOST
00104                 }                                                                                                                               // (Top Window Covering Everything Else)
00105         }
00106         else                                                                                                                            // If Fullscreen Was Not Selected
00107         {
00108                 // Adjust Window, Account For Window Borders
00109                 AdjustWindowRectEx (&windowRect, windowStyle, 0, windowExtendedStyle);
00110         }
00111 
00112         // Create The OpenGL Window
00113         window->hWnd = CreateWindowEx (windowExtendedStyle,                                     // Extended Style
00114                                                                    window->init.application->className, // Class Name
00115                                                                    window->init.title,                                  // Window Title
00116                                                                    windowStyle,                                                 // Window Style
00117                                                                    0, 0,                                                                // Window X,Y Position
00118                                                                    windowRect.right - windowRect.left,  // Window Width
00119                                                                    windowRect.bottom - windowRect.top,  // Window Height
00120                                                                    HWND_DESKTOP,                                                // Desktop Is Window's Parent
00121                                                                    0,                                                                   // No Menu
00122                                                                    window->init.application->hInstance, // Pass The Window Instance
00123                                                                    window);
00124 
00125         if (window->hWnd == 0)                                                                                          // Was Window Creation A Success?
00126         {
00127                 return FALSE;                                                                                                   // If Not Return False
00128         }
00129 
00130         window->hDC = GetDC (window->hWnd);                                                                     // Grab A Device Context For This Window
00131 
00132         if (window->hDC == 0)                                                                                           // Did We Get A Device Context?
00133         {
00134                 // Failed
00135                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00136                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00137                 return FALSE;                                                                                                   // Return False
00138         }
00139 
00140         PixelFormat = ChoosePixelFormat (window->hDC, &pfd);                            // Find A Compatible Pixel Format
00141         if (PixelFormat == 0)                                                                                           // Did We Find A Compatible Format?
00142         {
00143                 // Failed
00144                 ReleaseDC (window->hWnd, window->hDC);                                                  // Release Our Device Context
00145                 window->hDC = 0;                                                                                                // Zero The Device Context
00146                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00147                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00148                 return FALSE;                                                                                                   // Return False
00149         }
00150 
00151         if (SetPixelFormat (window->hDC, PixelFormat, &pfd) == FALSE)           // Try To Set The Pixel Format
00152         {
00153                 // Failed
00154                 ReleaseDC (window->hWnd, window->hDC);                                                  // Release Our Device Context
00155                 window->hDC = 0;                                                                                                // Zero The Device Context
00156                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00157                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00158                 return FALSE;                                                                                                   // Return False
00159         }
00160 
00161         window->hRC = wglCreateContext (window->hDC);                                           // Try To Get A Rendering Context
00162         if (window->hRC == 0)                                                                                           // Did We Get A Rendering Context?
00163         {
00164                 // Failed
00165                 ReleaseDC (window->hWnd, window->hDC);                                                  // Release Our Device Context
00166                 window->hDC = 0;                                                                                                // Zero The Device Context
00167                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00168                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00169                 return FALSE;                                                                                                   // Return False
00170         }
00171 
00172         // Make The Rendering Context Our Current Rendering Context
00173         if (wglMakeCurrent (window->hDC, window->hRC) == FALSE)
00174         {
00175                 // Failed
00176                 wglDeleteContext (window->hRC);                                                                 // Delete The Rendering Context
00177                 window->hRC = 0;                                                                                                // Zero The Rendering Context
00178                 ReleaseDC (window->hWnd, window->hDC);                                                  // Release Our Device Context
00179                 window->hDC = 0;                                                                                                // Zero The Device Context
00180                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00181                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00182                 return FALSE;                                                                                                   // Return False
00183         }
00184 
00185 #if USE_WINDOWS_HDC
00186         ShowWindow (window->hWnd, SW_NORMAL);                                                           // Make The Window Visible
00187 #endif /* USE_WINDOWS_HDC */
00188 
00189         ReshapeGL (window->init.width, window->init.height);                            // Reshape Our GL Window
00190 
00191         return TRUE;                                                                                                            // Window Creating Was A Success
00192                                                                                                                                                 // Initialization Will Be Done In WM_CREATE
00193 }
00194 
00195 BOOL DestroyWindowGL (GL_Window* window)                                                                // Destroy The OpenGL Window & Release Resources
00196 {
00197         if (window->hWnd != 0)                                                                                          // Does The Window Have A Handle?
00198         {       
00199                 if (window->hDC != 0)                                                                                   // Does The Window Have A Device Context?
00200                 {
00201                         wglMakeCurrent (window->hDC, 0);                                                        // Set The Current Active Rendering Context To Zero
00202                         if (window->hRC != 0)                                                                           // Does The Window Have A Rendering Context?
00203                         {
00204                                 wglDeleteContext (window->hRC);                                                 // Release The Rendering Context
00205                                 window->hRC = 0;                                                                                // Zero The Rendering Context
00206 
00207                         }
00208                         ReleaseDC (window->hWnd, window->hDC);                                          // Release The Device Context
00209                         window->hDC = 0;                                                                                        // Zero The Device Context
00210                 }
00211                 DestroyWindow (window->hWnd);                                                                   // Destroy The Window
00212                 window->hWnd = 0;                                                                                               // Zero The Window Handle
00213         }
00214 
00215         if (window->init.isFullScreen)                                                                          // Is Window In Fullscreen Mode
00216         {
00217                 ChangeDisplaySettings (NULL,0);                                                                 // Switch Back To Desktop Resolution
00218                 ShowCursor (TRUE);                                                                                              // Show The Cursor
00219         }       
00220         return TRUE;                                                                                                            // Return True
00221 }
00222 
00223 // Process Window Message Callbacks
00224 LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00225 {
00226         return DefWindowProc (hWnd, uMsg, wParam, lParam);                                      // Pass Unhandled Messages To DefWindowProc
00227 }
00228 
00229 BOOL RegisterWindowClass (Application* application)                                             // Register A Window Class For This Application.
00230 {                                                                                                                                               // TRUE If Successful
00231         // Register A Window Class
00232         WNDCLASSEX windowClass;                                                                                         // Window Class
00233         ZeroMemory (&windowClass, sizeof (WNDCLASSEX));                                         // Make Sure Memory Is Cleared
00234         windowClass.cbSize                      = sizeof (WNDCLASSEX);                                  // Size Of The windowClass Structure
00235         windowClass.style                       = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraws The Window For Any Movement / Resizing
00236         windowClass.lpfnWndProc         = (WNDPROC)(WindowProc);                                // WindowProc Handles Messages
00237         windowClass.hInstance           = application->hInstance;                               // Set The Instance
00238         windowClass.hbrBackground       = (HBRUSH)(COLOR_APPWORKSPACE);                 // Class Background Brush Color
00239         windowClass.hCursor                     = LoadCursor(NULL, IDC_ARROW);                  // Load The Arrow Pointer
00240         windowClass.lpszClassName       = application->className;                               // Sets The Applications Classname
00241         if (RegisterClassEx (&windowClass) == 0)                                                        // Did Registering The Class Fail?
00242         {
00243                 // NOTE: Failure, Should Never Happen
00244                 MessageBox (HWND_DESKTOP, "RegisterClassEx Failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
00245                 return FALSE;                                                                                                   // Return False (Failure)
00246         }
00247         return TRUE;                                                                                                            // Return True (Success)
00248 }
00249 
00250 int wgl_init( GL_Window *window, Application *application, Keys *keys, 
00251                           int res_image_width, int res_image_height ) 
00252 {
00253         // Fill Out Application Data
00254         application->className = "OpenGL";                                                                      // Application Class Name
00255         application->hInstance = NULL/*hInstance*/;                                                                     // Application Instance
00256 
00257 
00258         // Fill Out Window
00259         ZeroMemory (window, sizeof (GL_Window));                                                        // Make Sure Memory Is Zeroed
00260         window->keys                                    = keys;                                                         // Window Key Structure
00261         window->init.application                = application;                                          // Window Application
00262         window->init.title                      = "Omnimeeting GL HDC";                                 // Window Title
00263         window->init.width                      = res_image_width;                                              // Window Width
00264         window->init.height                     = res_image_height;                                             // Window Height
00265         window->init.bitsPerPixel       = 16;                                                                   // Bits Per Pixel
00266 
00267         ZeroMemory (keys, sizeof (Keys));                                                                       // Zero keys Structure
00268 
00269         // Ask The User If They Want To Start In FullScreen Mode?
00270         window->init.isFullScreen = FALSE;                                                                      // Run In Windowed Mode
00271 
00272 
00273         // Register A Class For Our Window To Use
00274         if (RegisterWindowClass (application) == FALSE)                                 // Did Registering A Class Fail?
00275         {
00276                 // Failure
00277                 MessageBox (HWND_DESKTOP, "Error Registering Window Class!", "Error", MB_OK | MB_ICONEXCLAMATION);
00278                 return -1;                                                                                                              // Terminate Application
00279         }
00280 
00281         g_createFullScreen = window->init.isFullScreen;                                         // g_createFullScreen Is Set To User Default
00282         // Create A Window
00283 
00284         window->init.isFullScreen = g_createFullScreen;                                 // Set Init Param Of Window Creation To Fullscreen?
00285         if (CreateWindowGL (window) == TRUE)                                                    // Was Window Creation Successful?
00286         {
00287                 /* o_O */
00288         }
00289         else                                                                                                                    // If Window Creation Failed
00290         {
00291                 // Error Creating Window
00292                 MessageBox (HWND_DESKTOP, "Error Creating OpenGL Window", "Error", MB_OK | MB_ICONEXCLAMATION);
00293                 g_isProgramLooping = FALSE;                                                                     // Terminate The Loop
00294         }
00295 
00296         return 0;
00297 }
00298 
00299 void wgl_deinit(GL_Window *window, Application *application, Keys *keys) {
00300         
00301         DestroyWindowGL( window );
00302         UnregisterClass ( application->className, application->hInstance );             // UnRegister Window Class
00303 
00304 }
00305 
00306 

Generated on Tue Dec 26 10:32:39 2006 for Omnimeeting by  doxygen 1.4.7