Criada classe Mapa; scrolling
[Projeto-PCG.git] / game.cpp
blobb147a737c9a54654fe81d24ceda1cfbebaa67b48
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(30,350,100,350);
18 mapa->novaLinha(200,320,250,320);
19 mapa->novaLinha(400,275,450,275);
20 mapa->novaLinha(200,200,250,200);
21 mapa->novaLinha(30,100,100,100);
24 bool init_GL()
26 //Set clear color
27 glClearColor( 1, 1, 1, 0 );
29 //Set projection
30 glMatrixMode( GL_PROJECTION );
31 glLoadIdentity();
32 glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
34 //Initialize modelview matrix
35 glMatrixMode( GL_MODELVIEW );
36 glLoadIdentity();
38 //Linhas
39 glLineWidth(2.5);
40 glEnable(GL_LINE_SMOOTH);
41 glEnable(GL_BLEND);
42 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
43 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
45 //If there was any errors
46 if( glGetError() != GL_NO_ERROR )
48 return false;
51 //If everything initialized
52 return true;
55 Game::Game()
57 gravityManager = new GravityManager;
59 //Initialize SDL
60 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
62 //erro
65 //Create Window
66 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
68 //erro
71 //Initialize OpenGL
72 if( !init_GL() )
74 //erro
77 //Set caption
78 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
81 void Game::show() {
82 glClear( GL_COLOR_BUFFER_BIT );
83 double x, y;
85 if (player->getX() <= SCREEN_WIDTH/2)
86 x = 0;
87 else if (player->getX() >= mapa->xmax() - SCREEN_WIDTH/2)
88 x = mapa->xmax() - SCREEN_WIDTH;
89 else
90 x = player->getX() - SCREEN_WIDTH/2;
92 if (player->getY() <= 2*SCREEN_HEIGHT/3)
93 y = 0;
94 else if (player->getY() >= mapa->ymax() - SCREEN_HEIGHT/3)
95 y = mapa->ymax() - SCREEN_HEIGHT;
96 else
97 y = player->getY() - 2*SCREEN_HEIGHT/3;
99 player->desenha();
100 mapa->desenha();
102 glMatrixMode( GL_PROJECTION );
103 glLoadIdentity();
104 glOrtho( x, x+SCREEN_WIDTH, y+SCREEN_HEIGHT, y, -1, 1 );
105 SDL_GL_SwapBuffers();
108 void Game::mainLoop() {
109 luaRun luaEnv;
110 luaEnv.registerScripts();
111 luaEnv.loadScripts();
112 Timer fps;
114 player = new Player(this);
115 geraMapa();
116 Controle c(*player);
117 bool quit = false;
118 while (!quit) {
119 fps.start();
120 //player events
121 c.eventLoop();
123 //colision, gravity
124 gravityManager->update();
126 //movements
127 player->move();
128 quit = c.getQuit();
129 show();
130 if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) {
131 SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );