📄 msg.cpp
字号:
////////////////////////////////////////////////////////////////////////////////
// Program : Framework
// File : m3g.cpp
// Note : Include freeglut.lib, glu32.lib & freeglut.dll,
// in running directory
////////////////////////////////////////////////////////////////////////////////
#include "gl/freeglut.h"
#include <string>
#include "engine.h"
#include "demo.h"
#include "font.h"
#include "cube.h"
#include "sphere.h"
#include "plane.h"
#include "camera.h"
#include "highscore.h"
#include "menu.h"
#include "sound.h"
#include <mmsystem.h>
#include <sstream>
using namespace std;
// #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
CCamera *camera ;
bool full_screen;
bool is_fullscreen;
float ground_level;
float world_size;
map < int, CObject* > objects;
float move =0.0;
int score;
int count_down;
string fps_string;
float elapseTime = 0.0;
bool game_over;
//TextureImage fontTextures [ 1 ];
CCube * player;
CPlane *ground;
CSphere *sphere;
int game_state ;
void RunFSM()
{
/*if(fsm_state == 0)
{
sphere->pos.x
std::cout<<"chase"<<std::endl;
}
else if(fsm_state == 1)
{
std::cout<<"run"<<std::endl;
}
else if(fsm_state == 2)
{
score++;
}*/
}
void UpdateTime()
{
static float frame_per_sec = 0.0;
static float last_time = 0.0;
static float last_frame_time = 0.0f;
// get time in milliseconds, convert to seconds by * 0.001
float current_time = timeGetTime() * 0.001f;
// calculate frame interval by current time minus last frame's time
frame_interval = current_time - last_frame_time;
last_frame_time = current_time;
frame_per_sec++;
if( current_time - last_time > 1.0f )
{
// set last frame time to current time
last_time = current_time;
// copy frames per second into a string
fps_string = "FPS : " + IntToString((int)frame_per_sec );
// reset frames per second
frame_per_sec = 0.0;
elapseTime++;
}
if (full_screen&&!is_fullscreen)
{
is_fullscreen=true;
glutFullScreen();
}
else if(!full_screen&&is_fullscreen)
{
is_fullscreen=false;
glutPositionWindow(100,80);
glutReshapeWindow(800,600);
}
}
void CheckGameOver()
{
if ( elapseTime > count_down )
{
if ( IsScoreInTop3( score ) )
game_state = highscore_entry_state;
else
game_state = gameover_state;
game_over = true;
}
}
void UpdateUI()
{
// draw textured font
glEnable( GL_BLEND );
glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, fontTextures[0].id);// Select Our Font Texture
glMatrixMode ( GL_PROJECTION ) ;
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f,640,480,0.0f,-1.0f,1.0f);// Create Ortho 640x480 View (0,0 At Top Left)
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPushMatrix(); // Store The Modelview Matrix
glLoadIdentity();
glColor3f(0.0f,1.0f,0.0f); // Set Color To Green
string s =" Score : ";
s = s + IntToString(score);
PrintList(50,15,0,s.c_str());
s= "Timer : ";
if (!game_over)
s = s + IntToString(count_down-(int)elapseTime);
else s = "Game Over ";
PrintList(260,15,0,s.c_str());
PrintList(500,15,0,fps_string.c_str());
glMatrixMode ( GL_PROJECTION ) ;
glPopMatrix();
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable( GL_BLEND );
}
void RenderScene()
{
UpdateTime();
CheckGameOver();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearDepth( 1.0 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity();
if ( game_state == menu_state )
{
if (msource)
{
if (UpdateMusic())
{
if(!Playing())
{
if(!PlayBack())
cout << "Error : Music stopped" << endl;
}
}
}
RenderMenu();
}
if( game_state == gameover_state )
{
RenderGameOver();
}
if ( game_state == highscore_entry_state && !entername )
{
RenderHighscoreEntry(); //
}
if ( game_state == play_state )
{
// gluLookAt( pos.x, pos.y, pos.z, view.x, view.y, view.z, up.x, up.y, up.z );
camera->LookAt();
ground->Render();
map <int, CObject*> :: iterator j = objects.begin();
for (; j != objects.end();j++)
{
//((*j).second)->Render();
CObject *obj = (*j).second;
obj->Render();
if ( player->bsphere.CollideWith( obj ) )
{
if ( obj->type == "sphere" )
{
objects.erase(j);
alSourcePlay(ssource);
// score++;
return;
}
if ( obj->type == "plane" )
{
CPlane *p = (CPlane*) obj;
player->MoveBackward(camera);
player->MoveBackward(camera);
}
}
}
player->Render();
// std::cout<<"pangle"<<player->angle<<std::endl;
// std::cout<<"espeed"<<sphere->speed<<std::endl;
UpdateUI();
glDisable( GL_DEPTH_TEST );
}
if ( game_state == highscore_view_state )
{
RenderHighscore();//
}
glutSwapBuffers();
}
void ProcessHits( GLuint id )
{
switch( id )
{
case START_BUTTON :
game_state = play_state;
if (msource)
ReleaseMusic();
break;
case HIGH_SCORE_BUTTON :
game_state = highscore_view_state;
/*if (msource)
ReleaseMusic();*/
break;
case QUIT_BUTTON :
glutLeaveMainLoop();
break;
case BACK_BUTTON :
game_state = menu_state;
break;
}
}
void ProcessSelection( int mx, int my )
{
GLfloat aspect;
static GLuint selectBuffer[32];
GLint viewport[4];
int hits;
glSelectBuffer(32,selectBuffer);
glGetIntegerv(GL_VIEWPORT,viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
glLoadIdentity();
gluPickMatrix(mx,viewport[3]-my,2,2,viewport);
aspect = (float) viewport[2]/(float)viewport[3];
gluPerspective(45.0,aspect,0.1,1000.0);
RenderScene();
glRenderMode( GL_RENDER );
hits = selectBuffer[0];
ProcessHits(selectBuffer[ 2 + hits ] );
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void InitApp()
{
bool result = ReadAppDataFile ( app_data_file );
world_size = parameter1;
ground_level = 0.0f;
full_screen = true;
is_fullscreen = false;
game_over = false;
count_down = 100;
game_state = menu_state;
InitHighscore();
ReadHighScoresFile();
intro_music_file = "media/believe.ogg";
collide_sound_file = "media/collide.wav";
audio_init = false;
if (alutInit(NULL, NULL))
sbuffer = alutCreateBufferFromFile( collide_sound_file.c_str());
if (sbuffer == AL_NONE)
{
cout << "Error : Loading sound file" << endl;
alutExit();
}
if(!audio_init)
alGenSources(1, &ssource);
audio_init = true;
alSourcei(ssource, AL_BUFFER, sbuffer);
if(alGetError() != AL_NO_ERROR)
{
cout << "Error : Binding audio data" << endl;
alutExit();
}
if (!(oggFile = fopen(intro_music_file.c_str(), "rb")))
cout << "Error : Reading file" << endl;
// could not open Ogg stream
if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) // initialised
{
fclose(oggFile);
cout << "Error : Reading file" << endl;
}
vorbisInfo = ov_info(&oggStream, -1); // not seekable
vorbisComment = ov_comment(&oggStream, -1);
if(vorbisInfo->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
alGenBuffers(2, buffers);
alGenSources(1, &msource);
}
void InitGL()
{
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
LoadTGA( &textures[ 0 ], "images/grass.tga" );
LoadTGA( &textures[ 1 ], "images/brick.tga" );
LoadTGA( &textures[ 4 ], "images/brick.tga" );
LoadTGA( &textures[ 5 ], "images/brick.tga" );
LoadTGA( &textures[ 6 ], "images/brick.tga" );
//menu
LoadTGA( &textures[ 21 ], "images/start.tga" );
LoadTGA( &textures[ 22 ], "images/high_score.tga" );
LoadTGA( &textures[ 23 ], "images/quit.tga" );
LoadTGA( &textures[ 24 ], "images/back.tga" );
LoadTGA( &textures[ 25 ], "images/gameover.tga" );
LoadTGA( &textures[ 26 ], "images/high_score_tag.tga" );
/*aw*/
// float wall_height = 0.5;
float wall_height = 0.5;
float df = world_size/wall_height; // dimension factor
float hworld_size = world_size / 2.0f;
ground= new CPlane( -hworld_size, ground_level, hworld_size,
hworld_size, ground_level, hworld_size,
hworld_size, ground_level, -hworld_size,
-hworld_size, ground_level, -hworld_size,
0.0f, 0.8f, 0.0f, 0, 1.0f );
/*objects.insert( map<int,CObject*>::value_type( 0,
new CPlane( -hworld_size, ground_level, hworld_size,
hworld_size, ground_level, hworld_size,
hworld_size, ground_level, -hworld_size,
-hworld_size, ground_level, -hworld_size,
0.0f, 0.8f, 0.0f, 0, 1.0f )));*/
objects.insert( map<int,CObject*>::value_type( 2,
new CSphere( 0.0f, 0.1f, -0.4f, 0.0f, 0.0f, 1.0f, 0.1f, true, 0.0f)));
objects.insert( map<int,CObject*>::value_type( 7,
new CSphere( 0.4f, 0.1f, -0.8f, 0.0f, 0.0f, 1.0f, 0.1f, true, 0.0f)));
objects.insert( map<int,CObject*>::value_type( 8,
new CSphere( -0.4f, 0.1f, -0.6f, 0.0f, 0.0f, 1.0f, 0.1f, true, 0.0f)));
player = new CCube( 0.0f, ground_level + 0.1f, 0.0f, 0.0f, 0.9f, 0.9f, 0.1f, true, 0.01f, true, false);
/*objects.insert( map<int,CObject*>::value_type( 3,
new CCube( 0.0f, ground_level + 0.1f, 0.0f, 0.0f, 0.9f, 0.9f, 0.1f,
true, 0.01f, true, true)));*/
// add back wall
objects.insert( map< int, CObject* >::value_type ( 1 ,
new CPlane ( -hworld_size, ground_level, -hworld_size,
hworld_size, ground_level, -hworld_size,
hworld_size, ground_level + wall_height, -hworld_size,
-hworld_size, ground_level + wall_height, -hworld_size,
1.0f, 1.0f, 1.0f,
1.0f,df )));
// add left wall
objects.insert( map< int, CObject* >::value_type ( 4 ,
new CPlane ( -hworld_size, ground_level, -hworld_size+world_size,
-hworld_size, ground_level, -hworld_size,
-hworld_size, ground_level + wall_height, -hworld_size,
-hworld_size, ground_level + wall_height, -hworld_size+world_size,
1.0f, 1.0f, 1.0f,
1.0f,df )));
// add right wall
objects.insert( map< int, CObject* >::value_type ( 5 ,
new CPlane ( hworld_size, ground_level, -hworld_size+world_size,
hworld_size, ground_level, -hworld_size,
hworld_size, ground_level + wall_height, -hworld_size,
hworld_size, ground_level + wall_height, -hworld_size+world_size,
1.0f, 1.0f, 1.0f,
1.0f,df )));
// add front wall
objects.insert( map< int, CObject* >::value_type ( 6 ,
new CPlane ( -hworld_size, ground_level, hworld_size,
hworld_size, ground_level, hworld_size,
hworld_size, ground_level + wall_height, hworld_size,
-hworld_size, ground_level + wall_height, hworld_size,
1.0f, 1.0f, 1.0f,
1.0f,df )));
BuildFont();
pos.x = 0.0; pos.y = 1.1; pos.z = hworld_size;
view.x = 0.0; view.y = 0.1; view.z = 0.0;
up.x = 0.0; up.y = 1.0; up.z = 0.0;
camera = new CCamera();
camera->Set( pos , player->pos, up , 0.01 );
}
void Resize( int width, int height )
{
glViewport ( 0, 0, width, height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, (float)(width)/(float)(height), 0.1, 1000 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
}
void NormalKeys( unsigned char key, int x, int y )
{
if ( game_state == play_state )
{
switch ( key )
{
case 'a' :
case 'A' :
player->TurnLeft(camera);
break ;
case 's' :
case 'S' :
//player->MoveBackward(camera);
break ;
case 'w' :
case 'W' :
player->MoveForward(camera);
break ;
case 'd' :
case 'D' :
player->TurnRight(camera);
break ;
case 27 :
ShutdownFont();
glutLeaveMainLoop();
break;
default :
break;
}
}
if ( game_state == gameover_state )
{
switch( key )
{
case 27 :
ShutdownFont();
glutLeaveMainLoop();
break;
}
}
if ( game_state == highscore_entry_state )
{
int len;
switch( key )
{
case 27 :
glutLeaveMainLoop();
break;
case 8 :
len = player_name.length();
if ( len > 0 )
player_name = player_name.substr(0,len-1);
break;
case 13 :
high_scores.insert(multimap<int,string>::value_type(score,player_name));
SaveHighScores();
entername = true;
glutLeaveMainLoop();
break;
default :
if (( key < 256 ) && ( key >= 27 ))
{
len = player_name.length();
if ( len < 8)
player_name=player_name+(char)key;
}
break;
}
}
}
void SpecialKeys( int key, int x, int y )
{
switch ( key )
{
case GLUT_KEY_UP :
camera->Up();
break ;
case GLUT_KEY_DOWN :
camera->Down();
break ;
case GLUT_KEY_LEFT :
break ;
case GLUT_KEY_RIGHT :
break ;
case GLUT_KEY_F1 :
if(full_screen)
{
full_screen = false;
}
else if(!full_screen)
{
full_screen = true;
}
// pos.x = 0.0; pos.y = 0.3; pos.z = 3.0;
break ;
case GLUT_KEY_F2 :
// pos.x = 0.0; pos.y = 3.5; pos.z = 0.8;
break ;
case GLUT_KEY_F4 :
player->showsphere = !player->showsphere;
break;
default:
break;
}
glutPostRedisplay();
}
void ProcessMouseClick( int button, int state, int x, int y )
{
switch( button )
{
case GLUT_LEFT_BUTTON :
ProcessSelection(x,y);
break;
case GLUT_RIGHT_BUTTON :
break;
case GLUT_MIDDLE_BUTTON :
break;
}
}
void ProcessMouseMove( int x, int y )
{}
int main( int argc, char *argv[] )
{
glutInit( &argc, argv );
InitApp();
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition(100,80);
glutInitWindowSize( 800,600 );
glutCreateWindow( "Framework" );
InitGL();
glutIdleFunc( RenderScene );
glutReshapeFunc( Resize );
glutDisplayFunc( RenderScene );
glutKeyboardFunc( NormalKeys );
glutSpecialFunc( SpecialKeys );
glutMouseFunc( ProcessMouseClick );
glutPassiveMotionFunc( ProcessMouseMove );
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -