You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

182 lines
5.2 KiB

  1. #include <iostream>
  2. #include <exception>
  3. #include <windows.h>
  4. #include <Wingdi.h>
  5. #include <GL/gl.h>
  6. #include <stdio.h>
  7. #include "helper.h"
  8. #include "example.h"
  9. const char g_szClassName[] = "OpenGLWindowClass";
  10. HDC hdc;
  11. HGLRC hglrc;
  12. HWND hwnd;
  13. int done = 0;
  14. bool canRender = false;
  15. int screenw, screenh;
  16. void SysShutdown (void)
  17. {
  18. done = 1;
  19. exampleFinish();
  20. wglMakeCurrent(hdc, NULL);
  21. wglDeleteContext(hglrc);
  22. PostQuitMessage(0);
  23. }
  24. void setPixelFormat()
  25. {
  26. PIXELFORMATDESCRIPTOR pfd =
  27. {
  28. sizeof(PIXELFORMATDESCRIPTOR),
  29. 1,
  30. PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
  31. PFD_TYPE_RGBA,
  32. 32,
  33. 0,0,0,0,0,0,0,0,0,0,0,0,0, // useles parameters
  34. 16,
  35. 0,0,0,0,0,0,0
  36. };
  37. int indexPixelFormat = ChoosePixelFormat(hdc, &pfd);
  38. SetPixelFormat(hdc, indexPixelFormat, &pfd);
  39. }
  40. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  41. {
  42. try
  43. {
  44. switch(msg)
  45. {
  46. case WM_CREATE:
  47. if ((hdc = GetDC(hwnd)) == NULL) // get device context
  48. {
  49. MessageBox(hwnd, "Failed to Get the Window Device Context", "Device Context Error", MB_OK);
  50. SysShutdown();
  51. break;
  52. }
  53. setPixelFormat();
  54. if ((hglrc = wglCreateContext(hdc)) == NULL)
  55. {
  56. MessageBox(hwnd, "Failed to Create the OpenGL Rendering Context", "OpenGL Rendering Context Error", MB_OK);
  57. SysShutdown();
  58. break;
  59. }
  60. if (!wglMakeCurrent(hdc, hglrc))
  61. {
  62. MessageBox(hwnd, "Failed to make OpenGL Rendering Context current", "OpenGL Rendering Context Error", MB_OK);
  63. SysShutdown();
  64. break;
  65. }
  66. glDisable(GL_DEPTH_TEST);
  67. glDisable(GL_CULL_FACE);
  68. if (!exampleInit())
  69. {
  70. SysShutdown();
  71. break;
  72. }
  73. canRender = true;
  74. break;
  75. case WM_SIZE:
  76. screenw = LOWORD(lParam);
  77. screenh = HIWORD(lParam);
  78. break;
  79. case WM_PAINT:
  80. if (!canRender)
  81. break;
  82. glClearColor(1.0, 1.0, 1.0, 1.0);
  83. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  84. glViewport(0, 0, screenw, screenh);
  85. glMatrixMode(GL_PROJECTION);
  86. glLoadIdentity();
  87. glOrtho(0, screenw, screenh, 0, -10, 10);
  88. glMatrixMode(GL_MODELVIEW);
  89. glLoadIdentity();
  90. if (!exampleRender(screenw, screenh))
  91. SysShutdown();
  92. SwapBuffers(hdc);
  93. break;
  94. case WM_CLOSE:
  95. SysShutdown();
  96. DestroyWindow(hwnd);
  97. break;
  98. case WM_DESTROY:
  99. SysShutdown();
  100. PostQuitMessage(0);
  101. break;
  102. default:
  103. return DefWindowProc(hwnd, msg, wParam, lParam);
  104. }
  105. return 0;
  106. }
  107. catch(const std::exception& ex)
  108. {
  109. MessageBox(hwnd, ex.what(), "Exception", MB_OK);
  110. SysShutdown();
  111. }
  112. }
  113. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  114. {
  115. try
  116. {
  117. WNDCLASSEX wc;
  118. MSG Msg;
  119. wc.cbSize = sizeof(WNDCLASSEX);
  120. wc.style = 0;
  121. wc.lpfnWndProc = WndProc;
  122. wc.cbClsExtra = 0;
  123. wc.cbWndExtra = 0;
  124. wc.hInstance = hInstance;
  125. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  126. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  127. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  128. wc.lpszMenuName = NULL;
  129. wc.lpszClassName = g_szClassName;
  130. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  131. if(!RegisterClassEx(&wc))
  132. {
  133. MessageBox(NULL, "Window Registration Failed!", "Error!",
  134. MB_ICONEXCLAMATION | MB_OK);
  135. return 0;
  136. }
  137. if ((hwnd = CreateWindowEx(
  138. WS_EX_CLIENTEDGE,
  139. g_szClassName,
  140. "libTextSuite example",
  141. WS_OVERLAPPEDWINDOW,
  142. CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
  143. NULL, NULL, hInstance, NULL)) == NULL)
  144. {
  145. MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
  146. return 0;
  147. }
  148. ShowWindow(hwnd, nCmdShow);
  149. UpdateWindow(hwnd);
  150. while(GetMessage(&Msg, NULL, 0, 0) > 0 && done == 0)
  151. {
  152. TranslateMessage(&Msg);
  153. DispatchMessage(&Msg);
  154. }
  155. return Msg.wParam;
  156. }
  157. catch(const std::exception& ex)
  158. {
  159. std::cout << "Exception: " << ex.what() << std::endl;
  160. }
  161. }