Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #define WIN32_EXTRA_LEAN
- #include <windows.h>
- #include "GLWindow.h"
- #include "Example.h"
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR cmdLine,
- int cmdShow){
- //Set our window settings
- const int windowWidth = 1024;
- const int windowHeight = 768;
- const int windowBPP = 32;
- const int windowFullscreen = false;
- //This is out window
- GLWindow programWindow(hInstance);
- //The example of the GL code
- Example example;
- //Attach our example to our window
- programWindow.attachExample(&example);
- //Attempt to create the window
- if (!programWindow.create(windowWidth, windowHeight, windowBPP, windowFullscreen)){
- MessageBox(NULL, "Unable to create the OpenGL Window", "An error occurred", MB_ICONERROR|MB_OK);
- programWindow.destroy();
- return 1;
- }
- if (!example.init()){ //Initialise our example
- MessageBox(NULL, "Could not initialise the application", "An error occurred", MB_ICONERROR|MB_OK);
- programWindow.destroy();
- return 1;
- }
- //This is the main loop, we render frames until isRunning is false
- while(programWindow.isRunning()){
- programWindow.processEvents(); //Process any window events
- //We get the time that's passed since the previous frame
- float elapsedTime = programWindow.getElapsedSeconds();
- example.prepare(elapsedTime); //Do any pre-rensering logic
- example.render();
- programWindow.swapBuffers();
- }
- }
- bool GLWindow::create(int width, int height, int bpp, bool fullscreen){
- DWORD dwExStyle; //Window extended style
- DWORD dwStyle; //Window style
- m_isFullscreen = fullscreen;
- m_windowRect.left = (long)0; //Set left value to 0
- m_windowRect.right = (long)width;
- m_windowRect.top = (long)0;
- m_windowRect.bottom = (long)height;
- //Fill out the window class structure]
- m_windowClass.cbSIze = sizeof(WNDCLASSEX);
- m_windowClass.style = CS_HREDRAW | CS_VREDRAW;
- m_windowClass.lpfnWndProc = GLWindow::StaticWndProc; //We set our static member as the event handler
- m_windowClass.cbClsExtra = 0;
- m_windowClass.cbWndExtra = 0;
- m_windowClass.hInstance = m_hInstance;
- m_windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default
- m_windowClass.hCurcor = LoadCursor(NULL, IDC_ARROW); //default
- m_windowClass.hbrBackground = NULL;
- m_windowClass.lpszMenuName = NULL;
- m_windowClass.lpszClassName = "GLClass";
- m_windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //Windows logo small icon
- //register the windows class
- if (!registerClassEx(&m_windowClass)){
- MessageBox(NULL, "Failed to register window class", NULL, MB_OK);
- return false;
- }
- if (m_isFullscreen){ //If we are fullscreen, we need to change the display mode
- DEVMODE dmScreenSettings;
- memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
- dmScreenSettings.dmSize = sizeof(dmScreenSettings);
- dmScreenSettings.dmPelsWidth = width; //Screen width
- dmScreenSettings.dmPelsHeight = height; //Screen height
- dmScreenSettings.dmBitsPerPel = bpp; //Bits per pixel
- dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
- if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
- //Setting display mode failed, switch to windowed
- MessageBox(NULL, "Display mode failed", NULL, MB_OK);
- m_isFullscreen = false;
- }
- }
- if (m_isFullscreen){
- dwExStyle = WS_EX_APPWINDOW;
- dwStyle = WS_POPUP;
- ShowCursor(false);
- }else{
- dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
- dwStyle = WS_OVERLAPPEDWINDOW;
- }
- AdjustWindowRectEx(&m_windowRect, dwStyle, false, dwExStyle); //Adjust window to true requested size
- //Class registered, so now create out window
- m_hwnd = CreateWindowEx(Null, //Extended style
- "GLClass", //Class name
- "BOGLGP - Chapter 2 - OpenGL Application", //App name
- dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
- 0,0, //x,y coordinate
- m_windowRect.right - m_windowRect.left, //width
- m_windowRect.bottom - m_windowRect.top, //height
- NULL, //handle to parent
- NULL, //handle to menu
- m_hInstance, //application instance
- this); //We pass a pointer to the GLWindow here
- //Check if window creation failed (hwnd would equal NULL)
- if (!m_hwnd)
- return 0;
- m_hdc = GetDC(m_hwnd);
- ShowWindow(m_hwnd, SW_SHOW); //display the window
- UpdateWindow(m_hwnd); //update the window
- m_lastTime = GetTickCount() / 1000.0f; //Initialise the timer
- return true;
- }
- LRESULT CALLBACK GLWindow::StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
- GLWindow* window = NULL;
- //If this is the create message
- if (uMsg == WM_CREATE){
- //Get the pointer we stored during create
- window = (GLWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
- //Associate the window pointer with the hwnd for the other events to access
- SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)window);
- }else{
- //if this is not a creation event then we should have stored a pointer to the window
- window = (GLWindow*)GetWindowLongPtr(hWnd, GWL_USERDATA);
- if (!window){
- //Do the default event handling
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- }
- //Call out window's member WndProc (allows us to access member variables)
- return window->WndProc(hWnd, uMsg, wParam, lParam);
- }
- LRESULT GLWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_CREATE:
- {
- m_hdc = GetDC(hWnd);
- setupPixelFormat();
- //Set the version that we want, in this case 3.0
- int attribs[]={
- WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
- WGL_CONTEXT_MINOR_VERSION_ARB, 0,
- 0}; //zero indicated the end of the array
- //Create temporary context so we can get a pointer to the function
- HGLRC tmpContext = wglCreateContext(m_hdc);
- //Make it current
- wglMakeCurrent(m_hdc, tmpContext);
- //Get function pointer
- wglCreateContextAttribsARB = (PFNWGLCREATETEXTATTRIBSARBPROC) wglGetPrcoAddress("wglCreateContextAttribsARB");
- //If this is NULL then OpenGL 3.0 is not supported
- if (!wglCreateContextARB)
- {
- MessageBox(NULL, "OpenGL 3.0 is not supported", "An error occurred", MB_ICONERROR | MB_OK);
- DestroyWindow(hWnd);
- return 0;
- }
- //Create an OpenGL 3.0 context using the new function
- m_hglrc = wglCreateContextARB(m_hdc, 0, attribs);
- //Delete the temporary context
- wglDeleteContext(tmpContext);
- //Make the GL3 context current
- wglMakeCurrent(m_hdc, m_hglrc);
- m_isRunning = true; //Mark our window as running
- }
- break;
- case WM_DESTROY: //window destroy
- case WM_CLOSE: //Window is closing
- wglMakeCurrent(m_hdc, NULL);
- wglDeleteContext(h_hglrc);
- m_isRunning = false; //Stop the main loop
- PostQuitMessage(0);
- break;
- case WM_SIZE:
- {
- int height = HIWORD(lParam); //retrieve width and height
- int width = LOWORD(lParam);
- getAttachedExample()->onResize(width, height); //Call the example's resize method
- }
- break;
- case WM_KEYDOWN:
- if(wParam == VK_ESCAPE) //If the escape key is pressed
- {
- DestroyWindow(m_hwnd); //Send a WM_DESTORY message
- }
- break;
- default:
- break;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- void GLWindow::processEvents()
- {
- MSG msg;
- //While tere are messages in the queue, store them in msg
- while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- //Process the messages one-by-one
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement