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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
#include <SDL/SDL_ttf.h>
#include <SDL/SDL.h>
/*
largeur et hauteur d'ecran
*/
#define largeur 800
#define hauteur 600
/*Temps avant progression*/
#define period 30
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
enum
{
accueil,
menu,
jeu,
score,
option,
NB_ETAT
};
/*============DISPLAYER=============*/
typedef struct displayer displayer;
struct displayer
{
SDL_Surface* screen;
};
void blit_img(displayer* d,SDL_Surface* img, int x,int y, SDL_Surface* screen)
{
SDL_Rect position;
position.x = x;
position.y = y;
SDL_BlitSurface(img, NULL, d->screen, &position);
}
void blit_background(displayer* d,SDL_Surface* background)
{
blit_img(d,background,0,0,d->screen);
}
void blit_sprite(displayer* s,SDL_Surface* img, int x,int y)
{
blit_img(s,img,x-img->w/2,y-img->h/2,s->screen);
}
void flip_screen(displayer* d)
{
SDL_Flip(d->screen);
}
displayer* create_display()
{
displayer* d=malloc(sizeof(*d));
d->screen= SDL_SetVideoMode(largeur, hauteur, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
return d;
}
void destruct_display(displayer* d)
{
free(d);
}
/*GAME STATE*/
typedef struct game_state game_state;
typedef void (*display_fct)(game_state*,displayer*);
typedef void (*event_fct)(game_state*);
typedef void (*initializer_fct)(game_state*,displayer*);
struct game;
struct game_state
{
event_fct event_handler;
display_fct display_handler;
initializer_fct initializer;
SDL_Surface* background;
int next;
int continuer;
int initialized;
};
game_state* create_game_state(display_fct d,event_fct e,initializer_fct i)
{
game_state* g=malloc(sizeof(*g));
/*
Affectation des fonctions de gestions et des donnees partagees.
*/
g->event_handler=e;
g->display_handler=d;
g->initializer=i;
g->initialized=0;
g->continuer=1;
return g;
}
void run_state(game_state* s,displayer* d)
{
s->event_handler(s);
s->display_handler(s,d);
}
void destruct_state(game_state* s)
{
SDL_FreeSurface(s->background);
free(s);
}
/*===========ACCUEIL==============*/
void event_acceuil(game_state* state)
{
SDL_Event event;
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
state->continuer = 0;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
state->continuer = 0;
break;
case SDLK_RETURN:
state->next=menu;
break;
default:
break;
}
default:
break;
}
}
void display_acceuil(game_state* s,displayer* d)
{
blit_background(d,s->background);
flip_screen(d);
printf("DISPLAY ACCEUIL\n");
}
void init_acceuil(game_state* state,displayer* d)
{
state->background=SDL_CreateRGBSurface(SDL_HWSURFACE,largeur,hauteur,32,0,0,0,0);
SDL_FillRect(state->background, NULL, SDL_MapRGB(d->screen->format, 0, 255, 255));
state->initialized=1;
state->next=accueil;
}
/*======================GAME==================*/
typedef struct game game;
struct game
{
/** Donne du jeu*/
game_state* states[NB_ETAT];
game_state* current_state;
};
game* create_game(displayer* d)
{
game* g=malloc(sizeof(*g));
/*Construction en memoire des differents etats de jeu. ces derniers ne sont pas encore valide
*/
g->states[accueil]=create_game_state(&display_acceuil,&event_acceuil,init_acceuil);
init_acceuil(g->states[accueil],d);
/*On demarre par l'etat d'accueil*/
g->current_state=g->states[accueil];
return g;
}
/*Boucle principale du jeu*/
void run_game(game* g,displayer* d)
{
while(g->current_state->continuer)
{
/*Traite les evenements et l'affichage*/
run_state(g->current_state,d);
/*permet de passer a l'etat suivant quand next est modifie dans la fonction de traitement de evenements*/
g->current_state=g->states[g->current_state->next];
if(!(g->current_state->initialized))
{
g->current_state->initializer(g->current_state,d);
}
}
}
void destruct_game(game* g)
{
destruct_state(g->states[accueil]);
destruct_state(g->states[menu]);
free(g);
}
/*===========MAIN==============*/
int main(int argc, char *argv[])
{
game* g=0;
displayer* d=0;
srand(time(NULL));
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
if(TTF_Init() == -1)
{
fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
d=create_display();
g=create_game(d);
run_game(g,d);
destruct_game(g);
destruct_display(d);
TTF_Quit();
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager