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.
 
 
 
 
 

159 lines
4.3 KiB

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