Removida projeção da init_GL
[Projeto-PCG.git] / game.cpp
bloba03e96a224481c541106c714859b3e7b01ce3f28
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "player.h"
6 #include "controle.h"
8 const int SCREEN_WIDTH = 640;
9 const int SCREEN_HEIGHT = 480;
10 const int SCREEN_BPP = 32;
11 const int FRAMES_PER_SECOND = 60;
13 void Game::geraMapa() {
14 const double alturaChao = 400;
15 mapa = new Mapa(800, 600, player, gravityManager);
16 mapa->novaLinha(0,alturaChao,mapa->xmax(),alturaChao);
17 mapa->novaLinha(0,mapa->ymax(),mapa->xmax(),mapa->ymax());
18 mapa->novaLinha(30,350,100,350);
19 mapa->novaLinha(200,320,250,320);
20 mapa->novaLinha(400,275,450,275);
21 mapa->novaLinha(200,200,250,200);
22 mapa->novaLinha(30,100,100,100);
25 bool init_GL()
27 //Set clear color
28 glClearColor( 1, 1, 1, 0 );
30 //Initialize modelview matrix
31 glMatrixMode( GL_MODELVIEW );
32 glLoadIdentity();
34 //Linhas
35 glLineWidth(2.5);
36 glEnable(GL_LINE_SMOOTH);
37 glEnable(GL_BLEND);
38 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
39 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
41 //If there was any errors
42 if( glGetError() != GL_NO_ERROR )
44 return false;
47 //If everything initialized
48 return true;
51 Game::Game()
53 gravityManager = new GravityManager;
55 //Initialize SDL
56 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
58 //erro
61 //Create Window
62 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
64 //erro
67 //Initialize OpenGL
68 if( !init_GL() )
70 //erro
73 //Set caption
74 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
77 void Game::show() {
78 glClear( GL_COLOR_BUFFER_BIT );
79 double x, y;
81 if (player->getX() <= SCREEN_WIDTH/2)
82 x = 0;
83 else if (player->getX() >= mapa->xmax() - SCREEN_WIDTH/2)
84 x = mapa->xmax() - SCREEN_WIDTH;
85 else
86 x = player->getX() - SCREEN_WIDTH/2;
88 if (player->getY() <= 2*SCREEN_HEIGHT/3)
89 y = 0;
90 else if (player->getY() >= mapa->ymax() - SCREEN_HEIGHT/3)
91 y = mapa->ymax() - SCREEN_HEIGHT;
92 else
93 y = player->getY() - 2*SCREEN_HEIGHT/3;
95 player->desenha();
96 mapa->desenha();
98 glMatrixMode( GL_PROJECTION );
99 glLoadIdentity();
100 glOrtho( x, x+SCREEN_WIDTH, y+SCREEN_HEIGHT, y, -1, 1 );
101 SDL_GL_SwapBuffers();
104 void Game::mainLoop() {
105 luaRun luaEnv;
106 luaEnv.registerScripts();
107 luaEnv.loadScripts();
108 Timer fps;
110 player = new Player(this);
111 geraMapa();
112 Controle c(*player);
113 bool quit = false;
114 while (!quit) {
115 fps.start();
116 //player events
117 c.eventLoop();
119 //colision, gravity
120 gravityManager->update();
122 //movements
123 player->move();
124 quit = c.getQuit();
125 show();
126 if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) {
127 SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );