00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <windows.h>
00011 #include <gl/gl.h>
00012 #include <gl/glu.h>
00013
00014 #include "wgl_window_management.h"
00015
00016 #define WM_TOGGLEFULLSCREEN (WM_USER+1) // Application Define Message For Toggling
00017
00018 static BOOL g_isProgramLooping;
00019 static BOOL g_createFullScreen;
00020
00021 void TerminateApplication (GL_Window* window)
00022 {
00023 PostMessage (window->hWnd, WM_QUIT, 0, 0);
00024 g_isProgramLooping = FALSE;
00025 }
00026
00027 void ToggleFullscreen (GL_Window* window)
00028 {
00029 PostMessage (window->hWnd, WM_TOGGLEFULLSCREEN, 0, 0);
00030 }
00031
00032 void ReshapeGL (int width, int height)
00033 {
00034 glViewport (0, 0, (GLsizei)(width), (GLsizei)(height));
00035 glMatrixMode (GL_PROJECTION);
00036 glLoadIdentity ();
00037 gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),
00038 1.0f, 100.0f);
00039 glMatrixMode (GL_MODELVIEW);
00040 glLoadIdentity ();
00041 }
00042
00043 BOOL ChangeScreenResolution (int width, int height, int bitsPerPixel)
00044 {
00045 DEVMODE dmScreenSettings;
00046 ZeroMemory (&dmScreenSettings, sizeof (DEVMODE));
00047 dmScreenSettings.dmSize = sizeof (DEVMODE);
00048 dmScreenSettings.dmPelsWidth = width;
00049 dmScreenSettings.dmPelsHeight = height;
00050 dmScreenSettings.dmBitsPerPel = bitsPerPixel;
00051 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
00052 if (ChangeDisplaySettings (&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
00053 {
00054 return FALSE;
00055 }
00056 return TRUE;
00057 }
00058
00059 BOOL CreateWindowGL (GL_Window* window)
00060 {
00061
00062 DWORD windowStyle = WS_OVERLAPPEDWINDOW;
00063 DWORD windowExtendedStyle = WS_EX_APPWINDOW;
00064
00065 PIXELFORMATDESCRIPTOR pfd =
00066 {
00067 sizeof (PIXELFORMATDESCRIPTOR),
00068 1,
00069 PFD_DRAW_TO_WINDOW |
00070 PFD_SUPPORT_OPENGL |
00071 PFD_DOUBLEBUFFER,
00072 PFD_TYPE_RGBA,
00073 window->init.bitsPerPixel,
00074 0, 0, 0, 0, 0, 0,
00075 0,
00076 0,
00077 0,
00078 0, 0, 0, 0,
00079 16,
00080 0,
00081 0,
00082 PFD_MAIN_PLANE,
00083 0,
00084 0, 0, 0
00085 };
00086
00087 RECT windowRect = {0, 0, window->init.width, window->init.height};
00088
00089 GLuint PixelFormat;
00090
00091 if (window->init.isFullScreen == TRUE)
00092 {
00093 if (ChangeScreenResolution (window->init.width, window->init.height, window->init.bitsPerPixel) == FALSE)
00094 {
00095
00096 MessageBox (HWND_DESKTOP, "Mode Switch Failed.\nRunning In Windowed Mode.", "Error", MB_OK | MB_ICONEXCLAMATION);
00097 window->init.isFullScreen = FALSE;
00098 }
00099 else
00100 {
00101 ShowCursor (FALSE);
00102 windowStyle = WS_POPUP;
00103 windowExtendedStyle |= WS_EX_TOPMOST;
00104 }
00105 }
00106 else
00107 {
00108
00109 AdjustWindowRectEx (&windowRect, windowStyle, 0, windowExtendedStyle);
00110 }
00111
00112
00113 window->hWnd = CreateWindowEx (windowExtendedStyle,
00114 window->init.application->className,
00115 window->init.title,
00116 windowStyle,
00117 0, 0,
00118 windowRect.right - windowRect.left,
00119 windowRect.bottom - windowRect.top,
00120 HWND_DESKTOP,
00121 0,
00122 window->init.application->hInstance,
00123 window);
00124
00125 if (window->hWnd == 0)
00126 {
00127 return FALSE;
00128 }
00129
00130 window->hDC = GetDC (window->hWnd);
00131
00132 if (window->hDC == 0)
00133 {
00134
00135 DestroyWindow (window->hWnd);
00136 window->hWnd = 0;
00137 return FALSE;
00138 }
00139
00140 PixelFormat = ChoosePixelFormat (window->hDC, &pfd);
00141 if (PixelFormat == 0)
00142 {
00143
00144 ReleaseDC (window->hWnd, window->hDC);
00145 window->hDC = 0;
00146 DestroyWindow (window->hWnd);
00147 window->hWnd = 0;
00148 return FALSE;
00149 }
00150
00151 if (SetPixelFormat (window->hDC, PixelFormat, &pfd) == FALSE)
00152 {
00153
00154 ReleaseDC (window->hWnd, window->hDC);
00155 window->hDC = 0;
00156 DestroyWindow (window->hWnd);
00157 window->hWnd = 0;
00158 return FALSE;
00159 }
00160
00161 window->hRC = wglCreateContext (window->hDC);
00162 if (window->hRC == 0)
00163 {
00164
00165 ReleaseDC (window->hWnd, window->hDC);
00166 window->hDC = 0;
00167 DestroyWindow (window->hWnd);
00168 window->hWnd = 0;
00169 return FALSE;
00170 }
00171
00172
00173 if (wglMakeCurrent (window->hDC, window->hRC) == FALSE)
00174 {
00175
00176 wglDeleteContext (window->hRC);
00177 window->hRC = 0;
00178 ReleaseDC (window->hWnd, window->hDC);
00179 window->hDC = 0;
00180 DestroyWindow (window->hWnd);
00181 window->hWnd = 0;
00182 return FALSE;
00183 }
00184
00185 #if USE_WINDOWS_HDC
00186 ShowWindow (window->hWnd, SW_NORMAL);
00187 #endif
00188
00189 ReshapeGL (window->init.width, window->init.height);
00190
00191 return TRUE;
00192
00193 }
00194
00195 BOOL DestroyWindowGL (GL_Window* window)
00196 {
00197 if (window->hWnd != 0)
00198 {
00199 if (window->hDC != 0)
00200 {
00201 wglMakeCurrent (window->hDC, 0);
00202 if (window->hRC != 0)
00203 {
00204 wglDeleteContext (window->hRC);
00205 window->hRC = 0;
00206
00207 }
00208 ReleaseDC (window->hWnd, window->hDC);
00209 window->hDC = 0;
00210 }
00211 DestroyWindow (window->hWnd);
00212 window->hWnd = 0;
00213 }
00214
00215 if (window->init.isFullScreen)
00216 {
00217 ChangeDisplaySettings (NULL,0);
00218 ShowCursor (TRUE);
00219 }
00220 return TRUE;
00221 }
00222
00223
00224 LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00225 {
00226 return DefWindowProc (hWnd, uMsg, wParam, lParam);
00227 }
00228
00229 BOOL RegisterWindowClass (Application* application)
00230 {
00231
00232 WNDCLASSEX windowClass;
00233 ZeroMemory (&windowClass, sizeof (WNDCLASSEX));
00234 windowClass.cbSize = sizeof (WNDCLASSEX);
00235 windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
00236 windowClass.lpfnWndProc = (WNDPROC)(WindowProc);
00237 windowClass.hInstance = application->hInstance;
00238 windowClass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE);
00239 windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
00240 windowClass.lpszClassName = application->className;
00241 if (RegisterClassEx (&windowClass) == 0)
00242 {
00243
00244 MessageBox (HWND_DESKTOP, "RegisterClassEx Failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
00245 return FALSE;
00246 }
00247 return TRUE;
00248 }
00249
00250 int wgl_init( GL_Window *window, Application *application, Keys *keys,
00251 int res_image_width, int res_image_height )
00252 {
00253
00254 application->className = "OpenGL";
00255 application->hInstance = NULL;
00256
00257
00258
00259 ZeroMemory (window, sizeof (GL_Window));
00260 window->keys = keys;
00261 window->init.application = application;
00262 window->init.title = "Omnimeeting GL HDC";
00263 window->init.width = res_image_width;
00264 window->init.height = res_image_height;
00265 window->init.bitsPerPixel = 16;
00266
00267 ZeroMemory (keys, sizeof (Keys));
00268
00269
00270 window->init.isFullScreen = FALSE;
00271
00272
00273
00274 if (RegisterWindowClass (application) == FALSE)
00275 {
00276
00277 MessageBox (HWND_DESKTOP, "Error Registering Window Class!", "Error", MB_OK | MB_ICONEXCLAMATION);
00278 return -1;
00279 }
00280
00281 g_createFullScreen = window->init.isFullScreen;
00282
00283
00284 window->init.isFullScreen = g_createFullScreen;
00285 if (CreateWindowGL (window) == TRUE)
00286 {
00287
00288 }
00289 else
00290 {
00291
00292 MessageBox (HWND_DESKTOP, "Error Creating OpenGL Window", "Error", MB_OK | MB_ICONEXCLAMATION);
00293 g_isProgramLooping = FALSE;
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 );
00303
00304 }
00305
00306