Skip to content

Commit d61c513

Browse files
committed
Added fixed frame rate
1 parent 61c732a commit d61c513

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

Game.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Game::~Game()
1313
//dtor
1414
}
1515

16-
bool Game::inicialitzar(char* title, int xpos, int ypos, int width, int height, Uint32 flags)
16+
bool Game::init(char* title, int xpos, int ypos, int width, int height, Uint32 flags)
1717
{
1818
//try init SDL
1919
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
@@ -45,7 +45,7 @@ bool Game::inicialitzar(char* title, int xpos, int ypos, int width, int height,
4545
return false;
4646
}
4747
cout << "incialitacio correcte " << endl;
48-
m_funcionant = true;
48+
m_running = true;
4949

5050
//load images/sprites on database;
5151
TextureManager::Instance()->load("img/animate-alpha.png", "animate_dog", m_pRenderer);
@@ -84,15 +84,15 @@ void Game::update()
8484
}
8585
}
8686

87-
void Game::tractarEvents()
87+
void Game::handleEvents()
8888
{
8989
SDL_Event event;
9090
if (SDL_PollEvent(&event))//poll pila
9191
{
9292
switch (event.type) //triem tipus d'event
9393
{
9494
case SDL_QUIT:
95-
m_funcionant = false;
95+
m_running = false;
9696
break;
9797

9898
default:
@@ -109,7 +109,7 @@ void Game::clean()
109109
SDL_Quit();
110110
}
111111

112-
bool Game::getFuncionant() const
112+
bool Game::running() const
113113
{
114-
return m_funcionant;
114+
return m_running;
115115
}

Game.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class Game //SINGLETON
3434
~Game();
3535

3636
//Game methods
37-
bool inicialitzar(char* title, int xpos, int ypos, int width, int height, Uint32 flags); //conjunt de coses que configurar al iniciar el programa
37+
bool init(char* title, int xpos, int ypos, int width, int height, Uint32 flags); //conjunt de coses que configurar al iniciar el programa
3838
void render(); //conjunt de coses que fan veure en pantalla
3939
void update();//actualitzacions bufferianes
40-
void tractarEvents();//tractament de evenets (inputs)
40+
void handleEvents();//tractament de evenets (inputs)
4141
void clean();//delete
4242

4343
//GETs
44-
bool getFuncionant() const;
44+
bool running() const;
4545

4646

4747
private:
@@ -51,7 +51,7 @@ class Game //SINGLETON
5151
static Game* s_pInstance;
5252

5353
//Atributs
54-
bool m_funcionant; //Atribut per mirar si continua el loop del joc.
54+
bool m_running; //Atribut per mirar si continua el loop del joc.
5555
SDL_Window* m_pWindow; //Window del joc.
5656
SDL_Renderer* m_pRenderer; //Renderer del joc.
5757
int m_currentFrame;

main.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,32 @@
88

99
#include "Game.h"
1010

11+
//MAIN CONSTANTS
12+
const int FPS = 60; ///////////////////_______________////
13+
const int DELAY_TIME = 1000.0f / FPS;//fixed framerate////
14+
1115
using namespace std;
1216

1317
int main ( int argc, char** argv )
1418
{
15-
if(TheGame::Instance()->inicialitzar("Chapter 1", 100, 100, 640, 480, 0))
19+
Uint32 frameStart, frameTime;
20+
21+
if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, 0))
1622
{
17-
while (TheGame::Instance()->getFuncionant())
23+
while (TheGame::Instance()->running())
1824
{
19-
TheGame::Instance()->tractarEvents();
25+
frameStart = SDL_GetTicks(); //fixed frame rate var
26+
27+
TheGame::Instance()->handleEvents();
2028
TheGame::Instance()->update();
2129
TheGame::Instance()->render();
2230

23-
SDL_Delay(10);
31+
frameTime = SDL_GetTicks() - frameStart; //fixed frame rate var
32+
33+
if (frameTime < DELAY_TIME) //fixed frame rate
34+
{
35+
SDL_Delay((int)(DELAY_TIME - frameTime)); //delay to achieve fixed frame rate
36+
}
2437
}
2538
TheGame::Instance()->clean();
2639
}

0 commit comments

Comments
 (0)