Browse Source

GUI : #define des dimensions de l'écran, ajout de la fonction d'affichage de message en jeu (pas finie)

master
Nikos 8 years ago
parent
commit
dbce363d5e
4 changed files with 68 additions and 11 deletions
  1. +62
    -10
      gui.c
  2. +1
    -1
      gui.h
  3. +5
    -0
      guidemo.c
  4. BIN
      img/msgboard.png

+ 62
- 10
gui.c View File

@ -8,7 +8,7 @@
#include "api.h"
#include "gui.h"
/*#define rant*/
/*#define rant // spout messages if the machine is too slow */
#define null_die(instruction, error) do if(!(instruction)) { fprintf(stderr, "(fatal) %s%s", error, "\n"); exit(1); } while(0)
@ -19,18 +19,25 @@ SDL_Surface *IMG_LoadBoost(const char *path) {
return ret;
}
#define SCREEN_X 800
#define SCREEN_Y 600
struct gui_resource_t display_start() {
struct gui_resource_t res;
/* création fenêtre */
SDL_Init(SDL_INIT_VIDEO);
res.screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
res.screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("跳棋", NULL);
/* chargement images */
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);
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");
@ -107,14 +114,12 @@ void draw_all(struct gui_resource_t *res, struct game_state_t *gamestate, SDL_Su
int index_lines[] = {1, 3, 6, 10, 23, 35, 46, 56, 65, 75, 86, 98, 111, 115, 118, 120};
int index_line_offsets[] = {0, 0, 2, 2, 8, 11, 11, 9, 9, 8, 10, 10, 12, 7, 3, 1, 1};
int line = 0;
#define CENTER_X 400
#define CENTER_Y 300
#define PAWNSIZE 28
#define FUZZSPACING 19
#define HORIZSPACING 10
#define VERTSPACING 5
pawn_pos.x = CENTER_X - PAWNSIZE/2 - (PAWNSIZE+HORIZSPACING);
pawn_pos.y = (PAWNSIZE/2+8);
pawn_pos.x = SCREEN_X/2 - PAWNSIZE/2 - (PAWNSIZE+HORIZSPACING);
pawn_pos.y = PAWNSIZE/2 + 8;
int i;
for(i = 0; i < 121; i++) {
if(i == index_lines[line]) {
@ -125,11 +130,11 @@ void draw_all(struct gui_resource_t *res, struct game_state_t *gamestate, SDL_Su
pawn_pos.x += PAWNSIZE+HORIZSPACING;
}
/* matrix rotation on pawn_pos using cos & sin of param angle */
pawn_pos_rot.x = pawn_pos.x + PAWNSIZE/2 - CENTER_X;
pawn_pos_rot.y = pawn_pos.y + PAWNSIZE/2 - CENTER_Y;
pawn_pos_rot.x = pawn_pos.x + PAWNSIZE/2 - SCREEN_X/2;
pawn_pos_rot.y = pawn_pos.y + PAWNSIZE/2 - SCREEN_Y/2;
pawn_pos_rot = coord_rotate(pawn_pos_rot, sinA, cosA);
pawn_pos_rot.x -= PAWNSIZE/2 - CENTER_X;
pawn_pos_rot.y -= PAWNSIZE/2 - CENTER_Y;
pawn_pos_rot.x -= PAWNSIZE/2 - SCREEN_X/2;
pawn_pos_rot.y -= PAWNSIZE/2 - SCREEN_Y/2;
SDL_BlitSurface(res->pawn_img[gamestate->board[i]], NULL, buffer, &pawn_pos_rot);
/* export coords */
if(export) {
@ -219,6 +224,52 @@ 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) {
#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);
/* 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 */
uint32_t time1, time2; /* mesure du temps de rendu de frame */
SDL_Event event;
int frame;
for(frame = 0; frame <= duration/MANIFRAMETIME; frame++) {
time1 = SDL_GetTicks();
while(SDL_PollEvent(&event))
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};
SDL_BlitSurface(zoomed_msg_img, NULL, res.screen, &clip); /* dessin du message */
SDL_FreeSurface(zoomed_msg_img);
SDL_Flip(res.screen);
/* attente du temps restant dans la frame */
time2 = SDL_GetTicks();
if(time2-time1 < MANIFRAMETIME)
SDL_Delay(MANIFRAMETIME - (time2-time1));
#ifdef rant
else
fprintf(stderr,"(rant) animation: the machine is slow, frame took %dms\n", time2-time1);
#endif
}
SDL_FreeSurface(bgbuffer);
SDL_FreeSurface(msgbuffer);
return frame != duration/MANIFRAMETIME + 1;
}
void display_close(struct gui_resource_t res) {
int i;
@ -228,6 +279,7 @@ void display_close(struct gui_resource_t res) {
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]);


+ 1
- 1
gui.h View File

@ -1,5 +1,5 @@
struct gui_resource_t {
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *pawn_img[7];
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *msgboard_img, *pawn_img[7];
};
struct gui_resource_t display_start();


+ 5
- 0
guidemo.c View File

@ -84,6 +84,11 @@ int main(int argc, char* argv[]) {
break;
}
if( display_animsg(gui_res, gamestate, player2, "lol!\n元気?", 2000/*ms*/) ) {
quit = 1;
break;
}
player1 = player2;
}


BIN
img/msgboard.png View File

Before After
Width: 420  |  Height: 136  |  Size: 117 KiB

Loading…
Cancel
Save