SDLGameEngine

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# src/sgegamestate.c

    00001 /*
    00002  * Copyright (c) 2007 Heiko Irrgang
    00003  *
    00004  * The license and distribution terms for this file may be
    00005  * found in the file COPYING in this distribution or at
    00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
    00007  */
    00008
    00009 #include <sge.h>
    00010 #include <assert.h>
    00011
    00012 SGEGAMESTATEMANAGER* sgeGameStateManagerNew(void)
    00013 {
    00014         SGEGAMESTATEMANAGER *ret;
    00015         sgeNew(ret, SGEGAMESTATEMANAGER);
    00016         return ret;
    00017 }
    00018
    00019 SGEGAMESTATE* sgeGameStateNew(void)
    00020 {
    00021         SGEGAMESTATE *ret;
    00022         sgeNew(ret, SGEGAMESTATE);
    00023         return ret;
    00024 }
    00025
    00026 void sgeGameStateManagerQuit(SGEGAMESTATEMANAGER *manager)
    00027 {
    00028         manager->quit = 1;
    00029 }
    00030
    00031 int sgeGameStateManagerChange(SGEGAMESTATEMANAGER *manager, SGEGAMESTATE *next_state)
    00032 {
    00033         if (manager->current && manager->current->onStateChange) {
    00034                 if (!manager->current->onStateChange(manager->current, next_state)) {
    00035                         return 0;
    00036                 }
    00037         }
    00038         manager->current = next_state;
    00039         next_state->manager = manager;
    00040         return 1;
    00041 }
    00042
    00043 void sgeGameStateManagerRun(SGEGAMESTATEMANAGER *manager, int fps)
    00044 {
    00045         SDL_Event event;
    00046         SGETIMER t;
    00047         sgeClearEvents();
    00048         t=sgeStartRedrawTimer(fps);
    00049
    00050         assert(manager->current);
    00051         manager->quit = 0;
    00052
    00053         while (SDL_WaitEvent(&event) && !manager->quit) {
    00054                 switch (event.type) {
    00055                         case SDL_USEREVENT:
    00056                                 if (event.user.code==SGEREDRAW && manager->current->onRedraw) {
    00057                                         manager->current->onRedraw(manager->current);
    00058                                 }
    00059                                 sgeEventResetInputs(&manager->event_state);
    00060                                 break;
    00061                         case SDL_KEYDOWN:
    00062                                 if (manager->current->onKeyDown!=NULL) {
    00063                                         if (manager->current->onKeyDown(manager->current, &event)) {
    00064                                                 break;
    00065                                         }
    00066                                 }
    00067                                 sgeEventApply(&manager->event_state, &event);
    00068                                 break;
    00069                         case SDL_KEYUP:
    00070                                 if (manager->current->onKeyUp!=NULL) {
    00071                                         if (manager->current->onKeyUp(manager->current, &event)) {
    00072                                                 break;
    00073                                         }
    00074                                 }
    00075                                 sgeEventApply(&manager->event_state, &event);
    00076                                 break;
    00077                         case SDL_JOYBUTTONUP:
    00078                                 if (manager->current->onJoystickButtonUp!=NULL) {
    00079                                         if (manager->current->onJoystickButtonUp(manager->current, &event)) {
    00080                                                 break;
    00081                                         }
    00082                                 }
    00083                                 sgeEventApply(&manager->event_state, &event);
    00084                                 break;
    00085                         case SDL_JOYBUTTONDOWN:
    00086                                 if (manager->current->onJoystickButtonDown!=NULL) {
    00087                                         if (manager->current->onJoystickButtonDown(manager->current, &event)) {
    00088                                                 break;
    00089                                         }
    00090                                 }
    00091                                 sgeEventApply(&manager->event_state, &event);
    00092                                 break;
    00093                         case SDL_MOUSEBUTTONDOWN:
    00094                                 if (manager->current->onMouseDown!=NULL) {
    00095                                         manager->current->onMouseDown(manager->current, &event);
    00096                                 }
    00097                                 break;
    00098                         case SDL_MOUSEBUTTONUP:
    00099                                 if (manager->current->onMouseUp!=NULL) {
    00100                                         manager->current->onMouseUp(manager->current, &event);
    00101                                 }
    00102                                 break;
    00103                         case SDL_MOUSEMOTION:
    00104                                 if (manager->current->onMouseMove!=NULL) {
    00105                                         manager->current->onMouseMove(manager->current, &event);
    00106                                 }
    00107                                 break;
    00108                         case SDL_JOYAXISMOTION:
    00109                                 if (manager->current->onJoystickMove!=NULL) {
    00110                                         manager->current->onJoystickMove(manager->current, &event);
    00111                                 }
    00112                                 break;
    00113                         case SDL_QUIT:
    00114                                 manager->quit = 1;
    00115                                 break;
    00116                 }
    00117         }
    00118
    00119         sgeStopRedrawTimer(t);
    00120 }

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines