Gambiarra sobre erro em Shooter::getCollision
[Projeto-PCG.git] / game.cpp
blob9b1bc3edb4e20ecff40b1b2a78a84ef5e7fb8418
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "controleteclado.h"
6 #include "controlewii.h"
7 #include "shotmanager.h"
8 #include "weaponitem.h"
10 void Game::loadMap(std::string mapname) {
11 if (mapa != NULL)
12 delete mapa;
13 mapa = new Mapa(mapname,this);
16 void Game::addPlatform(Platform* plat) {
17 gravityManager->addPlatform(plat);
20 void Game::removePlatforms() {
21 gravityManager->removePlatforms();
24 bool init_GL() {
25 //Set clear color
26 glClearColor( 1, 1, 1, 0 );
28 //Initialize modelview matrix
29 glMatrixMode( GL_MODELVIEW );
30 glLoadIdentity();
32 //Z-buffer
33 glEnable(GL_DEPTH_TEST);
34 glDepthFunc(GL_LEQUAL);
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(ConfigManager *cfg) {
54 gravityManager = new GravityManager;
55 mapa = NULL;
56 player = NULL;
57 config = cfg;
58 //Initialize SDL
59 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
61 //erro
64 //Create Window
65 if( SDL_SetVideoMode( config->screen["width"], config->screen["height"], config->screen["bpp"], SDL_OPENGL | SDL_RESIZABLE ) == 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(aim.x,aim.y,0);
83 drawCircle(10,10);
84 glPopMatrix();
87 void Game::show() {
88 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
89 double x, y;
90 int width = config->screen["width"];
91 int height = config->screen["height"];
93 if (player->getX() <= width/2)
94 x = 0;
95 else if (player->getX() >= mapa->xmax() - width/2)
96 x = mapa->xmax() - width;
97 else
98 x = player->getX() - width/2;
100 if (player->getY() <= 2*height/3)
101 y = 0;
102 else if (player->getY() >= mapa->ymax() - height/3)
103 y = mapa->ymax() - height;
104 else
105 y = player->getY() - 2*height/3;
106 camera.x = x;
107 camera.y = y;
108 player->desenha();
109 mapa->desenha();
110 shotManager->desenha();
112 glMatrixMode( GL_PROJECTION );
113 glLoadIdentity();
114 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -2, 2 );
115 SDL_GL_SwapBuffers();
118 void Game::reloadLua() {
119 config->load();
120 resize(config->screen["width"], config->screen["height"]);
121 loadMap(config->maps.front());
122 weaponManager->loadWeapons();
123 shotManager->clearShots();
124 player->equip(weaponManager->getWeapon("Shotgun"));
127 void Game::setSpawn(Ponto spawn) {
128 this->spawn = spawn;
131 void Game::resize(GLsizei x, GLsizei y) {
132 config->screen["width"] = x;
133 config->screen["height"] = y;
134 SDL_SetVideoMode(x, y, config->screen["bpp"], SDL_OPENGL | SDL_RESIZABLE);
135 glViewport(0, 0, x, y);
138 WeaponItem* Game::dropWeapon(std::string name) {
139 return weaponManager->getItem(name);
142 void Game::mainLoop() {
143 Timer fps;
144 shotManager = new ShotManager;
145 weaponManager = new WeaponManager(this);
146 collisionManager = new CollisionManager;
147 weaponManager->loadWeapons();
148 loadMap(config->maps.front());
150 player = new Player(this, spawn, config->player["speed"]);
151 Controle *c;
152 if (false)
153 c = new ControleTeclado(*player);
154 else
155 c = new ControleWii(*player);
156 player->equip(weaponManager->getWeapon("Shotgun"));
158 collisionManager->subscribe(player);
159 std::list<WeaponItem*>::iterator itW;
160 for (itW = mapa->items.begin(); itW != mapa->items.end(); itW++) {
161 collisionManager->subscribe(*itW);
162 gravityManager->subscribe(*itW);
165 bool quit = false;
166 rate = 1.0;
167 while (!quit) {
168 int ifps = config->screen["fps"];
169 fps.start();
170 //player events
171 c->handleEvents();
173 //collision, gravity
174 gravityManager->update();
175 collisionManager->update();
177 player->animate();
179 //movements
180 player->move();
181 shotManager->move();
182 mapa->move();
183 quit = c->getQuit();
184 show();
185 rate = ((double)fps.get_ticks())/ifps;
186 if (rate < 1.0)
187 rate = 1.0;
188 if (fps.get_ticks() < 1000 / ifps ) {
189 SDL_Delay( ( 1000 / ifps ) - fps.get_ticks() );
192 SDL_Quit();