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

- File List
- Globals

# eventhandler.c

Go to the documentation of this file.

    00001
    00005 //    /**
    00006 //      *@defgroup eventhandler eventhandler.c
    00007 //      *Handles all keyboard, mouse and button events
    00008 //      *@{
    00009 //      */
    00010
    00011 /*----------------------------------------------------------------------------------------------------------*/
    00012
    00013 #ifndef _ALLEGRO5_
    00014 #define _ALLEGRO5_
    00015 #include <allegro5/allegro5.h>
    00016 #endif // _ALLEGRO5_
    00017
    00018 #ifndef _ALLEGRO5FONT_
    00019 #define _ALLEGRO5FONT_
    00020 #include <allegro5/allegro_font.h>
    00021 #endif // _ALLEGRO5FONT_
    00022
    00023 #ifndef _CHIPMUNK_
    00024 #define _CHIPMUNK_
    00025 #include "chipmunk.h"
    00026 #endif // _CHIPMUNK_
    00027
    00028 #ifndef _STDIO_
    00029 #define _STDIO_
    00030 #include <stdio.h>
    00031 #endif // _STDIO_
    00032
    00033 #ifndef _COMMONS_
    00034 #define _COMMONS_
    00035 #include "../include/commons.h"
    00036 #endif // _COMMONS_
    00037
    00038 #ifndef _KEYBOARD_
    00039 #define _KEYBOARD_
    00040 #include "../include/keyboard.h"
    00041 #endif // _KEYBOARD_
    00042
    00043 #ifndef _MOUSE_
    00044 #define _MOUSE_
    00045 #include "../include/mouse.h"
    00046 #endif // _MOUSE_
    00047
    00048 #ifndef _BUTTONS_
    00049 #define _BUTTONS_
    00050 #include "../include/buttons.h"
    00051 #endif // _BUTTONS_
    00052
    00053 #ifndef _SPRITES_
    00054 #define _SPRITES_
    00055 #include "../include/sprites.h"
    00056 #endif // _SPRITES_
    00057
    00058 #ifndef _MAINGAME_
    00059 #define _MAINGAME_
    00060 #include "../include/maingame.h"
    00061 #endif // _MAINGAME_
    00062
    00063 #ifndef _OPTIONS_
    00064 #define _OPTIONS_
    00065 #include "../include/options.h"
    00066 #endif // _OPTIONS_
    00067
    00068 #ifndef _HOWTOPLAY_
    00069 #define _HOWTOPLAY_
    00070 #include "../include/howtoplay.h"
    00071 #endif // _HOWTOPLAY_
    00072
    00073 #ifndef _HIGHSCORES_
    00074 #define _HIGHSCORES_
    00075 #include "../include/highscores.h"
    00076 #endif // _HIGHSCORES_
    00077
    00078 #ifndef _PAUSE_
    00079 #define _PAUSE_
    00080 #include "../include/pause.h"
    00081 #endif // _PAUSE_
    00082
    00083 #ifndef _GAMEOVER_
    00084 #define _GAMEOVER_
    00085 #include "../include/gameover.h"
    00086 #endif // _GAMEOVER_
    00087
    00088 #ifndef _RENDER_
    00089 #define _RENDER_
    00090 #include "../include/render.h"
    00091 #endif // _RENDER_
    00092
    00093 #ifndef _INITS_
    00094 #define _INITS_
    00095 #include "../include/inits.h"
    00096 #endif // _INITS_
    00097
    00098 #ifndef _EVENTHANDLER_
    00099 #define _EVENTHANDLER_
    00100 #include "../include/eventhandler.h"
    00101 #endif // _EVENTHANDLER_
    00102
    00103 /*----------------------------------------------------------------------------------------------------------*/
    00104
    00105 static _Bool mpressed = false;
    00106
    00107 /*----------------------------------------------------------------------------------------------------------*/
    00108
    00109 // Checks for keyboard input, called once every frame
    00110 void* keyhandler(String key, void *data)
    00111 {
    00112     GDATAPTR game = (GDATAPTR)data;
    00113     if((key[ALLEGRO_KEY_W] & KEYNEW) || (key[ALLEGRO_KEY_UP] & KEYNEW))
    00114         {
    00115             set_anim_flags_on_sprite(search_sprite_list_for_element("STICKMAN"), MUP);
    00116         }
    00117         if((key[ALLEGRO_KEY_S] & KEYNEW) || (key[ALLEGRO_KEY_DOWN] & KEYNEW))
    00118         {
    00119             set_anim_flags_on_sprite(search_sprite_list_for_element("STICKMAN"), MDOWN);
    00120         }
    00121         if((key[ALLEGRO_KEY_SPACE] & KEYNEW))
    00122         {
    00123         set_anim_flags_on_sprite(search_sprite_list_for_element("STICKMAN"), MATTACK);
    00124         }
    00125         if((key[ALLEGRO_KEY_ESCAPE] & KEYPRESSED))
    00126         {
    00127             game->exit = true;
    00128             game->gamestarted = false;
    00129         }
    00130         if((key[ALLEGRO_KEY_M] & KEYNEW))
    00131         {
    00132         return_to_main_menu(game);
    00133         }
    00134         if((key[ALLEGRO_KEY_P] & KEYNEW))
    00135         {
    00136             if(game->paused)
    00137             {
    00138             exit_pause(game);
    00139             mpressed = true;
    00140             }
    00141             else
    00142             {
    00143             game->paused = true;
    00144             }
    00145         }
    00146         return NULL;
    00147 }
    00148
    00149
    00150 /*----------------------------------------------------------------------------------------------------------*/
    00151
    00152 // Checks for mouse input, called once every frame
    00153 void* mousehandler(MOUSE* mouse_s, void *data)
    00154 {
    00155     MOUSE *mouse = (MOUSE*)mouse_s;
    00156     GDATAPTR game = (GDATAPTR)data;
    00157
    00158     if((mouse->button & MOUSE_L_NEW) && game->gamestarted)
    00159     {
    00160         SPRITESPTR sprite = search_sprite_list_for_element("BRICK_WALL");
    00161         SPRITESPTR fsprite = sprite;
    00162         if(sprite != NULL)
    00163         {
    00164             do
    00165             {
    00166                 if(strcmp(sprite->slabel, "BRICK_WALL") == 0)
    00167                     set_anim_flags_on_sprite(sprite, MDOWN);
    00168                 sprite = sprite->nextsprite;
    00169             } while(sprite != fsprite);
    00170         }
    00171     }
    00172 //    if((mouse->button & MOUSE_R))
    00173 //    {
    00174 //        set_anim_flags_on_sprite(search_sprite_list_for_element("STICKMAN"), MRIGHT);
    00175 //    }
    00176 //    if(mouse->dz > 0)
    00177 //    {
    00178 //        set_anim_flags_on_sprite(search_sprite_list_for_element("STICKMAN"), MRIGHT);
    00179 //    }
    00180
    00181     return NULL;
    00182 }
    00183
    00184
    00185 /*----------------------------------------------------------------------------------------------------------*/
    00186
    00187 // Handles button events, may be called once every frame
    00188 void* buttonhandler(BUTTONPTR button, void* data, int type)
    00189 {
    00190     typedef enum button_pressed {
    00191         NONE,
    00192         STARTB,
    00193         HOWTOPLAYB,
    00194         OPTIONSB,
    00195         HIGHSCOREB,
    00196         EXITB
    00197     } BPRESSED;
    00198
    00199     GDATAPTR game = (GDATAPTR)data;
    00200     static BPRESSED bpressed = NONE;
    00201
    00202     if(strcmp(button->blabel, "STARTBUTTON") == 0 && type == BPRESSED_CALL)
    00203     {
    00204         game_start_init();
    00205         execute_on_button_press();
    00206         bpressed = STARTB;
    00207     }
    00208     if(strcmp(button->blabel, "HOWTOPLAYBUTTON") == 0 && type == BPRESSED_CALL)
    00209     {
    00210         execute_on_button_press();
    00211         bpressed = HOWTOPLAYB;
    00212     }
    00213     if(strcmp(button->blabel, "OPTIONSBUTTON") == 0 && type == BPRESSED_CALL)
    00214     {
    00215         execute_on_button_press();
    00216         bpressed = OPTIONSB;
    00217     }
    00218     if(strcmp(button->blabel, "HIGHSCOREBUTTON") == 0 && type == BPRESSED_CALL)
    00219     {
    00220         execute_on_button_press();
    00221         bpressed = HIGHSCOREB;
    00222     }
    00223     if(strcmp(button->blabel, "EXITBUTTON") == 0 && type == BPRESSED_CALL)
    00224     {
    00225         execute_on_button_press();
    00226         bpressed = EXITB;
    00227     }
    00228     if(strcmp(button->blabel, "FULLSCREEN") == 0 && type == BPRESSED_CALL)
    00229     {
    00230         (al_get_display_flags() & ALLEGRO_FULLSCREEN_WINDOW) ?
    00231         al_toggle_display_flag(ALLEGRO_FULLSCREEN_WINDOW, false), al_resize_display(800, 600) :
    00232         al_toggle_display_flag(ALLEGRO_FULLSCREEN_WINDOW, true);
    00233
    00234         if(strcmp(button->btext.text, "FULLSCREEN") == 0)
    00235             button->btext.text = "WINDOWED";
    00236         else
    00237             button->btext.text = "FULLSCREEN";
    00238     }
    00239     if(strcmp(button->blabel, "FPS") == 0 && type == BPRESSED_CALL)
    00240     {
    00241         set_fps_status(!get_fps_status());
    00242
    00243         if(strcmp(button->btext.text, "FPS COUNTER: ON") == 0)
    00244             button->btext.text = "FPS COUNTER: OFF";
    00245         else
    00246             button->btext.text = "FPS COUNTER: ON";
    00247     }
    00248     if(strcmp(button->blabel, "BACK") == 0 && type == BPRESSED_CALL)
    00249     {
    00250         return_to_main_menu(game);
    00251     }
    00252     if(strcmp(button->blabel, "RESUME") == 0 && type == BPRESSED_CALL)
    00253     {
    00254         exit_pause(game);
    00255         mpressed = true;
    00256     }
    00257     if(strcmp(button->blabel, "EXITGAME") == 0 && type == BPRESSED_CALL)
    00258     {
    00259         game->exit = true;
    00260     }
    00261     if(strcmp(button->blabel, "QUITTOMENU") == 0 && type == BPRESSED_CALL)
    00262     {
    00263         return_to_main_menu(game);
    00264     }
    00265     if(strcmp(button->blabel, "QUITTOMENUGO") == 0 && type == BPRESSED_CALL)
    00266     {
    00267         return_to_main_menu(game);
    00268     }
    00269     if(strcmp(button->blabel, "CLEAR") == 0 && type == BPRESSED_CALL)
    00270     {
    00271         set_bflags_on_button(button, BLRELEASED);
    00272         remove_bflags_on_button(button, BLPRESSED);
    00273         remove_bflags_on_button(button, BVISIBLE);
    00274         clear_highscores();
    00275     }
    00276     if(type == CBANIMATION_CALL && (game->options || game->howtoplay || game->highscores || game->paused) && mpressed)
    00277     {
    00278         mpressed = false;
    00279         if(game->paused)
    00280             {
    00281                 pause_cleanup();
    00282                 game->gamestarted = true;
    00283                 game->paused = false;
    00284             }
    00285             else
    00286             {
    00287             execute_on_M_press();
    00288             options_cleanup();
    00289             howtoplay_cleanup();
    00290             highscores_cleanup();
    00291             gameover_cleanup();
    00292
    00293             game->gamestarted = false;
    00294             game->exit = false;
    00295             game->options = false;
    00296             game->highscores = false;
    00297             game->howtoplay = false;
    00298             }
    00299     }
    00300     if(type == CBANIMATION_CALL && bpressed != NONE)
    00301     {
    00302         BUTTONPTR currentptr, firstptr = get_first_element_from_button_list();
    00303         currentptr = search_button_list_for_element(&firstptr, "STARTBUTTON");
    00304         remove_bflags_on_button(currentptr, BVISIBLE);
    00305         currentptr = search_button_list_for_element(&firstptr, "HOWTOPLAYBUTTON");
    00306         remove_bflags_on_button(currentptr, BVISIBLE);
    00307         currentptr = search_button_list_for_element(&firstptr, "OPTIONSBUTTON");
    00308         remove_bflags_on_button(currentptr, BVISIBLE);
    00309         currentptr = search_button_list_for_element(&firstptr, "HIGHSCOREBUTTON");
    00310         remove_bflags_on_button(currentptr, BVISIBLE);
    00311         currentptr = search_button_list_for_element(&firstptr, "EXITBUTTON");
    00312         remove_bflags_on_button(currentptr, BVISIBLE);
    00313
    00314         switch (bpressed)
    00315         {
    00316             case NONE: break;
    00317             case STARTB: game->gamestarted = true; break;
    00318             case HOWTOPLAYB: game->howtoplay = true; break;
    00319             case OPTIONSB: game->options = true; break;
    00320             case HIGHSCOREB: game->highscores = true; break;
    00321             case EXITB: game->exit = true; break;
    00322             default: bpressed = NONE; break;
    00323         }
    00324         bpressed = NONE;
    00325     }
    00326     return NULL;
    00327 }
    00328
    00329 /*----------------------------------------------------------------------------------------------------------*/
    00330
    00331 void execute_on_button_press(void)
    00332 {
    00333     BUTTONPTR currentptr, firstptr = get_first_element_from_button_list();
    00334     currentptr = search_button_list_for_element(&firstptr, "STARTBUTTON");
    00335     currentptr->bdata.x = 263;
    00336     currentptr->bdata.limit = 805;
    00337     currentptr->bdata.aflags = BRIGHT;
    00338     currentptr = search_button_list_for_element(&firstptr, "HOWTOPLAYBUTTON");
    00339     currentptr->bdata.x = 263;
    00340     currentptr->bdata.x += 10;
    00341     currentptr->bdata.limit = 815;
    00342     currentptr->bdata.aflags = BRIGHT;
    00343     currentptr = search_button_list_for_element(&firstptr, "OPTIONSBUTTON");
    00344     currentptr->bdata.x = 263;
    00345     currentptr->bdata.x += 20;
    00346     currentptr->bdata.limit = 825;
    00347     currentptr->bdata.aflags = BRIGHT;
    00348     currentptr = search_button_list_for_element(&firstptr, "HIGHSCOREBUTTON");
    00349     currentptr->bdata.x = 263;
    00350     currentptr->bdata.x += 30;
    00351     currentptr->bdata.limit = 835;
    00352     currentptr->bdata.aflags = BRIGHT;
    00353     currentptr = search_button_list_for_element(&firstptr, "EXITBUTTON");
    00354     currentptr->bdata.x = 263;
    00355     currentptr->bdata.x += 40;
    00356     currentptr->bdata.limit = 845;
    00357     currentptr->bdata.aflags = BRIGHT;
    00358 }
    00359
    00360 /*----------------------------------------------------------------------------------------------------------*/
    00361
    00362 void execute_on_M_press(void)
    00363 {
    00364     BUTTONPTR currentptr, firstptr = get_first_element_from_button_list();
    00365     currentptr = search_button_list_for_element(&firstptr, "STARTBUTTON");
    00366     currentptr->bdata.x = -280;
    00367     currentptr->bdata.limit = 263;
    00368     currentptr->bdata.aflags = BRIGHT;
    00369     set_bflags_on_button(currentptr, BVISIBLE);
    00370     currentptr = search_button_list_for_element(&firstptr, "HOWTOPLAYBUTTON");
    00371     currentptr->bdata.x = -290;
    00372     currentptr->bdata.limit = 263;
    00373     currentptr->bdata.aflags = BRIGHT;
    00374     set_bflags_on_button(currentptr, BVISIBLE);
    00375     currentptr = search_button_list_for_element(&firstptr, "OPTIONSBUTTON");
    00376     currentptr->bdata.x = -300;
    00377     currentptr->bdata.limit = 263;
    00378     currentptr->bdata.aflags = BRIGHT;
    00379     set_bflags_on_button(currentptr, BVISIBLE);
    00380     currentptr = search_button_list_for_element(&firstptr, "HIGHSCOREBUTTON");
    00381     currentptr->bdata.x = -310;
    00382     currentptr->bdata.limit = 263;
    00383     currentptr->bdata.aflags = BRIGHT;
    00384     set_bflags_on_button(currentptr, BVISIBLE);
    00385     currentptr = search_button_list_for_element(&firstptr, "EXITBUTTON");
    00386     currentptr->bdata.x = -320;
    00387     currentptr->bdata.limit = 263;
    00388     currentptr->bdata.aflags = BRIGHT;
    00389     set_bflags_on_button(currentptr, BVISIBLE);
    00390 }
    00391
    00392 /*----------------------------------------------------------------------------------------------------------*/
    00393
    00394 void return_to_main_menu(GDATAPTR game)
    00395 {
    00396     if(game->options)
    00397     {
    00398         exit_options(game);
    00399         mpressed = true;
    00400     }
    00401     if(game->howtoplay)
    00402     {
    00403         exit_howtoplay(game);
    00404         mpressed = true;
    00405     }
    00406     if(game->highscores)
    00407     {
    00408         exit_highscores(game);
    00409         mpressed = true;
    00410     }
    00411     if(game->paused)
    00412     {
    00413         execute_on_M_press();
    00414         maingame_cleanup();
    00415         pause_cleanup();
    00416
    00417         game->gamestarted = false;
    00418         game->exit = false;
    00419         game->options = false;
    00420         game->howtoplay = false;
    00421         game->paused = false;
    00422     }
    00423     if(game->gamestarted)
    00424     {
    00425         execute_on_M_press();
    00426         maingame_cleanup();
    00427         gameover_cleanup();
    00428
    00429         game->gamestarted = false;
    00430         game->exit = false;
    00431         game->options = false;
    00432         game->howtoplay = false;
    00433         game->paused = false;
    00434     }
    00435 }
    00436
    00437 /*----------------------------------------------------------------------------------------------------------*/
    00438
    00439 //    /**
    00440 //      *@}
    00441 //      */