Original Post
Hi all. I'm trying to make an OpenGL basecode on Windows for my future projects but having a small problem. Altough the window shows the scene perfectly, the application's button on the taskbar appears after 2-3 seconds from the showing of the window itself. It doesn't bother me much right now but I'm worried if it is part of a bigger bug that I don't see right now. Any help (and suggestion for other parts of the code) is appreciated.
Full source code(5 kb) is here:
http://hotfile.com/dl/70663612/23d8a3f/source.zip.html
But the bug is probably in this file:
Full source code(5 kb) is here:
http://hotfile.com/dl/70663612/23d8a3f/source.zip.html
But the bug is probably in this file:
#include "WGLApp.hpp"WGLApp::WGLApp(){ m_hWnd = NULL; m_hDC = NULL; m_hRC = NULL; m_hInstance = NULL; m_title = "Window Title"; m_width = 640; m_height = 480; m_bitsPerPixel = 32; m_isFullscreen = false; m_isWindowActive = false; m_keys = new bool[256]; m_videoDevice = new OpenGLDevice();}WGLApp::~WGLApp(){ delete m_videoDevice; delete m_keys;}void WGLApp::createWindow(const char* title, int width, int height, int bitsPerPixel, bool isFullscreen, HINSTANCE hInstance){ m_title = title; m_width = width; m_height = height; m_bitsPerPixel = bitsPerPixel; m_isFullscreen = isFullscreen; m_hInstance = hInstance; WNDCLASSEX windowClass; ZeroMemory(&windowClass, sizeof (WNDCLASSEX)); windowClass.cbSize = sizeof(WNDCLASSEX); windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; windowClass.lpfnWndProc = staticWndProc; windowClass.cbClsExtra = 0; windowClass.cbWndExtra = 0; windowClass.hInstance = m_hInstance; windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); windowClass.hbrBackground = NULL; windowClass.lpszMenuName = NULL; windowClass.lpszClassName = "WGLApp"; if (RegisterClassEx(&windowClass) == 0) MessageBox (HWND_DESKTOP, "Registering Window Class Failed!", "Error", MB_OK | MB_ICONEXCLAMATION); if (m_isFullscreen == true) { DEVMODE screenSettings; ZeroMemory(&screenSettings, sizeof(screenSettings)); screenSettings.dmSize = sizeof(screenSettings); screenSettings.dmPelsWidth = m_width; screenSettings.dmPelsHeight = m_height; screenSettings.dmBitsPerPel = m_bitsPerPixel; screenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; if (ChangeDisplaySettings(&screenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) // if it has failed... m_isFullscreen = false; // ...try windowed mode } DWORD windowStyle = WS_OVERLAPPEDWINDOW; DWORD windowExtendedStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; if (m_isFullscreen == true) { windowExtendedStyle = WS_EX_APPWINDOW; windowStyle = WS_POPUP; ShowCursor(false); } else { windowExtendedStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; windowStyle = WS_OVERLAPPEDWINDOW; } RECT windowRect = {0, 0, m_width, m_height}; AdjustWindowRectEx(&windowRect, windowStyle, FALSE, windowExtendedStyle); m_hWnd = CreateWindowEx(windowExtendedStyle, // Extended Style "WGLApp", // Class Name m_title, // Window Title windowStyle | // Window Style WS_CLIPCHILDREN | // Required for an... WS_CLIPSIBLINGS, // ...OpenGL window 0, 0, // Window X,Y Position windowRect.right - windowRect.left, // Window Width windowRect.bottom - windowRect.top, // Window Height HWND_DESKTOP, // Desktop Is Window's Parent 0, // No Menu m_hInstance, // Pass The Window Instance this);}void WGLApp::destroyWindow(){ if (m_isFullscreen) { ChangeDisplaySettings(NULL, 0); ShowCursor(true); } wglMakeCurrent(NULL,NULL); wglDeleteContext(m_hRC); m_hRC = NULL; ReleaseDC(m_hWnd, m_hDC); m_hDC = NULL; DestroyWindow(m_hWnd); m_hWnd = NULL; UnregisterClass("WGLApp", m_hInstance); m_hInstance = NULL;}void WGLApp::update(){ MSG msg; if(PeekMessage(&msg, m_hWnd, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { m_isRunning = false; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } if(m_isWindowActive == true) { m_videoDevice->draw(); SwapBuffers(m_hDC); if(m_keys[VK_ESCAPE] == true) m_isRunning = false; }}void WGLApp::setupPixelFormat(){ int nPixelFormat; PIXELFORMATDESCRIPTOR pfd = { sizeof (PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format m_bitsPerPixel, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; nPixelFormat = ChoosePixelFormat(m_hDC, &pfd); SetPixelFormat(m_hDC, nPixelFormat, &pfd);}LRESULT CALLBACK WGLApp::staticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ WGLApp* pParent; if(message == WM_NCCREATE) { pParent = (WGLApp*)((LPCREATESTRUCT)lParam)->lpCreateParams; SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)pParent); } else { pParent = (WGLApp*)GetWindowLongPtr(hWnd,GWL_USERDATA); if(!pParent) return DefWindowProc(hWnd, message, wParam, lParam); } pParent->m_hWnd = hWnd; return pParent->wndProc(message, wParam, lParam);}LRESULT WGLApp::wndProc(UINT message, WPARAM wParam, LPARAM lParam){ switch(message) { case WM_CREATE: m_hDC = GetDC(m_hWnd); setupPixelFormat(); m_hRC = wglCreateContext(m_hDC); wglMakeCurrent(m_hDC, m_hRC); m_isWindowActive = true; ShowWindow(m_hWnd, SW_SHOW); SetForegroundWindow(m_hWnd); SetFocus(m_hWnd); m_videoDevice->initialize(); m_videoDevice->resize(m_width, m_height); return 0; break; case WM_ACTIVATE: if (!HIWORD(wParam)) m_isWindowActive = true; else m_isWindowActive= false; return 0; break; case WM_SYSCOMMAND: switch (wParam) { case SC_SCREENSAVE: // screensaver trying to start case SC_MONITORPOWER: // monitor going to powersave mode // returning 0 prevents either from happening return 0; default: break; } break; case WM_CLOSE: destroyWindow(); PostQuitMessage(0); return 0; break; case WM_DESTROY: PostQuitMessage(0); return 0; break; case WM_SIZE: m_width = LOWORD(lParam); m_height = HIWORD(lParam); m_videoDevice->resize(m_width, m_height); return 0; break; case WM_KEYDOWN: m_keys[wParam] = true; return 0; break; case WM_KEYUP: m_keys[wParam] = false; return 0; break; default: break; } return (DefWindowProc(m_hWnd, message, wParam, lParam));}