-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
194 lines (138 loc) · 5.15 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (c) 2012 Cheese and Bacon Games, LLC */
/* This file is licensed under the MIT License. */
/* See the file docs/LICENSE.txt for the full license text. */
#include "main.h"
#include "world.h"
#ifdef GAME_OS_OSX
#include <CoreFoundation/CoreFoundation.h>
#endif
using namespace std;
void game_loop(){
//The maximum number of frames to be skipped.
int max_frameskip=5;
double next_game_tick=(double)SDL_GetTicks();
double next_render_tick=(double)SDL_GetTicks();
//The number of logic updates that have occurred since the last render.
int number_of_updates=0;
Timer timer_frame_rate;
Timer timer_logic_frame_rate;
Timer timer_ms_per_frame;
Timer ms_per_frame_update;
double ms_per_frame=0.0;
int frame_count=0;
int frame_rate=0;
int logic_frame_count=0;
int logic_frame_rate=0;
timer_frame_rate.start();
timer_logic_frame_rate.start();
timer_ms_per_frame.start();
ms_per_frame_update.start();
while(true){
if(ms_per_frame_update.get_ticks()>=100){
ms_per_frame=(double)timer_ms_per_frame.get_ticks();
ms_per_frame_update.start();
}
timer_ms_per_frame.start();
if(timer_frame_rate.get_ticks()>=1000){
frame_rate=frame_count;
frame_count=0;
timer_frame_rate.start();
}
//For our game loop, we first update game logic, and then render. The two are kept separate and independent.
number_of_updates=0;
//We consume SKIP_TICKS sized chunks of time, which ultimately translates to updating the logic UPDATE_LIMIT times a second.
//We also check to see if we've updated logic max_frameskip times without rendering, at which point we render.
while((double)SDL_GetTicks()>next_game_tick && number_of_updates<max_frameskip){
if(timer_logic_frame_rate.get_ticks()>=1000){
logic_frame_rate=logic_frame_count;
logic_frame_count=0;
timer_logic_frame_rate.start();
}
logic_frame_count++;
//We are doing another game logic update.
//If this reaches max_frameskip, we will render.
number_of_updates++;
//Clamp the time step to something reasonable.
if(abs((double)SDL_GetTicks()-next_game_tick)>SKIP_TICKS*2.0){
next_game_tick=(double)SDL_GetTicks()-SKIP_TICKS*2.0;
}
//Consume another SKIP_TICKS sized chunk of time.
next_game_tick+=SKIP_TICKS;
//Now we update the game logic:
if(engine_interface.need_to_reinit){
engine_interface.need_to_reinit=false;
main_window.reinitialize();
}
engine_interface.rebuild_window_data();
network.receive_packets();
update.ai();
update.input();
network.send_updates();
network.send_input();
update.tick();
update.movement();
update.events();
update.animate();
update.camera(frame_rate,ms_per_frame,logic_frame_rate);
}
//Now that we've handled logic updates, we do our rendering.
if((double)SDL_GetTicks()>next_render_tick){
frame_count++;
if(abs((double)SDL_GetTicks()-next_render_tick)>SKIP_TICKS_RENDER*2.0){
next_render_tick=(double)SDL_GetTicks()-SKIP_TICKS_RENDER*2.0;
}
next_render_tick+=SKIP_TICKS_RENDER;
update.render(frame_rate,ms_per_frame,logic_frame_rate);
}
}
}
int handle_app_events(void* userdata,SDL_Event* event){
switch(event->type){
case SDL_APP_TERMINATING:
message_log.add_error("The OS is terminating this application, shutting down...");
engine_interface.quit();
return 0;
default:
return 1;
}
}
int main(int argc,char* args[]){
#ifdef GAME_OS_OSX
//Set the working directory to the Resources directory of our bundle.
char path[PATH_MAX];
CFURLRef url=CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
CFURLGetFileSystemRepresentation(url,true,(uint8_t*)path,PATH_MAX);
CFRelease(url);
chdir(path);
#endif
if(!main_window.init_sdl()){
return 1;
}
#ifdef GAME_OS_ANDROID
if(!file_io.external_storage_available()){
return 2;
}
#endif
if(!engine_interface.load_data_engine()){
return 3;
}
if(!engine_interface.load_save_location()){
return 4;
}
engine_interface.make_directories();
message_log.clear_error_log();
int things_loaded=0;
int things_to_load=1;
if(!load_world()){
return 5;
}
engine_interface.render_loading_screen((double)++things_loaded/(double)things_to_load,"Initializing");
game.start();
for(int i=0;i<engine_interface.starting_windows.size();i++){
engine_interface.get_window(engine_interface.starting_windows[i])->toggle_on(true,true);
}
SDL_SetEventFilter(handle_app_events,NULL);
SDL_EventState(SDL_DROPFILE,SDL_ENABLE);
game_loop();
return 0;
}