SDLGameEngine

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

- File List
- Globals

# src/sgeevent.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
    00011 enum sgeKey {
    00012         sgeUpKey=SDLK_UP,
    00013         sgeDownKey=SDLK_DOWN,
    00014         sgeLeftKey=SDLK_LEFT,
    00015         sgeRightKey=SDLK_RIGHT,
    00016         sgeStartKey=SDLK_RETURN,
    00017         sgeSelectKey=SDLK_BACKSPACE,
    00018         sgeFireKey=SDLK_SPACE,
    00019         sgeL1Key=SDLK_COMMA,
    00020         sgeR1Key=SDLK_PERIOD,
    00021         sgeAKey=SDLK_v,
    00022         sgeBKey=SDLK_b,
    00023         sgeXKey=SDLK_n,
    00024         sgeYKey=SDLK_m,
    00025         sgeVolUpKey=SDLK_PLUS,
    00026         sgeVolDownKey=SDLK_MINUS
    00027 };
    00028
    00029 enum sgeJoy {
    00030         sgeUpJoy=VK_UP,
    00031         sgeDownJoy=VK_DOWN,
    00032         sgeLeftJoy=VK_LEFT,
    00033         sgeRightJoy=VK_RIGHT,
    00034         sgeStartJoy=VK_START,
    00035         sgeSelectJoy=VK_SELECT,
    00036         sgeFireJoy=VK_TAT,
    00037         sgeL1Joy=VK_FL,
    00038         sgeR1Joy=VK_FR,
    00039         sgeAJoy=VK_FA,
    00040         sgeBJoy=VK_FB,
    00041         sgeXJoy=VK_FX,
    00042         sgeYJoy=VK_FY,
    00043         sgeVolUpJoy=VK_VOL_UP,
    00044         sgeVolDownJoy=VK_VOL_DOWN
    00045 };
    00046
    00047 static int sgeKeyCheck(SDL_Event *e, enum sgeKey key, enum sgeJoy joy, int type) {
    00048         if (
    00049                         (e->type==SDL_KEYUP) ||
    00050                         (e->type==SDL_JOYBUTTONUP) ||
    00051                         (e->type==SDL_KEYDOWN) ||
    00052                         (e->type==SDL_JOYBUTTONDOWN)
    00053         ) {
    00054                 if (
    00055                                 ((type==PRESSED) && (e->type==SDL_KEYDOWN) && (e->key.keysym.sym==(SDLKey)key)) ||
    00056                                 ((type==PRESSED) && (e->type==SDL_JOYBUTTONDOWN) && (e->jbutton.button==joy))
    00057                 ) {
    00058                         return 1;
    00059                 }
    00060                 if (
    00061                                 ((type==RELEASED) && (e->type==SDL_KEYUP) && (e->key.keysym.sym==(SDLKey)key)) ||
    00062                                 ((type==RELEASED) && (e->type==SDL_JOYBUTTONUP) && (e->jbutton.button==joy))
    00063                 ) {
    00064                         return 1;
    00065                 }
    00066         }
    00067         return 0;
    00068 }
    00069
    00070 int sgeKeyUp(SDL_Event *e, int type) {
    00071         return sgeKeyCheck(e, sgeUpKey, sgeUpJoy, type);
    00072 }
    00073
    00074 int sgeKeyDown(SDL_Event *e, int type) {
    00075         return sgeKeyCheck(e, sgeDownKey, sgeDownJoy, type);
    00076 }
    00077
    00078 int sgeKeyLeft(SDL_Event *e, int type) {
    00079         return sgeKeyCheck(e, sgeLeftKey, sgeLeftJoy, type);
    00080 }
    00081
    00082 int sgeKeyRight(SDL_Event *e, int type) {
    00083         return sgeKeyCheck(e, sgeRightKey, sgeRightJoy, type);
    00084 }
    00085
    00086 int sgeKeyStart(SDL_Event *e, int type) {
    00087         return sgeKeyCheck(e, sgeStartKey, sgeStartJoy, type);
    00088 }
    00089
    00090 int sgeKeySelect(SDL_Event *e, int type) {
    00091         return sgeKeyCheck(e, sgeSelectKey, sgeSelectJoy, type);
    00092 }
    00093
    00094 int sgeKeyFire(SDL_Event *e, int type) {
    00095         return sgeKeyCheck(e, sgeFireKey, sgeFireJoy, type);
    00096 }
    00097
    00098 int sgeKeyL1(SDL_Event *e, int type) {
    00099         return sgeKeyCheck(e, sgeL1Key, sgeL1Joy, type);
    00100 }
    00101
    00102 int sgeKeyR1(SDL_Event *e, int type) {
    00103         return sgeKeyCheck(e, sgeR1Key, sgeR1Joy, type);
    00104 }
    00105
    00106 int sgeKeyA(SDL_Event *e, int type) {
    00107         return sgeKeyCheck(e, sgeAKey, sgeAJoy, type);
    00108 }
    00109
    00110 int sgeKeyB(SDL_Event *e, int type) {
    00111         return sgeKeyCheck(e, sgeBKey, sgeBJoy, type);
    00112 }
    00113
    00114 int sgeKeyX(SDL_Event *e, int type) {
    00115         return sgeKeyCheck(e, sgeXKey, sgeXJoy, type);
    00116 }
    00117
    00118 int sgeKeyY(SDL_Event *e, int type) {
    00119         return sgeKeyCheck(e, sgeYKey, sgeYJoy, type);
    00120 }
    00121
    00122 int sgeKeyVolUp(SDL_Event *e, int type) {
    00123         return sgeKeyCheck(e, sgeVolUpKey, sgeVolUpJoy, type);
    00124 }
    00125
    00126 int sgeKeyVolDown(SDL_Event *e, int type) {
    00127         return sgeKeyCheck(e, sgeVolDownKey, sgeVolDownJoy, type);
    00128 }
    00129
    00130 void sgeClearEvents() {
    00131         SDL_Event dummy;
    00132         while (SDL_PollEvent(&dummy)) {}
    00133 }
    00134
    00135 static void state_check(SGEEVENTSTATEINPUT *input, SDL_Event *event, enum sgeKey key, enum sgeJoy joy)
    00136 {
    00137         if ((event->type==SDL_KEYDOWN && event->key.keysym.sym==(SDLKey)key) || (event->type==SDL_JOYBUTTONDOWN && event->jbutton.button==joy)) {
    00138                 input->held = 1;
    00139                 input->pressed =1;
    00140         }
    00141         if ((event->type==SDL_KEYUP && event->key.keysym.sym==(SDLKey)key) || (event->type==SDL_JOYBUTTONUP && event->jbutton.button==joy)) {
    00142                 input->held = 0;
    00143                 input->released = 1;
    00144         }
    00145 }
    00146
    00147 void sgeEventApply(SGEEVENTSTATE *state, SDL_Event *event)
    00148 {
    00149         switch (event->type) {
    00150                 case SDL_KEYDOWN:
    00151                 case SDL_KEYUP:
    00152                 case SDL_JOYBUTTONUP:
    00153                 case SDL_JOYBUTTONDOWN:
    00154                         state_check(&state->up, event, sgeUpKey, sgeUpJoy);
    00155                         state_check(&state->down, event, sgeDownKey, sgeDownJoy);
    00156                         state_check(&state->left, event, sgeLeftKey, sgeLeftJoy);
    00157                         state_check(&state->right, event, sgeRightKey, sgeRightJoy);
    00158                         state_check(&state->start, event, sgeStartKey, sgeStartJoy);
    00159                         state_check(&state->select, event, sgeSelectKey, sgeSelectJoy);
    00160                         state_check(&state->fire, event, sgeFireKey, sgeFireJoy);
    00161                         state_check(&state->l1, event, sgeL1Key, sgeL1Joy);
    00162                         state_check(&state->r1, event, sgeR1Key, sgeR1Joy);
    00163                         state_check(&state->a, event, sgeAKey, sgeAJoy);
    00164                         state_check(&state->b, event, sgeBKey, sgeBJoy);
    00165                         state_check(&state->x, event, sgeXKey, sgeXJoy);
    00166                         state_check(&state->y, event, sgeYKey, sgeYJoy);
    00167                         state_check(&state->volUp, event, sgeVolUpKey, sgeVolUpJoy);
    00168                         state_check(&state->volDown, event, sgeVolDownKey, sgeVolDownJoy);
    00169         }
    00170 }
    00171
    00172 void sgeEventResetInputs(SGEEVENTSTATE *state)
    00173 {
    00174         state->up.pressed = state->up.released = 0;
    00175         state->down.pressed = state->down.released = 0;
    00176         state->left.pressed = state->left.released = 0;
    00177         state->right.pressed = state->right.released = 0;
    00178         state->start.pressed = state->start.released = 0;
    00179         state->select.pressed = state->select.released = 0;
    00180         state->fire.pressed = state->fire.released = 0;
    00181         state->l1.pressed = state->l1.released = 0;
    00182         state->r1.pressed = state->r1.released = 0;
    00183         state->a.pressed = state->a.released = 0;
    00184         state->b.pressed = state->b.released = 0;
    00185         state->x.pressed = state->x.released = 0;
    00186         state->y.pressed = state->y.released = 0;
    00187         state->volUp.pressed = state->volUp.released = 0;
    00188         state->volDown.pressed = state->volDown.released = 0;
    00189 }
    00190

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines