diff --git a/Makefile b/Makefile index 7d92f84b231b2e49011862d770d61ff9edece1eb..aa941ec2d823bf88d0508eb5d3bbf26e2a55179c 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ LIBS = -L /SDL2_ttf/.libs -L /SDL2_image/.libs LDFLAGS = `sdl2-config --cflags --libs` INCLUDES = -I ./SDL2_ttf -I ./SDL2_image EXEC = main -SRC = main.c player.c map.c textures.c data.c +SRC = main.c player.c map.c graphics.c OBJ = $(SRC:.c=.o) all: $(EXEC) diff --git a/data.c b/data.c deleted file mode 100644 index 7ceb10d5f405eb20dfc5147c922c3e1fc1b85c8a..0000000000000000000000000000000000000000 --- a/data.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "data.h" - -walls_t* init_wall(int px,int py,int postx,int posty){ - walls_t* walls= malloc(sizeof(walls_t)); - walls->bricks.w=BRICKW; - walls->bricks.h=BRICKH; - walls->bricks.x=BRICKW*postx; - walls->bricks.y=BRICKH*posty; - walls->Src.x=BRICKW*px; - walls->Src.y=BRICKH*py; - walls->Src.w=BRICKW; - walls->Src.h=BRICKH; - return walls; -} \ No newline at end of file diff --git a/data.h b/data.h deleted file mode 100644 index bf755b22674d5cf93add5eae9f03ee9670414002..0000000000000000000000000000000000000000 --- a/data.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef DATA_H -#define DATA_H - -#include "player.h" - -#define SCREENH 900 -#define SCREENW 600 -#define BRICKH 32 -#define BRICKW 32 - -typedef struct walls_s -{ - SDL_Rect bricks; - SDL_Rect Src; - SDL_Texture* texture; -}walls_t; - - -walls_t* init_wall(int px,int py,int postx,int posty); - -#endif \ No newline at end of file diff --git a/data.o b/data.o deleted file mode 100644 index c3d9e8eb6c42ee6f4a9633cad815151659319c94..0000000000000000000000000000000000000000 Binary files a/data.o and /dev/null differ diff --git a/main b/main index b21f89a0c997f3903bb15d701406f27fef76937d..063774eaaee2dac64da7908e129236424fe9fe55 100644 Binary files a/main and b/main differ diff --git a/main.c b/main.c index f632906bd014abb3e38fca98e900995e63b8b907..aa1cbc279dcb766f606dda2663d5d760a418f79a 100644 --- a/main.c +++ b/main.c @@ -1,97 +1,36 @@ -#include "data.h" +#include "graphics.h" + int main() { - int nbl = 0; - int nbc = 0; - char** map =lire_map("ressources/maze_map.txt"); + int nbl = 0,nbc = 0; + char** map =lire_map("ressources/maze_map.txt");//lire le fichier taille_map("ressources/maze_map.txt",&nbl,&nbc); - player_t*player = init_player(1,1); + player_t*player = init_player(0,2);//initialisation du joueur SDL_Window* fenetre; // Déclaration de la fenêtre - walls_t* walls; - bool terminer=false; - if(SDL_Init(SDL_INIT_VIDEO) < 0) // Initialisation de la SDL - { - printf("Erreur d’initialisation de la SDL: %s",SDL_GetError()); - SDL_Quit(); - return EXIT_FAILURE; - } - // Créer la fenêtre - fenetre = SDL_CreateWindow("Fenetre SDL", SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, 700, 700, SDL_WINDOW_RESIZABLE); - if(fenetre == NULL) // En cas d’erreur - { - printf("Erreur de la creation d’une fenetre: %s",SDL_GetError()); - SDL_Quit(); - return EXIT_FAILURE; - } - - + walls_t* walls=allocation_Walls(); SDL_Renderer* ecran; - ecran = SDL_CreateRenderer(fenetre, -1, SDL_RENDERER_ACCELERATED); + //initialisation du renderer et la fenetre + init_sdl(&fenetre,&ecran,SCREENW,SCREENH); // Charger l’images - SDL_Texture* sprite= charger_image("ressources/pavage.bmp",ecran); - player->texture= charger_image("ressources/personnage.bmp",ecran); - SDL_Event event; + apply_texture(walls,ecran); + SDL_Event event; // Boucle principale - while(!terminer){ - while (SDL_PollEvent(&event)){ - if (event.type ==SDL_QUIT){ - terminer = true; - } - - //si une touche est appuyée - if(event.type == SDL_KEYDOWN){ - if (event.key.keysym.sym==SDLK_ESCAPE){ //si on appui sur la touche Echap, on quitte le jeu. - terminer= true; - } - if(event.key.keysym.sym == SDLK_LEFT){ - movement(map,player,'q'); // fléche vers la gauche - change_movement_player(player,1,1); - } - if(event.key.keysym.sym == SDLK_RIGHT){ - movement(map,player,'d'); // fléche vers la droite - change_movement_player(player,1,2); - } - if(event.key.keysym.sym == SDLK_UP){ - movement(map,player,'z');// fléche vers le haut - change_movement_player(player,1,3); - } - if(event.key.keysym.sym == SDLK_DOWN){ - movement(map,player,'s');// fléche vers le bas - change_movement_player(player,1,0); - } - } - - } + while(!player->gameover){ + handle_events(&event,player,map); + gamrover(player,map,nbc,nbl); SDL_RenderClear(ecran); - for (int i=0;i<nbl;i++){ - for (int j = 0; j< nbc; j++) - { - if(map[i][j]=='L'){ - walls=init_wall(j,i,9,3); - SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src)); - - } - else if(map[i][j]=='#'){ - walls=init_wall(j,i,10,3); - SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src)); - } - else if (map[i][j]=='S'){ - walls=init_wall(j,i,13,3); - SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src)); - } - } - } - SDL_RenderCopy(ecran,player->texture,&(player->DestR),&(player->Src)); + affichage_bricks(ecran,walls,map,nbc,nbl); + SDL_RenderCopy(ecran,walls->texture_joueur,&(player->DestR),&(player->Src)); SDL_RenderPresent(ecran); SDL_UpdateWindowSurface(fenetre); } // Libérer de la mémoire SDL_DestroyRenderer(ecran); - SDL_DestroyTexture(sprite); desallouer_tab_2D(map,nbl); free_player(player); + clean_walls(walls); // Quitter SDL SDL_DestroyWindow(fenetre); SDL_Quit(); diff --git a/main.o b/main.o deleted file mode 100644 index 08c29c80ae1ab08815cefa7486f4e5fa6340075c..0000000000000000000000000000000000000000 Binary files a/main.o and /dev/null differ diff --git a/map.h b/map.h index cda2856670c79f75818bad3ca2995e7dec89aff8..f1bf10beef158b0fd52f38b381dbcd9eb9181566 100644 --- a/map.h +++ b/map.h @@ -1,6 +1,10 @@ #ifndef MAP_H #define MAP_H -#include "textures.h" + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <SDL2/SDL.h> char** allouer_tab_2D(int n,int m); diff --git a/map.o b/map.o deleted file mode 100644 index d34eb32d16bdece090fccb4237a5d13e1fe5c84d..0000000000000000000000000000000000000000 Binary files a/map.o and /dev/null differ diff --git a/player.c b/player.c index 71826ed445438a3db62bc020c3381e67ec542494..94c1b9bcd0f12e44d5507349d15695aa39b78301 100644 --- a/player.c +++ b/player.c @@ -21,12 +21,8 @@ player_t* init_player(int posc,int posl){ } void change_movement_player(player_t* player,int posc,int posl){ - player->DestR.h=PLAYERH; - player->DestR.w=PLAYERW; player->DestR.x=PLAYERW*posc; player->DestR.y=PLAYERH*posl; - player->Src.w=PLAYERW; - player->Src.h=PLAYERH; player->Src.x=PLAYERW*player->x; player->Src.y=PLAYERH*player->y; } @@ -90,22 +86,22 @@ void movement(char** tab,player_t *player,char deplacement){ { case 'z': if(handle_movement_up(tab,player)){ - player->y--; + player->y -= SPEED; } break; case 's': if(handle_movement_down(tab,player)){ - player->y++; + player->y+=SPEED; } break; case 'd': if(handle_movement_right(tab,player)){ - player->x++; + player->x+=SPEED; } break; case 'q': if(handle_movement_left(tab,player)){ - player->x--; + player->x-=SPEED; } break; } diff --git a/player.h b/player.h index bc074458eb9b38a2264378826846d96f1e52d89d..4ecfe1b26473d1830496d6376635cf9a70375f59 100644 --- a/player.h +++ b/player.h @@ -4,6 +4,7 @@ #define PLAYERH 32 #define PLAYERW 32 +#define SPEED 1 typedef struct player_s { @@ -14,8 +15,6 @@ typedef struct player_s int score; SDL_Rect DestR; SDL_Rect Src; - SDL_Texture* texture; - int speed; int lives; int gameover; }player_t; @@ -26,6 +25,7 @@ bool handle_movement_up(char** tab,player_t*player); bool handle_movement_down(char** tab,player_t*player); bool handle_movement_left(char** tab,player_t*player); bool handle_movement_right(char** tab,player_t*player); +bool handle_mouvement_trap(char** tab,player_t* player); void movement(char** tab,player_t *player,char deplacement); void gamrover(player_t* player,char** tab, int nbc,int nbl); void free_player(player_t* player); diff --git a/player.o b/player.o deleted file mode 100644 index eead894fb1f0bd998a24149d7b64fb7cd399147e..0000000000000000000000000000000000000000 Binary files a/player.o and /dev/null differ diff --git a/ressources/maze_map.txt b/ressources/maze_map.txt index d1934dbab8fc6ebd6a91c4541a454c416a6d7751..c459893a1f5d41d0aa0a3a4f18dfe8376b20569e 100644 --- a/ressources/maze_map.txt +++ b/ressources/maze_map.txt @@ -1,24 +1,24 @@ ########################################### -#L#LLLLLLLLLLL#LLLL########LLLLLLL###LL#### -#LLL######L##LLL##L########L#####L###L##### -##########L#######L##############L######### -##LLLLLLLLL#######L##LLLLLLLLLLLLLLLLLL#### -##L###############L##L######L#####L###L#### -##LLLL############LLLL######L#####L###L#### -############################L#####L###L#### -############LLLLLLLLLLLLLLLLL#####L###L#### -############L######L######L#######L######## -####LLLLLLLLL######L######L######LLLL###### -####L##############L######L#####LL##L###### -####L#LLLLLLLL###############LLLL###L###### -####L#L######L#####LLLL#############LLL#### -####LLLL#####L#####L##L#####LLL####LL###### -#############L#####L##L###########LL####### -#LLLLLLLLLLLLL#####L##L##LLLLLLLLLLLLLL#### -#L#################L##LLLL######L###L###### -#L#################L##L#LL######L###S###### -########################################### -########################################### -########################################### +#LLLLLLLLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLL# +#L#########################L#########L###L# +#L#LLLLLLLLLLLLLLLLLLLLLLLLL#############L# +#L#L#####################################L# +#L#L##################################L##L# +#L####################################L##L# +#L####################################L##L# +#L####################################L##L# +#L#################L######L##############L# +#L#################L######L##############L# +#L##L##############L######L#########L####L# +#L##L###############################L####L# +#L##L#L######L######################LLL##L# +#L###########L#####L##L#####LLL####LL####L# +#L###########L#####L##L###########LL#####L# +#L#################L##L##LLLLLLLLLLLLLL##L# +#L#################L##LLLL######L###L####L# +#L#################L##L#LL######L###S####L# +#L#LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL#L# +#L############L##########################L# +#LLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLLLLLLLL# ########################################### diff --git a/ressources/personnage.bmp b/ressources/personnage.bmp index e9617918c5d5e3106d1a790944abc9234906435f..6ce9aaf00392ccc828b06ec5f5d26b1c361be327 100644 Binary files a/ressources/personnage.bmp and b/ressources/personnage.bmp differ diff --git a/textures.c b/textures.c deleted file mode 100644 index a092778edd1d988a73ab22738f056e093c5cf872..0000000000000000000000000000000000000000 --- a/textures.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "textures.h" - -SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer) -{ - SDL_Surface *img = NULL; - SDL_Texture *texture = NULL; - img = SDL_LoadBMP(nomfichier); - if (NULL== img){ - fprintf(stderr,"Erreur dans le chargement image: %s", SDL_GetError()); - return NULL; - } - SDL_SetColorKey(img,SDL_TRUE, SDL_MapRGB(img->format,255,0,255)); - texture = SDL_CreateTextureFromSurface(renderer, img); - // Libérer une surface - SDL_FreeSurface(img); - if (texture == NULL){ - fprintf(stderr,"Erreur dans le chargement image: %s", SDL_GetError()); - return NULL; - } - return texture; -} diff --git a/textures.h b/textures.h deleted file mode 100644 index 91bc7cf4159f5f21aa0129ed0d0107c1a904b1e4..0000000000000000000000000000000000000000 --- a/textures.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef TEXTURES_H -#define TEXTURES_H - -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> -#include <SDL2/SDL.h> - - -SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer); - -#endif \ No newline at end of file diff --git a/textures.o b/textures.o deleted file mode 100644 index d2b5a1172fb8c4b8082578fb8d75347cd2fe8c8d..0000000000000000000000000000000000000000 Binary files a/textures.o and /dev/null differ