Browse Source

GUI totalement modulaire (gui.h, gui.c, guidemo.c pour test)

master
Nikos 8 years ago
parent
commit
0ff9feed31
4 changed files with 117 additions and 92 deletions
  1. +7
    -3
      Makefile
  2. +82
    -0
      gui.c
  3. +25
    -0
      gui.h
  4. +3
    -89
      guidemo.c

+ 7
- 3
Makefile View File

@ -21,12 +21,16 @@ dummy : dummy.c
ai:
make -C ai
guidemo : guidemo.c
gcc -Wall -lSDL -lSDL_image -o guidemo guidemo.c
gui : gui.c api.h
gcc -c -Wall gui.c
guidemo : guidemo.c gui
gcc -Wall -lSDL -lSDL_image -o guidemo guidemo.c gui.o
clean :
rm *.o
make -C ai clean
distclean :
rm api.h main dummy guidemo
rm -f *.o api.h main dummy guidemo
#make -C ai distclean TODO?

+ 82
- 0
gui.c View File

@ -0,0 +1,82 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <unistd.h> /* exit */
#include <stdio.h>
#include "gui.h"
#define null_die(instruction, error) do if(!(instruction)) { fprintf(stderr, "(fatal) %s%s", error, "\n"); exit(1); } while(0)
struct gui_resource_t display_start() {
struct gui_resource_t res;
/* création fenêtre */
SDL_Init(SDL_INIT_EVERYTHING);
res.screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
/* chargement images */
null_die(res.title_img = IMG_Load("img/title.png"), "cannot load title img");
null_die(res.bgnd_img = IMG_Load("img/background.png"), "cannot load bg img");
null_die(res.board_img = IMG_Load("img/board.png"), "cannot load board img");
null_die(res.pawn_img[0] = IMG_Load("img/pawn0-void.png"), "cannot load void pawn img");
null_die(res.pawn_img[1] = IMG_Load("img/pawn1-yellow.png"), "cannot load yellow pawn img");
null_die(res.pawn_img[2] = IMG_Load("img/pawn2-black.png"), "cannot load black pawn img");
null_die(res.pawn_img[3] = IMG_Load("img/pawn3-blue.png"), "cannot load blue pawn img");
null_die(res.pawn_img[4] = IMG_Load("img/pawn4-red.png"), "cannot load red pawn img");
null_die(res.pawn_img[5] = IMG_Load("img/pawn5-green.png"), "cannot load green pawn img");
null_die(res.pawn_img[6] = IMG_Load("img/pawn6-purple.png"), "cannot load purple pawn img");
return res;
}
void display_render_board(struct gui_resource_t res, struct game_state_t gamestate /*, enum hole_t currentplayer*/) {
int i;
/* positions pour les images */
SDL_Rect pawn_pos, zero_pos = {0, 0};
/* dessin du fond */
SDL_BlitSurface(res.bgnd_img, NULL, res.screen, &zero_pos);
SDL_BlitSurface(res.title_img, NULL, res.screen, &zero_pos);
SDL_BlitSurface(res.board_img, NULL, res.screen, &zero_pos);
/* #include <SDL/SDL_rotozoom.h>
SDL_Surface *rotated_board_img = rotozoomSurface(res.board_img, (360.0/6.0)*currentplayer, 0.0, 1); */
/* dessin des pions */
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 PAWNSIZE ((short)28)
#define FUZZSPACING ((short)20)
#define HORIZSPACING ((short)9)
#define VERTSPACING ((short)4)
pawn_pos.x = 400 - PAWNSIZE/2 - (PAWNSIZE+HORIZSPACING);
pawn_pos.y = (PAWNSIZE/2+16);
for(i = 0; i < 121; i++) {
if(i == index_lines[line]) {
line++;
pawn_pos.y += PAWNSIZE + VERTSPACING;
pawn_pos.x += ((line%2)?-1:1)*FUZZSPACING - index_line_offsets[line]*(PAWNSIZE+HORIZSPACING);
} else {
pawn_pos.x += PAWNSIZE+HORIZSPACING;
}
/* TO ADD: matrix rotation on pawn_pos using param currentplayer */
SDL_BlitSurface(res.pawn_img[gamestate.board[i]], NULL, res.screen, &pawn_pos);
}
}
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);
for( i = 0; i < 7; i++)
SDL_FreeSurface(res.pawn_img[i]);
/* Fermeture fenêtre */
SDL_Quit();
}

+ 25
- 0
gui.h View File

@ -0,0 +1,25 @@
#ifndef game_state_t
#include "api.h"
#endif
struct gui_resource_t {
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *pawn_img[7];
};
struct gui_resource_t display_start();
void display_render_board(struct gui_resource_t res, struct game_state_t gamestate/*, enum hole_t currentplayer*/);
/*void display_move_pawn(struct gui_resource_t res, struct game_state_t board, int startpos, int endpos); */
/*
SDL_Rect pos_board = {0,0}; ou plus, à voir
(blit la board en utilisant du code commun à display_board() sans le pion de startpos) buffer
loop {
interpolation de position pour obtenir &pawn_pos
SDL_BlitSurface(buffer, NULL, screen, &pos_board);
SDL_BlitSurface(res.pawn_imgs[], NULL, screen, &pawn_pos);
SDL_Flip(screen)
}
*/
void display_close();

+ 3
- 89
guidemo.c View File

@ -1,100 +1,14 @@
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <unistd.h> /* exit */
#include <stdio.h>
#include <SDL/SDL.h>
#define null_die(instruction, error) do if(!(instruction)) { fprintf(stderr, "(fatal) %s%s", error, "\n"); exit(1); } while(0)
/* TODO remove this */
enum hole_t { none, color1, color2, color3, color4, color5, color6 };
struct game_state_t {
enum hole_t board[121];
};
struct gui_resource_t {
SDL_Surface *screen, *title_img, *bgnd_img, *board_img, *pawn_img[7];
};
struct gui_resource_t display_start() {
struct gui_resource_t res;
/* création fenêtre */
SDL_Init(SDL_INIT_EVERYTHING);
res.screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
/* chargement images */
null_die(res.title_img = IMG_Load("img/title.png"), "cannot load title img");
null_die(res.bgnd_img = IMG_Load("img/background.png"), "cannot load bg img");
null_die(res.board_img = IMG_Load("img/board.png"), "cannot load board img");
null_die(res.pawn_img[0] = IMG_Load("img/pawn0-void.png"), "cannot load void pawn img");
null_die(res.pawn_img[1] = IMG_Load("img/pawn1-yellow.png"), "cannot load yellow pawn img");
null_die(res.pawn_img[2] = IMG_Load("img/pawn2-black.png"), "cannot load black pawn img");
null_die(res.pawn_img[3] = IMG_Load("img/pawn3-blue.png"), "cannot load blue pawn img");
null_die(res.pawn_img[4] = IMG_Load("img/pawn4-red.png"), "cannot load red pawn img");
null_die(res.pawn_img[5] = IMG_Load("img/pawn5-green.png"), "cannot load green pawn img");
null_die(res.pawn_img[6] = IMG_Load("img/pawn6-purple.png"), "cannot load purple pawn img");
return res;
}
void display_render_board(struct gui_resource_t res, struct game_state_t gamestate /*, enum hole_t currentplayer*/) {
int i;
/* positions pour les images */
SDL_Rect pawn_pos, zero_pos = {0, 0};
/* dessin du fond */
SDL_BlitSurface(res.bgnd_img, NULL, res.screen, &zero_pos);
SDL_BlitSurface(res.title_img, NULL, res.screen, &zero_pos);
SDL_BlitSurface(res.board_img, NULL, res.screen, &zero_pos);
/* #include <SDL/SDL_rotozoom.h>
SDL_Surface *rotated_board_img = rotozoomSurface(res.board_img, (360.0/6.0)*currentplayer, 0.0, 1); */
/* dessin des pions */
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 PAWNSIZE ((short)28)
#define FUZZSPACING ((short)20)
#define HORIZSPACING ((short)9)
#define VERTSPACING ((short)4)
pawn_pos.x = 400 - PAWNSIZE/2 - (PAWNSIZE+HORIZSPACING);
pawn_pos.y = (PAWNSIZE/2+16);
for(i = 0; i < 121; i++) {
if(i == index_lines[line]) {
line++;
pawn_pos.y += PAWNSIZE + VERTSPACING;
pawn_pos.x += ((line%2)?-1:1)*FUZZSPACING - index_line_offsets[line]*(PAWNSIZE+HORIZSPACING);
} else {
pawn_pos.x += PAWNSIZE+HORIZSPACING;
}
/* TO ADD: matrix rotation on pawn_pos using param currentplayer */
SDL_BlitSurface(res.pawn_img[gamestate.board[i]], NULL, res.screen, &pawn_pos);
}
}
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);
for( i = 0; i < 7; i++)
SDL_FreeSurface(res.pawn_img[i]);
/* Fermeture fenêtre */
SDL_Quit();
}
#include "gui.h"
int main(int argc, char* argv[]) {
struct game_state_t gamestate = {{1,1,1,1,1,1,1,1,1,1,5,5,5,5,0,0,0,0,0,2,2,2,2,5,5,5,0,0,0,0,0,0,2,2,2,5,5,0,0,0,0,0,0,0,2,2,5,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,6,0,0,0,0,0,0,0,0,4,6,6,0,0,0,0,0,0,0,4,4,6,6,6,0,0,0,0,0,0,4,4,4,6,6,6,6,0,0,0,0,0,4,4,4,4,3,3,3,3,3,3,3,3,3,3}};
struct gui_resource_t gui_res;
gui_res = display_start(gui_res);
display_render_board(gui_res, gamestate);
/* Gestion de l'interactivité */


Loading…
Cancel
Save