Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Problem with window creation

Started by Optln Sep 20, 2010 at 11:18 AM 5 replies 1.1k views
Original Post
Optln
Optln
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:
#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));}
yadango
yadango
From your source code:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow){	Game reaper;	reaper.createWindow("OpenGL Window", 1024, 768, 32, false, hInstance);	reaper.initialize();	reaper.run();	reaper.clean();	return 0;}


void Game::run(){	while(m_isRunning == true)	{		update();	}}


EDIT: Oops... nevermind... found your message loop in update. Checking now.
Buckeye
Buckeye
Not sure if it's related, but somewhere you should have:
ShowWindow(m_hwnd, SW_SHOW);UpdateWindow(m_hwnd);

after you create it.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
yadango
yadango
Get rid of m_hWnd from PeekMessage.
Optln
Optln
Quote:
Original post by Buckeye
Not sure if it's related, but somewhere you should have:
ShowWindow(m_hwnd, SW_SHOW);UpdateWindow(m_hwnd);

after you create it.

I put them to WM_CREATE event. It is called after creating the window as far as I know.

Quote:
Original post by yadango
From your source code:

*** Source Snippet Removed ***

*** Source Snippet Removed ***

EDIT: Oops... nevermind... found your message loop in update. Checking now.


Thanks in advance.
yadango
yadango
Don't forget to try removing m_hWnd from PeekMessage and replace it with 0. Worked for me.
Optln
Optln
Okay, thanks again. I'll send a message to the author of this article which I think I got that part of the code from: http://www.cprogramming.com/tutorial/opengl_first_windows_app.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.