Sign in to follow this  
ajm113

Window Will Not Display! (Win32 Windows)

Recommended Posts

Hello, I was wounder can someone help me find this error in my script that stops me from rendering the window in my code in Win32 C++? The program runs, but does not stop exit or eather render the window. Create Function

	bool GameOGL::Create(WindowParameters* pWndParam) {

		m_pWndParam			= pWndParam;

		m_pszTitle			= pWndParam->m_pszTitle;
		m_pszClassName		= pWndParam->m_pszTitle;
		m_b32Bit			= pWndParam->b32Bit;
		m_bIsFullScreen		= !(pWndParam->bWindowed);
		m_Inst				= pWndParam->hInst;
		m_iHeight			= pWndParam->iHeight;
		m_iRefreshRate		= pWndParam->iRefreshRate;
		m_fps				= (float)pWndParam->iRefreshRate;
		m_iWidth			= pWndParam->iWidth;


	if((m_iHeight <= 0) || (m_iWidth <= 0) || (m_iRefreshRate <= 0)) {

	m_pErrorHandler->SetError(EC_Error, "Invalid Parameters\n" 
							  "Width = %d\n" 
							  "Height = %d\n" 
							  "Refresh = %d",
							  m_iWidth,
							  m_iHeight,
							  m_iRefreshRate);
			return false;
	}


		m_WindowClassEx.cbClsExtra			= NULL;
		m_WindowClassEx.cbSize				= sizeof(WNDCLASSEX);
		m_WindowClassEx.cbWndExtra			= NULL;
		m_WindowClassEx.hbrBackground		= NULL;
		m_WindowClassEx.hCursor				= LoadCursor(NULL, IDC_ARROW);
		m_WindowClassEx.hIcon				= LoadIcon(NULL, IDI_WINLOGO);
		m_WindowClassEx.hIconSm				= LoadIcon(NULL, IDI_WINLOGO);

		m_WindowClassEx.hInstance			= m_Inst;
		m_WindowClassEx.lpfnWndProc			= WindowProc;
		m_WindowClassEx.lpszClassName		= m_pszClassName;
		m_WindowClassEx.lpszMenuName		= NULL;

		m_WindowClassEx.style				= CS_HREDRAW | CS_VREDRAW | CS_CLASSDC | CS_DBLCLKS;


		if(!RegisterClassEx(&m_WindowClassEx)) {
		m_Inst = NULL;
		m_pErrorHandler->SetError(EC_Windows, "Connot register %s with Windows.", m_pszClassName);
		return false;
		}

		DWORD dwstyle, dwstyleEx;
		if(m_bIsFullScreen) {
		dwstyle = WS_POPUP | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX;
		dwstyleEx = WS_EX_APPWINDOW;
		}else{ //Windowed Game!
		dwstyle = WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
		dwstyleEx = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

		}

		RECT rcWnd;
		//correct window size so that active area = width x height
		rcWnd.left   = 0;
		rcWnd.right  = m_iWidth;
		rcWnd.top    = 0;
		rcWnd.bottom = m_iHeight;
		AdjustWindowRectEx(&rcWnd, dwstyle, FALSE, dwstyleEx);

		m_hWnd = CreateWindowEx(dwstyleEx, 
			m_pszClassName,
			m_pszClassName,
			dwstyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
			0,0,
			rcWnd.right  - rcWnd.left,
			rcWnd.bottom - rcWnd.top,
			NULL,
			NULL,
			m_Inst,(void*)this);


		if(m_hWnd == NULL)
		{
		DestroyWnd();
		m_pErrorHandler->SetError(EC_Windows,"Cannot create window.");
		
		return false;

		}

		pWndParam->hWnd=m_hWnd;

		m_hDC = GetDC(m_hWnd);

		if(m_hDC == NULL) {

		DestroyWnd();
		m_pErrorHandler->SetError(EC_Windows, "Could not get Window's DC!\n");
		return false;
		}

		pWndParam->hDC=m_hDC;

		//Move window to the center!
		if(pWndParam->bWindowed) {
		int cx = GetSystemMetrics(SM_CXSCREEN);
		int cy = GetSystemMetrics(SM_CYSCREEN);
		int px = 0;
		int py = 0;

		if(cx > m_iWidth) {

			px = (int)((cx - m_iWidth)/2);
		}

		if(cy > m_iHeight) {

			py = (int) ((cy - m_iHeight)/2);
		}

		MoveWindow(m_hWnd, px, py, m_iWidth, m_iHeight, FALSE);

		}

		//Difine pixel format

		PIXELFORMATDESCRIPTOR pfd;
		memset( &pfd, NULL, sizeof(pfd));
		pfd.nSize = sizeof(pfd);
		pfd.nVersion = 1;
		pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
		pfd.iPixelType = PFD_TYPE_RGBA;
		pfd.cColorBits = (m_b32Bit ? 32 : 16 );
		pfd.cDepthBits = 16;
		pfd.iLayerType = PFD_MAIN_PLANE	;

		int iPixelFormat = ChoosePixelFormat( m_hDC, &pfd );

		if(!iPixelFormat) {
		DestroyWnd();
		m_pErrorHandler->SetError(EC_OpenGL, "Could not find suitable OpenGL pixels format to use!\n");
		return false;
		}

		if(!SetPixelFormat ( m_hDC, iPixelFormat, &pfd )) {
		DestroyWnd();
		m_pErrorHandler->SetError(EC_OpenGL,"Could not set OpenGL pixel format!\n");
		return false;
		}

		m_hGLRC = wglCreateContext (m_hDC);
		if(m_hGLRC == NULL) {
		DestroyWnd();
		m_pErrorHandler->SetError(EC_OpenGL,"Could not create OpenGL context!\n");
		return false;
		}


		if(!wglMakeCurrent(m_hDC, m_hGLRC)) {
		DestroyWnd();
		m_pErrorHandler->SetError(EC_OpenGL,"Could not make OpenGL context!\n");
		return false;
		}

		if(!DescribePixelFormat(m_hDC, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
		DestroyWnd();
		m_pErrorHandler->SetError(EC_OpenGL,"Could not get selected pixel format!");
		return false;
		}

		m_b32Bit = (pfd.cColorBits == 32 ? true : false);
		m_bZBuffer = (pfd.cDepthBits > 0 ? true : false);

			if(m_bIsFullScreen){

				DEVMODE dmScreenSettings;
				memset (&dmScreenSettings, 0, sizeof(dmScreenSettings));

				dmScreenSettings.dmSize = sizeof(dmScreenSettings);
				dmScreenSettings.dmPelsWidth = m_iWidth;
				dmScreenSettings.dmPelsHeight = m_iHeight;
				dmScreenSettings.dmBitsPerPel = ( m_b32Bit ? 32 : 16 );
				dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

				if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
				{
				DestroyWnd();
				m_pErrorHandler->SetError(EC_Windows,"Failed To Switch to %dx%d fullscreen mode.\n");
				return false;
				}

			ShowWindow(m_hWnd, SW_SHOW);
			SetForegroundWindow(m_hWnd);
			SetFocus(m_hWnd);
			UpdateWindow(m_hWnd);

		
		}
			return true;

	}


Please help, I do know it mite be a much to ask for, but I have spent 3 hours or so looking for a typo or error. [Edited by - ajm113 on January 1, 2008 7:36:18 PM]

Share this post


Link to post
Share on other sites
First, try using the [ source ] and [ /source ] tags to enclose your code, which will make it much more readable for everyone else.

Second, it would be nice to see the WndProc method. Also, I may have missed it with the strange formatting, but it appears that if fullscreen is set to false, the window will never get shown or have its style set to WS_VISIBLE.

Share this post


Link to post
Share on other sites
Ive never seen code like this before!

m_pszTitle= pWndParam->m_pszTitle;

ETA: Oh it comes out normal when I post this! Must be something to do with the source tags.

Thought I had forgotten how to code c++ then!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this