| 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 | |
| 18 | #include <GL/gl.h> |
| 19 | #include <GL/glu.h> |
| 20 | #include <SDL/SDL.h> |
| 21 | |
| 22 | #include "debug.h" |
| 23 | #include "handleSignal.h" |
| 24 | |
| 25 | #include "game.h" |
| 26 | #include "ticks.h" |
| 27 | |
| 28 | #include "graphics/graphics.h" |
| 29 | #include "input/inputManager.h" |
| 30 | #include "config/config.h" |
| 31 | |
| 32 | /// ***** Private Method Headers ***** |
| 33 | |
| 34 | void init(); |
| 35 | |
| 36 | void sighandler( int sig ); |
| 37 | |
| 38 | void run(); |
| 39 | void clean(); |
| 40 | |
| 41 | void updateFPSCounters(); |
| 42 | |
| 43 | void handleInput(); |
| 44 | void update(float); |
| 45 | void draw(); |
| 46 | |
| 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 |
| 54 | * |
| 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 | */ |
| 61 | int target_UPS = 100; |
| 62 | |
| 63 | int ups, fps; |
| 64 | int update_Count, draw_Count; |
| 65 | long int last_Second; |
| 66 | |
| 67 | int startUpdateThread(void*); |
| 68 | int startDrawThread(void*); |
| 69 | |
| 70 | /// ***** MAIN Method ***** |
| 71 | int main(int argc, char** args) |
| 72 | { |
| 73 | init(); |
| 74 | run(); |
| 75 | clean(); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /// ***** Initializers/Cleaners ***** |
| 80 | |
| 81 | void init() |
| 82 | { |
| 83 | installSignal(); |
| 84 | |
| 85 | graphics::init(); |
| 86 | DPF(0, "Graphics initialized"); |
| 87 | |
| 88 | game::init(); |
| 89 | DPF(0, "Game initialized"); |
| 90 | |
| 91 | input::init(); |
| 92 | DPF(0, "Input initialized"); |
| 93 | |
| 94 | cfg::init(); |
| 95 | DPF(0, "Configs initialized"); |
| 96 | |
| 97 | DPF(0, "Initialization Complete"); |
| 98 | } |
| 99 | |
| 100 | void clean() |
| 101 | { |
| 102 | DPF(0, "Cleaning up"); |
| 103 | |
| 104 | cfg::clean(); |
| 105 | |
| 106 | input::clean(); |
| 107 | |
| 108 | game::clean(); |
| 109 | |
| 110 | graphics::clean(); |
| 111 | } |
| 112 | |
| 113 | /// ***** Private Methods ***** |
| 114 | |
| 115 | void run() |
| 116 | { |
| 117 | is_Running = true; |
| 118 | |
| 119 | SDL_Thread* updates = SDL_CreateThread(startUpdateThread, NULL); |
| 120 | |
| 121 | startDrawThread(NULL); |
| 122 | |
| 123 | SDL_WaitThread(updates, NULL); |
| 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; |
| 133 | |
| 134 | // NOT thread safe, but they're estimates anyways |
| 135 | update_Count -= ups; |
| 136 | draw_Count -= fps; |
| 137 | |
| 138 | last_Second = tickCountMicro(); |
| 139 | |
| 140 | #ifdef FPSUPS |
| 141 | cout << "ups:\t" << ups << endl; |
| 142 | cout << "fps:\t" << fps << endl; |
| 143 | #endif |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void handleInput() |
| 148 | { |
| 149 | input::update(); |
| 150 | |
| 151 | game::handleInput(); |
| 152 | |
| 153 | if(cfg::endGame()) |
| 154 | is_Running = false; |
| 155 | } |
| 156 | |
| 157 | void update(float time_step) |
| 158 | { |
| 159 | game::update(time_step); |
| 160 | update_Count++; |
| 161 | } |
| 162 | |
| 163 | void draw() |
| 164 | { |
| 165 | game::draw(); |
| 166 | |
| 167 | SDL_GL_SwapBuffers(); |
| 168 | |
| 169 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 170 | |
| 171 | draw_Count++; |
| 172 | } |
| 173 | |
| 174 | int startUpdateThread(void*) |
| 175 | { |
| 176 | while(is_Running) |
| 177 | { |
| 178 | handleInput(); |
| 179 | update(10); |
| 180 | |
| 181 | updateFPSCounters(); |
| 182 | |
| 183 | SDL_Delay(10); |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int startDrawThread(void*) |
| 190 | { |
| 191 | while(is_Running) |
| 192 | { |
| 193 | draw(); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |