Browse Source

GUI : fonction d'affichage de message finie (ajout du rendu de texte)

utilisation de la mémoire vidéo
master
Nikos 8 years ago
parent
commit
fd479d13bb
5 changed files with 27 additions and 14 deletions
  1. +2
    -2
      Makefile
  2. +21
    -11
      gui.c
  3. +3
    -0
      gui.h
  4. +1
    -1
      guidemo.c
  5. BIN
      img/cmunbmr.otf

+ 2
- 2
Makefile View File

@ -16,7 +16,7 @@ api.h : dameschinoises.h dameschinoises.patch
patch -o api.h dameschinoises.h dameschinoises.patch
main : main.o gui
gcc main.o gui.o -o main -pedantic $(LDFLAGS) -lSDL -lSDL_image -lSDL_gfx -lm $(WARNINGS)
gcc main.o gui.o -o main -pedantic $(LDFLAGS) -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf -lm $(WARNINGS)
main.o : main.c api.h
gcc -c main.c $(CFLAGS) $(WARNINGS)
@ -33,7 +33,7 @@ gui : gui.c api.h
gcc -c gui.c $(CFLAGS) $(WARNINGS)
guidemo : guidemo.c gui
gcc guidemo.c gui.o -o guidemo $(CFLAGS) $(LDFLAGS) -lSDL -lSDL_image -lSDL_gfx -lm $(WARNINGS)
gcc guidemo.c gui.o -o guidemo $(CFLAGS) $(LDFLAGS) -lSDL -lSDL_image -lSDL_gfx -lSDL_ttf -lm $(WARNINGS)
clean :
rm -f *.o


+ 21
- 11
gui.c View File

@ -25,9 +25,11 @@ SDL_Surface *IMG_LoadBoost(const char *path) {
struct gui_resource_t display_start() {
struct gui_resource_t res;
null_die(SDL_Init(SDL_INIT_VIDEO) != -1, "SDL initialization failed");
null_die(TTF_Init() != -1, "TTF initialization failed");
/* création fenêtre */
SDL_Init(SDL_INIT_VIDEO);
res.screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, 32, SDL_SWSURFACE);
res.screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("跳棋", NULL);
/* chargement images */
@ -46,6 +48,10 @@ struct gui_resource_t display_start() {
null_die(res.pawn_img[5] = IMG_LoadBoost("img/pawn5-green.png"), "cannot load green pawn img");
null_die(res.pawn_img[6] = IMG_LoadBoost("img/pawn6-purple.png"), "cannot load purple pawn img");
/* 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");
return res;
}
@ -217,7 +223,7 @@ int display_animove_pawn(struct gui_resource_t res, struct game_state_t gamestat
SDL_Delay(PANIFRAMETIME - (time2-time1));
#ifdef rant
else
fprintf(stderr,"(rant) animation: the machine is slow, frame took %dms\n", time2-time1);
fprintf(stderr,"(rant) pawn animation: the machine is slow, frame took %dms\n", time2-time1);
#endif
}
SDL_FreeSurface(buffer);
@ -233,7 +239,11 @@ int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enu
/* 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 ttf msg → msgbuffer TODO */
SDL_Color text_color = {20, 18, 17}; /* RGB */
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);
uint32_t time1, time2; /* mesure du temps de rendu de frame */
SDL_Event event;
@ -245,13 +255,12 @@ int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enu
if(event.type == SDL_QUIT)
frame = duration/MANIFRAMETIME + 2;
/* interpolation non-linéaire de progression 0→1 */
double progress = sin( ((double)frame)/((double)(duration/MANIFRAMETIME))*M_PI/2 );
SDL_BlitSurface(bgbuffer, NULL, res.screen, NULL); /* dessin du fond */
SDL_Surface *zoomed_msg_img = rotozoomSurface(msgbuffer, (1-progress)*30/M_PI, 2-progress, frame == duration/MANIFRAMETIME);
SDL_SetAlpha(zoomed_msg_img, SDL_SRCALPHA|SDL_RLEACCEL, (char)round(progress*256));
SDL_Rect clip = { SCREEN_X/2 - zoomed_msg_img->w/2, SCREEN_Y/2 - zoomed_msg_img->h/2};
double progress = ((double)frame)/((double)(duration/MANIFRAMETIME)); /* 0→1 */
SDL_Surface *zoomed_msg_img = rotozoomSurface(msgbuffer, (1-progress)*10/M_PI, 1.5-progress, 1/*antialias*/);
SDL_SetAlpha(zoomed_msg_img, SDL_SRCALPHA|SDL_RLEACCEL, (int)round(sin(progress*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_FreeSurface(zoomed_msg_img);
@ -263,7 +272,7 @@ int display_animsg(struct gui_resource_t res, struct game_state_t gamestate, enu
SDL_Delay(MANIFRAMETIME - (time2-time1));
#ifdef rant
else
fprintf(stderr,"(rant) animation: the machine is slow, frame took %dms\n", time2-time1);
fprintf(stderr,"(rant) text animation: the machine is slow, frame took %dms\n", time2-time1);
#endif
}
SDL_FreeSurface(bgbuffer);
@ -285,4 +294,5 @@ void display_close(struct gui_resource_t res) {
/* Fermeture fenêtre */
SDL_Quit();
TTF_Quit();
}

+ 3
- 0
gui.h View File

@ -1,5 +1,8 @@
#include <SDL/SDL_ttf.h>
struct gui_resource_t {
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *msgboard_img, *pawn_img[7];
TTF_Font *font_big, *font_small;
};
struct gui_resource_t display_start();


+ 1
- 1
guidemo.c View File

@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {
break;
}
if( display_animsg(gui_res, gamestate, player2, "lol!\n元気?", 2000/*ms*/) ) {
if( display_animsg(gui_res, gamestate, player2, "Next player’s turn!", 1200/*ms*/) ) {
quit = 1;
break;
}


BIN
img/cmunbmr.otf View File


Loading…
Cancel
Save