Commit | Line | Data |
---|---|---|
e68f847b PG |
1 | /* |
2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
ad9f1fb6 PG |
18 | #include <GL/gl.h> |
19 | #include <GL/glu.h> | |
20 | #include <SDL/SDL.h> | |
21 | ||
f7b3b2eb | 22 | #include "debug.h" |
7cbd4505 | 23 | #include "handleSignal.h" |
f7b3b2eb | 24 | |
ad9f1fb6 PG |
25 | #include "game.h" |
26 | #include "ticks.h" | |
44b079f8 PG |
27 | |
28 | #include "graphics/graphics.h" | |
f7b3b2eb | 29 | #include "input/inputManager.h" |
b1d92c2f | 30 | #include "config/config.h" |
ad9f1fb6 | 31 | |
ad9f1fb6 | 32 | /// ***** Private Method Headers ***** |
617dcc71 | 33 | |
ad9f1fb6 PG |
34 | void init(); |
35 | ||
7cbd4505 PG |
36 | void sighandler( int sig ); |
37 | ||
ad9f1fb6 | 38 | void run(); |
617dcc71 | 39 | void clean(); |
ad9f1fb6 | 40 | |
ad9f1fb6 PG |
41 | void updateFPSCounters(); |
42 | ||
68b2316d | 43 | void handleInput(); |
ad9f1fb6 PG |
44 | void update(float); |
45 | void draw(); | |
46 | ||
ad9f1fb6 PG |
47 | /// ***** Private Variables ***** |
48 | ||
49 | // variable used to determine if it is time to shutdown | |
50 | bool is_Running; | |
51 | ||
52 | /* Values for the main game loop | |
53 | * target_UPS := the amount of updates that is wanted in one second | |
ce53f20f | 54 | * |
ad9f1fb6 PG |
55 | * ups := updates per second for the last second |
56 | * fps := frames per second for the last second | |
57 | * update_Count := counts this seconds updates | |
58 | * draw_Count := counts this seconds draws | |
59 | * last_Second := stores the time of the last second, used for ups and fps | |
60 | */ | |
d279b77b | 61 | int target_UPS = 100; |
ad9f1fb6 | 62 | |
ce53f20f | 63 | int ups, fps; |
ad9f1fb6 PG |
64 | int update_Count, draw_Count; |
65 | long int last_Second; | |
66 | ||
ce53f20f PG |
67 | int startUpdateThread(void*); |
68 | int startDrawThread(void*); | |
d279b77b | 69 | |
617dcc71 | 70 | /// ***** MAIN Method ***** |
3c3c195d | 71 | int main(int argc, char** args) |
617dcc71 PG |
72 | { |
73 | init(); | |
74 | run(); | |
75 | clean(); | |
76 | return 0; | |
77 | } | |
78 | ||
79 | /// ***** Initializers/Cleaners ***** | |
80 | ||
ad9f1fb6 PG |
81 | void init() |
82 | { | |
7cbd4505 PG |
83 | installSignal(); |
84 | ||
68b2316d | 85 | graphics::init(); |
5f3520c8 | 86 | DPF(0, "Graphics initialized"); |
3c3c195d | 87 | |
68b2316d | 88 | game::init(); |
5f3520c8 | 89 | DPF(0, "Game initialized"); |
3c3c195d | 90 | |
68b2316d | 91 | input::init(); |
5f3520c8 | 92 | DPF(0, "Input initialized"); |
3c3c195d | 93 | |
b1d92c2f | 94 | cfg::init(); |
5f3520c8 | 95 | DPF(0, "Configs initialized"); |
b1d92c2f | 96 | |
5f3520c8 | 97 | DPF(0, "Initialization Complete"); |
ad9f1fb6 PG |
98 | } |
99 | ||
617dcc71 | 100 | void clean() |
44b079f8 | 101 | { |
ca2d526e | 102 | DPF(0, "Cleaning up"); |
44b079f8 | 103 | |
b1d92c2f PG |
104 | cfg::clean(); |
105 | ||
68b2316d | 106 | input::clean(); |
617dcc71 | 107 | |
68b2316d | 108 | game::clean(); |
44b079f8 | 109 | |
68b2316d | 110 | graphics::clean(); |
44b079f8 PG |
111 | } |
112 | ||
617dcc71 PG |
113 | /// ***** Private Methods ***** |
114 | ||
ad9f1fb6 PG |
115 | void run() |
116 | { | |
117 | is_Running = true; | |
d279b77b | 118 | |
ce53f20f | 119 | SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); |
d279b77b | 120 | |
ce53f20f | 121 | startDrawThread(NULL); |
ad9f1fb6 | 122 | |
ce53f20f | 123 | SDL_WaitThread(updates, NULL); |
ad9f1fb6 PG |
124 | } |
125 | ||
126 | void updateFPSCounters() | |
127 | { | |
128 | // Check if a second has passed to recalculate UPS and FPS | |
129 | if (tickCountMicro() - last_Second >= 1000000) | |
130 | { | |
131 | ups = update_Count; | |
132 | fps = draw_Count; | |
ce53f20f PG |
133 | |
134 | // NOT thread safe, but they're estimates anyways | |
135 | update_Count -= ups; | |
136 | draw_Count -= fps; | |
ad9f1fb6 PG |
137 | |
138 | last_Second = tickCountMicro(); | |
44b079f8 | 139 | |
7adc59fe PG |
140 | #ifdef FPSUPS |
141 | cout << "ups:\t" << ups << endl; | |
142 | cout << "fps:\t" << fps << endl; | |
143 | #endif | |
ad9f1fb6 PG |
144 | } |
145 | } | |
146 | ||
68b2316d | 147 | void handleInput() |
ad9f1fb6 | 148 | { |
ce53f20f | 149 | input::update(); |
f7b3b2eb | 150 | |
ce53f20f | 151 | game::handleInput(); |
ad9f1fb6 | 152 | |
ce53f20f PG |
153 | if(cfg::endGame()) |
154 | is_Running = false; | |
ad9f1fb6 PG |
155 | } |
156 | ||
157 | void update(float time_step) | |
158 | { | |
ce53f20f | 159 | game::update(time_step); |
ad9f1fb6 | 160 | update_Count++; |
ad9f1fb6 PG |
161 | } |
162 | ||
163 | void draw() | |
164 | { | |
ce53f20f | 165 | game::draw(); |
d279b77b | 166 | |
ce53f20f PG |
167 | SDL_GL_SwapBuffers(); |
168 | ||
169 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
ad9f1fb6 | 170 | |
ce53f20f PG |
171 | draw_Count++; |
172 | } | |
d279b77b | 173 | |
ce53f20f PG |
174 | int startUpdateThread(void*) |
175 | { | |
176 | while(is_Running) | |
177 | { | |
178 | handleInput(); | |
179 | update(10); | |
d279b77b | 180 | |
ce53f20f | 181 | updateFPSCounters(); |
d279b77b | 182 | |
ce53f20f PG |
183 | SDL_Delay(10); |
184 | } | |
ad9f1fb6 | 185 | |
ce53f20f PG |
186 | return 0; |
187 | } | |
ad9f1fb6 | 188 | |
ce53f20f PG |
189 | int startDrawThread(void*) |
190 | { | |
191 | while(is_Running) | |
192 | { | |
193 | draw(); | |
194 | } | |
25d84412 | 195 | |
ce53f20f | 196 | return 0; |
ad9f1fb6 | 197 | } |