/
gamebase.cpp
156 lines (124 loc) · 3.81 KB
/
gamebase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "gamebase.hpp"
#include <time.h>
#include "shader/title.hpp"
Game* Game::instance = NULL;
Game::Game(char *title_,int w,int h){
if (instance == NULL){
isClosing = false;
instance = this;
canDebug = false;
backBuffer = NULL;
canDelete = false;
if (not SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK)){
std::cout <<"Sdl started.";
}
if (not IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF)){
printf("IMG FAIL");
}
if (TTF_Init() == -1 ){
printf("TTF FAIL");
}
window = SDL_CreateWindow( title_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_RESIZABLE ); //SDL_WINDOW_RESIZABLE
strcpy(title,title_);
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer){
printf("Fail render!");
}
SDL_SetRenderDrawBlendMode(GetRenderer(),SDL_BLENDMODE_BLEND);
dt = frameStart = frames = 0;
nextUpdate = 1000;
CalculateDeltaTime();
srand(time(NULL));
storedState =new Title();
olt = SDL_GetTicks();
InputManager::GetInstance().InitJoySticks();
}
}
Game::~Game(){
stateStack.pop();
//delete title;
isClosing = true;
printf("Closing!\n");
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
printf("Closing text\n");
TTF_Quit();
printf("Closing img\n");
IMG_Quit();
printf("Closing sdl\n");
SDL_Quit();
std::cout << "Quit game.\n";
}
Game *Game::GetInstance(){
return instance;
}
void Game::Update(){
float dt = std::min(GetDeltaTime(),1.2f );
stateStack.top()->Update(dt);
}
void Game::Render(){
stateStack.top()->Render();
}
void Game::UpdateTitleAsFps(float w8){
if (nextUpdate <= SDL_GetTicks()){
char Buff[100];
FPS = frames;
sprintf(Buff,"%s (FPS: %f) = %f = %f",title,frames*1000.0/(1000.0-(SDL_GetTicks()-olt)),GetDeltaTime(),w8);
SDL_SetWindowTitle(window, Buff);
olt = SDL_GetTicks()+1000;
nextUpdate = olt;
updateFPS=true;
frames = 0;
}else{
updateFPS= false;
}
}
void Game::Run(){
if (storedState != NULL){
stateStack.emplace(storedState);
storedState =NULL;
}
while (!SDL_QuitRequested() and not stateStack.top()->RequestedQuit()){
restart_cycle:
frameStart = dt;
CalculateDeltaTime();
InputManager::GetInstance().Update();
if (ConfigManager::GetInstance().IsResized())
goto restart_cycle;
Update();
if (InputManager::GetInstance().KeyPress(SDLK_F11)){
static bool full = false;
if (!full)
SDL_SetWindowFullscreen(window, SDL_TRUE);
else
SDL_SetWindowFullscreen(window, SDL_FALSE);
full = !full;
}
Render();
SDL_RenderPresent(GetRenderer() );
//Limitar dinamicamente o tempo de cada frame para atingir sempre que possivel 30 fps
float n = GetDeltaTime()*100.0;
if (stateStack.top()->RequestedDeleted()){
if (stateStack.empty() && storedState == NULL){
break;
}else{
stateStack.pop();
}
}
if (storedState != NULL){
stateStack.emplace(storedState);
stateStack.top()->Begin();
storedState =NULL;
}
n = n-(1000.0/MAXFPS);
n = n < 0 ? 0 : n;
n = (1000.0/MAXFPS)-n < 0 ? 0 : n;
//SDL_Delay( std::max( (1000.0/MAXFPS)-n - 5, 0.0) );
frames++;
UpdateTitleAsFps((1000.0/MAXFPS)-n );
}
isClosing = true;
};
State &Game::GetCurrentState(){
return *stateStack.top();
}