1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
struct game_data
{
string name;
};
typedef void (*display_fct)(game_state*,displayer*);
typedef void (*event_fct)(game_state*);
struct game_state
{
game_data* common_data;
/*Pointeur pour acceder aux données spécifique de chaque game_state*/
/*
void* specific_data;
*/
event_fct event_handler;
display_fct display_handler;
SDL_Surface* background;
SDL_Event event;
int continuer;
};
struct game
{
/** Donne du jeu*/
game_data common_data;
game_state* acceuil,jeu;
game_state* current_state;
};
/*=====================*/
int main(int argc, char *argv[])
{
game* g=0;
displayer* d=0;
/*...*/
d=create_display();
g=create_game(d);
run_game(g,d);
destruct_game(g);
return EXIT_SUCCESS;
}
/*=====================*/
game* create_game(displayer* d)
{
game* g=malloc(sizeof(*g));
init_data(&(g->common_data));
g->acceuil=create_game_state(&display_acceuil,&event_acceuil,g);
g->jeu=create_game_state(&display_jeu,&event_jeu,g);
/*Initialisation specifique a accueil*/
init_acceuil(g->acceuil,d);
g->current_state=g->acceuil;
return g;
}
/*Boucle principale du jeu*/
void run_game(game* g,displayer* d)
{
int t1=0,t2=0;
while(g->current_state->continuer)
{
t2 = SDL_GetTicks();
if (t2 - t1 > period)
{
t1 = t2;
}
else
{
SDL_Delay(period/2);
run_state(g->current_state,d);
}
}
}
game_state* create_game_state(display_fct d,event_fct e,game* game)
{
game_state* g=malloc(sizeof(*g));
g->event_handler=e;
g->display_handler=d;
g->common_data=&(game->common_data);
g->continuer=1;
return g;
}
void run_state(game_state* s,displayer* d)
{
s->event_handler(s);
s->display_handler(s,d);
}
void event_acceuil(game_state* state){/**/}
void display_acceuil(game_state* state,displayer*){/**/}
void event_jeu(game_state* state){/**/}
void display_jeu(game_state* state,displayer*){/**/} |
Partager