/[pcsx2_0.9.7]/trunk/plugins/zzogl-pg/opengl/GLWin32.cpp
ViewVC logotype

Contents of /trunk/plugins/zzogl-pg/opengl/GLWin32.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 2 months ago) by william
File size: 10585 byte(s)
re-commit (had local access denied errors when committing)
1 /* ZZ Open GL graphics plugin
2 * Copyright (c)2009-2010 zeydlitz@gmail.com, arcum42@gmail.com
3 * Based on Zerofrog's ZeroGS KOSMOS (c)2005-2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "GS.h"
21 #include "GLWin.h"
22
23 #ifdef GL_WIN32_WINDOW
24
25 HWND GShwnd = NULL;
26 HDC hDC = NULL; // Private GDI Device Context
27 HGLRC hRC = NULL; // Permanent Rendering Context
28
29 LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
30 {
31 static int nWindowWidth = 0, nWindowHeight = 0;
32
33 switch (msg)
34 {
35 case WM_DESTROY:
36 PostQuitMessage(0);
37 return 0;
38
39 case WM_KEYDOWN:
40 // switch(wParam) {
41 // case VK_ESCAPE:
42 // SendMessage(hWnd, WM_DESTROY, 0L, 0L);
43 // break;
44 // }
45 break;
46
47 case WM_SIZE:
48 nWindowWidth = lParam & 0xffff;
49 nWindowHeight = lParam >> 16;
50 GLWin.UpdateWindowSize(nWindowWidth, nWindowHeight);
51 break;
52
53 case WM_SIZING:
54 // if button is 0, then just released so can resize
55 if (GetSystemMetrics(SM_SWAPBUTTON) ? !GetAsyncKeyState(VK_RBUTTON) : !GetAsyncKeyState(VK_LBUTTON))
56 {
57 SetDeviceSize(nWindowWidth, nWindowHeight);
58 }
59 break;
60
61 case WM_SETCURSOR:
62 SetCursor(NULL);
63 break;
64 }
65
66 return DefWindowProc(hWnd, msg, wParam, lParam);
67 }
68
69 bool GLWindow::CreateWindow(void *pDisplay)
70 {
71 RECT rc, rcdesktop;
72 rc.left = 0;
73 rc.top = 0;
74 rc.right = conf.width;
75 rc.bottom = conf.height;
76
77 WNDCLASSEX wc;
78 HINSTANCE hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
79 DWORD dwExStyle, dwStyle;
80
81 wc.cbSize = sizeof(WNDCLASSEX);
82 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Move, And Own DC For Window
83 wc.lpfnWndProc = (WNDPROC) MsgProc; // MsgProc Handles Messages
84 wc.cbClsExtra = 0; // No Extra Window Data
85 wc.cbWndExtra = 0; // No Extra Window Data
86 wc.hInstance = hInstance; // Set The Instance
87 wc.hIcon = NULL;
88 wc.hIconSm = NULL; // Load The Default Icon
89 wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
90 wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // No Background Required For GL
91 wc.lpszMenuName = NULL; // We Don't Want A Menu
92 wc.lpszClassName = "PS2EMU_ZEROGS"; // Set The Class Name
93
94 RegisterClassEx(&wc);
95
96 if (conf.fullscreen())
97 {
98 dwExStyle = WS_EX_APPWINDOW;
99 dwStyle = WS_POPUP;
100 }
101 else
102 {
103 dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
104 dwStyle = WS_OVERLAPPEDWINDOW | WS_BORDER;
105 }
106
107 dwStyle |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
108 AdjustWindowRectEx(&rc, dwStyle, false, dwExStyle);
109
110 GetWindowRect(GetDesktopWindow(), &rcdesktop);
111
112 GShwnd = CreateWindowEx( dwExStyle, // Extended Style For The Window
113 "PS2EMU_ZEROGS", // Class Name
114 "ZZOgl", // Window Title
115 dwStyle, // Selected Window Style
116 (rcdesktop.right - (rc.right - rc.left)) / 2, // Window Position
117 (rcdesktop.bottom - (rc.bottom - rc.top)) / 2, // Window Position
118 rc.right - rc.left, // Calculate Adjusted Window Width
119 rc.bottom - rc.top, // Calculate Adjusted Window Height
120 NULL, // No Parent Window
121 NULL, // No Menu
122 hInstance, // Instance
123 NULL); // Don't Pass Anything To WM_CREATE
124
125 if (GShwnd == NULL)
126 {
127 ZZLog::Error_Log("Failed to create window. Exiting...");
128 return false;
129 }
130
131 if (pDisplay != NULL) *(HWND*)pDisplay = GShwnd;
132
133 // set just in case
134 SetWindowLongPtr(GShwnd, GWLP_WNDPROC, (LPARAM)(WNDPROC)MsgProc);
135
136 ShowWindow(GShwnd, SW_SHOWDEFAULT);
137
138 UpdateWindow(GShwnd);
139
140 SetFocus(GShwnd);
141
142 if (pDisplay == NULL) ZZLog::Error_Log("Failed to create window. Exiting...");
143 return (pDisplay != NULL);
144 }
145
146 bool GLWindow::ReleaseContext()
147 {
148 if (hRC) // Do We Have A Rendering Context?
149 {
150 if (!wglMakeCurrent(NULL, NULL)) // Are We Able To Release The DC And RC Contexts?
151 {
152 MessageBox(NULL, "Release Of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
153 }
154
155 if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
156 {
157 MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
158 }
159
160 hRC = NULL; // Set RC To NULL
161 }
162
163 if (hDC && !ReleaseDC(GShwnd, hDC)) // Are We Able To Release The DC
164 {
165 MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
166 hDC = NULL; // Set DC To NULL
167 }
168
169 return true;
170 }
171
172 void GLWindow::CloseWindow()
173 {
174 if (GShwnd != NULL)
175 {
176 DestroyWindow(GShwnd);
177 GShwnd = NULL;
178 }
179 }
180
181 bool GLWindow::DisplayWindow(int _width, int _height)
182 {
183 GLuint PixelFormat; // Holds The Results After Searching For A Match
184 DWORD dwExStyle; // Window Extended Style
185 DWORD dwStyle; // Window Style
186
187 RECT rcdesktop;
188 GetWindowRect(GetDesktopWindow(), &rcdesktop);
189
190 if (conf.fullscreen())
191 {
192 backbuffer.w = rcdesktop.right - rcdesktop.left;
193 backbuffer.h = rcdesktop.bottom - rcdesktop.top;
194
195 dwExStyle = WS_EX_APPWINDOW;
196 dwStyle = WS_POPUP;
197 ShowCursor(false);
198 }
199 else
200 {
201 dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
202 dwStyle = WS_OVERLAPPEDWINDOW;
203 backbuffer.w = _width;
204 backbuffer.h = _height;
205 }
206 dwStyle |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
207
208 RECT rc;
209
210 rc.left = 0;
211 rc.top = 0;
212 rc.right = backbuffer.h;
213 rc.bottom = backbuffer.h;
214 AdjustWindowRectEx(&rc, dwStyle, false, dwExStyle);
215 int X = (rcdesktop.right - rcdesktop.left) / 2 - (rc.right - rc.left) / 2;
216 int Y = (rcdesktop.bottom - rcdesktop.top) / 2 - (rc.bottom - rc.top) / 2;
217
218 SetWindowLong(GShwnd, GWL_STYLE, dwStyle);
219 SetWindowLong(GShwnd, GWL_EXSTYLE, dwExStyle);
220
221 SetWindowPos(GShwnd, HWND_TOP, X, Y, rc.right - rc.left, rc.bottom - rc.top, SWP_SHOWWINDOW);
222
223 if (conf.fullscreen())
224 {
225 DEVMODE dmScreenSettings;
226 memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
227 dmScreenSettings.dmSize = sizeof(dmScreenSettings);
228 dmScreenSettings.dmPelsWidth = backbuffer.w;
229 dmScreenSettings.dmPelsHeight = backbuffer.h;
230 dmScreenSettings.dmBitsPerPel = 32;
231 dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
232
233 // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
234
235 if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
236 {
237 if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe GL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
238 conf.setFullscreen(false);
239 else
240 return false;
241 }
242 }
243 else
244 {
245 // change to default resolution
246 ChangeDisplaySettings(NULL, 0);
247 }
248
249 PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
250
251 {
252 sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
253 1, // Version Number
254 PFD_DRAW_TO_WINDOW | // Format Must Support Window
255 PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
256 PFD_DOUBLEBUFFER, // Must Support Double Buffering
257 PFD_TYPE_RGBA, // Request An RGBA Format
258 32, // Select Our Color Depth
259 0, 0, 0, 0, 0, 0, // Color Bits Ignored
260 0, // 8bit Alpha Buffer
261 0, // Shift Bit Ignored
262 0, // No Accumulation Buffer
263 0, 0, 0, 0, // Accumulation Bits Ignored
264 24, // 24Bit Z-Buffer (Depth Buffer)
265 8, // 8bit Stencil Buffer
266 0, // No Auxiliary Buffer
267 PFD_MAIN_PLANE, // Main Drawing Layer
268 0, // Reserved
269 0, 0, 0 // Layer Masks Ignored
270 };
271
272 if (!(hDC = GetDC(GShwnd)))
273 {
274 MessageBox(NULL, "(1) Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
275 return false;
276 }
277
278 if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
279 {
280 MessageBox(NULL, "(2) Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
281 return false;
282 }
283
284 if (!SetPixelFormat(hDC, PixelFormat, &pfd))
285 {
286 MessageBox(NULL, "(3) Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
287 return false;
288 }
289
290 if (!(hRC = wglCreateContext(hDC)))
291 {
292 MessageBox(NULL, "(4) Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
293 return false;
294 }
295
296 if (!wglMakeCurrent(hDC, hRC))
297 {
298 MessageBox(NULL, "(5) Can't Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
299 return false;
300 }
301
302 UpdateWindow(GShwnd);
303
304 return true;
305 }
306
307 void GLWindow::SwapGLBuffers()
308 {
309 static u32 lastswaptime = 0;
310
311 if (glGetError() != GL_NO_ERROR) ZZLog::Debug_Log("glError before swap!");
312
313 SwapBuffers(hDC);
314 lastswaptime = timeGetTime();
315 }
316
317 void GLWindow::SetTitle(char *strtitle)
318 {
319 if (!conf.fullscreen()) SetWindowText(GShwnd, strtitle);
320 }
321
322 void GLWindow::ResizeCheck()
323 {
324
325 }
326
327
328 extern void ChangeDeviceSize(int nNewWidth, int nNewHeight);
329
330 void GLWindow::ProcessEvents()
331 {
332 MSG msg;
333
334 ZeroMemory(&msg, sizeof(msg));
335
336 while (1)
337 {
338 if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
339 {
340 switch (msg.message)
341 {
342 case WM_KEYDOWN :
343 int my_KeyEvent = msg.wParam;
344 bool my_bShift = !!(GetKeyState(VK_SHIFT) & 0x8000);
345
346 switch (msg.wParam)
347 {
348 case VK_F5:
349 case VK_F6:
350 case VK_F7:
351 case VK_F9:
352 OnFKey(msg.wParam - VK_F1 + 1, my_bShift);
353 break;
354
355 case VK_ESCAPE:
356
357 if (conf.fullscreen())
358 {
359 // destroy that msg
360 conf.setFullscreen(false);
361 ChangeDeviceSize(conf.width, conf.height);
362 UpdateWindow(GShwnd);
363 continue; // so that msg doesn't get sent
364 }
365 else
366 {
367 SendMessage(GShwnd, WM_DESTROY, 0, 0);
368 return;
369 }
370
371 break;
372 }
373
374 break;
375 }
376
377 TranslateMessage(&msg);
378 DispatchMessage(&msg);
379 }
380 else
381 {
382 break;
383 }
384 }
385
386 if ((GetKeyState(VK_MENU) & 0x8000) && (GetKeyState(VK_RETURN) & 0x8000))
387 {
388 conf.zz_options.fullscreen = !conf.zz_options.fullscreen;
389
390 SetDeviceSize(
391 (conf.fullscreen()) ? 1280 : conf.width,
392 (conf.fullscreen()) ? 960 : conf.height);
393 }
394 }
395
396
397 #endif

  ViewVC Help
Powered by ViewVC 1.1.22