⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 Zodspark is a Car racing simulation game built on VC++ using opengl library
💻 CPP
📖 第 1 页 / 共 5 页
字号:
CreateTexture(texture, "texture/building1.bmp",8);
CreateTexture(texture, "texture/building2.bmp",9);
CreateTexture(texture, "texture/building3.bmp",10);
CreateTexture(texture, "texture/building4.bmp",11);
CreateTexture(texture, "texture/direction.bmp",12);
CreateTexture(texture, "texture/direction2.bmp",13);
CreateTexture(texture, "texture/jiit.bmp",14);
CreateTexture(texture, "texture/jiit1.bmp",15);
CreateTexture(texture, "texture/jiit2.bmp",16);
CreateTexture(texture, "texture/start.bmp",17);
CreateTexture(texture, "texture/end.bmp",18);
CreateTexture(texture, "texture/car.bmp",19);
CreateTexture(texture, "texture/redcar.bmp",20);
CreateTexture(texture, "texture/dir1.bmp",21);
CreateTexture(texture, "texture/dir2.bmp",22);
CreateTexture(texture, "texture/dir3.bmp",23);
CreateTexture(texture, "texture/dir4.bmp",24);
CreateTexture(texture, "texture/dir5.bmp",25);
CreateTexture(texture, "texture/screen.bmp",26);
CreateTexture(texture, "texture/scanner1.bmp",27);
CreateTexture(texture, "texture/scanner2.bmp",28);
CreateTexture(texture, "texture/scanner3.bmp",29);
CreateTexture(texture, "texture/scanner4.bmp",30);
CreateTexture(texture, "texture/scanner5.bmp",31);
CreateTexture(texture, "texture/scanner6.bmp",32);
CreateTexture(texture, "texture/scanner7.bmp",33);
CreateTexture(texture, "texture/scanner8.bmp",34);
CreateTexture(texture, "texture/scanner9.bmp",35);
CreateTexture(texture, "texture/prog1.bmp",37);
CreateTexture(texture, "texture/prog3.bmp",39);
CreateTexture(texture, "texture/prog4.bmp",40);
CreateTexture(texture, "texture/prog5.bmp",41);
CreateTexture(texture, "texture/prog6.bmp",42);
CreateTexture(texture, "texture/prog7.bmp",43);
CreateTexture(texture, "texture/prog8.bmp",44);
CreateTexture(texture, "texture/prog9.bmp",45);
CreateTexture(texture, "texture/prog2.bmp",38);







   // Set up the material information for our objects.
   // This will allow our objects to have a shininess to it from the light.  Add to the realism.
   // First we enable color material, set up the materials for ambient and diffuse colors,
   // set the color of the specular of the material, then finally set the max amount of shinines.
   // The highest number for the last function is 128 (for the max).
 //  glEnable(GL_COLOR_MATERIAL);
  // glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  // glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
  // glMateriali(GL_FRONT, GL_SHININESS, 128);

	// Enables Depth Testing
//gluPerspective(45,5,0.1,100000);

	//glDepthRange(0.0,1.0);
	SizeOpenGLScreen(width, height);					// Setup the screen translations and viewport
}

///////////////////////////////// DE INIT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function cleans up and then posts a quit message to the window
/////
///////////////////////////////// DE INIT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void DeInit()
{
	if (g_hRC)											
	{
		wglMakeCurrent(NULL, NULL);						// This frees our rendering memory and sets everything back to normal
		wglDeleteContext(g_hRC);						// Delete our OpenGL Rendering Context	
	}
	
	if (g_hDC) 
		ReleaseDC(g_hWnd, g_hDC);						// Release our HDC from memory
		
	if(g_bFullScreen)									// If we were in full screen
	{
		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop
		ShowCursor(TRUE);								// Show Mouse Pointer
	}

	UnregisterClass("Zod Spark", g_hInstance);		// Free the window class

	PostQuitMessage (0);								// Post a QUIT message to the window
}


///////////////////////////////// WIN MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function handles registering and creating the window.
/////
///////////////////////////////// WIN MAIN \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev, PSTR cmdline, int ishow)
{	
	HWND hWnd;

	// Check if we want full screen or not
	if(MessageBox(NULL, "Click Yes to go to full screen (Recommended)", "Options", MB_YESNO | MB_ICONQUESTION) == IDNO)
		g_bFullScreen = false;
	
	// Create our window with our function we create that passes in the:
	// Name, width, height, any flags for the window, if we want fullscreen of not, and the hInstance
	hWnd = CreateMyWindow("Zod Spark", SCREEN_WIDTH, SCREEN_HEIGHT, 0, g_bFullScreen, hInstance);

	// If we never got a valid window handle, quit the program
	if(hWnd == NULL) return true;

	// INIT OpenGL
	Init(hWnd);													

	// Run our message loop and after it's done, return the result
	return MainLoop();						
}
//end ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
float k = 150,ang=1.5707963267;
bool  g_bFullScreen = true;								// Set full screen as default
HWND  g_hWnd;											// This is the handle for the window
RECT  g_rRect;											// This holds the window dimensions
HDC   g_hDC;											// General HDC - (handle to device context)
HGLRC g_hRC;											// General OpenGL_DC - Our Rendering Context for OpenGL
HINSTANCE g_hInstance;	
								// This holds the global hInstance for UnregisterClass() in DeInit()
float a=10;
float v=0;
float angle=1.5707963267;
float t=0.016*10;
struct cvector
{
	float x;
	float z;
};


struct status
{
	cvector velocity;
	cvector acceleration;
	cvector position;
	cvector r_pos_left;
	cvector r_pos_right;
};
cvector calc_acc(float a,float angle)
{
	cvector acc;
	acc.x=a*cos(angle);
	acc.z=a*sin(angle);
	return acc;
}

cvector calc_velocity(cvector acc,float t,cvector u)
{
	cvector v;
	v.x=u.x+acc.x*t;
	v.z=u.z+acc.z*t;
	return v;
}
cvector calc_position(cvector u,float t,cvector initp,cvector a)
{
	cvector pos;
	pos.x=initp.x+u.x*t+a.x*t*t*0.5;
	pos.z=initp.z-u.z*t+a.z*t*t*0.5;
	return pos;
}
cvector calc_leftr(float angle, cvector pos, float r)
{
	cvector rad;
	rad.x=pos.x-r*sin(angle);
	rad.z=pos.z+r*cos(angle);
	return rad;
}
cvector calc_rightr(float angle, cvector pos, float r)
{
	cvector rad;
	rad.x=pos.x+r*sin(angle);
	rad.z=pos.z-r*cos(angle);
	return rad;
}
cvector setv(float v, float angle)
{
	cvector vel;
	vel.x=v*cos(angle);
	vel.z=v*sin(angle);
	return(vel);
}

status st;
// The controls are: 

// Left Mouse Button - Changes the Render mode from normal to wireframe.
// Right Mouse Button - Turns lighting On/Off
// Left Arrow Key - Spins the model to the left
// Right Arrow Key - Spins the model to the right
// Escape - Quits

#define FILE_NAME  "audi.ase"				
#define FILE_NAME2 "wall1.ase"// This is the 3D file we will load.
#define MAX_TEXTURE 50

UINT g_Texture[MAX_TEXTURES];						// This holds the texture info, referenced by an ID

CLoadASE g_LoadASE;										// This is ASE class.  This should go in a good model class.
t3DModel g_3DModel;
t3DModel g_3DWall;
// This holds the 3D Model info that we load in

int   g_ViewMode	  = GL_TRIANGLES;					// We want the default drawing mode to be normal
bool  g_bLighting     = true;							// Turn lighting on initially
float g_RotateX		  = 0.0f;							// This is the current value at which the model is rotated
float g_RotationSpeed = 0.8f;							// This is the speed that our model rotates.  (-speed rotates left)

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////


///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function initializes the game window.
/////
///////////////////////////////// INIT GAME WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void g_material(t3DModel &g_3DModel)
{
	for(int i = 0; i < g_3DModel.numOfMaterials; i++)
	{
		// Check to see if there is a file name to load in this material
		if(strlen(g_3DModel.pMaterials[i].strFile) > 0)
		{
			// Use the name of the texture file to load the bitmap, with a texture ID (i).
			// We pass in our global texture arrow, the name of the texture, and an ID to reference it.	
			//CreateTexture(g_Texture, g_3DModel.pMaterials[i].strFile, i);			
		}

		// Set the texture ID for this material
		g_3DModel.pMaterials[i].texureId = i;
	}
}

void Init(HWND hWnd)
{
	g_hWnd = hWnd;										// Assign the window handle to a global window handle
	GetClientRect(g_hWnd, &g_rRect);					// Assign the windows rectangle to a global RECT
	InitializeOpenGL(g_rRect.right, g_rRect.bottom);	// Init OpenGL with the global rect

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////

	// First we need to actually load the .ASE file.  We just pass in an address to
	// our t3DModel structure and the file name string we want to load ("Statue.Ase").

	g_LoadASE.ImportASE(&g_3DModel, FILE_NAME);			// Load our .Ase file into our model structure
	g_LoadASE.ImportASE(&g_3DWall,FILE_NAME2);
	// Depending on how many textures we found, load each one (Assuming .BMP)
	// If you want to load other files than bitmaps, you will need to adjust CreateTexture().
	// Below, we go through all of the materials and check if they have a texture map to load.
	// Otherwise, the material just holds the color information and we don't need to load a texture.
	
	// Go through all the materials
	g_material(g_3DModel);
	g_material(g_3DWall);
	// Here, we turn on a lighting and enable lighting.  We don't need to
	// set anything else for lighting because we will just take the defaults.
	// We also want color, so we turn that on

	glEnable(GL_LIGHT0);							// Turn on a light with defaults set
	glEnable(GL_LIGHTING);							// Turn on lighting
	glEnable(GL_COLOR_MATERIAL);					// Allow color

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
	
}


///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function Handles the main game loop
/////
///////////////////////////////// MAIN GAME LOOP \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void gobject(t3DModel &g_3DModel)
{
	for(int i = 0; i < g_3DModel.numOfObjects; i++)
	{
		// Free the faces, normals, vertices, and texture coordinates.
		delete [] g_3DModel.pObject[i].pFaces;
		delete [] g_3DModel.pObject[i].pNormals;
		delete [] g_3DModel.pObject[i].pVerts;
		delete [] g_3DModel.pObject[i].pTexVerts;
	}
}
WPARAM MainLoop()
{
	MSG msg;
	while(1)											// Do our infinate loop
	{													// Check if there was a message
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { 
			if(msg.message == WM_QUIT)					// If the message wasnt to quit
				break;
            TranslateMessage(&msg);						// Find out what the message does
            DispatchMessage(&msg);						// Execute the message
        }
		else											// if there wasn't a message
		{   

	//delay(3000);
	//while(tex2>=300)
	
	/*		if(tex2==1)
		{
		/*glBindTexture(GL_TEXTURE_2D, texture[26]);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);	glVertex3f(100.0, -40.0f, -100.0);
		glTexCoord2f(1.0f, 0.0f);	glVertex3f(100.0, 40.0f, -100.0);
		glTexCoord2f(1.0f, 1.0f);	glVertex3f(-100.0, 40.0f, -100.0);
		glTexCoord2f(0.0f, 1.0f);	glVertex3f(-100.0, -40.0f,-100.0);
		glEnd();
		glBindTexture(GL_TEXTURE_2D, texture[7]);

	glBegin(GL_QUADS);
		//glTexCoord2f(0.0f, 0.0f);	glVertex3f(2200.0, 0.0f, 100.0/*-7*19*100*///);
	//	glTexCoord2f(1.0f, 0.0f);	glVertex3f(2200.0, 40.0f, 100.0/*-7*19*100*/);
//		glTexCoord2f(1.0f, 1.0f);	glVertex3f(2200.0, 40.0f, 300.0/*-7*19*100*/);
//		glTexCoord2f(0.0f, 1.0f);	glVertex3f(2200.0, 0.0f, 300.0/*-7*19*100*/);
//	glEnd();*/
			//Renderstart();
//sleep(10000);
//tex2++;
	/*	}
			else*/
		RenderScene();								// Update the screen	

	
        } 
	}

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
gobject(g_3DModel);
gobject(g_3DWall);
	// When we are done, we need to free all the model data
	// We do this by walking through all the objects and freeing their information

	// Go through all the objects in the scene
	

//////////// *** NEW *** ////////// *** NEW *** ///////////// *** NEW *** ////////////////////
	DeInit();											// Clean up and free all allocated memory

	return(msg.wParam);									// Return from the program
}

///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
/////	This function renders the entire scene.
/////
///////////////////////////////// RENDER SCENE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
int FPS()					// This function calculates FPS
{
	static float fps           = 0.0f;						
    static float previousTime  = 0.0f;	
	static char  strFPS[20]    = {0};
	static float fps1=0.0f;
	float currentTime = (GetTickCount() * 0.001f);				

    ++fps;	// Increment the FPS counter

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -