diff --git a/Makefile b/Makefile index aa941ec2d823bf88d0508eb5d3bbf26e2a55179c..29ec9b185226cd0ad695ce866f9eab65e05a9f34 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ CC = gcc -CFLAGS = -W -Wall -ansi -std=c99 -g +CFLAGS = -W -Wall -ansi -std=c99 -g LIBS = -L /SDL2_ttf/.libs -L /SDL2_image/.libs -LDFLAGS = `sdl2-config --cflags --libs` +LDFLAGS = `sdl2-config --cflags --libs` -lSDL2_ttf INCLUDES = -I ./SDL2_ttf -I ./SDL2_image EXEC = main -SRC = main.c player.c map.c graphics.c +SRC = main.c player.c map.c graphics.c trap.c sdl2-ttf-light.c OBJ = $(SRC:.c=.o) all: $(EXEC) diff --git a/graphics.c b/graphics.c index cd1e139696bcc357822026dcd33861313c474344..cdf0f39df829f0a4619c8f16245ee1e9d11e366f 100644 --- a/graphics.c +++ b/graphics.c @@ -46,10 +46,10 @@ void apply_texture(walls_t* walls,SDL_Renderer* ecran){ } void init_wall(walls_t* walls,int px,int py,int postx,int posty){ - walls->bricks.w=BRICKW; - walls->bricks.h=BRICKH; - walls->bricks.x=BRICKW*postx; - walls->bricks.y=BRICKH*posty; + walls->DestR.w=BRICKW; + walls->DestR.h=BRICKH; + walls->DestR.x=BRICKW*postx; + walls->DestR.y=BRICKH*posty; walls->Src.x=BRICKW*px; walls->Src.y=BRICKH*py; walls->Src.w=BRICKW; @@ -62,26 +62,52 @@ void affichage_bricks(SDL_Renderer* ecran,walls_t* walls,char** map,int nbc,int { if(map[i][j]=='L'){ init_wall(walls,j,i,9,3); - SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src)); + SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src)); } else if(map[i][j]=='#'){ init_wall(walls,j,i,10,3); - SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src)); + SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src)); } else if (map[i][j]=='S'){ init_wall(walls,j,i,13,3); - SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src)); + SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src)); + } + else if (map[i][j]=='T'){ + init_wall(walls,j,i,13,4); + SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src)); } } } } -void handle_events(SDL_Event *event, player_t* player,char** map){ +void affichage_joueur(player_t* player,walls_t* walls,SDL_Renderer* ecran){ + walls->DestR.h=PLAYERH; + walls->DestR.w=PLAYERW; + walls->DestR.x=PLAYERW*player->posc; + walls->DestR.y=PLAYERH*player->posl; + walls->Src.w=PLAYERW; + walls->Src.h=PLAYERH; + walls->Src.x=PLAYERW*player->x; + walls->Src.y=PLAYERH*player->y; + SDL_RenderCopy(ecran,walls->texture_joueur,&(walls->DestR),&(walls->Src)); +} + +void change_movement_player(player_t* player,walls_t* walls,int posc,int posl){ + player->posc=posc; + player->posl=posl; + walls->DestR.x=PLAYERW*player->posc; + walls->DestR.y=PLAYERH*player->posl; + walls->Src.x=PLAYERW*player->x; + walls->Src.y=PLAYERH*player->y; +} + + + +void handle_events(SDL_Event *event,walls_t* walls,player_t* player,char** map){ while (SDL_PollEvent(event)){ if (event->type ==SDL_QUIT){ player->gameover=1; } - //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. @@ -89,28 +115,40 @@ void handle_events(SDL_Event *event, player_t* player,char** map){ } if(event->key.keysym.sym == SDLK_LEFT){ movement(map,player,'q'); // fléche vers la gauche - change_movement_player(player,0,1); - change_movement_player(player,1,1); + change_movement_player(player,walls,1,1); } if(event->key.keysym.sym == SDLK_RIGHT){ movement(map,player,'d'); // fléche vers la droite - change_movement_player(player,0,2); - change_movement_player(player,1,2); + change_movement_player(player,walls,1,2); } if(event->key.keysym.sym == SDLK_UP){ movement(map,player,'z');// fléche vers le haut - change_movement_player(player,0,3); - change_movement_player(player,1,3); + change_movement_player(player,walls,1,3); } if(event->key.keysym.sym == SDLK_DOWN){ movement(map,player,'s');// fléche vers le bas - change_movement_player(player,0,0); - change_movement_player(player,1,0); + change_movement_player(player,walls,1,0); } } } } +/* +void apply_text(SDL_Renderer* ecran,int x,int y,int width,int height,const char* message){ + TTF_Font* font=TTF_OpenFont("ressources/angelina.TTF",14); + SDL_Color white={255,255,255,255}; + SDL_Surface* surfaceText=TTF_RenderText_Solid(font,message,white); + SDL_Texture* text=SDL_CreateTextureFromSurface(ecran,surfaceText); + SDL_Rect textRect; + textRect.h=height; + textRect.w=width; + textRect.x=x; + textRect.y=y; + TTF_CloseFont(font); + SDL_RenderCopy(ecran,text,NULL,&textRect); + SDL_FreeSurface(surfaceText); + SDL_DestroyTexture(text); +}*/ void clean_walls(walls_t* walls){ SDL_DestroyTexture(walls->texture_joueur); diff --git a/graphics.h b/graphics.h index e44cbcb50934c97f7346e5e22c430b9631b435a4..bf28212ce65e4e78c59b4ce8191709e0c697d189 100644 --- a/graphics.h +++ b/graphics.h @@ -10,7 +10,7 @@ typedef struct walls_s { - SDL_Rect bricks; + SDL_Rect DestR; SDL_Rect Src; SDL_Texture* texture_joueur; SDL_Texture* texture_brick; @@ -22,6 +22,9 @@ SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer); void apply_texture(walls_t* walls,SDL_Renderer* ecran); void init_wall(walls_t* walls,int px,int py,int postx,int posty); void affichage_bricks(SDL_Renderer* ecran,walls_t* walls,char** map,int nbc,int nbl); -void handle_events(SDL_Event *event,player_t* player,char** map); +void affichage_joueur(player_t* player,walls_t* walls,SDL_Renderer* ecran); +void change_movement_player(player_t* player,walls_t* walls,int posc,int posl); +void handle_events(SDL_Event *event,walls_t* walls,player_t* player,char** map); +//void apply_text(SDL_Renderer* ecran,int x,int y,int width,int height,const char* message); void clean_walls(walls_t* walls); #endif \ No newline at end of file diff --git a/main b/main index 063774eaaee2dac64da7908e129236424fe9fe55..16f068485188d2af9068598fae00b604f74d1c5a 100644 Binary files a/main and b/main differ diff --git a/main.c b/main.c index aa1cbc279dcb766f606dda2663d5d760a418f79a..6e4660cbbcf5b31783f1348a05ee1a30a098110f 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,13 @@ -#include "graphics.h" +#include "trap.h" int main() { - int nbl = 0,nbc = 0; + trap_t trap1= {"the capital of france","paris"}; + trap_t trap2= {"light bulb inventor","tesla"}; + trap_t trap3={"the origine of the frensh language","latin"}; + trap_t trap4={"Who won the world cup in 2006","italy"}; + int nbl = 0,nbc = 0,i=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(0,2);//initialisation du joueur @@ -17,11 +21,15 @@ int main() SDL_Event event; // Boucle principale while(!player->gameover){ - handle_events(&event,player,map); - gamrover(player,map,nbc,nbl); + if (map[player->y][player->x]=='T'){ + showTrap(trap1,player,&event); + map[player->y][player->x]='L'; + } + handle_events(&event,walls,player,map); + gameover(player,map,nbc,nbl); SDL_RenderClear(ecran); affichage_bricks(ecran,walls,map,nbc,nbl); - SDL_RenderCopy(ecran,walls->texture_joueur,&(player->DestR),&(player->Src)); + affichage_joueur(player,walls,ecran); SDL_RenderPresent(ecran); SDL_UpdateWindowSurface(fenetre); } diff --git a/map.h b/map.h index f1bf10beef158b0fd52f38b381dbcd9eb9181566..79eb4fbdf2d8e1ad91d5cf34dfe9b2fb7fbe1ce8 100644 --- a/map.h +++ b/map.h @@ -5,6 +5,8 @@ #include <stdlib.h> #include <stdbool.h> #include <SDL2/SDL.h> +#include <SDL2/SDL_image.h> +#include <SDL2/SDL_ttf.h> char** allouer_tab_2D(int n,int m); diff --git a/player.c b/player.c index 94c1b9bcd0f12e44d5507349d15695aa39b78301..ce3d32adfee096d8390c6c36e1e2fc569632e779 100644 --- a/player.c +++ b/player.c @@ -5,30 +5,15 @@ player_t* init_player(int posc,int posl){ player->x=1; player->y=1; player->score=0; - 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; + player->posc=posc; + player->posl=posl; player->lives=3; player->gameover=0; return player; } -void change_movement_player(player_t* player,int posc,int posl){ - player->DestR.x=PLAYERW*posc; - player->DestR.y=PLAYERH*posl; - player->Src.x=PLAYERW*player->x; - player->Src.y=PLAYERH*player->y; -} - -void gamrover(player_t* player,char** tab, int nbc,int nbl){ +void gameover(player_t* player,char** tab, int nbc,int nbl){ for (int i = 0; i<nbl ; i++) { for (int j = 0; j<nbc; j++) @@ -40,11 +25,14 @@ void gamrover(player_t* player,char** tab, int nbc,int nbl){ } } } + if (player->lives==0){ + player->gameover=1; + } } bool handle_movement_up(char** tab,player_t*player) { - if (tab[player->y-1][player->x]=='L'|| tab[player->y-1][player->x]=='S'){ + if (tab[player->y-1][player->x]=='L'|| tab[player->y-1][player->x]=='S' || tab[player->y-1][player->x]=='T'){ return true; } else{ @@ -54,7 +42,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'|| tab[player->y+1][player->x]=='S'){ + if (tab[player->y+1][player->x]=='L'|| tab[player->y+1][player->x]=='S'|| tab[player->y+1][player->x]=='T'){ return true; } else{ @@ -64,7 +52,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'|| tab[player->y][player->x-1]=='S'){ + if (tab[player->y][player->x-1]=='L'|| tab[player->y][player->x-1]=='S'||tab[player->y][player->x-1]=='T'){ return true; } else{ @@ -73,7 +61,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'||tab[player->y][player->x+1]=='S'){ + if (tab[player->y][player->x+1]=='L'||tab[player->y][player->x+1]=='S'||tab[player->y][player->x+1]=='T'){ return true; } else{ diff --git a/player.h b/player.h index 4ecfe1b26473d1830496d6376635cf9a70375f59..c3ed96d26ab087d73be9d34d106c8da5358dc2f9 100644 --- a/player.h +++ b/player.h @@ -10,24 +10,21 @@ typedef struct player_s { int x; int y; - int width; - int height; + int posc; + int posl; int score; - SDL_Rect DestR; - SDL_Rect Src; int lives; int gameover; }player_t; 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); 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 gameover(player_t* player,char** tab, int nbc,int nbl); void free_player(player_t* player); #endif \ No newline at end of file diff --git a/ressources/bg_trap.bmp b/ressources/bg_trap.bmp new file mode 100644 index 0000000000000000000000000000000000000000..cb3c1d297d12557b09fe57b4c56d7394ea816d62 Binary files /dev/null and b/ressources/bg_trap.bmp differ diff --git a/ressources/maze_map.txt b/ressources/maze_map.txt index c459893a1f5d41d0aa0a3a4f18dfe8376b20569e..6494410fea68ee9ae9fb6263630daaa59c28a8ad 100644 --- a/ressources/maze_map.txt +++ b/ressources/maze_map.txt @@ -1,5 +1,5 @@ ########################################### -#LLLLLLLLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLL# +#LTLLLLLLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLL# #L#########################L#########L###L# #L#LLLLLLLLLLLLLLLLLLLLLLLLL#############L# #L#L#####################################L# @@ -13,11 +13,11 @@ #L##L###############################L####L# #L##L#L######L######################LLL##L# #L###########L#####L##L#####LLL####LL####L# -#L###########L#####L##L###########LL#####L# +#T###########L#####L##L###########LL#####L# #L#################L##L##LLLLLLLLLLLLLL##L# -#L#################L##LLLL######L###L####L# +#L#################L##LLLL######L###T####L# #L#################L##L#LL######L###S####L# -#L#LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL#L# +#L#LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLTLLL#L# #L############L##########################L# #LLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLLLLLLLL# ########################################### diff --git a/ressources/ok_button.bmp b/ressources/ok_button.bmp new file mode 100644 index 0000000000000000000000000000000000000000..67369bfe83f2fd40e2a1be105ca8e1535b8fe63d Binary files /dev/null and b/ressources/ok_button.bmp differ diff --git a/ressources/only.ttf b/ressources/only.ttf new file mode 100644 index 0000000000000000000000000000000000000000..4aacbfe0a25fbe828760584a44fb6eff1d6ce172 Binary files /dev/null and b/ressources/only.ttf differ diff --git a/sdl2-ttf-light.c b/sdl2-ttf-light.c new file mode 100644 index 0000000000000000000000000000000000000000..177f419e1f8efe646dd1e99f1603e757678e0f07 --- /dev/null +++ b/sdl2-ttf-light.c @@ -0,0 +1,40 @@ +#include "sdl2-ttf-light.h" + + + + + +void init_ttf(){ + if(TTF_Init()==-1) { + printf("TTF_Init: %s\n", TTF_GetError()); + } +} + + + +TTF_Font * load_font(const char *path, int font_size){ + TTF_Font *font = TTF_OpenFont(path, font_size); + if(font == NULL){ + fprintf(stderr, "Erreur pendant chargement font: %s\n", SDL_GetError()); + } + return font; +} + + + + + +void apply_text(SDL_Renderer *renderer,int x, int y, int w, int h, const char *text, TTF_Font *font){ + SDL_Color color = { 255, 0, 255 ,0}; + + SDL_Surface* surface = TTF_RenderText_Solid(font, text, color); + + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_Rect dstrect2 = {x, y, w, h}; + SDL_RenderCopy(renderer, texture, NULL, &dstrect2); + +} + +void clean_font(TTF_Font * font){ + TTF_CloseFont(font); +} diff --git a/sdl2-ttf-light.h b/sdl2-ttf-light.h new file mode 100644 index 0000000000000000000000000000000000000000..cf22425c26750d4cbe223700500675c07ca5ac07 --- /dev/null +++ b/sdl2-ttf-light.h @@ -0,0 +1,42 @@ +#ifndef __SDL2_TTF_LIGHT__H +#define __SDL2_TTF_LIGHT__H + +#include <SDL2/SDL.h> +#include <SDL2/SDL_ttf.h> + +/** + * \brief La fonction initialise l'environnement TTF +*/ +void init_ttf(); + + +/** + * \brief La fonction charge une police + * \param path le chemin du fichier correpondant à la police + * \param font_size la taille de la police + * \return la police chargée +*/ + +TTF_Font * load_font(const char* path, int font_size); + +/** + * \brief La fonction applique un texte dans une certaine police sur le renderer à une certaine position et avec une certaine dimension + * \param renderer le renderer + * \param x abscisse du coin en haut à gauche du texte + * \param y son abscisse + * \param w la largeur du message + * \param h sa hauteur + * \param text le texte à afficher + * \param font la police +*/ +void apply_text(SDL_Renderer *renderer,int x, int y, int w, int h, const char *text, TTF_Font *font); + + + +/** + * \brief La fonction nettoie une police en mémoire + * \param font la police +*/ +void clean_font(TTF_Font * font); + +#endif diff --git a/trap.c b/trap.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..7565e8b08b54a57ad1c81275604768fa3d4c1882 100644 --- a/trap.c +++ b/trap.c @@ -0,0 +1,57 @@ +#include "trap.h" + +trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4){ + trap_t* traps=malloc(4*sizeof(trap_t)); + traps[0]=trap1; + traps[1]=trap2; + traps[2]=trap3; + traps[3]=trap4; + return traps; +} + +void showTrap(trap_t trap,player_t* player,SDL_Event* event){ + SDL_Init(SDL_INIT_VIDEO); + SDL_Window* fenetre=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,600,SDL_WINDOW_OPENGL); + SDL_Renderer* renderer=SDL_CreateRenderer(fenetre,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + TTF_Font* font; + init_ttf(); + font=load_font("ressources/only.ttf",14); + bool end=false; + const char* reponse_j; + const char* r; + SDL_SetRenderDrawColor(renderer,255,0,0,255); + while(!end){ + apply_text(renderer,0,0,100,200,"trap.question",font); + while (SDL_PollEvent(event)) + { + if (event->type ==SDL_QUIT){ + end=true; + } + if (event->type == SDL_KEYDOWN){ + if (event->key.keysym.sym==SDLK_ESCAPE){ //si on appui sur la touche Echap, on quitte le jeu. + end=true; + } + if(event->key.keysym.sym==SDLK_KP_ENTER){ + if(strcmp(reponse_j,trap.answer)==0){ + player->score +=10; + end=true; + }else{ + player->lives--; + } + } + else{ + r= SDL_GetKeyName(event->key.keysym.sym); + reponse_j +=*r; + apply_text(renderer,0,201,100,200,reponse_j,font); + } + } + } + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); + } + // Quitter SDL + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(fenetre); + clean_font(font); + SDL_Quit(); +} \ No newline at end of file diff --git a/trap.h b/trap.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..deeebf1f15e592f118fb9edc51435c0b013455cf 100644 --- a/trap.h +++ b/trap.h @@ -0,0 +1,15 @@ +#ifndef TRAP_H +#define TRAP_H +#include "graphics.h" +#include "sdl2-ttf-light.h" + +typedef struct trap_s{ + const char* question; + const char* answer; +}trap_t; + +trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4); +void showTrap(trap_t trap,player_t* player,SDL_Event* event); +//void check_Trap(player_t* player,trap_t trap,char** map,int* i,walls_t* walls); + +#endif \ No newline at end of file