| 1 | // Copyright 2005 by Anthony Liekens [email protected] |
| 2 | |
| 3 | #ifdef HAVE_CONFIG_H |
| 4 | # include "config.h" |
| 5 | #else |
| 6 | #define VERSION no-version |
| 7 | #endif |
| 8 | |
| 9 | #include <sstream> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "sdl_driver.h" |
| 13 | #include "settings.h" |
| 14 | |
| 15 | #include "canvas.h" |
| 16 | |
| 17 | #include "game.h" |
| 18 | #include "main.h" |
| 19 | |
| 20 | #include "menumanager.h" |
| 21 | #include "videooptions.h" |
| 22 | #include "gameoptions.h" |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | Main::Main() |
| 27 | : game(0), running(true), lastTicks(0), fpsCounter(0), fps(0) |
| 28 | { |
| 29 | driver = new SDLDriver(*this); |
| 30 | menuManager = new MenuManager(*this); |
| 31 | } |
| 32 | |
| 33 | Main::~Main() |
| 34 | { |
| 35 | delete menuManager; |
| 36 | delete driver; |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | Main::startSinglePlayerGame(int planets, int players) |
| 41 | { |
| 42 | GameOptions &gameOptions = menuManager->getGameOptions(); |
| 43 | |
| 44 | if( planets <= 1 ) |
| 45 | { |
| 46 | cerr << "Resetting to least of 2 planets" << endl; |
| 47 | planets = 2; |
| 48 | } |
| 49 | |
| 50 | if( players > planets - 1 ) |
| 51 | { |
| 52 | cerr << "Resetting computer players to maximum of " << planets - 1 << endl; |
| 53 | players = planets - 1; |
| 54 | } |
| 55 | |
| 56 | if( players < 1 ) |
| 57 | { |
| 58 | cerr << "Resetting computer players to at least 1" << endl; |
| 59 | players = 1; |
| 60 | } |
| 61 | |
| 62 | gameOptions.setPlanets(planets); |
| 63 | gameOptions.setPlayers(players); |
| 64 | |
| 65 | startSinglePlayerGame(gameOptions); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | Main::startSinglePlayerGame(GameOptions &gameOptions) |
| 70 | { |
| 71 | if (game) |
| 72 | { |
| 73 | delete game; |
| 74 | game = 0; |
| 75 | } |
| 76 | |
| 77 | menuManager->hide(); |
| 78 | |
| 79 | game = new Game(gameOptions); |
| 80 | game->setPaused(false); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | Main::quit() |
| 85 | { |
| 86 | running = false; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Main::showMenu() |
| 91 | { |
| 92 | if (menuManager->isVisible()) |
| 93 | return; |
| 94 | |
| 95 | menuManager->show(); |
| 96 | |
| 97 | // Just in case there is a game, pause it. |
| 98 | if (game) |
| 99 | game->setPaused(true); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | Main::applyVideoOptions(VideoOptions &videoOptions) |
| 105 | { |
| 106 | // Causes the game to pause which is nice when the CRT/TFT is resyncing. |
| 107 | showMenu(); |
| 108 | |
| 109 | Settings::setFullscreen(videoOptions.getFullscreen()); |
| 110 | Settings::setScreenSize(videoOptions.getScreenWidth(), videoOptions.getScreenHeight()); |
| 111 | |
| 112 | Canvas::initScreen(); |
| 113 | |
| 114 | menuManager->resize(); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Main::handle(GameAction gameAction, int value) |
| 119 | { |
| 120 | switch (gameAction) |
| 121 | { |
| 122 | case GA_SENSE_CANCEL: |
| 123 | if (!value) |
| 124 | menuManager->senseFinished(false); |
| 125 | break; |
| 126 | case GA_SENSE_COMPLETE: |
| 127 | if (!value) |
| 128 | menuManager->senseFinished(true); |
| 129 | break; |
| 130 | case GA_TOGGLE_FULLSCREEN: |
| 131 | if (value) return; |
| 132 | { |
| 133 | VideoOptions &vo = menuManager->getVideoOptions(); |
| 134 | vo.toggleFullscreen(); |
| 135 | applyVideoOptions(vo); |
| 136 | } |
| 137 | break; |
| 138 | case GA_LEAVE: |
| 139 | // It is important to act on keyrelease since this is the same |
| 140 | // for what guichan reacts. By doing so it is predictable that |
| 141 | // after handling GA_LEAVE (which may make the menusystem visible) |
| 142 | // guichan's logic is called. |
| 143 | if (value) |
| 144 | return; |
| 145 | |
| 146 | // Main does not care about making the menu system invisible. This is done |
| 147 | // from inside the menu system. |
| 148 | if (!menuManager->isVisible()) |
| 149 | menuManager->show(); |
| 150 | |
| 151 | break; |
| 152 | case GA_SCREENSHOT: |
| 153 | if (value) |
| 154 | return; |
| 155 | |
| 156 | SDL_SaveBMP( SDL_GetVideoSurface(), "screenshot.bmp" ); |
| 157 | break; |
| 158 | default: |
| 159 | // Let the game instance handle the rest |
| 160 | if (game) |
| 161 | game->handle(gameAction, value); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | Main::renderFPS() |
| 168 | { |
| 169 | stringstream s; |
| 170 | |
| 171 | fpsCounter++; |
| 172 | |
| 173 | s << fps; |
| 174 | Canvas::drawText( Settings::getScreenWidth() - 32, 5, s.str().c_str(), 0x80, 0x80, 0x80 ); |
| 175 | |
| 176 | if( SDL_GetTicks() - lastTicks >= 1000 ) { |
| 177 | lastTicks = SDL_GetTicks(); |
| 178 | fps = fpsCounter; |
| 179 | fpsCounter = 0; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | Main::run() |
| 186 | { |
| 187 | bool b; |
| 188 | |
| 189 | // Main loop: loop forever. |
| 190 | while (running) { |
| 191 | |
| 192 | Canvas::drawBox(0, 0, Settings::getScreenWidth(), Settings::getScreenHeight(), 0, 0, 0); |
| 193 | |
| 194 | if (game) |
| 195 | game->render(); |
| 196 | menuManager->render(); |
| 197 | renderFPS(); |
| 198 | Canvas::updateScreen(); |
| 199 | |
| 200 | b = menuManager->update(); |
| 201 | |
| 202 | if (game) |
| 203 | { |
| 204 | if (!game->run(b)) |
| 205 | { |
| 206 | delete game; |
| 207 | game = 0; |
| 208 | menuManager->showSinglePlayerMenu(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Poll for events, and handle the ones we care about. |
| 213 | driver->loop(); |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | int main(int argc, char** argv) { |
| 219 | int numberOfPlanets = -1; |
| 220 | int numberOfComputerPlayers = -1; |
| 221 | |
| 222 | cout << "This is " << PACKAGE_STRING << endl; |
| 223 | cout << "(c) 2003 Anthony Liekens" << endl; |
| 224 | cout << "(c) 2007 Qonk development team" << endl; |
| 225 | cout << "This game is free software released under the GNU General Public License, " << endl; |
| 226 | cout << "either version 2 of the license or, at your option, any later version. " << endl; |
| 227 | |
| 228 | Settings::init(); |
| 229 | |
| 230 | switch (argc) { |
| 231 | case 3: |
| 232 | numberOfPlanets = atoi( argv[ 1 ] ); |
| 233 | numberOfComputerPlayers = atoi( argv[ 2 ] ); |
| 234 | break; |
| 235 | case 1: |
| 236 | // Do nothing. |
| 237 | break; |
| 238 | default: |
| 239 | cerr << "Usage: " << argv[ 0 ] << " numberOfPlanets numberOfComputerPlayers" << endl; |
| 240 | exit(1); |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | Main *main = new Main(); |
| 245 | |
| 246 | if (numberOfPlanets == -1 |
| 247 | && numberOfComputerPlayers == -1) |
| 248 | main->showMenu(); |
| 249 | else |
| 250 | main->startSinglePlayerGame(numberOfPlanets, numberOfComputerPlayers); |
| 251 | |
| 252 | main->run(); |
| 253 | |
| 254 | delete main; |
| 255 | |
| 256 | Settings::store(); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | Main::startSensing() |
| 263 | { |
| 264 | driver->setSenseMode(true); |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | Main::finishSensing(GameAction ga, bool completed) |
| 269 | { |
| 270 | if (completed) |
| 271 | { |
| 272 | Settings::set(ga, driver->getSensedInput()); |
| 273 | driver->initActionMap(); |
| 274 | } |
| 275 | |
| 276 | driver->setSenseMode(false); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | Main::resetGameAction(GameAction ga) |
| 281 | { |
| 282 | Settings::unset(ga); |
| 283 | driver->setSenseMode(true); |
| 284 | driver->setSenseMode(false); |
| 285 | } |
| 286 |