ne_ziggy

glwclass.h

Nov 8th, 2011
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. #ifndef _WINDOWS_H_
  2. #define _WINDOWS_H_
  3.  
  4. #include <windows.h>
  5. #include <math.h>
  6. #define exitError exit(GetLastError())
  7.  
  8. #ifndef _OPENGL_H_
  9.     #define _OPENGL_H_
  10.     #include <gl\gl.h>
  11. #endif
  12.  
  13. #ifndef _KEYS_H_
  14.     #define _KEYS_H_
  15.     bool VKEY[256];
  16.     #define VK_A 65
  17.     #define VK_B 66
  18.     #define VK_C 67
  19.     #define VK_D 68
  20.     #define VK_E 69
  21.     #define VK_F 70
  22.     #define VK_G 71
  23.     #define VK_H 72
  24.     #define VK_I 73
  25.     #define VK_J 74
  26.     #define VK_K 75
  27.     #define VK_L 76
  28.     #define VK_M 77
  29.     #define VK_N 78
  30.     #define VK_O 79
  31.     #define VK_P 80
  32.     #define VK_Q 81
  33.     #define VK_R 82
  34.     #define VK_S 83
  35.     #define VK_T 84
  36.     #define VK_U 85
  37.     #define VK_V 86
  38.     #define VK_W 87
  39.     #define VK_X 88
  40.     #define VK_Y 89
  41.     #define VK_Z 90
  42. #endif
  43. POINT MOUSE;
  44.  
  45. class glWindow {
  46. public:
  47.     glWindow(HINSTANCE, int, const char*, const char*, int, int, int, bool);
  48.     // glWindow(handle, min|max, window title, class name, height, width, depth, fullscreen);
  49.     ~glWindow();
  50.  
  51.     void Frustum(GLdouble, GLdouble, GLdouble, GLdouble);
  52.     void SwapBuffer();
  53.     static LRESULT CALLBACK WndProcS(HWND, UINT, WPARAM, LPARAM);
  54.  
  55.     bool ACTIVE; //minimize flag
  56.     bool FULLSCREEN;
  57.  
  58. private:
  59.     WNDCLASSEX hWC;
  60.     HGLRC hRC;
  61.     HWND hWnd;
  62.     HDC hDC;
  63.     HINSTANCE hInstance;
  64.     const char* szClass;
  65.  
  66.     LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  67.     void ResizeWindow(int, int); // ResizeWindow(height, width);
  68. };
  69.  
  70. glWindow::glWindow(HINSTANCE f_hInstance, int nCmdShow, const char* szTitle, const char* f_szClass, int f_iWidth, int f_iHeight, int iBits, bool f_bFullscreen) {
  71.     PIXELFORMATDESCRIPTOR pFD;
  72.     int     PixelFormat;
  73.     RECT    hRect;
  74.     DWORD   dwExStyle;
  75.     DWORD   dwStyle;
  76.  
  77.     //Creates windows class with OpenGL
  78.     hWC.cbSize          =   sizeof(WNDCLASSEX);
  79.     hWC.style           =   CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  80.     hWC.lpfnWndProc     =   WndProcS;
  81.     hWC.cbClsExtra      =   0;
  82.     hWC.cbWndExtra      =   0;
  83.     hWC.hInstance       =   f_hInstance;
  84.     hInstance           =   f_hInstance;
  85.     hWC.hIcon           =   LoadIcon(NULL, IDI_APPLICATION);
  86.     hWC.hCursor         =   LoadCursor(NULL, IDC_ARROW);
  87.     hWC.hbrBackground     = NULL;
  88.     hWC.lpszMenuName        =   NULL;
  89.     hWC.lpszClassName       =   f_szClass;
  90.     szClass                 =   f_szClass;
  91.     hWC.hIconSm         =   LoadIcon(NULL, IDI_APPLICATION);
  92.  
  93.     hRect.left           = (long) 0;
  94.     hRect.right          = (long) f_iWidth;
  95.     hRect.top           = (long) 0;
  96.     hRect.bottom        = (long) f_iHeight;
  97.  
  98.     //Fullscreen logic
  99.     FULLSCREEN = f_bFullscreen;
  100.     if(FULLSCREEN) {
  101.         DEVMODE dmScreenSettings;
  102.         memset(&dmScreenSettings,0,sizeof(dmScreenSettings));           // Makes Sure Memory's Cleared
  103.         dmScreenSettings.dmSize       = sizeof(dmScreenSettings);       // Size Of The Devmode Structure
  104.         dmScreenSettings.dmPelsWidth    = f_iWidth;
  105.         dmScreenSettings.dmPelsHeight   = f_iHeight;
  106.         dmScreenSettings.dmBitsPerPel   = iBits;
  107.         dmScreenSettings.dmFields     = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  108.  
  109.         if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
  110.             if (MessageBox(NULL,"Fullscreen is not supported by\nyour video card. Use Windowed Mode instead?","ERROR",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) {
  111.                 FULLSCREEN = false;
  112.                 dwExStyle   = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  113.                 dwStyle     = WS_OVERLAPPEDWINDOW;
  114.             }
  115.             else { exitError; }
  116.         }
  117.         else {
  118.             dwExStyle=WS_EX_APPWINDOW;                              // Window Extended Style
  119.             dwStyle=WS_POPUP;                                       // Windows Style
  120.             ShowCursor(FALSE);                                      // Hide Mouse Pointer
  121.         }
  122.     }
  123.     else {
  124.         dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;         // Window Extended Style
  125.         dwStyle = WS_OVERLAPPEDWINDOW;
  126.     }
  127.  
  128.     if(!RegisterClassEx(&hWC)) { exitError; }
  129.     hWnd = CreateWindowEx(dwExStyle,
  130.                          f_szClass,
  131.                          szTitle,
  132.                          dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  133.                          0, 0,
  134.                          hRect.right-hRect.left,
  135.                          hRect.bottom-hRect.top,
  136.                          NULL,
  137.                          NULL,
  138.                          f_hInstance,
  139.                          NULL);
  140.     if(!hWnd) { MessageBox(NULL,"Cannot create window.","ERROR",MB_OK|MB_ICONSTOP); exitError; }
  141.     if(!(hDC = GetDC(hWnd))) { MessageBox(NULL,"Cannot get DC.","ERROR",MB_OK|MB_ICONSTOP); exitError; }
  142.  
  143.     //pixel format descriptor for device context
  144.     ZeroMemory(&pFD, sizeof(pFD));
  145.     pFD.nSize = sizeof(pFD);
  146.     pFD.nVersion = 1;
  147.     pFD.dwFlags = PFD_DRAW_TO_WINDOW |
  148.                   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  149.     pFD.iPixelType = PFD_TYPE_RGBA;
  150.     pFD.cColorBits = 32;
  151.     pFD.cDepthBits = 24;
  152.     pFD.iLayerType = PFD_MAIN_PLANE;
  153.  
  154.     if(!SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pFD), &pFD)) { exitError; }
  155.     if(!(hRC = wglCreateContext(hDC))) { exitError; }
  156.     if(!wglMakeCurrent(hDC, hRC)) { exitError; }
  157.  
  158.     ShowWindow(hWnd, nCmdShow);
  159.         SetForegroundWindow(hWnd);
  160.         SetFocus(hWnd);
  161.         ACTIVE = true;
  162.  
  163.     //initial opengl drawing setup
  164.     ResizeWindow(f_iWidth, f_iHeight);
  165.     glClearDepth(1.0f);
  166.         glEnable(GL_DEPTH_TEST);
  167.         glDepthFunc(GL_LEQUAL);
  168. }
  169.  
  170. glWindow::~glWindow() {
  171.     if(FULLSCREEN) { ChangeDisplaySettings(NULL, 0); ShowCursor(TRUE); }
  172.     if(!wglMakeCurrent(NULL, NULL)) exitError;
  173.     if(!wglDeleteContext(hRC)) exitError;
  174.     if( hDC && !ReleaseDC(hWnd, hDC)) exitError;
  175.     if(hWnd && !DestroyWindow(hWnd)) exitError;
  176.     if(!UnregisterClass(szClass,hInstance)) exitError;
  177. }
  178.  
  179. //this static WndProc returns the private member WndProc
  180. LRESULT CALLBACK glWindow::WndProcS(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
  181.     glWindow *wnd = 0;
  182.     wnd = reinterpret_cast<glWindow *>(::GetWindowLong(hwnd, GWL_USERDATA));
  183.     return wnd->WndProc(hwnd, message, wparam, lparam);
  184. }
  185.  
  186. LRESULT CALLBACK glWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  187.     switch(uMsg) {
  188.         case WM_KEYDOWN: {
  189.             VKEY[wParam] = true;
  190.             break;
  191.         }
  192.         case WM_KEYUP: {
  193.             VKEY[wParam] = false;
  194.             break;
  195.         }
  196.         case WM_SIZE: {
  197.             ResizeWindow(LOWORD(lParam), HIWORD(lParam));
  198.             break;
  199.         }
  200.         case WM_CLOSE: {
  201.             PostQuitMessage(0);
  202.             break;
  203.         }
  204.         default: {
  205.             return DefWindowProc(hWnd, uMsg, wParam, lParam);
  206.         }
  207.     }
  208.     return 0;
  209. }
  210.  
  211. void glWindow::ResizeWindow(GLsizei width, GLsizei height) {
  212.     if (height==0) { height=1; } //prevent Divide-By-Zero
  213.  
  214.     glViewport(0,0,width,height);
  215.     glMatrixMode(GL_PROJECTION); //set up aspect ratio
  216.         glLoadIdentity();
  217.         Frustum(65.0f,(float) width/ (float) height, 0.1f, 1000.0f);
  218.  
  219.     glMatrixMode(GL_MODELVIEW); //switch back to model to continue drawing
  220.     glLoadIdentity();
  221. }
  222.  
  223. void glWindow::Frustum(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar) {
  224.     GLdouble fW, fH;
  225.  
  226.     fH = tan( fovY / 360 * PI ) * zNear;
  227.     fW = fH * aspect;
  228.     glFrustum( -fW, fW, -fH, fH, zNear, zFar );
  229. }
  230.  
  231. void glWindow::SwapBuffer(void) {
  232.     SwapBuffers(hDC);
  233.     //this is pretty much only here to keep OOP consistent (hDC is private). notice there is no 's' at the end
  234.     //of the member function
  235. }
  236.  
  237. #endif
Advertisement
Add Comment
Please, Sign In to add comment