Browse Source

GUI : ajout de la fonction d'affichage du nom du joueur.

master
Nikos 7 years ago
parent
commit
47c5c5de6c
3 changed files with 54 additions and 27 deletions
  1. +39
    -12
      gui.c
  2. +6
    -6
      gui.h
  3. +9
    -9
      guidemo.c

+ 39
- 12
gui.c View File

@@ -36,10 +36,14 @@ struct gui_resource_t display_start() {
null_die(res.title_img = IMG_LoadBoost("img/title.png"), "cannot load title img");
null_die(res.bgnd_img = IMG_LoadBoost("img/background.png"), "cannot load bg img");
null_die(res.board_img = IMG_LoadBoost("img/board.png"), "cannot load board img");
SDL_Surface *msgboard_srcimg;
null_die(msgboard_srcimg = IMG_Load("img/msgboard.png"), "cannot load message board img");
res.msgboard_img = SDL_DisplayFormat(msgboard_srcimg);
SDL_FreeSurface(msgboard_srcimg);

/* cas spécial pour la variation de transparence */
SDL_Surface *srcimg;
null_die(srcimg = IMG_Load("img/msgboard.png"), "cannot load message board img");
res.msgboard_img = SDL_DisplayFormat(srcimg);
SDL_FreeSurface(srcimg);

null_die(res.nameboard_img = IMG_LoadBoost("img/nameboard.png"), "cannot load name board img");
null_die(res.pawn_img[0] = IMG_LoadBoost("img/pawn0-void.png"), "cannot load void pawn img");
null_die(res.pawn_img[1] = IMG_LoadBoost("img/pawn1-yellow.png"), "cannot load yellow pawn img");
null_die(res.pawn_img[2] = IMG_LoadBoost("img/pawn2-black.png"), "cannot load black pawn img");
@@ -50,11 +54,26 @@ struct gui_resource_t display_start() {

/* chargement police de caractères */
null_die(res.font_big = TTF_OpenFont("img/cmunbmr.otf", 34), "cannot load font");
null_die(res.font_small = TTF_OpenFont("img/cmunbmr.otf", 18), "cannot load font");
null_die(res.font_small = TTF_OpenFont("img/cmunbmr.otf", 19), "cannot load font");

return res;
}

void draw_nameboard(const struct gui_resource_t *res, const char *name, SDL_Surface *destbuffer, double hide) {
SDL_Surface *nbbuffer = SDL_ConvertSurface(res->nameboard_img, res->nameboard_img->format, res->nameboard_img->flags);
/* rendu du nom */
SDL_Color text_color = {20, 18, 17, 0}; /* R, G, B, unused TODO: use parm color */
SDL_Surface *text_img = TTF_RenderUTF8_Blended(res->font_small, name, text_color);
SDL_Rect textclip = { nbbuffer->w/2 - text_img->w/2, nbbuffer->h/2 - text_img->h/2, 0, 0 };
/* copie sur le fond */
SDL_BlitSurface(text_img, NULL, nbbuffer, &textclip); /* dessin du texte */
SDL_FreeSurface(text_img);
/* copie du tout dans le buffer de destination */
SDL_Rect destclip = { SCREEN_X - nbbuffer->w + (short)(hide*(double)nbbuffer->w), 0, 0, 0 };
SDL_BlitSurface(nbbuffer, NULL, destbuffer, &destclip);
SDL_FreeSurface(nbbuffer);
}

SDL_Rect coord_rotate(SDL_Rect coord, double sin, double cos) {
SDL_Rect res;
res.x = (short)round( cos * ((double)coord.x) + sin * ((double)coord.y) );
@@ -143,13 +162,14 @@ void draw_all(const struct gui_resource_t *res, const struct game_state_t *games
}
}

void display_render_board(const struct gui_resource_t *res, const 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, const char *playername) {
null_die(res, "ヌルポ。。。ガ! (gui_resource)");
null_die(gamestate, "ヌルポ。。。ガ! (game_state)");
draw_all(res, gamestate, NULL, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL);
draw_nameboard(res, playername, res->screen, 0);
}

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_anirotate_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, enum hole_t nextplayer, const char *nextplayername) {
null_die(res, "ヌルポ。。。ガ! (gui_resource)");
null_die(gamestate, "ヌルポ。。。ガ! (game_state)");

@@ -165,11 +185,14 @@ int display_anirotate_board(const struct gui_resource_t *res, const struct game_
while(SDL_PollEvent(&event))
if(event.type == SDL_QUIT)
return 1;

double progress = ((double)frame)/((double)(ANIMTIME/ANIFRAMETIME));
/* progrès non-linéaire (cosinus au cours du temps) */
double anim_progress = ( ((double)nextplayer) - ((double)currentplayer) ) * ( cos(M_PI*((double)frame)/((double)(ANIMTIME/ANIFRAMETIME))+M_PI) + 1 ) / 2;
double anim_progress = ( ((double)nextplayer) - ((double)currentplayer) ) * ( cos(M_PI*progress + 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 */
draw_nameboard(res, (progress < 0.5)?playername:nextplayername, res->screen, sin(progress*M_PI));
SDL_Flip(res->screen);

/* attente du temps restant dans la frame : stabilisation de la vitesse d'exécution */
@@ -184,7 +207,7 @@ int display_anirotate_board(const struct gui_resource_t *res, const struct game_
return 0;
}

int display_animove_pawn(const 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, const char *playername, int startpos, int endpos) {
null_die(res, "ヌルポ。。。ガ! (gui_resource)");

#define PANIFRAMETIME 40 /* 40ms → 25fps */
@@ -195,6 +218,7 @@ int display_animove_pawn(const struct gui_resource_t *res, struct game_state_t g
SDL_Surface *buffer = SDL_ConvertSurface(res->bgnd_img, res->bgnd_img->format, res->bgnd_img->flags);
struct pos_export_t pos_import = { {startpos, endpos}, {{0, 0, 0, 0}, {SCREEN_X, SCREEN_Y, 0, 0}} };
draw_all(res, &gamestate, buffer, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, &pos_import);
draw_nameboard(res, playername, buffer, 0);

uint32_t time1, time2; /* mesure du temps de rendu de frame */
SDL_Event event;
@@ -242,7 +266,7 @@ int find_pawn_index(Uint16 click_x, Uint16 click_y, const struct pawns_pxcoords_
return res;
}

int display_usermove_pawn(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, struct move_t *move, int *moremoves) {
int display_usermove_pawn(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, struct move_t *move, int *moremoves) {
null_die(res, "ヌルポ。。。ガ! (gui_resource)");
null_die(gamestate, "ヌルポ。。。ガ! (game_state)");
null_die(move, "ヌルポ。。。ガ! (move)");
@@ -255,7 +279,7 @@ int display_usermove_pawn(const struct gui_resource_t *res, const struct game_st
int quit = 0;
while(!quit) {
/* (ré)affichage du plateau dans son état d'avant le mouvement */
display_render_board(res, gamestate, currentplayer);
display_render_board(res, gamestate, currentplayer, playername);
SDL_Flip(res->screen);

/* attente du début de glisser-déposer */
@@ -287,6 +311,7 @@ int display_usermove_pawn(const struct gui_resource_t *res, const struct game_st
struct game_state_t tmp_gamestate = *gamestate; /* copie du plateau */
tmp_gamestate.board[move->startPos] = none; /* on enlève le pion en déplacement */
draw_all(res, &tmp_gamestate, bgbufferdrag, ((((double)currentplayer)-1)*M_PI)/3, 1/*antialias*/, NULL); /* dessin du plateau sans pion en buffer */
draw_nameboard(res, playername, bgbufferdrag, 0);

/* attente de la fin de glisser-déposer */
int release = 0;
@@ -329,7 +354,7 @@ int display_usermove_pawn(const struct gui_resource_t *res, const struct game_st
return quit;
}

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_animsg(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, const char *msg, uint32_t duration) {
null_die(res, "ヌルポ。。。ガ! (gui_resource)");
null_die(gamestate, "ヌルポ。。。ガ! (game_state)");

@@ -338,6 +363,7 @@ int display_animsg(const struct gui_resource_t *res, const struct game_state_t *
/* 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);
draw_nameboard(res, playername, bgbuffer, 0);

/* mise en buffer de l'image du message */
SDL_Surface *msgbuffer = SDL_ConvertSurface(res->msgboard_img, res->msgboard_img->format, res->msgboard_img->flags);
@@ -392,6 +418,7 @@ void display_close(struct gui_resource_t *res) {
SDL_FreeSurface(res->title_img);
SDL_FreeSurface(res->board_img);
SDL_FreeSurface(res->msgboard_img);
SDL_FreeSurface(res->nameboard_img);
int i;
for( i = 0; i < 7; i++)
SDL_FreeSurface(res->pawn_img[i]);


+ 6
- 6
gui.h View File

@@ -1,7 +1,7 @@
#include <SDL/SDL_ttf.h>

struct gui_resource_t {
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *msgboard_img, *pawn_img[7];
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *msgboard_img, *nameboard_img, *pawn_img[7];
TTF_Font *font_big, *font_small;
};

@@ -12,15 +12,15 @@ struct gui_resource_t display_start();
void display_close(struct gui_resource_t *res);

/* simple rendu interne du plateau, il faut SDL_Flip() pour l'afficher */
void display_render_board(const struct gui_resource_t *res, const 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, const char *playername);


/* les fonctions suivantes retournent 1 si l'utilisateur ferme l'application, sinon 0 */

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_animsg(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, const char *msg, uint32_t duration);

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_anirotate_board(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, enum hole_t nextplayer, const char *nextplayername);

int display_animove_pawn(const 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, const char *playername, int startpos, int endpos);

int display_usermove_pawn(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, struct move_t *move, int *moremoves);
int display_usermove_pawn(const struct gui_resource_t *res, const struct game_state_t *gamestate, enum hole_t currentplayer, const char *playername, struct move_t *move, int *moremoves);

+ 9
- 9
guidemo.c View File

@@ -31,14 +31,14 @@ int main(void) {

gui_res = display_start(&gui_res);

display_render_board(&gui_res, &gamestate, 1);
display_render_board(&gui_res, &gamestate, 1, "Name");

int quit = 0;

/* un peu d'interactivité */
SDL_Event event;

if( display_animsg(&gui_res, &gamestate, 1, "Click to start!", 1200/*ms*/) )
if( display_animsg(&gui_res, &gamestate, 1, "Name", "Click to start!", 1200/*ms*/) )
quit = 1;

while(!quit) {
@@ -77,13 +77,13 @@ int main(void) {
struct move_t user_move;
int moremoves;
if(player1 == 1) {
if( display_animsg(&gui_res, &gamestate, player1, "Your turn!", 1200/*ms*/) ) {
if( display_animsg(&gui_res, &gamestate, player1, "Name", "Your turn!", 1200/*ms*/) ) {
quit = 1;
break;
}
do {
display_animsg(&gui_res, &gamestate, player1, "Move a pawn or right-click", 1200/*ms*/);
if( display_usermove_pawn(&gui_res, &gamestate, player1, &user_move, &moremoves) ) {
display_animsg(&gui_res, &gamestate, player1, "Name", "Move a pawn or right-click", 1200/*ms*/);
if( display_usermove_pawn(&gui_res, &gamestate, player1, "Name", &user_move, &moremoves) ) {
quit = 1;
break;
}
@@ -94,12 +94,12 @@ int main(void) {
} while(moremoves);
if(quit)
break;
if( display_animsg(&gui_res, &gamestate, player1, "You’ve done your move!", 1200/*ms*/) ) {
if( display_animsg(&gui_res, &gamestate, player1, "Name", "You’ve done your move!", 1200/*ms*/) ) {
quit = 1;
break;
}
} else {
if( display_animove_pawn(&gui_res, gamestate, player1, pawn1, pawn2) ) {
if( display_animove_pawn(&gui_res, gamestate, player1, "Name", pawn1, pawn2) ) {
quit = 1;
break;
}
@@ -107,12 +107,12 @@ int main(void) {
gamestate.board[pawn1] = none;
}

if( display_anirotate_board(&gui_res, &gamestate, player1, player2) ) {
if( display_anirotate_board(&gui_res, &gamestate, player1, "Name", player2, "Name2") ) {
quit = 1;
break;
}

if( display_animsg(&gui_res, &gamestate, player2, "Next player’s turn!", 1200/*ms*/) ) {
if( display_animsg(&gui_res, &gamestate, player2, "Name2", "Next player’s turn!", 1200/*ms*/) ) {
quit = 1;
break;
}


Loading…
Cancel
Save