Last Hour Pack
[Projeto-PCG.git] / game.cpp
blobb30d8ab2263a87fea6adf8bc68aea6d5f1bfc0b6
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"
9 void Game::loadMap(std::string mapname) {
10 if (mapa != NULL)
11 delete mapa;
12 mapa = new Mapa(mapname,this);
15 void Game::addPlatform(Platform* plat) {
16 gravityManager->addPlatform(plat);
19 void Game::removePlatforms() {
20 gravityManager->removePlatforms();
23 bool init_GL() {
24 //Set clear color
25 glClearColor( 1, 1, 1, 0 );
27 //Initialize modelview matrix
28 glMatrixMode( GL_MODELVIEW );
29 glLoadIdentity();
31 //Z-buffer
32 glEnable(GL_DEPTH_TEST);
33 glDepthFunc(GL_LEQUAL);
35 //Linhas
36 glLineWidth(2.5);
37 glEnable(GL_LINE_SMOOTH);
38 glEnable(GL_BLEND);
39 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
40 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
42 //If there was any errors
43 if( glGetError() != GL_NO_ERROR )
45 return false;
48 //If everything initialized
49 return true;
52 Game::Game(ConfigManager *cfg) {
53 gravityManager = new GravityManager;
54 mapa = NULL;
55 player = NULL;
56 config = cfg;
57 //Initialize SDL
58 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
60 //erro
63 //Create Window
64 if( SDL_SetVideoMode( config->screen["width"], config->screen["height"], config->screen["bpp"], SDL_OPENGL | SDL_RESIZABLE ) == NULL )
66 //erro
69 //Initialize OpenGL
70 if( !init_GL() )
72 //erro
75 //Set caption
76 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
79 void Game::desenhaMira(Ponto aim) {
80 glPushMatrix();
81 glTranslatef(camera.x+aim.x,camera.y+aim.y,0);
82 drawCircle(10,10);
83 glPopMatrix();
86 void Game::show() {
87 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
88 double x, y;
89 int width = config->screen["width"];
90 int height = config->screen["height"];
92 if (player->getX() <= width/2)
93 x = 0;
94 else if (player->getX() >= mapa->xmax() - width/2)
95 x = mapa->xmax() - width;
96 else
97 x = player->getX() - width/2;
99 if (player->getY() <= 2*height/3)
100 y = 0;
101 else if (player->getY() >= mapa->ymax() - height/3)
102 y = mapa->ymax() - height;
103 else
104 y = player->getY() - 2*height/3;
105 camera.x = x;
106 camera.y = y;
107 player->desenha();
108 mapa->desenha();
109 shotManager->desenha();
111 glMatrixMode( GL_PROJECTION );
112 glLoadIdentity();
113 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -2, 2 );
114 SDL_GL_SwapBuffers();
117 void Game::reloadLua() {
118 config->load();
119 resize(config->screen["width"], config->screen["height"]);
120 loadMap(config->maps.front());
121 weaponManager->loadWeapons();
122 shotManager->clearShots();
123 player->equip(weaponManager->getWeapon("Shotgun"));
126 void Game::setSpawn(Ponto spawn) {
127 this->spawn = spawn;
130 void Game::resize(GLsizei x, GLsizei y) {
131 config->screen["width"] = x;
132 config->screen["height"] = y;
133 SDL_SetVideoMode(x, y, config->screen["bpp"], SDL_OPENGL | SDL_RESIZABLE);
134 glViewport(0, 0, x, y);
137 void Game::mainLoop() {
138 Timer fps;
140 loadMap(config->maps.front());
142 player = new Player(this, spawn, config->player["speed"]);
143 Controle *c;
144 if (true)
145 c = new ControleTeclado(*player);
146 else
147 c = new ControleWii(*player);
149 shotManager = new ShotManager;
150 weaponManager = new WeaponManager(this);
151 collisionManager = new CollisionManager;
152 weaponManager->loadWeapons();
153 player->equip(weaponManager->getWeapon("Shotgun"));
155 collisionManager->subscribe(player);
156 bool quit = false;
157 while (!quit) {
158 int ifps = config->screen["fps"];
159 fps.start();
160 //player events
161 c->handleEvents();
163 //collision, gravity
164 gravityManager->update();
165 collisionManager->update();
167 //movements
168 player->move();
169 shotManager->move();
170 quit = c->getQuit();
171 show();
172 rate = ((double)fps.get_ticks())/ifps;
173 if (fps.get_ticks() < 1000 / ifps ) {
174 SDL_Delay( ( 1000 / ifps ) - fps.get_ticks() );
177 SDL_Quit();