Browse Source

GUI : structs gamestate et ressource passés par pointeur const au lieu de copie, pour économie de mémoire.

master
Nikos 8 years ago
parent
commit
45e7a74c40
4 changed files with 51 additions and 51 deletions
  1. +28
    -28
      gui.c
  2. +6
    -6
      gui.h
  3. +7
    -7
      guidemo.c
  4. +10
    -10
      main.c

+ 28
- 28
gui.c View File

@ -55,7 +55,7 @@ struct gui_resource_t display_start() {
return res;
}
SDL_Event display_until_event(struct gui_resource_t res) {
SDL_Event display_until_event(const struct gui_resource_t *res) {
#define FIXEDFRAMETIME 125 /* 125ms → 8fps */
SDL_Event event;
uint32_t time1, time2; /* mesure du temps de rendu de frame */
@ -64,7 +64,7 @@ SDL_Event display_until_event(struct gui_resource_t res) {
if(SDL_PollEvent(&event))
break;
SDL_Flip(res.screen);
SDL_Flip(res->screen);
/* attente du temps restant dans la frame */
time2 = SDL_GetTicks();
@ -152,11 +152,11 @@ void draw_all(const struct gui_resource_t *res, const struct game_state_t *games
}
}
void display_render_board(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer) {
draw_all(&res, &gamestate, NULL, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL);
void display_render_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer) {
draw_all(res, gamestate, NULL, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL);
}
int display_anirotate_board(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, enum hole_t nextplayer) {
int display_anirotate_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, enum hole_t nextplayer) {
#define ANIFRAMETIME 40 /* 40ms → 25fps */
#define ANIMTIME 2000 /* 2000ms → 2s */
@ -173,8 +173,8 @@ int display_anirotate_board(struct gui_resource_t res, struct game_state_t games
double anim_progress = ( ((double)nextplayer) - ((double)currentplayer) ) * ( cos(M_PI*((double)frame)/((double)(ANIMTIME/ANIFRAMETIME))+M_PI) + 1 ) / 2;
double angle = ( anim_progress + (((double)currentplayer)-1) ) * M_PI / 3;
draw_all(&res, &gamestate, NULL, angle, frame == ANIMTIME/ANIFRAMETIME, NULL); /* antialiasing si dernière frame, sinon pas */
SDL_Flip(res.screen);
draw_all(res, gamestate, NULL, angle, frame == ANIMTIME/ANIFRAMETIME, NULL); /* antialiasing si dernière frame, sinon pas */
SDL_Flip(res->screen);
/* attente du temps restant dans la frame : stabilisation de la vitesse d'exécution */
time2 = SDL_GetTicks();
@ -188,15 +188,15 @@ int display_anirotate_board(struct gui_resource_t res, struct game_state_t games
return 0;
}
int display_animove_pawn(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, int startpos, int endpos) {
int display_animove_pawn(const struct gui_resource_t *res, struct game_state_t gamestate, enum hole_t currentplayer, int startpos, int endpos) {
#define PANIFRAMETIME 40 /* 40ms → 25fps */
#define PANIMTIME 1000 /* 1000ms → 1s */
enum hole_t pawn = gamestate.board[startpos]; /* conserve la couleur du pion qui sera déplacé */
gamestate.board[startpos] = none; /* on dessine un trou dans le fond, car le pion va en partir */
SDL_Surface *buffer = SDL_ConvertSurface(res.bgnd_img, res.bgnd_img->format, res.bgnd_img->flags);
SDL_Surface *buffer = SDL_ConvertSurface(res->bgnd_img, res->bgnd_img->format, res->bgnd_img->flags);
struct pos_export_t pos_import = { {startpos, endpos} };
draw_all(&res, &gamestate, buffer, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, &pos_import);
draw_all(res, &gamestate, buffer, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, &pos_import);
uint32_t time1, time2; /* mesure du temps de rendu de frame */
SDL_Event event;
@ -213,9 +213,9 @@ int display_animove_pawn(struct gui_resource_t res, struct game_state_t gamestat
double cos_progress = ( cos(M_PI*((double)frame)/((double)(PANIMTIME/PANIFRAMETIME))+M_PI) + 1 ) / 2;
pawn_pos.x = pos_import.pixpos[0].x + (short)round( ((double)(pos_import.pixpos[1].x - pos_import.pixpos[0].x)) * cos_progress);
pawn_pos.y = pos_import.pixpos[0].y + (short)round( ((double)(pos_import.pixpos[1].y - pos_import.pixpos[0].y)) * cos_progress);
SDL_BlitSurface(buffer, NULL, res.screen, NULL);
SDL_BlitSurface(res.pawn_img[pawn], NULL, res.screen, &pawn_pos);
SDL_Flip(res.screen);
SDL_BlitSurface(buffer, NULL, res->screen, NULL);
SDL_BlitSurface(res->pawn_img[pawn], NULL, res->screen, &pawn_pos);
SDL_Flip(res->screen);
/* attente du temps restant dans la frame */
time2 = SDL_GetTicks();
@ -230,17 +230,17 @@ int display_animove_pawn(struct gui_resource_t res, struct game_state_t gamestat
return frame != PANIMTIME/PANIFRAMETIME + 1;
}
int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, const char *msg, uint32_t duration) {
int display_animsg(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *msg, uint32_t duration) {
#define MANIFRAMETIME 40 /* 40ms → 25fps */
/* mise en buffer de l'image de fond */
SDL_Surface *bgbuffer = SDL_ConvertSurface(res.bgnd_img, res.bgnd_img->format, res.bgnd_img->flags);
draw_all(&res, &gamestate, bgbuffer, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL);
SDL_Surface *bgbuffer = SDL_ConvertSurface(res->bgnd_img, res->bgnd_img->format, res->bgnd_img->flags);
draw_all(res, gamestate, bgbuffer, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL);
/* mise en buffer de l'image du message */
SDL_Surface *msgbuffer = SDL_ConvertSurface(res.msgboard_img, res.msgboard_img->format, res.msgboard_img->flags);
SDL_Surface *msgbuffer = SDL_ConvertSurface(res->msgboard_img, res->msgboard_img->format, res->msgboard_img->flags);
SDL_Color text_color = {20, 18, 17}; /* RGB */
SDL_Surface *text_img = TTF_RenderUTF8_Blended(res.font_big, msg, text_color);
SDL_Surface *text_img = TTF_RenderUTF8_Blended(res->font_big, msg, text_color);
SDL_Rect textclip = { msgbuffer->w/2 - text_img->w/2, msgbuffer->h/2 - text_img->h/2 };
SDL_BlitSurface(text_img, NULL, msgbuffer, &textclip); /* dessin du texte */
SDL_FreeSurface(text_img);
@ -255,17 +255,17 @@ int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enu
if(event.type == SDL_QUIT)
frame = duration/MANIFRAMETIME + 2;
SDL_BlitSurface(bgbuffer, NULL, res.screen, NULL); /* dessin du fond */
SDL_BlitSurface(bgbuffer, NULL, res->screen, NULL); /* dessin du fond */
double progress = ((double)frame)/((double)(duration/MANIFRAMETIME)); /* 0→1 */
double sqprogress = (progress-1)*(progress-1);
SDL_Surface *zoomed_msg_img = rotozoomSurface(msgbuffer, sqprogress*10/M_PI, sqprogress+0.6, 1/*antialias*/);
SDL_SetAlpha(zoomed_msg_img, SDL_SRCALPHA|SDL_RLEACCEL, (int)round(sin(sqprogress*M_PI)*255));
SDL_Rect clip = { SCREEN_X/2 - zoomed_msg_img->w/2, SCREEN_Y/2 - zoomed_msg_img->h/2 };
SDL_BlitSurface(zoomed_msg_img, NULL, res.screen, &clip); /* dessin du message */
SDL_BlitSurface(zoomed_msg_img, NULL, res->screen, &clip); /* dessin du message */
SDL_FreeSurface(zoomed_msg_img);
SDL_Flip(res.screen);
SDL_Flip(res->screen);
/* attente du temps restant dans la frame */
time2 = SDL_GetTicks();
@ -281,17 +281,17 @@ int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enu
return frame != duration/MANIFRAMETIME + 1;
}
void display_close(struct gui_resource_t res) {
void display_close(struct gui_resource_t *res) {
int i;
/* Libération mémoire */
SDL_FreeSurface(res.screen);
SDL_FreeSurface(res.bgnd_img);
SDL_FreeSurface(res.title_img);
SDL_FreeSurface(res.board_img);
SDL_FreeSurface(res.msgboard_img);
SDL_FreeSurface(res->screen);
SDL_FreeSurface(res->bgnd_img);
SDL_FreeSurface(res->title_img);
SDL_FreeSurface(res->board_img);
SDL_FreeSurface(res->msgboard_img);
for( i = 0; i < 7; i++)
SDL_FreeSurface(res.pawn_img[i]);
SDL_FreeSurface(res->pawn_img[i]);
/* Fermeture fenêtre */
SDL_Quit();


+ 6
- 6
gui.h View File

@ -7,18 +7,18 @@ struct gui_resource_t {
struct gui_resource_t display_start();
SDL_Event display_until_event(struct gui_resource_t res);
SDL_Event display_until_event(const struct gui_resource_t *res);
void display_render_board(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer);
void display_render_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer);
/* les fonctions display_ani* retournent 1 si l'utilisateur ferme l'application, sinon 0 */
int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, const char *msg, uint32_t duration);
int display_animsg(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *msg, uint32_t duration);
int display_anirotate_board(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, enum hole_t nextplayer);
int display_anirotate_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, enum hole_t nextplayer);
int display_animove_pawn(struct gui_resource_t res, struct game_state_t gamestate, enum hole_t currentplayer, int startpos, int endpos);
int display_animove_pawn(const struct gui_resource_t *res, struct game_state_t gamestate, enum hole_t currentplayer, int startpos, int endpos);
void display_close();
void display_close(struct gui_resource_t *res);

+ 7
- 7
guidemo.c View File

@ -29,9 +29,9 @@ int main(int argc, char* argv[]) {
};
struct gui_resource_t gui_res;
gui_res = display_start(gui_res);
gui_res = display_start(&gui_res);
display_render_board(gui_res, gamestate, 1);
display_render_board(&gui_res, &gamestate, 1);
int quit = 0;
@ -39,7 +39,7 @@ int main(int argc, char* argv[]) {
SDL_Event event;
while(!quit) {
event = display_until_event(gui_res);
event = display_until_event(&gui_res);
if(event.type == SDL_QUIT) {
quit = 1;
@ -72,19 +72,19 @@ int main(int argc, char* argv[]) {
}
}
if( display_animove_pawn(gui_res, gamestate, player1, pawn1, pawn2) ) {
if( display_animove_pawn(&gui_res, gamestate, player1, pawn1, pawn2) ) {
quit = 1;
break;
}
gamestate.board[pawn2] = gamestate.board[pawn1];
gamestate.board[pawn1] = none;
if( display_anirotate_board(gui_res, gamestate, player1, player2) ) {
if( display_anirotate_board(&gui_res, &gamestate, player1, player2) ) {
quit = 1;
break;
}
if( display_animsg(gui_res, gamestate, player2, "Next player’s turn!", 1200/*ms*/) ) {
if( display_animsg(&gui_res, &gamestate, player2, "Next player’s turn!", 1200/*ms*/) ) {
quit = 1;
break;
}
@ -92,7 +92,7 @@ int main(int argc, char* argv[]) {
player1 = player2;
}
display_close(gui_res);
display_close(&gui_res);
return 0;
}

+ 10
- 10
main.c View File

@ -417,7 +417,7 @@ int main(int argc, char** argv) {
/* ouverture de la fenêtre graphique */
puts("(GUI) Opening window");
struct gui_resource_t gui_res = display_start(gui_res);
struct gui_resource_t gui_res = display_start(&gui_res);
/* dlsym inutile, sert à contourner le bug dlsym sdl sur openbsd */
dlsym(NULL,"");
dlerror();
@ -458,13 +458,13 @@ int main(int argc, char** argv) {
/* rafraîchissement du plateau à l'écran */
printf("(GUI) Rendering board, branch %d\n", player_state[0].branch);
display_render_board(gui_res, game_state, player_state[0].branch);
display_render_board(&gui_res, &game_state, player_state[0].branch);
SDL_Flip(gui_res.screen);
if( (buffer = (char*) malloc( (23+number_length(nb_game_end+1)+number_length(nb_game)) *sizeof(char) ) ) == NULL)
display_animsg(gui_res, game_state, player_state[0].branch, "Début de la manche", 1200);
display_animsg(&gui_res, &game_state, player_state[0].branch, "Début de la manche", 1200);
else {
snprintf(buffer, 22+number_length(nb_game_end+1)+number_length(nb_game), "Début de la manche %d/%d", nb_game_end+1, nb_game);
display_animsg(gui_res, game_state, player_state[0].branch, buffer, 1200);
display_animsg(&gui_res, &game_state, player_state[0].branch, buffer, 1200);
free(buffer);
}
@ -496,17 +496,17 @@ int main(int argc, char** argv) {
}
#endif
if(validation_movement != jump_valid && movement.start_pos != movement.end_pos && movement.start_pos >= 0 && movement.end_pos >= 0 && movement.start_pos < 121 && movement.end_pos < 121)
quit += display_animove_pawn(gui_res, game_state_copy, player_state[i].branch, movement.start_pos, movement.end_pos);
quit += display_animove_pawn(&gui_res, game_state_copy, player_state[i].branch, movement.start_pos, movement.end_pos);
/* on cherche à valider le coup */
if(validation_movement == invalid) {
#ifdef debug
fprintf(stderr, "mouvement (%d,%d) invalide\n",movement.start_pos, movement.end_pos);
#endif
if( (buffer = (char*) malloc( 28 *sizeof(char) ) ) == NULL)
display_animsg(gui_res, game_state, player_state[i].branch, "Coup non valide", 1000);
display_animsg(&gui_res, &game_state, player_state[i].branch, "Coup non valide", 1000);
else {
snprintf(buffer, 28, "Coup non valide (%d erreur%s)", player_state[i].error+1, (player_state[i].error+1>1)?"s":"");
display_animsg(gui_res, game_state, player_state[i].branch, buffer, 1200);
display_animsg(&gui_res, &game_state, player_state[i].branch, buffer, 1200);
free(buffer);
}
if(++(player_state[i].error)==3) {
@ -556,11 +556,11 @@ int main(int argc, char** argv) {
/* le joueur a joué, rotation du plateau à l'écran vers le suivant */
if( (nb_player_end < nb_player-1) && !quit) { // TODO condition identique au while, c'est pour quand on arrive à la fin de la partie, ne pas tourner le plateau
printf("(GUI) Rotating board, branch %d → %d\n", player_state[j].branch, player_state[i].branch);
quit += display_anirotate_board(gui_res, game_state, player_state[j].branch, player_state[i].branch + (j+1!=i)*6 /*places des joueurs actuel et suivant, ∈[1-6]*/);
quit += display_anirotate_board(&gui_res, &game_state, player_state[j].branch, player_state[i].branch + (j+1!=i)*6 /*places des joueurs actuel et suivant, ∈[1-6]*/);
}
}
puts("fin de la partie");
display_animsg(gui_res, game_state, player_state[i].branch, "Manche terminée", 1200);
display_animsg(&gui_res, &game_state, player_state[i].branch, "Manche terminée", 1200);
/* pour les joueurs qui n'ont pas perdu */
for (i=0; i < nb_player ; i++)
if(player_state[i].error < 3)
@ -585,7 +585,7 @@ int main(int argc, char** argv) {
free(duration_games);
/* fermeture de la fenêtre graphique */
display_close(gui_res);
display_close(&gui_res);
return 0;
}


Loading…
Cancel
Save