Advertisement
Guest User

GLWindow.cpp

a guest
Dec 3rd, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.49 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #define WIN32_EXTRA_LEAN
  3.  
  4. #include <windows.h>
  5. #include "GLWindow.h"
  6. #include "Example.h"
  7.  
  8. int WINAPI WinMain(HINSTANCE hInstance,
  9.                    HINSTANCE hPrevInstance,
  10.                    LPSTR cmdLine,
  11.                    int cmdShow){
  12.    
  13.     //Set our window settings
  14.     const int windowWidth = 1024;
  15.     const int windowHeight = 768;
  16.     const int windowBPP = 32;
  17.     const int windowFullscreen = false;
  18.  
  19.     //This is out window
  20.     GLWindow programWindow(hInstance);
  21.  
  22.     //The example of the GL code
  23.     Example example;
  24.  
  25.     //Attach our example to our window
  26.     programWindow.attachExample(&example);
  27.  
  28.     //Attempt to create the window
  29.     if (!programWindow.create(windowWidth, windowHeight, windowBPP, windowFullscreen)){
  30.         MessageBox(NULL, "Unable to create the OpenGL Window", "An error occurred", MB_ICONERROR|MB_OK);
  31.         programWindow.destroy();
  32.         return 1;
  33.     }
  34.  
  35.     if (!example.init()){ //Initialise our example
  36.         MessageBox(NULL, "Could not initialise the application", "An error occurred", MB_ICONERROR|MB_OK);
  37.         programWindow.destroy();
  38.         return 1;
  39.     }
  40.  
  41.     //This is the main loop, we render frames until isRunning is false
  42.     while(programWindow.isRunning()){
  43.         programWindow.processEvents(); //Process any window events
  44.  
  45.         //We get the time that's passed since the previous frame
  46.         float elapsedTime = programWindow.getElapsedSeconds();
  47.  
  48.         example.prepare(elapsedTime); //Do any pre-rensering logic
  49.         example.render();
  50.  
  51.         programWindow.swapBuffers();
  52.     }
  53. }
  54.  
  55. bool GLWindow::create(int width, int height, int bpp, bool fullscreen){
  56.     DWORD   dwExStyle;      //Window extended style
  57.     DWORD   dwStyle;        //Window style
  58.  
  59.     m_isFullscreen = fullscreen;
  60.  
  61.     m_windowRect.left = (long)0; //Set left value to 0
  62.     m_windowRect.right = (long)width;
  63.     m_windowRect.top = (long)0;
  64.     m_windowRect.bottom = (long)height;
  65.  
  66.     //Fill out the window class structure]
  67.     m_windowClass.cbSIze                    = sizeof(WNDCLASSEX);
  68.     m_windowClass.style                     = CS_HREDRAW | CS_VREDRAW;
  69.     m_windowClass.lpfnWndProc               = GLWindow::StaticWndProc; //We set our static member as the event handler
  70.     m_windowClass.cbClsExtra                = 0;
  71.     m_windowClass.cbWndExtra                = 0;
  72.     m_windowClass.hInstance                 = m_hInstance;
  73.     m_windowClass.hIcon                     = LoadIcon(NULL, IDI_APPLICATION); //default
  74.     m_windowClass.hCurcor                   = LoadCursor(NULL, IDC_ARROW); //default
  75.     m_windowClass.hbrBackground             = NULL;
  76.     m_windowClass.lpszMenuName              = NULL;
  77.     m_windowClass.lpszClassName             = "GLClass";
  78.     m_windowClass.hIconSm                   = LoadIcon(NULL, IDI_WINLOGO); //Windows logo small icon
  79.  
  80.     //register the windows class
  81.     if (!registerClassEx(&m_windowClass)){
  82.         MessageBox(NULL, "Failed to register window class", NULL, MB_OK);
  83.         return false;
  84.     }
  85.  
  86.  
  87.     if (m_isFullscreen){ //If we are fullscreen, we need to change the display mode
  88.         DEVMODE dmScreenSettings;
  89.  
  90.         memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
  91.         dmScreenSettings.dmSize = sizeof(dmScreenSettings);
  92.  
  93.         dmScreenSettings.dmPelsWidth = width;       //Screen width
  94.         dmScreenSettings.dmPelsHeight = height;     //Screen height
  95.         dmScreenSettings.dmBitsPerPel = bpp;        //Bits per pixel
  96.         dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  97.  
  98.         if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
  99.             //Setting display mode failed, switch to windowed
  100.             MessageBox(NULL, "Display mode failed", NULL, MB_OK);
  101.             m_isFullscreen = false;
  102.         }
  103.     }
  104.  
  105.     if (m_isFullscreen){
  106.         dwExStyle = WS_EX_APPWINDOW;
  107.         dwStyle = WS_POPUP;
  108.         ShowCursor(false);
  109.     }else{
  110.         dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  111.         dwStyle = WS_OVERLAPPEDWINDOW;
  112.     }
  113.  
  114.     AdjustWindowRectEx(&m_windowRect, dwStyle, false, dwExStyle); //Adjust window to true requested size
  115.  
  116.     //Class registered, so now create out window
  117.     m_hwnd = CreateWindowEx(Null,                                           //Extended style
  118.                             "GLClass",                                      //Class name
  119.                             "BOGLGP - Chapter 2 - OpenGL Application",      //App name
  120.                             dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,   
  121.                             0,0,                                            //x,y coordinate
  122.                             m_windowRect.right - m_windowRect.left,         //width
  123.                             m_windowRect.bottom - m_windowRect.top,         //height
  124.                             NULL,                                           //handle to parent
  125.                             NULL,                                           //handle to menu
  126.                             m_hInstance,                                    //application instance
  127.                             this);                                          //We pass a pointer to the GLWindow here
  128.  
  129.     //Check if window creation failed (hwnd would equal NULL)
  130.     if (!m_hwnd)
  131.         return 0;
  132.  
  133.     m_hdc = GetDC(m_hwnd);
  134.  
  135.     ShowWindow(m_hwnd, SW_SHOW);    //display the window
  136.     UpdateWindow(m_hwnd);           //update the window
  137.  
  138.     m_lastTime = GetTickCount() / 1000.0f; //Initialise the timer
  139.     return true;
  140.  
  141. }
  142.  
  143. LRESULT CALLBACK GLWindow::StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  144.     GLWindow* window = NULL;
  145.  
  146.     //If this is the create message
  147.     if (uMsg == WM_CREATE){
  148.         //Get the pointer we stored during create
  149.         window = (GLWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
  150.  
  151.         //Associate the window pointer with the hwnd for the other events to access
  152.         SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)window);
  153.     }else{
  154.         //if this is not a creation event then we should have stored a pointer to the window
  155.         window = (GLWindow*)GetWindowLongPtr(hWnd, GWL_USERDATA);
  156.  
  157.         if (!window){
  158.             //Do the default event handling
  159.             return DefWindowProc(hWnd, uMsg, wParam, lParam);
  160.         }
  161.     }
  162.  
  163.     //Call out window's member WndProc (allows us to access member variables)
  164.     return window->WndProc(hWnd, uMsg, wParam, lParam);
  165. }
  166.  
  167. LRESULT GLWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  168. {
  169.     switch(uMsg)
  170.     {
  171.     case WM_CREATE:
  172.     {
  173.         m_hdc = GetDC(hWnd);
  174.         setupPixelFormat();
  175.  
  176.         //Set the version that we want, in this case 3.0
  177.         int attribs[]={
  178.             WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
  179.             WGL_CONTEXT_MINOR_VERSION_ARB, 0,
  180.             0}; //zero indicated the end of the array
  181.  
  182.         //Create temporary context so we can get a pointer to the function
  183.         HGLRC tmpContext = wglCreateContext(m_hdc);
  184.         //Make it current
  185.         wglMakeCurrent(m_hdc, tmpContext);
  186.  
  187.         //Get function pointer
  188.         wglCreateContextAttribsARB = (PFNWGLCREATETEXTATTRIBSARBPROC) wglGetPrcoAddress("wglCreateContextAttribsARB");
  189.  
  190.  
  191.         //If this is NULL then OpenGL 3.0 is not supported
  192.         if (!wglCreateContextARB)
  193.         {
  194.             MessageBox(NULL, "OpenGL 3.0 is not supported", "An error occurred", MB_ICONERROR | MB_OK);
  195.             DestroyWindow(hWnd);
  196.             return 0;
  197.         }
  198.  
  199.         //Create an OpenGL 3.0 context using the new function
  200.         m_hglrc = wglCreateContextARB(m_hdc, 0, attribs);
  201.         //Delete the temporary context
  202.         wglDeleteContext(tmpContext);
  203.         //Make the GL3 context current
  204.         wglMakeCurrent(m_hdc, m_hglrc);
  205.  
  206.         m_isRunning = true; //Mark our window as running
  207.     }
  208.     break;
  209.     case WM_DESTROY: //window destroy
  210.     case WM_CLOSE: //Window is closing
  211.         wglMakeCurrent(m_hdc, NULL);
  212.         wglDeleteContext(h_hglrc);
  213.         m_isRunning = false; //Stop the main loop
  214.         PostQuitMessage(0);
  215.     break;
  216.     case WM_SIZE:
  217.     {
  218.         int height = HIWORD(lParam);        //retrieve width and height
  219.         int width = LOWORD(lParam);
  220.         getAttachedExample()->onResize(width, height); //Call the example's resize method
  221.     }
  222.     break;
  223.     case WM_KEYDOWN:
  224.         if(wParam == VK_ESCAPE) //If the escape key is pressed
  225.         {
  226.             DestroyWindow(m_hwnd); //Send a WM_DESTORY message
  227.         }
  228.     break;
  229.     default:
  230.         break;
  231.     }
  232.    
  233.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  234. }
  235.  
  236. void GLWindow::processEvents()
  237. {
  238.     MSG msg;
  239.  
  240.     //While tere are messages in the queue, store them in msg
  241.     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  242.     {
  243.         //Process the messages one-by-one
  244.         TranslateMessage(&msg);
  245.         DispatchMessage(&msg);
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement