diff --git a/Makefile b/Makefile index 80f33a5ae96c14e375965b768663b9d381590964..111d09b9a647f52029fb794d541a4af04e910369 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,15 @@ CC = gcc CFLAGS = -W -Wall -ansi -std=c99 -g +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 map.c player.c +SRC = main.c player.c map.c textures.c OBJ = $(SRC:.c=.o) all: $(EXEC) main: $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LIBS) $(LDFLAGS) %.o: %.c $(CC) $(CFLAGS) -o $@ -c $< clean: diff --git a/data.c b/data.c new file mode 100644 index 0000000000000000000000000000000000000000..3357bf80c7269352db0327f862cb49edcd8da5a8 --- /dev/null +++ b/data.c @@ -0,0 +1,21 @@ +#include "data.h" + +base_t* init_base(player_t* player,int px,int py,int postx,int posty){ + base_t* base= malloc(sizeof(base_t)); + base->gameover=0; + base->chances=3; + base->player=player; + base->wall=init_wall(px,py,postx,posty); +} +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; +} +//void gameOver(char** tab,int nbl,int nbc,base_t* base); diff --git a/data.h b/data.h new file mode 100644 index 0000000000000000000000000000000000000000..7d786448ba207b6cd6c45b559cc3aca6d857c67f --- /dev/null +++ b/data.h @@ -0,0 +1,30 @@ +#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; +}walls_t; + + +typedef struct base_s +{ + walls_t* wall; + player_t* player; + int gameover; + int chances; + +}base_t; +base_t* init_base(player_t* player,int px,int py,int postx,int posty); +walls_t* init_wall(int px,int py,int postx,int posty); +void gameOver(char** tab,int nbl,int nbc,base_t* base); + +#endif \ No newline at end of file diff --git a/fonctions_SDL.c b/fonctions_SDL.c new file mode 100644 index 0000000000000000000000000000000000000000..64ca2a792c05af2e650a9393c0ee18abf7e2d412 --- /dev/null +++ b/fonctions_SDL.c @@ -0,0 +1,11 @@ +#include "fonctions_SDL.h" + +SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer) +{ + SDL_Surface *img = SDL_LoadBMP(nomfichier); + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, img); + // Libérer une surface + SDL_FreeSurface(img); + return texture; +} + diff --git a/fonctions_SDL.h b/fonctions_SDL.h new file mode 100644 index 0000000000000000000000000000000000000000..566f6e222a850dbfbd75765ee3efbf6aba1c9a3f --- /dev/null +++ b/fonctions_SDL.h @@ -0,0 +1,12 @@ +#ifndef FONCTIONS_SDL_H +#define FONCTIONS_SDL_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 diff --git a/fonctions_SDL.o b/fonctions_SDL.o new file mode 100644 index 0000000000000000000000000000000000000000..03638496a0bb8c930aa6f570c39e46bdf9e4e5db Binary files /dev/null and b/fonctions_SDL.o differ diff --git a/main b/main new file mode 100644 index 0000000000000000000000000000000000000000..740ba5eea2b2ecca9bda35580b7975865a2076ca Binary files /dev/null and b/main differ diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..1956978d1484371bd5ab7735fb54cf919f248196 --- /dev/null +++ b/main.c @@ -0,0 +1,143 @@ +#include "data.h" + +int main() +{ + int nbl = 0; + int nbc = 0; + char** map =lire_map("ressources/maze_map.txt"); + taille_map("ressources/maze_map.txt",&nbl,&nbc); + player_t*player = init_player(32,32); + SDL_Window* fenetre; // Déclaration de la fenêtre + 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; + } + + + SDL_Renderer* ecran; + ecran = SDL_CreateRenderer(fenetre, -1, SDL_RENDERER_ACCELERATED); + // Charger l’images + SDL_Texture* sprite= charger_image("ressources/pavage.bmp",ecran); + //player->texture = charger_image("ressources/personnage1.bmp",ecran); + + int width; + int height; + int widthP; + int heightP; + Uint32 format; + int access; + int accessP; + Uint32 formatP; + SDL_QueryTexture(sprite,&format,&access,&width,&height); + //SDL_QueryTexture(player->texture,&formatP,&accessP,&widthP,&heightP); + height=height/10; + width=width/16; + widthP=widthP/6; + 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 + } + if(event.key.keysym.sym == SDLK_RIGHT){ + movement(map,player,'d'); // fléche vers la droite + } + if(event.key.keysym.sym == SDLK_UP){ + movement(map,player,'z');// fléche vers le haut + } + if(event.key.keysym.sym == SDLK_DOWN){ + movement(map,player,'s');// fléche vers le bas + } + } + + } + SDL_RenderClear(ecran); + SDL_Rect DestR[160]; + SDL_Rect DestR_P[6]; + SDL_Rect Src; + for (int i=0;i<nbl;i++){ + for (int j = 0; j< nbc; j++) + { + if(map[i][j]=='L'){ + DestR[i].x=8*width; + DestR[i].y=3*height; + DestR[i].w=width; + DestR[i].h=height; + Src.x=width*j; + Src.y=height*i; + Src.w=width; + Src.h=height; + SDL_RenderCopy(ecran,sprite,&DestR[i],&Src); + + } + else if(map[i][j]=='#'){ + DestR[i].x=9*width; + DestR[i].y=3*height; + DestR[i].w=width; + DestR[i].h=height; + Src.x=width*j; + Src.y=height*i; + Src.w=width; + Src.h=height; + SDL_RenderCopy(ecran,sprite,&DestR[i],&Src); + + } + else if (map[i][j]=='S'){ + DestR[i].x=13*width; + DestR[i].y=3*height; + DestR[i].w=width; + DestR[i].h=height; + Src.x=width*j; + Src.y=height*i; + Src.w=width; + Src.h=height; + SDL_RenderCopy(ecran,sprite,&DestR[i],&Src); + + } + } + } + DestR_P[player->y].x=1*widthP; + DestR_P[player->y].y=0; + DestR_P[player->y].w=widthP; + DestR_P[player->y].h=heightP; + Src.x=widthP*player->x; + Src.y=heightP*player->y; + Src.w=widthP; + Src.h=heightP; + //SDL_RenderCopy(ecran,player->texture,&DestR_P[player->y],&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); + // Quitter SDL + SDL_DestroyWindow(fenetre); + SDL_Quit(); + return 0; +} \ No newline at end of file diff --git a/main.o b/main.o new file mode 100644 index 0000000000000000000000000000000000000000..2efbb9e2fc33d14680e0086366c6bc8e438eb433 Binary files /dev/null and b/main.o differ diff --git a/map.h b/map.h index bbd6e42a998c2fde65097269db45cecb12bdf85b..cda2856670c79f75818bad3ca2995e7dec89aff8 100644 --- a/map.h +++ b/map.h @@ -1,7 +1,7 @@ #ifndef MAP_H #define MAP_H +#include "textures.h" -#include "player.h" char** allouer_tab_2D(int n,int m); void afficher_map(char** tab,int n,int m); diff --git a/map.o b/map.o new file mode 100644 index 0000000000000000000000000000000000000000..8364c52c0e9e88769ceaa0a43b272b77a43d710f Binary files /dev/null and b/map.o differ diff --git a/player.c b/player.c index 266c5c6eb3329346bdfc76e93433013f85ecc32c..028b3377ac28c1c1c78f83209e133185830cdb94 100644 --- a/player.c +++ b/player.c @@ -1,24 +1,47 @@ #include "player.h" -void init_player(player_t *player,char** tab,int nbl,int nbc,int w,int h){ - for (int i=0;i<nbl;i++){ - for (int j= 0; j < nbc; j++) - { - if(tab[i][j]=='P'){ - player->x=i; - player->y=j; - } - } - } +player_t* init_player(int posc,int posl){ + player_t* player= malloc(sizeof(player_t)); + player->x=1; + player->y=1; player->score=0; - player->chances=3; - player->height=h; - player->width=w; + player->height=PLAYERH; + player->width=PLAYERW; + 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; + return player; } +void change_movement_player(player_t* player,int posc,int posl){ + player->DestR.x=PLAYERW*posc; + player->DestR.y=PLAYERH*posl; +} + + +/* +void gameOver(char** tab,int nbl,int nbc,player_t* player){ + for (int i = 0; i<nbl ; i++) + { + for (int j = 0; j<nbc; j++) + { + if(tab[i][j]=='S'){ + if(player->x==j&& player->y==i){ + player->gameover=1; + } + } + } + } +} +*/ bool handle_movement_up(char** tab,player_t*player) { - if (tab[player->y-1][player->x]=='L'){ + if (tab[player->y-1][player->x]=='L'|| tab[player->y-1][player->x]=='S'){ return true; } else{ @@ -28,7 +51,7 @@ bool handle_movement_up(char** tab,player_t*player) bool handle_movement_down(char** tab,player_t*player) { - if (tab[player->y+1][player->x]=='L'){ + if (tab[player->y+1][player->x]=='L'|| tab[player->y+1][player->x]=='S'){ return true; } else{ @@ -38,7 +61,7 @@ bool handle_movement_down(char** tab,player_t*player) } bool handle_movement_left(char** tab,player_t*player) { - if (tab[player->y][player->x-1]=='L'){ + if (tab[player->y][player->x-1]=='L'|| tab[player->y][player->x-1]=='S'){ return true; } else{ @@ -47,7 +70,7 @@ bool handle_movement_left(char** tab,player_t*player) } bool handle_movement_right(char** tab,player_t*player) { - if (tab[player->y][player->x+1]=='L'){ + if (tab[player->y][player->x+1]=='L'||tab[player->y][player->x+1]=='S'){ return true; } else{ @@ -55,7 +78,7 @@ bool handle_movement_right(char** tab,player_t*player) } } -void movement(char** tab,player_t *player,char deplacement){ +void movement(char** tab,player_t *player,char deplacement){ switch (deplacement) { case 'z': @@ -80,3 +103,7 @@ void movement(char** tab,player_t *player,char deplacement){ break; } } + +void free_player(player_t* player){ + free(player); +} diff --git a/player.h b/player.h index 7b65d69bb80230f427a84c341b37c88da8653eab..ff7ac83ba49c0c262ea4acb19b48b0d2593ea467 100644 --- a/player.h +++ b/player.h @@ -1,8 +1,9 @@ #ifndef PLAYER_H #define PLAYER_H -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> +#include "map.h" + +#define PLAYERH 32 +#define PLAYERW 32 typedef struct player_s { @@ -11,14 +12,18 @@ typedef struct player_s int width; int height; int score; - int chances; + SDL_Rect DestR; + SDL_Rect Src; + int speed; }player_t; -void init_player(player_t *player,char** tab,int nbl,int nbc,int w,int h); +player_t* init_player(int posc,int posl); +void change_movement_player(player_t* player,int posc,int posl); 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); void movement(char** tab,player_t *player,char deplacement); +void free_player(player_t* player); #endif \ No newline at end of file diff --git a/player.o b/player.o new file mode 100644 index 0000000000000000000000000000000000000000..7abd67df921e9ad9633f60a02406bc1a0ead1d35 Binary files /dev/null and b/player.o differ diff --git a/ressources/angelina.TTF b/ressources/angelina.TTF new file mode 100644 index 0000000000000000000000000000000000000000..fe880ab9109467e86eed45cf29feb297eef4686e Binary files /dev/null and b/ressources/angelina.TTF differ diff --git a/ressources/arial.ttf b/ressources/arial.ttf new file mode 100644 index 0000000000000000000000000000000000000000..ff0815cd8c64b0a245ec780eb8d21867509155b5 Binary files /dev/null and b/ressources/arial.ttf differ diff --git a/ressources/maze_map.txt b/ressources/maze_map.txt index e75d3f584961549778dd3bb7bc3a8702be6ca4da..d1934dbab8fc6ebd6a91c4541a454c416a6d7751 100644 --- a/ressources/maze_map.txt +++ b/ressources/maze_map.txt @@ -1,22 +1,24 @@ -##################################################### -#P#LLLLLLLLLLL#LLLL########LLLLLLL###LLL####LLLLLLLL# -#LLL######L##LLL##L##############L###L#LLLLLL######## -##########L#######L##############L##########LLLLLL### -##LLLLLLLLL#######L##LLLLLLLLLLLLLLLLLL#####L####L### -##L###############L##L######L#####L###L#####L####L### -##LLLL############LLLL######L#####L###L#####L####L### -############################L#####L###L#####L####LLL# -############LLLLLLLLLLLLLLLLL#####L###LLLLLLL######## -############L######L######L#######L################## -####LLLLLLLLL######L######L######LLLL#######LLL###### -####L##############L######L#####LL##L#######L#L###### -####L#LLLLLLLL###############LLLL###L#######L#L###### -####L#L######L#####LLLL#############LLLLLLLLL#LLL#### -####LLLL#####L#####L##L#####LLL####LL###########L#### -#############L#####L##L###########LL############LLLL# -#LLLLLLLLLLLLL#####L##L##LLLLLLLLLL#LLLLLL########### -#L#################L##LLLL######L###L####L########### -#L#################L##L#LL######L###L####LLLL######## -#LLLLL###LLLLLLLLLLL##L#########LLLLL#######LLLLLLLL# -#####LLLLL############LLLLLL#######################S# -##################################################### +########################################### +#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###### +########################################### +########################################### +########################################### +########################################### + diff --git a/ressources/pavage.bmp b/ressources/pavage.bmp new file mode 100644 index 0000000000000000000000000000000000000000..6f1b339e8ffa2e71632656646d82fcf3c2ca384f Binary files /dev/null and b/ressources/pavage.bmp differ diff --git a/ressources/personnage1.bmp b/ressources/personnage1.bmp new file mode 100644 index 0000000000000000000000000000000000000000..6129c4cfb40ae3ab69ec03729c387f57cdd66fc4 Binary files /dev/null and b/ressources/personnage1.bmp differ diff --git a/textures.c b/textures.c new file mode 100644 index 0000000000000000000000000000000000000000..a092778edd1d988a73ab22738f056e093c5cf872 --- /dev/null +++ b/textures.c @@ -0,0 +1,21 @@ +#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 new file mode 100644 index 0000000000000000000000000000000000000000..91bc7cf4159f5f21aa0129ed0d0107c1a904b1e4 --- /dev/null +++ b/textures.h @@ -0,0 +1,12 @@ +#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