www.pudn.com > ÈýάÌì¿ÕÓëµØÐÎ.zip > Main.cpp, change:2001-10-15,size:8835b


// SkyDome Demo  -  October 2001 
// 
// Luis R. Sempé 
// visual@spheregames.com 
// Sphere Games (http://www.spheregames.com) 
//  
// You may use, copy, distribute or create derivative software 
// for any purpose. If you do, I'd appreciate it if you write me  
// and let me know what you do with it. Thanks! 
// Have fun! 
//////////////////////////////////////////////////// 
 
#include <windows.h> 
#include <mmsystem.h> 
#include <math.h> 
#include <stdio.h> 
 
#include "Main.h" 
#include "oglInit.h" 
#include "SkyDome.h" 
#include "SkyPlane.h" 
#include "Terrain.h" 
#include "font.h" 
 
bool bp = false; 
bool bf = false; 
bool bh = false; 
bool bw = false; 
 
bool fog = false; 
bool Dome = true; 
bool ShowHelp = false; 
bool Wireframe = false; 
 
float tx=5.0f, ty=-40.0f, tz=34.0f; 
float rx=0.0f, ry=0.0f, rz=0.0f; 
 
int WINAPI WinMain(	HINSTANCE	hInstance,			 
					HINSTANCE	hPrevInstance,		 
					LPSTR		lpCmdLine,			 
					int			nCmdShow)			 
{ 
	MSG		msg;									 
	BOOL	done=FALSE;								 
 
	if (!CreateGLWindow("SkyDome Demo", 640, 480, 16)) 
	{ 
		MessageBox(hWnd, "Unable to Create the App Window", "ERROR", MB_OK); 
		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 
		{ 
			// Set the sky texture 
			glBindTexture(GL_TEXTURE_2D, texture[0]); 
			 
			glLoadIdentity(); 
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		 
			 
			// Update the player's position 
			Move(-tx, -ty, -tz, -rx, -ry, -rz); 
 
			// Render the selected dome 
			if (Dome) 
				RenderSkyDome(); 
			else 
				RenderSkyPlane(); 
 
			// Set the terrain texture and render it 
			glBindTexture(GL_TEXTURE_2D, texture[1]); 
			RenderTerrain();		 
 
			// Print some information 
			glLoadIdentity(); 
			PostString("SkyDome Demo", 1, 0, 0); 
			PostString("Press H for Help",1, 1, 0); 
			PostString("Sphere Games (C) 2001", 60, 32, 0); 
 
			if (Dome) 
				PostString("SkyDome", 60, 0, 0); 
			else 
				PostString("SkyPlane", 60, 0, 0); 
 
			DisplayStats(); 
 
			if (ShowHelp) 
			{ 
				PostString("W    Toggle Wireframe On/Off", 1, 4, 0); 
				PostString("D    Change SkyDome Mode ", 1, 5, 0); 
				PostString("F    Toggle Fog On/Off", 1, 6, 0); 
				PostString("ESC  Quit", 1, 7, 0); 
			} 
 
			glLoadIdentity(); 
			glColor3f(1.0f, 1.0f, 1.0f); 
			RenderStrings(); 
 
			// if ESC was pressed, or the app isn't active, bail 
			if (!active  || keys[VK_ESCAPE])		 
				done=TRUE; 
			else 
			{ 
				SwapBuffers(hdc); 
 
				// Get input from the user 
 
				// Dome technique 
				if (keys['D'] && !bp) 
				{ 
					bp = true; 
					Dome = !Dome; 
				} 
 
				if (!keys['D']) 
					bp = false; 
 
				// Fog 
				if (keys['F'] && !bf) 
				{ 
					bf = true; 
 
					fog = !fog; 
					if (fog) 
						glEnable(GL_FOG); 
					else 
						glDisable(GL_FOG); 
				} 
 
				if (!keys['F']) 
					bf = false; 
 
				// Help 
				if (keys['H'] && !bh) 
				{ 
					bh = true; 
					ShowHelp = !ShowHelp; 
				} 
 
				if (!keys['H']) 
					bh = false; 
 
 
				// Wireframe 
				if (keys['W'] && !bw) 
				{ 
					bw = true; 
					Wireframe = !Wireframe; 
 
					if (Wireframe) 
					{ 
						glDisable(GL_TEXTURE_2D); 
						glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
					} 
					else 
					{ 
						glEnable(GL_TEXTURE_2D); 
						glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
					} 
				} 
 
				if (!keys['W']) 
					bw = false; 
 
				// Movement 
				if (keys[VK_UP]) 
				{ 
					tx += (float)sin(ry*3.1415926/180); 
					tz += (float)cos(ry*3.1415926/180); 
 
				} 
 
				if (keys[VK_DOWN]) 
				{ 
					tx -= (float)sin(ry*3.1415926/180); 
					tz -= (float)cos(ry*3.1415926/180); 
		 
				} 
 
				if (keys[VK_LEFT]) 
				{ 
					ry += 1.0f; 
				} 
 
				if (keys[VK_RIGHT]) 
				{ 
					ry -= 1.0f; 
				} 
 
				if (keys['A']) 
					ty -= 0.5f; 
 
				if (keys['Z']) 
					ty += 0.5f; 
 
			} 
		} 
	} 
 
	Shutdown(); 
 
	return (msg.wParam); 
} 
 
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) 
			{ 
				// disable screensaver and suspend 
				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: 
		{ 
			ResizeGLWindow(LOWORD(lParam),HIWORD(lParam)); 
			return 0; 
		} 
 
	} 
 
	return DefWindowProc(hWnd,uMsg,wParam,lParam); 
} 
 
BOOL CreateGLWindow(char* title, int width, int height, int bits) 
{ 
	GLuint		PixelFormat; 
	WNDCLASS	wc; 
	DWORD		dwExStyle; 
	DWORD		dwStyle; 
	RECT		WindowRect; 
	WindowRect.left=(long)0; 
	WindowRect.right=(long)width; 
	WindowRect.top=(long)0; 
	WindowRect.bottom=(long)height; 
 
	hInstance			= GetModuleHandle(NULL); 
	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
	wc.lpfnWndProc		= (WNDPROC) WndProc; 
	wc.cbClsExtra		= 0; 
	wc.cbWndExtra		= 0; 
	wc.hInstance		= hInstance; 
	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO); 
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW); 
	wc.hbrBackground	= NULL; 
	wc.lpszMenuName		= NULL; 
	wc.lpszClassName	= CLASS_NAME; 
 
	if (!RegisterClass(&wc)) 
	{ 
		MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
	 
	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; 
	dwStyle=WS_OVERLAPPEDWINDOW; 
 
	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); 
 
	if (!(hWnd=CreateWindowEx(	dwExStyle, 
								CLASS_NAME, 
								title, 
								dwStyle | 
								WS_CLIPSIBLINGS | 
								WS_CLIPCHILDREN, 
								0, 0, 
								WindowRect.right-WindowRect.left, 
								WindowRect.bottom-WindowRect.top, 
								NULL, 
								NULL, 
								hInstance, 
								NULL))) 
	{ 
		Shutdown(); 
		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,				 
		bits,						 
		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))) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
 
	if (!(PixelFormat=ChoosePixelFormat(hdc,&pfd))) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
 
	if(!SetPixelFormat(hdc,PixelFormat,&pfd)) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
 
	if (!(hrc=wglCreateContext(hdc))) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
 
	if(!wglMakeCurrent(hdc,hrc)) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
 
	ShowWindow(hWnd,SW_SHOW); 
	SetForegroundWindow(hWnd); 
	SetFocus(hWnd); 
	ResizeGLWindow(width, height); 
 
	if (!InitGL()) 
	{ 
		Shutdown(); 
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION); 
		return FALSE; 
	} 
	return TRUE; 
} 
 
void Shutdown() 
{ 
	ReleaseDome(); 
	ReleaseSkyPlane(); 
	ReleaseTerrain(); 
 
	if (hrc) 
	{ 
		if (!wglMakeCurrent(NULL, NULL)) 
			MessageBox(NULL, "Release of DC and RC failed.", "SHUTDOWN ERROR", MB_OK); 
 
		if (!wglDeleteContext(hrc)) 
			MessageBox(NULL, "Release RC Failed.", "SHUTDOWN ERROR", MB_OK); 
 
		hrc = NULL; 
	} 
 
	if (hdc && !ReleaseDC(hWnd,hdc)) 
	{ 
		MessageBox(NULL,"Release DC Failed.","SHUTDOWN ERROR",MB_OK); 
		hdc=NULL; 
	} 
 
	if (hWnd && !DestroyWindow(hWnd)) 
	{ 
		MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK); 
		hWnd=NULL; 
	} 
 
	if (!UnregisterClass(CLASS_NAME,hInstance)) 
	{ 
		MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK); 
		hInstance=NULL; 
	}	 
} 
 
void DisplayStats() 
{ 
	// framerate info 
	static DWORD frame = 0; 
	static DWORD timelast = timeGetTime(); 
	static int FPS = 0; 
	DWORD timecurrent = timeGetTime(); 
	if(timecurrent - timelast >= 1000){ 
		FPS   = frame; 
		frame = 0; 
		timelast = timecurrent; 
	} 
	else frame++; 
 
	char str[80]; 
	sprintf(str, "FPS: %d", FPS); 
	PostString(str, 1, 2, 0); 
 
}