Advertisement

Weird problem with window...

Started by March 16, 2003 05:22 PM
1 comment, last by Guzba 21 years, 8 months ago

  
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")

#define SCREEN_H 800
#define SCREEN_W 600
#define SCREEN_D 16

bool active = TRUE;
bool keys[256];

HGLRC           hRC	=	NULL;
HDC             hDC	=	NULL;
HWND            hWnd	=	NULL;
HINSTANCE       hInstance;

LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void resizeScene()
{
	glViewport(0, 0, SCREEN_W, SCREEN_H);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)SCREEN_W / (GLfloat)SCREEN_H, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int initGL(void)
{
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	return TRUE;
}

int renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	return TRUE;
}

void killWindow()
{
	ChangeDisplaySettings(NULL, 0);
	ShowCursor(TRUE);

	if(hRC)
	{
		if(!wglMakeCurrent(NULL, NULL))
		{
			MessageBox(NULL, "Release Of DC And RC Failed", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		}

		if(!wglDeleteContext(hRC))
		{
			MessageBox(NULL, "Release Rendering Context Failed", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		}
		hRC = NULL;
	}

	if(hDC && !ReleaseDC(hWnd, hDC))
	{
		MessageBox(NULL, "Release Device Context Failed", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		hDC = NULL;
	}

	if(hWnd && !DestroyWindow(hWnd))
	{
		MessageBox(NULL, "Could Not Release hWnd", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		hWnd = NULL;
	}

	if(!UnregisterClass("Space", hInstance))
	{
		MessageBox(NULL, "Could Not Unregister Class", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);
		hInstance = NULL;
	}

}

bool createWindow()
{
	WNDCLASS Window;

	GLuint PixelFormat;

	DWORD dwExStyle;
	DWORD dwStyle;

	RECT WindowRect;
	WindowRect.left		=(long)0;
	WindowRect.right	=(long)SCREEN_W;
	WindowRect.top		=(long)0;
	WindowRect.bottom	=(long)SCREEN_H;	

	hInstance				= GetModuleHandle(NULL);
	Window.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	Window.lpfnWndProc		= (WNDPROC) WndProc;
	Window.cbClsExtra		= 0;
	Window.cbWndExtra		= 0;
	Window.hInstance		= hInstance;
	Window.hIcon			= LoadIcon(NULL, IDI_WINLOGO);
	Window.hCursor			= LoadCursor(NULL, IDC_ARROW);
	Window.hbrBackground	= NULL;
	Window.lpszMenuName		= NULL;
	Window.lpszClassName	= "Space";

	if(!RegisterClass(&Window))
	{
		MessageBox(NULL, "Failed To Register The Window Class", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	DEVMODE dmScreenSettings;
	memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
	dmScreenSettings.dmSize = sizeof(dmScreenSettings);
	dmScreenSettings.dmPelsHeight	= SCREEN_H;
	dmScreenSettings.dmPelsWidth	= SCREEN_W;
	dmScreenSettings.dmBitsPerPel	= SCREEN_D;
	dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

	if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
	{
		return FALSE;
	}

	dwExStyle	=	WS_EX_APPWINDOW;
	dwStyle		=	WS_POPUP;
	ShowCursor(FALSE);

	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

	if(!(hWnd=CreateWindowEx(dwExStyle,
							 "Space",
							 "Space Quest",
							 WS_CLIPSIBLINGS |
							 WS_CLIPCHILDREN |
							 dwStyle,
							 0, 0,
							 WindowRect.right-WindowRect.left,
							 WindowRect.bottom-WindowRect.top,
							 NULL,
							 NULL,
							 hInstance,
							 NULL)))
	{
		killWindow();
		MessageBox(NULL, "Window Creation Error", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	static PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		SCREEN_D,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

	if (!(hDC = GetDC(hWnd)))
	{
		killWindow();
		MessageBox(NULL, "Can''t Create A GL Device Context", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
	{
		killWindow();
		MessageBox(NULL, "Can''t Find A Suitable PixelFormat", "ERROR",MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!SetPixelFormat(hDC, PixelFormat, &pfd))
	{
		killWindow();
		MessageBox(NULL, "Can''t Set The PixelFormat", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if (!(hRC = wglCreateContext(hDC)))
	{
		killWindow();
		MessageBox(NULL, "Can''t Create A GL Rendering Context", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	if(!wglMakeCurrent(hDC, hRC))
	{
		killWindow();
		MessageBox(NULL, "Can''t Activate The GL Rendering Context", "ERROR", MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	ShowWindow(hWnd, SW_SHOW);
	SetForegroundWindow(hWnd);
	SetFocus(hWnd);
	resizeScene();

	if(!initGL())
	{
		killWindow();
		MessageBox(NULL, "Initialization Failed", ERROR, MB_OK | MB_ICONEXCLAMATION);
		return FALSE;
	}

	return TRUE;
}

LRESULT CALLBACK WndProc(HWND	hWnd,
						 UINT	uMsg,
						 WPARAM	wParam,	
						 LPARAM	lParam)
{
	switch (uMsg)
	{
		case WM_ACTIVATE:
		{
			if (!HIWORD(wParam))
			{
				active = TRUE;
			}
			else
			{
				active = FALSE;
			}

			return 0;
		}

		case WM_SYSCOMMAND:
		{
			switch (wParam)
			{
				case SC_SCREENSAVE:
				case SC_MONITORPOWER:
				return 0;
			}
			break;
		}

		case WM_CLOSE:
		{
			PostQuitMessage(0);
			return 0;
		}

		case WM_KEYDOWN:
		{
			keys[wParam] = TRUE;
			return 0;
		}

		case WM_KEYUP:
		{
			keys[wParam] = FALSE;
			return 0;
		}

		case WM_SIZE:
		{
			resizeScene();
			return 0;
		}
	}

	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nShowCmd)
{
	MSG		msg;
	BOOL	done = FALSE;

	if (!createWindow())
	{
		return 0;
	}

	while(!done)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				done = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			if (active)
			{
				if (keys[VK_ESCAPE])
				{
					done = TRUE;
				}
				else
				{
					renderScene();
					SwapBuffers(hDC);
				}
			}

			if (keys[VK_F1])
			{
				keys[VK_F1] = FALSE;
				killWindow();
				if (!createWindow())
				{
					return 0;
				}
			}
		}
	}
	killWindow();
	return (msg.wParam);	
}
  
The problem is when i run the exe file it creates it just flashes on and flashes off but doesnt stay, and i cant figure out where i went wrong because it doesnt give any error messages hehe thanx for the help...
"I never finish what i sta.."Computer:ATI Radeon 9700 PROP4 2.66 GHz512 RDRAM
i''m surprised you get anything, given that a 600x800 fullscreen is normally not supported. try flipping your SCREEN_H and SCREEN_W values around and see if that doesn''t help.
Advertisement
hahaha thanx so much, i completely would have never noticed taht.. your a life saver :D
"I never finish what i sta.."Computer:ATI Radeon 9700 PROP4 2.66 GHz512 RDRAM

This topic is closed to new replies.

Advertisement