* The state machine takes care of passing all the events from SDL to the * active state, running any code within and blitting all the states and * cursor to the screen. This is run indefinitely until the game quits. */
| 140 | * cursor to the screen. This is run indefinitely until the game quits. |
| 141 | */ |
| 142 | void Game::run() |
| 143 | { |
| 144 | enum ApplicationState { RUNNING = 0, SLOWED = 1, PAUSED = 2 } runningState = RUNNING; |
| 145 | static const ApplicationState kbFocusRun[4] = { RUNNING, RUNNING, SLOWED, PAUSED }; |
| 146 | static const ApplicationState stateRun[4] = { SLOWED, PAUSED, PAUSED, PAUSED }; |
| 147 | // this will avoid processing SDL's resize event on startup, workaround for the heap allocation error it causes. |
| 148 | bool startupEvent = Options::allowResize; |
| 149 | while (!_quit) |
| 150 | { |
| 151 | // Clean up states |
| 152 | while (!_deleted.empty()) |
| 153 | { |
| 154 | delete _deleted.back(); |
| 155 | _deleted.pop_back(); |
| 156 | } |
| 157 | |
| 158 | // Initialize active state |
| 159 | if (!_init) |
| 160 | { |
| 161 | _init = true; |
| 162 | _states.back()->init(); |
| 163 | |
| 164 | // Unpress buttons |
| 165 | _states.back()->resetAll(); |
| 166 | |
| 167 | // Refresh mouse position |
| 168 | SDL_Event ev; |
| 169 | int x, y; |
| 170 | SDL_GetMouseState(&x, &y); |
| 171 | ev.type = SDL_MOUSEMOTION; |
| 172 | ev.motion.x = x; |
| 173 | ev.motion.y = y; |
| 174 | Action action = Action(&ev, _screen->getXScale(), _screen->getYScale(), _screen->getCursorTopBlackBand(), _screen->getCursorLeftBlackBand()); |
| 175 | _states.back()->handle(&action); |
| 176 | } |
| 177 | |
| 178 | // Process events |
| 179 | while (SDL_PollEvent(&_event)) |
| 180 | { |
| 181 | if (CrossPlatform::isQuitShortcut(_event)) |
| 182 | _event.type = SDL_QUIT; |
| 183 | switch (_event.type) |
| 184 | { |
| 185 | case SDL_QUIT: |
| 186 | quit(); |
| 187 | break; |
| 188 | case SDL_ACTIVEEVENT: |
| 189 | switch (reinterpret_cast<SDL_ActiveEvent*>(&_event)->state) |
| 190 | { |
| 191 | case SDL_APPACTIVE: |
| 192 | runningState = reinterpret_cast<SDL_ActiveEvent*>(&_event)->gain ? RUNNING : stateRun[Options::pauseMode]; |
| 193 | break; |
| 194 | case SDL_APPMOUSEFOCUS: |
| 195 | // We consciously ignore it. |
| 196 | break; |
| 197 | case SDL_APPINPUTFOCUS: |
| 198 | runningState = reinterpret_cast<SDL_ActiveEvent*>(&_event)->gain ? RUNNING : kbFocusRun[Options::pauseMode]; |
| 199 | break; |
no test coverage detected