Team Fortress 2 Source Code as on 22/4/2020
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.

190 lines
3.2 KiB

  1. //
  2. // mxToolKit (c) 1999 by Mete Ciragan
  3. //
  4. // file: mxGlWindow.cpp
  5. // implementation: Win32 API
  6. // last modified: Apr 21 1999, Mete Ciragan
  7. // copyright: The programs and associated files contained in this
  8. // distribution were developed by Mete Ciragan. The programs
  9. // are not in the public domain, but they are freely
  10. // distributable without licensing fees. These programs are
  11. // provided without guarantee or warrantee expressed or
  12. // implied.
  13. //
  14. #include "mxtk/mxGlWindow.h"
  15. #include <windows.h>
  16. //#include <ostream.h"
  17. static int g_formatMode = mxGlWindow::FormatDouble;
  18. static int g_formatColorBits = 24;
  19. static int g_formatDepthBits = 16;
  20. class mxGlWindow_i
  21. {
  22. public:
  23. HDC hdc;
  24. HGLRC hglrc;
  25. };
  26. mxGlWindow::mxGlWindow (mxWindow *parent, int x, int y, int w, int h, const char *label, int style)
  27. : mxWindow (parent, x, y, w, h, label, style)
  28. {
  29. PIXELFORMATDESCRIPTOR pfd =
  30. {
  31. sizeof (PIXELFORMATDESCRIPTOR), // size of this pfd
  32. 1, // version number
  33. PFD_DRAW_TO_WINDOW | // support window
  34. PFD_SUPPORT_OPENGL | // support OpenGL
  35. PFD_DOUBLEBUFFER, // double buffered
  36. PFD_TYPE_RGBA, // RGBA type
  37. 24, // 24-bit color depth
  38. 0, 0, 0, 0, 0, 0, // color bits ignored
  39. 0, // no alpha buffer
  40. 0, // shift bit ignored
  41. 0, // no accumulation buffer
  42. 0, 0, 0, 0, // accum bits ignored
  43. 16, // 32-bit z-buffer
  44. 0, // no stencil buffer
  45. 0, // no auxiliary buffer
  46. PFD_MAIN_PLANE, // main layer
  47. 0, // reserved
  48. 0, 0, 0 // layer masks ignored
  49. };
  50. d_this = new mxGlWindow_i;
  51. pfd.cColorBits = g_formatColorBits;
  52. pfd.cDepthBits = g_formatDepthBits;
  53. bool error = false;
  54. if ((d_this->hdc = GetDC ((HWND) getHandle ())) == NULL)
  55. {
  56. error = true;
  57. goto done;
  58. }
  59. int pfm;
  60. if ((pfm = ChoosePixelFormat (d_this->hdc, &pfd)) == 0)
  61. {
  62. error = true;
  63. goto done;
  64. }
  65. if (SetPixelFormat (d_this->hdc, pfm, &pfd) == FALSE)
  66. {
  67. error = true;
  68. goto done;
  69. }
  70. DescribePixelFormat (d_this->hdc, pfm, sizeof (pfd), &pfd);
  71. if ((d_this->hglrc = wglCreateContext (d_this->hdc)) == 0)
  72. {
  73. error = true;
  74. goto done;
  75. }
  76. if (!wglMakeCurrent (d_this->hdc, d_this->hglrc))
  77. {
  78. error = true;
  79. goto done;
  80. }
  81. setType (MX_GLWINDOW);
  82. setDrawFunc (0);
  83. done:
  84. if (error)
  85. delete this;
  86. }
  87. mxGlWindow::~mxGlWindow ()
  88. {
  89. if (d_this->hglrc)
  90. {
  91. wglMakeCurrent (NULL, NULL);
  92. //wglDeleteContext (d_this->hglrc);
  93. }
  94. if (d_this->hdc)
  95. ReleaseDC ((HWND) getHandle (), d_this->hdc);
  96. delete d_this;
  97. }
  98. int
  99. mxGlWindow::handleEvent (mxEvent *event)
  100. {
  101. return 0;
  102. }
  103. void
  104. mxGlWindow::redraw ()
  105. {
  106. makeCurrent ();
  107. if (d_drawFunc)
  108. d_drawFunc ();
  109. else
  110. draw ();
  111. swapBuffers ();
  112. }
  113. void
  114. mxGlWindow::draw ()
  115. {
  116. }
  117. int
  118. mxGlWindow::makeCurrent ()
  119. {
  120. if (wglMakeCurrent (d_this->hdc, d_this->hglrc))
  121. return 1;
  122. return 0;
  123. }
  124. int
  125. mxGlWindow::swapBuffers ()
  126. {
  127. if (SwapBuffers (d_this->hdc))
  128. return 1;
  129. return 0;
  130. }
  131. void
  132. mxGlWindow::setDrawFunc (void (*func) (void))
  133. {
  134. d_drawFunc = func;
  135. }
  136. void
  137. mxGlWindow::setFormat (int mode, int colorBits, int depthBits)
  138. {
  139. g_formatMode = mode;
  140. g_formatColorBits = colorBits;
  141. g_formatDepthBits = depthBits;
  142. }