save & load

This commit is contained in:
Tropicananass 2016-05-07 22:59:58 +02:00
parent bca387a6dc
commit 8b439c694f
19 changed files with 330 additions and 57 deletions

BIN
bin/hex

Binary file not shown.

View File

@ -3,7 +3,7 @@ CFLAGS=-Wall -g -std=c99 -c
#-lm `sdl-config --cflags --libs` -lSDL_ttf
LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf -lSDL_gfx -lSDL_mixer -lSDL_image
EXEC=bin/hex
SRC=src/hex.c src/affichage_plateau.c src/action_plateau.c src/affichage_menu_principal.c src/window.c src/param.c src/en_jeu.c src/menu_principal.c src/draw.c src/action_menu_principal.c src/sound.c src/testJeu.c
SRC=src/hex.c src/affichage_plateau.c src/action_plateau.c src/affichage_menu_principal.c src/window.c src/param.c src/en_jeu.c src/menu_principal.c src/draw.c src/action_menu_principal.c src/sound.c src/testJeu.c src/sauvegarde.c
#$(wildcard src/*.c)
OBJ=$(SRC:.c=.o)
@ -14,6 +14,7 @@ src/%.o: src/%.c
$(EXEC): $(OBJ)
$(CC) -o $@ $^ $(LDFLAGS)
mv src/*.o obj/
clean:

View File

@ -199,7 +199,7 @@ bool selection_menu (menu_t m, int* r)
{
case 01:
*r = M_CHARGER;
return false;
return true;
case 10:
*r = M_JOUER;
play_gong();

View File

@ -3,6 +3,7 @@
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_rotozoom.h>
#include <SDL/SDL_image.h>
#include <assert.h>
#include "param.h"
#include "draw.h"
@ -420,9 +421,12 @@ void banane (SDL_Surface* window)
bool end = false;
int i = 0;
int cur = 0;
char* passcode = "stopdancingstupidbanana";
bool pause = false;
while (!end)
{
if (!(SDL_GetTicks() % 100))
if (!(SDL_GetTicks() % 100) && !pause)
{
SDL_BlitSurface (gif [i%8], NULL, window, &p);
SDL_Flip (window);
@ -430,10 +434,27 @@ void banane (SDL_Surface* window)
i = (i + 1) % 8 + 8;
}
SDL_Event e;
SDL_PollEvent (&e);
if (e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE))
if (SDL_PollEvent (&e) && e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_p:
pause = true;
break;
case SDLK_r:
pause = false;
break;
default:;
}
//Uint8* key = SDL_GetKeyState(NULL);
if (e.key.keysym.sym == passcode[cur])
++cur;
else
cur = 0;
if (cur == strlen (passcode))
end = true;
}
}
SDL_BlitSurface (erase, NULL, window, &p);
SDL_Flip (window);

View File

@ -15,6 +15,8 @@ typedef struct s_scrolling {
void Affiche_menu_principal (menu_t m);
void Affiche_menu_options (SDL_Surface* window, int rayon, int l);
void Affiche_entry (menu_t m, char* entry, bool pointe);
scrolling_t init_scroll (SDL_Surface* window);

View File

@ -2,16 +2,100 @@
#include <math.h>
#include <stdlib.h>
#include <SDL/SDL_gfxPrimitives.h>
#include "param.h"
#include "draw.h"
#define DBORD 4
/* Internes */
void Define_rayon (plateau_t p)
{
int decal_droite = 40;
p->marge_vert = p->window->h / 6;
int width = p->window->w - 2 * decal_droite;
int height = p->window->h - 2 * p->marge_vert;
int r1 = (height - (NBSIDE - 1)) / (.5 + NBSIDE * 1.5);
int r2 = width / ((3 * NBSIDE - 1) * cos (PI/6));
if (r1 < r2)
{
p->r = r1 - r1%2;
p->marge_hori = p->window->w - decal_droite - 1.5 * (NBSIDE) * 2 * p->r * cos(PI/6);
}
else
{
p->r = r2 - r2%2;
p->marge_hori = p->window->w - decal_droite - 1.5 * (NBSIDE) * 2 * p->r * cos(PI/6);
}
}
void Bordures (plateau_t p)
{
// (horizontales) player 2
int l = (cos (PI / 6) - cos (5 * PI / 6)) * p->r;
int dx = p->marge_hori+ p->r - l / 2;
Sint16 x1 [5] = {dx,
dx - l / 2,
dx,
dx + NBSIDE * (l + 1) - .25 * l,
dx + NBSIDE * (l + 1)};
Sint16 y1 [5] = {p->marge_vert - p->r / 2,
p->marge_vert,
p->marge_vert + p->r / 2,
p->marge_vert + p->r / 2,
p->marge_vert - p->r / 2};
filledPolygonRGBA(p->window, x1, y1, 5, param->rgb_j2.r, param->rgb_j2.g, param->rgb_j2.b, 255);
dx += (NBSIDE - 1) * (l + 1) / 2;
Sint16 x2 [5] = {dx + .25 * l,
dx + NBSIDE * (l + 1),
dx + NBSIDE * (l + 1) + l / 2,
dx + NBSIDE * (l + 1),
dx};
int dy = p->marge_vert + 1.5 * p->r * NBSIDE + .5 * p->r;
Sint16 y2 [5] = {dy - p->r / 2,
dy - p->r / 2,
dy,
dy + p->r / 2,
dy + p->r / 2};
filledPolygonRGBA(p->window, x2, y2, 5, param->rgb_j2.r, param->rgb_j2.g, param->rgb_j2.b, 255);
Sint16 x3 [5] = {x1 [1],
x1 [1] - l / 2,
x2 [4],
x2 [0],
x1 [2]};
Sint16 y3 [5] = {y1 [1],
y1 [2],
y2 [4],
y2 [0],
y1 [2]};
filledPolygonRGBA(p->window, x3, y3, 5, param->rgb_j1.r, param->rgb_j1.g, param->rgb_j1.b, 255);
/*x [0] = ;
x [1] = ;
x [2] = ;
x [3] = ;
x [4] = ;
y [0] = ;
y [1] = ;
y [2] = ;
y [3] = ;
y [4] = ;
pos.x += (NBSIDE - 1) * (l + 1) / 2.;
pos.y += 1.5 * p->r * NBSIDE + .5 * p->r;
SDL_FillRect (p->window, &pos, param->j2);
// (Verticales) player 1
filledPolygonRGBA(p->window, x, y, 4, 255, 0, 255, 255);*/
SDL_Flip (p->window);
}
void Quadrille (plateau_t p)
{
Define_rayon(p);
Bordures (p);
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0);
Hexagon (hex, p->r, param->ex, param->in, DBORD, &(p->l));
for (int i = 0; i < NBSIDE; ++i)
@ -29,6 +113,9 @@ void Quadrille (plateau_t p)
void Quadrille_bis (plateau_t p)
{
Define_rayon(p);
Bordures (p);
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0);
SDL_Surface* hex1 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0);
SDL_Surface* hex2 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0);
@ -63,26 +150,6 @@ void Quadrille_bis (plateau_t p)
SDL_Flip (p->window);
}
void define_rayon (plateau_t p)
{
int decal_droite = 40;
p->marge_vert = p->window->h / 6;
int width = p->window->w - 2 * decal_droite;
int height = p->window->h - 2 * p->marge_vert;
int r1 = (height - (NBSIDE - 1)) / (.5 + NBSIDE * 1.5);
int r2 = width / ((3 * NBSIDE - 1) * cos (PI/6));
if (r1 < r2)
{
p->r = r1 - r1%2;
p->marge_hori = p->window->w - decal_droite - 1.5 * (NBSIDE) * 2 * p->r * cos(PI/6);
}
else
{
p->r = r2 - r2%2;
p->marge_hori = p->window->w - decal_droite - 1.5 * (NBSIDE) * 2 * p->r * cos(PI/6);
}
}
/*Externes*/
void Affiche_hexagon (plateau_t p, int x, int y, int state)
@ -126,22 +193,38 @@ plateau_t init_plateau (SDL_Surface* window)
plateau_t p = malloc (sizeof (struct s_plateau));
p->grid = malloc (sizeof (int) * NBSIDE * NBSIDE);
p->hist = malloc (sizeof (int) * NBSIDE * NBSIDE);
for (int x = 0; x <= NBSIDE * NBSIDE; ++x)
for (int x = 0; x < NBSIDE * NBSIDE; ++x)
{
p->grid [x] = 0;
p->hist [x] = 0;
p->hist [x] = -1;
}
p->nb_coups = 0;
p->window = window;
p->player = false;
define_rayon(p);
Quadrille (p);
return p;
}
plateau_t load_plateau (SDL_Surface* window, int* hist)
{
plateau_t p = malloc (sizeof (struct s_plateau));
p->grid = malloc (sizeof (int) * NBSIDE * NBSIDE);
p->hist = hist;
p->player = false;
for (int i = 0; i < NBSIDE * NBSIDE; ++i)
p->grid [i] = 0;
for (p->nb_coups = 0; p->nb_coups < NBSIDE * NBSIDE && hist [p->nb_coups] != -1; ++p->nb_coups)
{
p->grid [hist [p->nb_coups]] = PLAYER(p->player);
p->player = !p->player;
}
p->window = window;
Quadrille_bis (p);
return p;
}
plateau_t actu_plateau (plateau_t p)
{
define_rayon(p);
Quadrille_bis (p);
return p;
}

View File

@ -13,6 +13,8 @@ void Affiche_hexagon (plateau_t p, int x, int y, int state);
plateau_t init_plateau (SDL_Surface* window);
plateau_t load_plateau (SDL_Surface* window, int* hist);
plateau_t actu_plateau (plateau_t p);
void free_plateau (plateau_t p);

View File

@ -8,6 +8,7 @@
#include "draw.h"
#include <math.h>
#include <SDL/SDL_gfxPrimitives.h>
#include "globals.h"

View File

@ -12,11 +12,16 @@
#include "affichage_plateau.h"
#include "window.h"
#include "param.h"
#include "sauvegarde.h"
void en_jeu (SDL_Surface* window)
void en_jeu (SDL_Surface* window, int* hist)
{
Mix_PlayMusic(param->music, -1);
plateau_t plateau = init_plateau (window);
plateau_t plateau;
if (hist == NULL)
plateau = init_plateau (window);
else
plateau = load_plateau (window, hist);
vec2 c = {0, 0};
bool end = false;
while (!end)
@ -41,7 +46,12 @@ void en_jeu (SDL_Surface* window)
end = selection (plateau, c);
else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT)
deplacement(plateau, &event, &c);
else if (event.key.keysym.sym == SDLK_a || event.key.keysym.sym == SDLK_b || event.key.keysym.sym == SDLK_n)
east1 (window, event.key.keysym.sym);
else if (event.key.keysym.sym == SDLK_s)
{
sauvegarde ("1", plateau->hist, 0);
}
break;
case SDL_MOUSEBUTTONUP:
end = selection (plateau, c);

View File

@ -10,6 +10,6 @@
#include <SDL/SDL.h>
void en_jeu (SDL_Surface* window);
void en_jeu (SDL_Surface* window, int* hist);
#endif /* _EN_JEU_H_ */

View File

@ -9,7 +9,7 @@
#define DWIDTH 800
#define DHEIGHT 600
#define NBSIDE 11
#define NBSIDE param->size
#define PI 3.14159265
#define RAC3 1.73205080757
@ -68,6 +68,9 @@ typedef struct s_param {
unsigned char music_vol;
Mix_Chunk* click,* gong;
unsigned char chunk_vol;
int size;
}* param_t;
param_t param;
#endif /* _GLOBALS_H_ */

View File

@ -9,6 +9,7 @@
#include "menu_principal.h"
#include "en_jeu.h"
#include "param.h"
#include "sauvegarde.h"
int main(int argc, char **argv)
{
@ -23,14 +24,25 @@ int main(int argc, char **argv)
switch (retour)
{
case M_JOUER:
en_jeu (window);
en_jeu (window, NULL);
Reset_window(window);
break;
case M_CHARGER:
{
int * hist;
int ia;
printf (" -- %d\n", chargement ("1", &hist, &ia));
en_jeu (window, hist);
Reset_window(window);
break;
}
case M_QUITTER:
end = 1;
}
}
//en_jeu (window);
save_param(window);
Free_window (window);
return 0;
}

View File

@ -40,6 +40,8 @@ int menu_options (SDL_Surface* window, scrolling_t s, menu_t m)
{
Affiche_menu_options (window, m->r, m->l);
bool end = 0;
if (end)
end = 1;
/*while (!end)
{
SDL_Event event;
@ -77,6 +79,7 @@ int menu_options (SDL_Surface* window, scrolling_t s, menu_t m)
break;
}
}*/
return 0;
}
int menu_principal (SDL_Surface* window)

View File

@ -83,7 +83,7 @@ void load_param (SDL_Surface* w)
}
fclose (param_file);
}
param->size = 11;
Load_sound ();
}

View File

@ -10,8 +10,6 @@
#include "globals.h"
param_t param;
void load_param (SDL_Surface* w);
void save_param (SDL_Surface* w);

130
src/sauvegarde.c Normal file
View File

@ -0,0 +1,130 @@
#include "sauvegarde.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "globals.h"
int listeSauvegarde(char*** nomSave){
DIR* rep = opendir("save/");
if(rep==NULL){
return 0;
}
struct dirent* d;
int i=0;
int nbSave=0;
int ext;
while((d=readdir(rep))!=NULL)
{
ext=strlen(d->d_name);
if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav")){
nbSave++;
}
}
rewinddir(rep);
char ** tmp;
tmp=malloc(sizeof(char*)*(nbSave));
int j;
while((d=readdir(rep))!=NULL)
{
ext=strlen(d->d_name);
if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav")){
tmp[i]=malloc(sizeof(char)*(ext-2));
for(j=0;j<ext-4;j++){
tmp[i][j]= d->d_name[j];
}
tmp[i][ext-3]=0;
i++;
}
}
closedir(rep);
*nomSave = tmp;
return nbSave;
}
// Renvoi 0 si la sauvegarde a eu lieu et 1 si il y a eu problème
int sauvegarde (char * nom, int * tab, int IA)
{
DIR* rep = opendir("save/");
if(rep==NULL){
if(mkdir("save/", S_IRWXU)){
closedir(rep);
return ERROR_CREATE_DIR;
}
}
closedir(rep);
FILE* f_sauvegarde;
char desif[strlen(nom)+12];
strcpy(desif,"save/");
strcat(desif,nom);
strcat(desif,".sav");
f_sauvegarde=fopen(desif,"w+");
fprintf(f_sauvegarde,"\n%d\n",IA);
fprintf(f_sauvegarde,"%d\n",param->size);
int i;
for (i=0; i<param->size*param->size; ++i)
fprintf(f_sauvegarde,"%d\n", tab[i]);
fclose(f_sauvegarde);
return 0;
}
int chargement(char *nom , int **tab, int *IA)
{
FILE* f_chargement;
char desif[strlen(nom)+12];
int *t;
int a=0, i, taille;
strcpy(desif,"save/");
strcat(desif,nom);
strcat(desif,".sav");
f_chargement=fopen (desif, "r");
if (f_chargement == NULL)
return ERROR_OPENFILE;
a=fscanf(f_chargement, "%d", IA);
printf ("1\n");
if(!a || *IA>2 || *IA<0){
return ERROR_IA;
}
printf ("2\n");
fscanf(f_chargement, "%d", &taille);
if(taille<1){
return ERROR_SIZE;
}
printf ("3\n");
t=malloc(sizeof(int)*taille*taille);
a=1, i = -1;
printf ("4\n");
while(a>0){
i++;
a=fscanf(f_chargement, "%d" , &t[i]);
}
fclose(f_chargement);
if(i!=taille*taille){
return ERROR_NB_VALUE;
}
param->size=taille;
*tab=t;
return 0;
}
void supprimer(char * nom){
char desif[strlen(nom)+12];
strcpy(desif,"save/");
strcat(desif,nom);
strcat(desif,".sav");
unlink(desif);
}

9
src/sauvegarde.h Normal file
View File

@ -0,0 +1,9 @@
#define ERROR_IA 1
#define ERROR_SIZE 2
#define ERROR_NB_VALUE 3
#define ERROR_OPENFILE 4
#define ERROR_CREATE_DIR 11
int listeSauvegarde(char*** nomSave );
int sauvegarde (char * nom , int * tab, int IA);
int chargement (char *nom , int **tab, int *IA);

View File

@ -11,9 +11,9 @@
void Load_sound (void)
{
param->music_vol = .25 * MIX_MAX_VOLUME;
param->music_vol = .5 * MIX_MAX_VOLUME;
param->music = Mix_LoadMUS ("ressources/theme.mp3");
Mix_VolumeMusic (param->music_vol);
Mix_VolumeMusic (param->music_vol);//param->music_vol);
param->chunk_vol = .25 * MIX_MAX_VOLUME;
printf ("%d\n", param->chunk_vol);

View File

@ -2,30 +2,28 @@
#include "globals.h"
static int taille_ligne = NBSIDE;
int testRecursif(int *tab, int i, int J);
int testCaseDessus(int *tab, int i, int J){
if(tab[i+taille_ligne]==J && testRecursif(tab, i+taille_ligne,J)){//la seconde condition est testée uniquement si la première est vrai donc le testRecursifJ1 sera appelé ssi tab[i+taille_ligne]==J1
if(tab[i+NBSIDE]==J && testRecursif(tab, i+NBSIDE,J)){//la seconde condition est testée uniquement si la première est vrai donc le testRecursifJ1 sera appelé ssi tab[i+NBSIDE]==J1
return 1;
}
return 0;
}
int testCaseDessous(int *tab, int i, int J){
if(i/taille_ligne>0 && tab[i-taille_ligne]==J && testRecursif(tab, i-taille_ligne,J)){
if(i/NBSIDE>0 && tab[i-NBSIDE]==J && testRecursif(tab, i-NBSIDE,J)){
return 1;
}
return 0;
}
int testCaseG(int *tab, int i, int J){
if(i%taille_ligne!=0){
if(i%NBSIDE!=0){
if(tab[i-1]==J && testRecursif(tab, i-1, J)){
return 1;
}
if(tab[i+taille_ligne-1]==J && testRecursif(tab, i+taille_ligne-1, J)){
if(tab[i+NBSIDE-1]==J && testRecursif(tab, i+NBSIDE-1, J)){
return 1;
}
}
@ -33,8 +31,8 @@ int testCaseG(int *tab, int i, int J){
}
int testCaseD(int *tab, int i, int J){
if(i%taille_ligne!=taille_ligne-1){
if(i/taille_ligne>0 && tab[i-taille_ligne+1]==J && testRecursif(tab, i-taille_ligne+1, J)){
if(i%NBSIDE!=NBSIDE-1){
if(i/NBSIDE>0 && tab[i-NBSIDE+1]==J && testRecursif(tab, i-NBSIDE+1, J)){
return 1;
}
if(tab[i+1]==J && testRecursif(tab, i+1, J)){
@ -47,13 +45,13 @@ int testCaseD(int *tab, int i, int J){
int testRecursif(int *tab, int i, int J){// ne peut être appelée qu'avec un indice ou il y a un pion du joueur 1 (J1)
tab[i]=tab[i]+2;
if(J==J1){
if(i/taille_ligne==taille_ligne-1){
if(i/NBSIDE==NBSIDE-1){
return 1;
}
return testCaseDessus(tab,i,J) || testCaseG(tab,i,J) || testCaseD(tab,i,J) || testCaseDessous(tab,i,J);
}
else{
if(i%taille_ligne==taille_ligne-1){
if(i%NBSIDE==NBSIDE-1){
return 1;
}
return testCaseD(tab,i,J) || testCaseDessus(tab,i,J) || testCaseDessous(tab,i,J) || testCaseG(tab,i,J);
@ -64,7 +62,7 @@ int testRecursif(int *tab, int i, int J){// ne peut être appelée qu'avec un in
int testGagne(int *tab, int J){
int i=0, a=0;
if(J==J1){
while(!a && i<taille_ligne){
while(!a && i<NBSIDE){
if(tab[i]==J1){
a=testRecursif(tab, i, J);
//i++;//si une case à été testé la case adjacente aura forcément été testée donc on peut décaler de 2 cases
@ -73,15 +71,15 @@ int testGagne(int *tab, int J){
}
}
else{
while(!a && i<taille_ligne*taille_ligne){
while(!a && i<NBSIDE*NBSIDE){
if(tab[i]==J2){
a=testRecursif(tab, i, J2);
//i=i+taille_ligne;//si une case à été testé la case adjacente aura forcément été testée donc on peut décaler de 2 cases
//i=i+NBSIDE;//si une case à été testé la case adjacente aura forcément été testée donc on peut décaler de 2 cases
}
i=i+taille_ligne;
i=i+NBSIDE;
}
}
for(i=0; i<taille_ligne*taille_ligne; i++){
for(i=0; i<NBSIDE*NBSIDE; i++){
if(tab[i]>J2){
tab[i]=tab[i]-2;
}