Havia esquecido de passar a tecla para baixo para controleteclado
[Projeto-PCG.git] / game.cpp
blob1ced9ad14d38d53bf08430715cdb25cf4479b574
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "controleteclado.h"
7 const int SCREEN_WIDTH = 640;
8 const int SCREEN_HEIGHT = 480;
9 const int SCREEN_BPP = 32;
10 const int FRAMES_PER_SECOND = 60;
12 void Game::loadMap(std::string mapname) {
13 if (mapa != NULL)
14 delete mapa;
15 const double alturaChao = 400;
16 mapa = new Mapa(mapname,this);
19 void Game::addPlatform(Platform* plat) {
20 gravityManager->addPlatform(plat);
23 void Game::removePlatforms() {
24 gravityManager->removePlatforms();
27 bool init_GL()
29 //Set clear color
30 glClearColor( 1, 1, 1, 0 );
32 //Initialize modelview matrix
33 glMatrixMode( GL_MODELVIEW );
34 glLoadIdentity();
36 //Linhas
37 glLineWidth(2.5);
38 glEnable(GL_LINE_SMOOTH);
39 glEnable(GL_BLEND);
40 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
41 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
43 //If there was any errors
44 if( glGetError() != GL_NO_ERROR )
46 return false;
49 //If everything initialized
50 return true;
53 Game::Game()
55 gravityManager = new GravityManager;
56 mapa = NULL;
57 player = NULL;
58 //Initialize SDL
59 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
61 //erro
64 //Create Window
65 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
67 //erro
70 //Initialize OpenGL
71 if( !init_GL() )
73 //erro
76 //Set caption
77 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
80 void Game::desenhaMira(Ponto aim) {
81 glPushMatrix();
82 glTranslatef(camera.x+aim.x,camera.y+aim.y,0);
83 drawCircle(10,10);
84 glPopMatrix();
87 void Game::show() {
88 glClear( GL_COLOR_BUFFER_BIT );
89 double x, y;
91 if (player->getX() <= SCREEN_WIDTH/2)
92 x = 0;
93 else if (player->getX() >= mapa->xmax() - SCREEN_WIDTH/2)
94 x = mapa->xmax() - SCREEN_WIDTH;
95 else
96 x = player->getX() - SCREEN_WIDTH/2;
98 if (player->getY() <= 2*SCREEN_HEIGHT/3)
99 y = 0;
100 else if (player->getY() >= mapa->ymax() - SCREEN_HEIGHT/3)
101 y = mapa->ymax() - SCREEN_HEIGHT;
102 else
103 y = player->getY() - 2*SCREEN_HEIGHT/3;
104 camera.x = x;
105 camera.y = y;
106 player->desenha();
107 mapa->desenha();
109 glMatrixMode( GL_PROJECTION );
110 glLoadIdentity();
111 glOrtho( camera.x, camera.x+SCREEN_WIDTH, camera.y+SCREEN_HEIGHT, camera.y, -1, 1 );
112 SDL_GL_SwapBuffers();
115 void Game::reloadLua() {
116 loadMap(currentMap);
117 weaponManager->loadWeapons();
118 player->equip(weaponManager->getWeapon("Shotgun"));
121 void Game::setSpawn(Ponto spawn) {
122 this->spawn = spawn;
125 void Game::mainLoop() {
126 luaRun luaEnv;
127 luaEnv.registerScripts();
128 luaEnv.loadScripts();
129 Timer fps;
131 player = new Player(this);
132 currentMap = "map1.lua";
133 loadMap(currentMap);
134 player->setPosition(spawn.x,spawn.y);
135 ControleTeclado c(*player);
136 bool quit = false;
137 weaponManager = new WeaponManager;
138 weaponManager->loadWeapons();
139 player->equip(weaponManager->getWeapon("Shotgun"));
140 while (!quit) {
141 fps.start();
142 //player events
143 c.handleEvents();
145 //colision, gravity
146 gravityManager->update();
148 //movements
149 player->move();
150 quit = c.getQuit();
151 show();
152 if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) {
153 SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
156 SDL_Quit();