Skip to content
Snippets Groups Projects
Commit ec8624db authored by vautrin33u's avatar vautrin33u
Browse files

Pack de sprite de tests affichés dans jeu / changement sprite_t avec SDL_rect...

Pack de sprite de tests affichés dans jeu / changement sprite_t avec SDL_rect / fusion et suppression code source de gestion de SDL avec graphisme.c
parent 5c1d20fd
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,8 @@ LDFLAGS = `sdl2-config --cflags --libs` -lSDL2_ttf ...@@ -5,8 +5,8 @@ LDFLAGS = `sdl2-config --cflags --libs` -lSDL2_ttf
INCLUDES = -I./SDL2_ttf INCLUDES = -I./SDL2_ttf
EXEC1 = main EXEC1 = main
EXEC2 = tests EXEC2 = tests
SRC = logique.c gestion_fichiers.c graphisme.c fonctions_SDL.c main.c SRC = logique.c gestion_fichiers.c graphisme.c main.c
SRC2 = logique.c gestion_fichiers.c graphisme.c fonctions_SDL.c tests.c SRC2 = logique.c gestion_fichiers.c graphisme.c tests.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
OBJ2 = $(SRC2:.c=.o) OBJ2 = $(SRC2:.c=.o)
......
Ressources/compt0.bmp

29.3 KiB

Ressources/envoi0.bmp

14.7 KiB

Ressources/fond1.bmp

3.43 MiB | W: | H:

Ressources/fond1.bmp

1.37 MiB | W: | H:

Ressources/fond1.bmp
Ressources/fond1.bmp
Ressources/fond1.bmp
Ressources/fond1.bmp
  • 2-up
  • Swipe
  • Onion skin
Ressources/four0.bmp

44.2 KiB

Ressources/frigo0.bmp

44.2 KiB

No preview for this file type
Ressources/lavabo0.bmp

44.2 KiB

Ressources/trash0.bmp

29.3 KiB

/**
* \file fonctions_SDL.c
* \brief Fichier de gestion de l'affichage, du texte
* \author Guillaume Vautrin
* \version 1.0
*/
#include "fonctions_SDL.h"
SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer){
//Charge une image et retourne la surface de texture associée
SDL_Surface * image = SDL_LoadBMP(nomfichier);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
return texture;
}
SDL_Texture* charger_image_transparente(const char* nomfichier, SDL_Renderer* renderer, Uint8 r, Uint8 g, Uint8 b){
SDL_Surface * image = SDL_LoadBMP(nomfichier);
SDL_SetColorKey(image, SDL_TRUE, SDL_MapRGB(image->format, r, g, b));
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
return texture;
}
//int SDL_SetColorKey(SDL_Surface* surface, int flag, Uint32 key) ; // défini la couleur transparente dans une surface
//Uint32 SDL_MapRGB(const SDL_PixelFormat* format, Uint8 r, Uint8 g, Uint8 b); //map les couleurs d'une bitmap
SDL_Texture* charger_texte (const char* message, SDL_Renderer* renderer, TTF_Font *font, SDL_Color color){
SDL_Surface* texte = TTF_RenderText_Blended(font, message, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, texte);
SDL_FreeSurface(texte);
return texture;
}
void render_texte(SDL_Renderer *renderer, int x, int y, int w, int h, SDL_Texture* texte){
SDL_Rect rect = {x, y, w, h};
SDL_RenderCopy(renderer, texte, NULL, &rect);
}
/*
TTF_Font *TTF_OpenFont(const char *file, int size) ; //charge la police et applique la taille du texte
// Écrire le texte sur une surface SDL
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg) ;
// Fermer la police
void TTF_CloseFont(TTF_Font *font) ;
*/
\ No newline at end of file
/**
* \file fonctions_SDL.h
* \brief header des fonctions SDL
* \author Guillaume Vautrin
* \version 1.0
*/
#ifndef FONCTIONS_SDL_H
#define FONCTIONS_SDL_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer);
SDL_Texture* charger_image_transparente(const char* nomfichier, SDL_Renderer* renderer, Uint8 r, Uint8 g, Uint8 b) ;
SDL_Texture* charger_texte (const char* message, SDL_Renderer* renderer, TTF_Font *font, SDL_Color color);
void render_texte(SDL_Renderer *renderer, int x, int y, int w, int h, SDL_Texture* texte);
#endif
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
char** allouer_tab_2D (int lig, int col){ char** allouer_tab_2D (int lig, int col){
//Allocation d'un tableau de caractère à 2 dimensions
char ** tab = malloc(lig * sizeof(char *)); char ** tab = malloc(lig * sizeof(char *));
for(int i=0; i<lig; i++){ for(int i=0; i<lig; i++){
tab[i] = malloc(col * sizeof(char)); tab[i] = malloc(col * sizeof(char));
...@@ -18,6 +19,7 @@ char** allouer_tab_2D (int lig, int col){ ...@@ -18,6 +19,7 @@ char** allouer_tab_2D (int lig, int col){
} }
void desallouer_tab_2D (char** tab, int lig){ void desallouer_tab_2D (char** tab, int lig){
//désallocation d'un tab de car 2D
for (int i=0; i<lig; i++){ for (int i=0; i<lig; i++){
free(tab[i]); free(tab[i]);
} }
...@@ -25,6 +27,7 @@ void desallouer_tab_2D (char** tab, int lig){ ...@@ -25,6 +27,7 @@ void desallouer_tab_2D (char** tab, int lig){
} }
void afficher_tab_2D (char** tab, int lig, int col){ void afficher_tab_2D (char** tab, int lig, int col){
//Affiche le tableau 2D
for (int i=0; i<lig; i++){ for (int i=0; i<lig; i++){
for (int j=0; j<col; j++){ for (int j=0; j<col; j++){
printf("%c", tab[i][j]); printf("%c", tab[i][j]);
...@@ -36,6 +39,7 @@ void afficher_tab_2D (char** tab, int lig, int col){ ...@@ -36,6 +39,7 @@ void afficher_tab_2D (char** tab, int lig, int col){
} }
void taille_fichier (const char* nomFichier, int* nbLig, int* nbCol){ void taille_fichier (const char* nomFichier, int* nbLig, int* nbCol){
//Donne la taille d'un fichier texte (colonne et ligne)
char test[3000]; //Stocke les caractères d'une ligne char test[3000]; //Stocke les caractères d'une ligne
int ligne = 0; // nb de lignes int ligne = 0; // nb de lignes
int colonne =0; // nb de colonnes int colonne =0; // nb de colonnes
...@@ -60,6 +64,7 @@ void taille_fichier (const char* nomFichier, int* nbLig, int* nbCol){ ...@@ -60,6 +64,7 @@ void taille_fichier (const char* nomFichier, int* nbLig, int* nbCol){
} }
char** lire_fichier (const char* nomFichier){ char** lire_fichier (const char* nomFichier){
//Traduit le contenu d'un fichier dans un tableau de char
FILE* fichier = fopen(nomFichier, "r"); FILE* fichier = fopen(nomFichier, "r");
if (fichier == NULL){ if (fichier == NULL){
printf("Erreur: fichier inaccessible \n"); printf("Erreur: fichier inaccessible \n");
...@@ -93,6 +98,7 @@ char** lire_fichier (const char* nomFichier){ ...@@ -93,6 +98,7 @@ char** lire_fichier (const char* nomFichier){
} }
void genere_fichier (const char* nomFichier, char** source, int ligne, int colonne){ void genere_fichier (const char* nomFichier, char** source, int ligne, int colonne){
//Génère un fichier a partir d'un tableau de caractère
FILE* fichier = NULL; FILE* fichier = NULL;
fichier = fopen(nomFichier, "w"); fichier = fopen(nomFichier, "w");
if (fichier !=NULL){ if (fichier !=NULL){
......
...@@ -15,7 +15,25 @@ void clean_textures(textures_t *textures){ ...@@ -15,7 +15,25 @@ void clean_textures(textures_t *textures){
if (textures->fond != NULL){ if (textures->fond != NULL){
SDL_DestroyTexture(textures->fond); SDL_DestroyTexture(textures->fond);
} }
if (textures->comptoire != NULL){
SDL_DestroyTexture(textures->comptoire);
}
if (textures->envoi[0] != NULL){
SDL_DestroyTexture(textures->envoi[0]);
}
if (textures->frigo[0] != NULL){
SDL_DestroyTexture(textures->frigo[0]);
}
if (textures->four[0] != NULL){
SDL_DestroyTexture(textures->four[0]);
}
if (textures->lavabo[0] != NULL){
SDL_DestroyTexture(textures->lavabo[0]);
}
if (textures->poubelle != NULL){
SDL_DestroyTexture(textures->poubelle);
}
} }
void nettoyage_graphisme (SDL_Renderer *renderer, textures_t *textures, SDL_Window *fenetre){ void nettoyage_graphisme (SDL_Renderer *renderer, textures_t *textures, SDL_Window *fenetre){
...@@ -27,6 +45,13 @@ void nettoyage_graphisme (SDL_Renderer *renderer, textures_t *textures, SDL_Wind ...@@ -27,6 +45,13 @@ void nettoyage_graphisme (SDL_Renderer *renderer, textures_t *textures, SDL_Wind
void init_textures (SDL_Renderer *renderer, textures_t *textures, TTF_Font *font){ void init_textures (SDL_Renderer *renderer, textures_t *textures, TTF_Font *font){
textures->fond = charger_image("Ressources/fond1.bmp", renderer); textures->fond = charger_image("Ressources/fond1.bmp", renderer);
textures->joueur = charger_image ("Ressources/joueur.bmp", renderer); textures->joueur = charger_image ("Ressources/joueur.bmp", renderer);
textures->comptoire = charger_image ("Ressources/compt0.bmp", renderer);
textures->envoi[0] = charger_image ("Ressources/envoi0.bmp", renderer);
textures->frigo[0] = charger_image ("Ressources/frigo0.bmp", renderer);
textures->four[0] = charger_image ("Ressources/four0.bmp", renderer);
textures->poubelle = charger_image ("Ressources/trash0.bmp", renderer);
textures->lavabo[0] = charger_image ("Ressources/lavabo0.bmp", renderer);
// SDL_Color color = { 255, 0, 255 }; // SDL_Color color = { 255, 0, 255 };
//... = charger_texte ("Score : ", renderer, font, color); //... = charger_texte ("Score : ", renderer, font, color);
} }
...@@ -37,20 +62,26 @@ void apply_background (SDL_Renderer *renderer, textures_t *textures){ ...@@ -37,20 +62,26 @@ void apply_background (SDL_Renderer *renderer, textures_t *textures){
} }
} }
void apply_sprite (SDL_Renderer *renderer, SDL_Texture *image, sprite_t *sprite){
// applique la texture d'un sprite
SDL_Rect SrC = create_SDL_rect_from_texture(image);
SDL_Rect Des = create_SDL_rect_from_sprite(sprite);
SDL_RenderCopy(renderer, image, &SrC , &Des);
}
void apply_graphics (SDL_Renderer *renderer, textures_t *textures, world_t *world){ void apply_graphics (SDL_Renderer *renderer, textures_t *textures, world_t *world){
//maj les texture selon la position des éléments différents //maj les texture selon la position des éléments différents
if (textures->joueur !=NULL){ if (textures->joueur !=NULL){
apply_sprite (renderer, textures->joueur, &world->joueur); SDL_RenderCopy(renderer, textures->joueur, NULL, &world->joueur.rect);
}
if (textures->poubelle !=NULL){
SDL_RenderCopy(renderer, textures->poubelle, NULL, &world->poubelle.rect);
}
if (textures->lavabo[0] !=NULL){
SDL_RenderCopy(renderer, textures->lavabo[0], NULL, &world->lavabo.rect);
}
if (textures->frigo[0] !=NULL){
SDL_RenderCopy(renderer, textures->frigo[0], NULL, &world->frigo.rect);
}
if (textures->four[0] !=NULL){
SDL_RenderCopy(renderer, textures->four[0], NULL, &world->four.rect);
}
if (textures->envoi[0] !=NULL){
SDL_RenderCopy(renderer, textures->envoi[0], NULL, &world->envoi.rect);
} }
} }
...@@ -62,7 +93,7 @@ void colorier_rect (SDL_Renderer *renderer, SDL_Rect rectangle, SDL_Color couleu ...@@ -62,7 +93,7 @@ void colorier_rect (SDL_Renderer *renderer, SDL_Rect rectangle, SDL_Color couleu
} }
} }
} }
/*
SDL_Rect create_SDL_rect_from_texture (SDL_Texture * texture){ SDL_Rect create_SDL_rect_from_texture (SDL_Texture * texture){
//Permet de créer un SDL_rect retournant la texture de la taille de la texture (SOURCE) //Permet de créer un SDL_rect retournant la texture de la taille de la texture (SOURCE)
int w; int w;
...@@ -80,11 +111,7 @@ SDL_Rect create_SDL_rect (int x, int y, int w, int h){ ...@@ -80,11 +111,7 @@ SDL_Rect create_SDL_rect (int x, int y, int w, int h){
rectangle.h = h; rectangle.h = h;
return rectangle; return rectangle;
} }
*/
SDL_Rect create_SDL_rect_from_sprite (sprite_t* sprite){
//Permet de créer un SDL_rect basé sur les données d'un sprite
return create_SDL_rect (sprite->x, sprite->y, sprite->w, sprite->h);
}
void update_graphics (SDL_Renderer *renderer, world_t *world, textures_t *textures){ void update_graphics (SDL_Renderer *renderer, world_t *world, textures_t *textures){
...@@ -93,4 +120,45 @@ void update_graphics (SDL_Renderer *renderer, world_t *world, textures_t *textur ...@@ -93,4 +120,45 @@ void update_graphics (SDL_Renderer *renderer, world_t *world, textures_t *textur
apply_graphics(renderer, textures, world); apply_graphics(renderer, textures, world);
SDL_RenderPresent(renderer); // Récupère les infos actualisés de render et les affiches SDL_RenderPresent(renderer); // Récupère les infos actualisés de render et les affiches
} }
\ No newline at end of file
SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer){
//Charge une image et retourne la surface de texture associée
SDL_Surface * image = SDL_LoadBMP(nomfichier);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
return texture;
}
SDL_Texture* charger_image_transparente(const char* nomfichier, SDL_Renderer* renderer, Uint8 r, Uint8 g, Uint8 b){
SDL_Surface * image = SDL_LoadBMP(nomfichier);
SDL_SetColorKey(image, SDL_TRUE, SDL_MapRGB(image->format, r, g, b));
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, image);
SDL_FreeSurface(image);
return texture;
}
//int SDL_SetColorKey(SDL_Surface* surface, int flag, Uint32 key) ; // défini la couleur transparente dans une surface
//Uint32 SDL_MapRGB(const SDL_PixelFormat* format, Uint8 r, Uint8 g, Uint8 b); //map les couleurs d'une bitmap
SDL_Texture* charger_texte (const char* message, SDL_Renderer* renderer, TTF_Font *font, SDL_Color color){
SDL_Surface* texte = TTF_RenderText_Blended(font, message, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, texte);
SDL_FreeSurface(texte);
return texture;
}
void render_texte(SDL_Renderer *renderer, int x, int y, int w, int h, SDL_Texture* texte){
SDL_Rect rect = {x, y, w, h};
SDL_RenderCopy(renderer, texte, NULL, &rect);
}
/*
TTF_Font *TTF_OpenFont(const char *file, int size) ; //charge la police et applique la taille du texte
// Écrire le texte sur une surface SDL
SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg) ;
// Fermer la police
void TTF_CloseFont(TTF_Font *font) ;
*/
\ No newline at end of file
...@@ -9,13 +9,19 @@ ...@@ -9,13 +9,19 @@
#ifndef GRAPHISME_H #ifndef GRAPHISME_H
#define GRAPHISME_H #define GRAPHISME_H
#include "fonctions_SDL.h"
#include "logique.h" #include "logique.h"
#include "constantes.h" #include "constantes.h"
struct textures_s{ struct textures_s{
SDL_Texture* fond; /*!< Texture liée à l'image du fond de l'écran. */ SDL_Texture* fond; /*!< Texture liée à l'image du fond de l'écran. */
SDL_Texture* joueur ; /* Texture du joueur*/ SDL_Texture* joueur ; /* Texture du joueur*/
SDL_Texture* comptoire ; /*< Texture des comptoires */
SDL_Texture* envoi[1] ; /*< Tableau de texture de la zone d'envoi */
SDL_Texture* four[1] ; /*< Tableau de texture du four */
SDL_Texture* lavabo[1] ; /*< Tableau de texture du frigo */
SDL_Texture* poubelle ; /*< Texture de la poubelle */
SDL_Texture* frigo[1] ; /*< Tableau de texture du frigo */
//TTF_Font* police; /*Texture de la police d'écriture*/ //TTF_Font* police; /*Texture de la police d'écriture*/
}; };
/** /**
...@@ -25,6 +31,14 @@ struct textures_s{ ...@@ -25,6 +31,14 @@ struct textures_s{
typedef struct textures_s textures_t; typedef struct textures_s textures_t;
SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer);
SDL_Texture* charger_image_transparente(const char* nomfichier, SDL_Renderer* renderer, Uint8 r, Uint8 g, Uint8 b) ;
SDL_Texture* charger_texte (const char* message, SDL_Renderer* renderer, TTF_Font *font, SDL_Color color);
void render_texte(SDL_Renderer *renderer, int x, int y, int w, int h, SDL_Texture* texte);
/** /**
* \brief La fonction nettoie les textures * \brief La fonction nettoie les textures
* \param textures les textures * \param textures les textures
......
...@@ -27,7 +27,7 @@ void init_data(world_t * world){ //faux ...@@ -27,7 +27,7 @@ void init_data(world_t * world){ //faux
build_sprite_unique(&world->lavabo, 'L', LAVABO_LARGEUR, LAVABO_HAUTEUR, world->map); build_sprite_unique(&world->lavabo, 'L', LAVABO_LARGEUR, LAVABO_HAUTEUR, world->map);
build_sprite_unique(&world->poubelle, 't', POUBELLE_LARGEUR, POUBELLE_HAUTEUR, world->map); build_sprite_unique(&world->poubelle, 't', POUBELLE_LARGEUR, POUBELLE_HAUTEUR, world->map);
build_sprite_unique(&world->envoi, 'e', ZONE_ENVOI_LARGEUR, ZONE_ENVOI_HAUTEUR, world->map); build_sprite_unique(&world->envoi, 'e', ZONE_ENVOI_LARGEUR, ZONE_ENVOI_HAUTEUR, world->map);
build_sprites_tab(&world, 'X', BLOC_COMPT_SIZE, BLOC_COMPT_SIZE); //build_sprites_tab(&world, 'X', BLOC_COMPT_SIZE, BLOC_COMPT_SIZE);
/* /*
...@@ -72,13 +72,14 @@ void build_sprite_unique (sprite_t *sprite, char nom, int largeur, int hauteur, ...@@ -72,13 +72,14 @@ void build_sprite_unique (sprite_t *sprite, char nom, int largeur, int hauteur,
//Gestion des positions possibles. //Gestion des positions possibles.
if (comptX == largeur && comptY == hauteur){ if (comptX == largeur && comptY == hauteur){
init_sprite(sprite, nom, x, y, largeur, hauteur, 0, 0); init_sprite(sprite, nom, x, y+1-hauteur, largeur, hauteur, 0, 0);
} }
if (comptX == hauteur && comptY == largeur){ if (comptX == hauteur && comptY == largeur){
init_sprite(sprite, nom, x, y, hauteur, largeur, 0, 0); init_sprite(sprite, nom, x, y+1-largeur, hauteur, largeur, 0, 0);
} }
printf ("\n Sprite : %c, x: %d, y: %d", sprite->nom, sprite->rect.x, sprite->rect.y);
} }
/*
void build_sprites_tab (world_t* world, char nomSprite, int largeur, int hauteur){ void build_sprites_tab (world_t* world, char nomSprite, int largeur, int hauteur){
for (int i=0; i<SCREEN_HEIGHT; i++){ for (int i=0; i<SCREEN_HEIGHT; i++){
...@@ -86,14 +87,14 @@ void build_sprites_tab (world_t* world, char nomSprite, int largeur, int hauteur ...@@ -86,14 +87,14 @@ void build_sprites_tab (world_t* world, char nomSprite, int largeur, int hauteur
//Parcours du tableau pour repérer le premier caractère d'un //Parcours du tableau pour repérer le premier caractère d'un
} }
} }
} }*/
void init_sprite(sprite_t *sprite, char nom, int x, int y, int w, int h, int v, int state){ //faux void init_sprite(sprite_t *sprite, char nom, int x, int y, int w, int h, int v, int state){
//La fonction initialise tous les composants d'un sprite //La fonction initialise tous les composants d'un sprite
sprite->x = x; sprite->rect.x = x;
sprite->y = y; sprite->rect.y = y;
sprite->w = w; sprite->rect.w = w;
sprite->h = h; sprite->rect.h = h;
sprite->v = v; sprite->v = v;
sprite->etat = state; sprite->etat = state;
sprite->is_visible = 0; sprite->is_visible = 0;
...@@ -145,34 +146,34 @@ int estFini(world_t *world){ ...@@ -145,34 +146,34 @@ int estFini(world_t *world){
return world->gameover; return world->gameover;
} }
void limites_externes (sprite_t *sprite, world_t *world){ void limites_externes (sprite_t *sprite){
//Défini les limites selon les murs //Défini les limites selon les murs
if (sprite->x + sprite->w > SCREEN_WIDTH-SIZE_MUR){ if (sprite->rect.x + sprite->rect.w > SCREEN_WIDTH-SIZE_MUR){
//mur droite //mur droite
sprite->x = SCREEN_WIDTH - (SIZE_MUR + sprite->w); sprite->rect.x = SCREEN_WIDTH - (SIZE_MUR + sprite->rect.w);
} }
if (sprite->x < SIZE_MUR){ if (sprite->rect.x < SIZE_MUR){
//mur gauche //mur gauche
sprite->x = SIZE_MUR; sprite->rect.x = SIZE_MUR;
} }
if (sprite->y + sprite->h > SCREEN_HEIGHT-SIZE_MUR){ if (sprite->rect.y + sprite->rect.h > SCREEN_HEIGHT-SIZE_MUR){
//mur bas //mur bas
sprite->y = SCREEN_HEIGHT - (SIZE_MUR + sprite->h); sprite->rect.y = SCREEN_HEIGHT - (SIZE_MUR + sprite->rect.h);
} }
if (sprite->y < SIZE_MUR){ if (sprite->rect.y < SIZE_MUR){
//mur haut //mur haut
sprite->y = SIZE_MUR; sprite->rect.y = SIZE_MUR;
} }
} }
void reset_sprite_on_map (sprite_t *sprite, world_t *world){ void reset_sprite_on_map (sprite_t *sprite, world_t *world){
//On efface la position du joueur sur map //On efface la position du joueur sur map
for (int i=sprite->y; i < sprite->y + sprite->h ; i++){ for (int i=sprite->rect.y; i < sprite->rect.y + sprite->rect.h ; i++){
for (int j = sprite->x ; j < sprite->x + sprite->w ; j++){ for (int j = sprite->rect.x ; j < sprite->rect.x + sprite->rect.w ; j++){
if (world->map[i][j]== sprite->nom){ if (world->map[i][j]== sprite->nom){
world->map[i][j] = ' '; world->map[i][j] = ' ';
} }
...@@ -182,87 +183,13 @@ void reset_sprite_on_map (sprite_t *sprite, world_t *world){ ...@@ -182,87 +183,13 @@ void reset_sprite_on_map (sprite_t *sprite, world_t *world){
void place_sprite_on_map (sprite_t *sprite, world_t *world){ void place_sprite_on_map (sprite_t *sprite, world_t *world){
//on écrit la position du joueur sur la map //on écrit la position du joueur sur la map
for (int i=sprite->y; i < sprite->y + sprite->h ; i++){ for (int i=sprite->rect.y; i < sprite->rect.y + sprite->rect.h ; i++){
for (int j = sprite->x ; j < sprite->x + sprite->w ; j++){ for (int j = sprite->rect.x ; j < sprite->rect.x + sprite->rect.w ; j++){
world->map[i][j]= sprite->nom; world->map[i][j]= sprite->nom;
} }
} }
} }
void deplace_bas (sprite_t *sprite, world_t *world){
int compteur =0;
set_bas(&world->joueur);
world->joueur.y += world->joueur.v;
reset_sprite_on_map(sprite, world);
if (world->map[sprite->y + SIZE_JOUEUR][sprite->x] != ' ' || world->map[sprite->y + SIZE_JOUEUR][sprite->x + SIZE_JOUEUR] != ' '){
//Compter le nombre d'espace avant d'arriver au bloc, et placer le joueur contre le bloc interdit
for (int y= sprite->y; y<SIZE_JOUEUR; y++){
if (world->map[y][sprite->x] == ' '){
compteur++;
}
}
sprite->x -= compteur;
}
}
void deplace_haut (sprite_t *sprite, world_t *world){
int compteur =0;
set_haut(&world->joueur);
world->joueur.y -= world->joueur.v;
reset_sprite_on_map(sprite, world);
//déplace vers le haut
if (world->map[sprite->y][sprite->x] != ' ' || world->map[sprite->y][sprite->x + SIZE_JOUEUR] != ' '){
//Compter le nombre d'espace avant d'arriver au bloc, et placer le joueur contre le bloc interdit
for (int y= sprite->y; y<SIZE_JOUEUR; y++){
if (world->map[y][sprite->x] != ' '){
compteur++;
}
}
sprite->x += compteur;
}
}
void deplace_droite (sprite_t *sprite, world_t *world){
int compteur =0;
set_droite(&world->joueur);
world->joueur.x += world->joueur.v;
reset_sprite_on_map(sprite, world);
//déplace vers la droite
if (world->map[sprite->y][sprite->x + SIZE_JOUEUR] != ' ' || world->map[sprite->y + SIZE_JOUEUR][sprite->x + SIZE_JOUEUR] != ' '){
//Compter le nombre d'espace avant d'arriver au bloc, et placer le joueur contre le bloc interdit
for (int x= sprite->x; x<SIZE_JOUEUR; x++){
if (world->map[sprite->y][x] == ' '){
compteur++;
}
}
sprite->x -= compteur;
}
}
void deplace_gauche (sprite_t *sprite, world_t *world){
int compteur =0;
set_gauche(sprite);
sprite->x -= sprite->v;
reset_sprite_on_map(sprite, world);
//déplace vers la gauche
if (world->map[sprite->y][sprite->x] != ' ' || world->map[sprite->y + sprite->h][sprite->x] != ' '){
//Compter le nombre d'espace avant d'arriver au bloc, et placer le joueur contre le bloc interdit
for (int x= sprite->x; x<sprite->w; x++){
if (world->map[sprite->y][x] != ' '){
compteur++;
}
}
sprite->x += compteur;
}
}
void gestion_events(SDL_Event *event, world_t *world){ void gestion_events(SDL_Event *event, world_t *world){
SDL_PollEvent(event); SDL_PollEvent(event);
...@@ -275,22 +202,22 @@ void gestion_events(SDL_Event *event, world_t *world){ ...@@ -275,22 +202,22 @@ void gestion_events(SDL_Event *event, world_t *world){
world->gameover = 1; break; world->gameover = 1; break;
//Déplacement du joueur //Déplacement du joueur
case SDLK_UP: case SDLK_UP:
world->joueur.y -= world->joueur.v; world->joueur.rect.y -= world->joueur.v;
//deplace_haut(&world->joueur, world); //deplace_haut(&world->joueur, world);
break; break;
case SDLK_DOWN: case SDLK_DOWN:
world->joueur.y += world->joueur.v; world->joueur.rect.y += world->joueur.v;
//deplace_bas(&world->joueur, world); //deplace_bas(&world->joueur, world);
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
world->joueur.x += world->joueur.v; world->joueur.rect.x += world->joueur.v;
//deplace_droite(&world->joueur, world); //deplace_droite(&world->joueur, world);
break; break;
case SDLK_LEFT: case SDLK_LEFT:
world->joueur.x -= world->joueur.v; world->joueur.rect.x -= world->joueur.v;
//deplace_gauche(&world->joueur, world); //deplace_gauche(&world->joueur, world);
break; break;
//case SDLK_SPACE: //case SDLK_SPACE:
...@@ -351,7 +278,7 @@ char** init_map (){ ...@@ -351,7 +278,7 @@ char** init_map (){
void update_data (world_t *world){ void update_data (world_t *world){
const char *nomSave = "Ressources/save.txt"; const char *nomSave = "Ressources/save.txt";
limites_externes(&world->joueur, world); limites_externes(&world->joueur);
place_sprite_on_map(&world->joueur, world); place_sprite_on_map(&world->joueur, world);
//printf("Sprite joueur : x : %d | y: %d \n", world->joueur.x, world->joueur.y); //printf("Sprite joueur : x : %d | y: %d \n", world->joueur.x, world->joueur.y);
genere_fichier(nomSave, world->map, SCREEN_HEIGHT, SCREEN_WIDTH); genere_fichier(nomSave, world->map, SCREEN_HEIGHT, SCREEN_WIDTH);
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#ifndef LOGIQUE_H #ifndef LOGIQUE_H
#define LOGIQUE_H #define LOGIQUE_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
...@@ -23,13 +25,10 @@ ...@@ -23,13 +25,10 @@
*/ */
struct sprite_s{ struct sprite_s{
char nom; /*!< char du sprite utilisé sur map*/ char nom; /*!< char du sprite utilisé sur map*/
int x; /*!< abscisse du centre du sprite*/
int y; /*!< ordonnée du centre du sprite*/
int h; /*!< hauteur du sprite*/
int w; /*!< largeur du sprite*/
int v; /*!< vitesse du sprite*/ int v; /*!< vitesse du sprite*/
int etat; /*!< état du sprite; permet de varier son apparence */ int etat; /*!< état du sprite; permet de varier son apparence */
int is_visible; /*!< Gère la visibilité d'un sprite; 0 si visible, i sinon */ int is_visible; /*!< Gère la visibilité d'un sprite; 0 si visible, i sinon */
SDL_Rect rect; /*!< structure de position du sprite*/
}; };
/** /**
...@@ -181,39 +180,7 @@ int return_state (sprite_t *sprite); ...@@ -181,39 +180,7 @@ int return_state (sprite_t *sprite);
* @param sprite sprite pour lequel on test les limites * @param sprite sprite pour lequel on test les limites
* @param world données du monde * @param world données du monde
*/ */
void limites_externes (sprite_t *sprite, world_t *world); void limites_externes (sprite_t *sprite);
/**
* @brief Déplacement d'un sprite vers la gauche + limites
*
* @param sprite sprite qui se déplace
* @param world données du monde
*/
void deplace_gauche (sprite_t *sprite, world_t *world);
/**
* @brief Déplacement d'un sprite vers la droite + limites
*
* @param sprite sprite qui se déplace
* @param world données du monde
*/
void deplace_droite (sprite_t *sprite, world_t *world);
/**
* @brief Déplacement d'un sprite vers le haut + limites
*
* @param sprite sprite qui se déplace
* @param world données du monde
*/
void deplace_haut (sprite_t *sprite, world_t *world);
/**
* @brief Déplacement d'un sprite vers le bas + limites
*
* @param sprite sprite qui se déplace
* @param world données du monde
*/
void deplace_bas (sprite_t *sprite, world_t *world);
/** /**
* @brief Retire le sprite de la map - avant maj d'un déplacement * @brief Retire le sprite de la map - avant maj d'un déplacement
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "logique.h" #include "logique.h"
#include "constantes.h" #include "constantes.h"
#include "gestion_fichiers.h" #include "gestion_fichiers.h"
#include "fonctions_SDL.h"
#include "graphisme.h" #include "graphisme.h"
#include "math.h" #include "math.h"
#include <time.h> #include <time.h>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment