app-win32.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* San Angeles Observation OpenGL ES version example
  2. * Copyright 2004-2005 Jetro Lauha
  3. * All rights reserved.
  4. * Web: http://iki.fi/jetro/
  5. *
  6. * This source is free software; you can redistribute it and/or
  7. * modify it under the terms of EITHER:
  8. * (1) The GNU Lesser General Public License as published by the Free
  9. * Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version. The text of the GNU Lesser
  11. * General Public License is included with this source in the
  12. * file LICENSE-LGPL.txt.
  13. * (2) The BSD-style license that is included with this source in
  14. * the file LICENSE-BSD.txt.
  15. *
  16. * This source is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  19. * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
  20. *
  21. * $Id: app-win32.c,v 1.6 2005/02/24 20:29:00 tonic Exp $
  22. * $Revision: 1.6 $
  23. */
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #include <tchar.h>
  27. #ifdef UNDER_CE
  28. #include <aygshell.h>
  29. #endif
  30. #include <stdio.h>
  31. #include "importgl.h"
  32. #include "app.h"
  33. int gAppAlive = 1;
  34. static HINSTANCE sInstance;
  35. static const _TCHAR sAppName[] =
  36. _T("San Angeles Observation OpenGL ES version example (Win32)");
  37. static HWND sWnd;
  38. static int sWindowWidth = WINDOW_DEFAULT_WIDTH;
  39. static int sWindowHeight = WINDOW_DEFAULT_HEIGHT;
  40. static EGLDisplay sEglDisplay = EGL_NO_DISPLAY;
  41. static EGLConfig sEglConfig;
  42. static EGLContext sEglContext = EGL_NO_CONTEXT;
  43. static EGLSurface sEglSurface = EGL_NO_SURFACE;
  44. static void checkGLErrors()
  45. {
  46. GLenum error = glGetError();
  47. if (error != GL_NO_ERROR)
  48. {
  49. _TCHAR errorString[32];
  50. _stprintf(errorString, _T("0x%04x"), error);
  51. MessageBox(NULL, errorString, _T("GL Error"), MB_OK);
  52. }
  53. }
  54. static void checkEGLErrors()
  55. {
  56. EGLint error = eglGetError();
  57. if (error != EGL_SUCCESS)
  58. {
  59. _TCHAR errorString[32];
  60. _stprintf(errorString, _T("0x%04x"), error);
  61. MessageBox(NULL, errorString, _T("EGL Initialization Error"), MB_OK);
  62. }
  63. }
  64. static BOOL initEGL(HWND wnd)
  65. {
  66. static const EGLint configAttribs[] =
  67. {
  68. #if (WINDOW_BPP == 16)
  69. EGL_RED_SIZE, 5,
  70. EGL_GREEN_SIZE, 5,
  71. EGL_BLUE_SIZE, 5,
  72. #elif (WINDOW_BPP == 32)
  73. EGL_RED_SIZE, 8,
  74. EGL_GREEN_SIZE, 8,
  75. EGL_BLUE_SIZE, 8,
  76. #else
  77. #error WINDOW_BPP must be 16 or 32
  78. #endif
  79. EGL_DEPTH_SIZE, 16,
  80. EGL_ALPHA_SIZE, EGL_DONT_CARE,
  81. EGL_STENCIL_SIZE, EGL_DONT_CARE,
  82. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  83. EGL_NONE
  84. };
  85. EGLBoolean success;
  86. EGLint numConfigs;
  87. EGLint majorVersion;
  88. EGLint minorVersion;
  89. #ifdef PVRSDK
  90. HDC dc;
  91. #endif // PVRSDK
  92. #ifndef DISABLE_IMPORTGL
  93. int importGLResult;
  94. importGLResult = importGLInit();
  95. if (!importGLResult)
  96. return FALSE;
  97. #endif // !DISABLE_IMPORTGL
  98. #ifdef PVRSDK
  99. dc = GetDC(sWnd);
  100. sEglDisplay = eglGetDisplay(dc);
  101. #else
  102. sEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  103. #endif // !PVRSDK
  104. success = eglInitialize(sEglDisplay, &majorVersion, &minorVersion);
  105. if (success != EGL_FALSE)
  106. success = eglGetConfigs(sEglDisplay, NULL, 0, &numConfigs);
  107. if (success != EGL_FALSE)
  108. success = eglChooseConfig(sEglDisplay, configAttribs,
  109. &sEglConfig, 1, &numConfigs);
  110. if (success != EGL_FALSE)
  111. {
  112. sEglSurface = eglCreateWindowSurface(sEglDisplay, sEglConfig,
  113. wnd, NULL);
  114. if (sEglSurface == EGL_NO_SURFACE)
  115. success = EGL_FALSE;
  116. }
  117. if (success != EGL_FALSE)
  118. {
  119. sEglContext = eglCreateContext(sEglDisplay, sEglConfig, NULL, NULL);
  120. if (sEglContext == EGL_NO_CONTEXT)
  121. success = EGL_FALSE;
  122. }
  123. if (success != EGL_FALSE)
  124. success = eglMakeCurrent(sEglDisplay, sEglSurface,
  125. sEglSurface, sEglContext);
  126. if (success == EGL_FALSE)
  127. checkEGLErrors();
  128. return success;
  129. }
  130. static void deinitEGL()
  131. {
  132. eglMakeCurrent(sEglDisplay, NULL, NULL, NULL);
  133. eglDestroyContext(sEglDisplay, sEglContext);
  134. eglDestroySurface(sEglDisplay, sEglSurface);
  135. eglTerminate(sEglDisplay);
  136. #ifndef DISABLE_IMPORTGL
  137. importGLDeinit();
  138. #endif // !DISABLE_IMPORTGL
  139. }
  140. static LRESULT CALLBACK wndProc(HWND wnd, UINT message,
  141. WPARAM wParam, LPARAM lParam)
  142. {
  143. RECT rc;
  144. int useDefWindowProc = 0;
  145. switch (message)
  146. {
  147. case WM_CLOSE:
  148. DestroyWindow(wnd);
  149. gAppAlive = 0;
  150. break;
  151. case WM_DESTROY:
  152. PostQuitMessage(0);
  153. gAppAlive = 0;
  154. break;
  155. case WM_KEYDOWN:
  156. if (wParam == VK_ESCAPE || wParam == VK_RETURN)
  157. gAppAlive = 0;
  158. useDefWindowProc = 1;
  159. break;
  160. case WM_KEYUP:
  161. useDefWindowProc = 1;
  162. break;
  163. case WM_SIZE:
  164. GetClientRect(sWnd, &rc);
  165. sWindowWidth = rc.right;
  166. sWindowHeight = rc.bottom;
  167. break;
  168. default:
  169. useDefWindowProc = 1;
  170. }
  171. if (useDefWindowProc)
  172. return DefWindowProc(wnd, message, wParam, lParam);
  173. return 0;
  174. }
  175. int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance,
  176. LPTSTR cmdLine, int cmdShow)
  177. {
  178. MSG msg;
  179. WNDCLASS wc;
  180. DWORD windowStyle;
  181. int windowX, windowY;
  182. // not referenced:
  183. prevInstance = prevInstance;
  184. cmdLine = cmdLine;
  185. sInstance = instance;
  186. // register class
  187. wc.style = CS_HREDRAW | CS_VREDRAW;
  188. wc.lpfnWndProc = (WNDPROC)wndProc;
  189. wc.cbClsExtra = 0;
  190. wc.cbWndExtra = 0;
  191. wc.hInstance = sInstance;
  192. wc.hIcon = NULL;
  193. wc.hCursor = 0;
  194. wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  195. wc.lpszMenuName = NULL;
  196. wc.lpszClassName = sAppName;
  197. if (!RegisterClass(&wc))
  198. return FALSE;
  199. // init instance
  200. windowStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
  201. #ifdef UNDER_CE
  202. sWindowWidth = GetSystemMetrics(SM_CXSCREEN);
  203. sWindowHeight = GetSystemMetrics(SM_CYSCREEN);
  204. windowX = windowY = 0;
  205. #else
  206. windowStyle |= WS_OVERLAPPEDWINDOW;
  207. windowX = CW_USEDEFAULT;
  208. windowY = 0;
  209. #endif
  210. sWnd = CreateWindow(sAppName, sAppName, windowStyle,
  211. windowX, windowY,
  212. sWindowWidth, sWindowHeight,
  213. NULL, NULL, instance, NULL);
  214. if (!sWnd)
  215. return FALSE;
  216. ShowWindow(sWnd, cmdShow);
  217. #ifdef UNDER_CE
  218. SHFullScreen(sWnd,
  219. SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON | SHFS_HIDESTARTICON);
  220. MoveWindow(sWnd, 0, 0, sWindowWidth, sWindowHeight, TRUE);
  221. #endif
  222. UpdateWindow(sWnd);
  223. if (!initEGL(sWnd))
  224. return FALSE;
  225. appInit(sWindowWidth, sWindowHeight);
  226. while (gAppAlive)
  227. {
  228. while (PeekMessage(&msg, sWnd, 0, 0, PM_NOREMOVE))
  229. {
  230. if (GetMessage(&msg, sWnd, 0, 0))
  231. {
  232. TranslateMessage(&msg);
  233. DispatchMessage(&msg);
  234. }
  235. else
  236. gAppAlive = 0;
  237. }
  238. if (gAppAlive)
  239. {
  240. appRender(GetTickCount(), sWindowWidth, sWindowHeight);
  241. checkGLErrors();
  242. eglSwapBuffers(sEglDisplay, sEglSurface);
  243. checkEGLErrors();
  244. }
  245. }
  246. appDeinit();
  247. deinitEGL();
  248. return 0;
  249. }