diff --git a/main b/main index 9b50af123a7ffcacd67c51e0163e31a0a06ddbc7..685436504e224007a3fb86201a25c1d7f67eb16f 100644 Binary files a/main and b/main differ diff --git a/main.c b/main.c index 4df74937cd2ada1dbac5871b2e671094f44580cb..bc46e38ed0052b2115ecf3d56f8fadb3a1e29efa 100644 --- a/main.c +++ b/main.c @@ -3,9 +3,9 @@ int main() { - trap_t trap1= {"The capital of Yemen","Sanaa","Riyadh","Aden"}; + trap_t trap1= {{"The capital of Yemen","Sanaa","Riyadh","Aden"}}; //trap_t trap2= {"light bulb inventor","tesla"}; - //trap_t trap3={"the origine of the frensh language","latin"}; + //trap_t trap3={"the origin 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 @@ -18,12 +18,11 @@ int main() init_sdl(&fenetre,&ecran,SCREENW,SCREENH); // Charger l’images apply_texture(walls,ecran); - FontPosition_t fontPos=initFontPosition(); SDL_Event event; // Boucle principale while(!player->gameover){ if (map[player->y][player->x]=='T'){ - showTrap(trap1,player,&event); + showTrap(trap1,player,&event,0); map[player->y][player->x]='L'; } handle_events(&event,walls,player,map); diff --git a/sdl2-ttf-light.c b/sdl2-ttf-light.c deleted file mode 100644 index 177f419e1f8efe646dd1e99f1603e757678e0f07..0000000000000000000000000000000000000000 --- a/sdl2-ttf-light.c +++ /dev/null @@ -1,40 +0,0 @@ -#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 deleted file mode 100644 index cf22425c26750d4cbe223700500675c07ca5ac07..0000000000000000000000000000000000000000 --- a/sdl2-ttf-light.h +++ /dev/null @@ -1,42 +0,0 @@ -#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 71cc18387f00894dd715aa88398cb9a34938a109..51e8bed39494e688113fa51a70951dd8e187804a 100644 --- a/trap.c +++ b/trap.c @@ -1,44 +1,16 @@ #include "trap.h" -FontPosition_t initFontPosition(){ - FontPosition_t fontPos; - for(int i=0;i<4;i++){ - fontPos.position[i].h=TRAPHT; - fontPos.position[i].w=TRAPWT; - if(i==0){ - fontPos.position[i].x =60; - fontPos.position[i].y =30; - }else{ - fontPos.position[i].x =60; - fontPos.position[i].y=30+fontPos.position[i].y; - } - } - //fontPos.font= TTF_OpenFont("ressources/arial.ttf",20); - return fontPos; -} -SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font, const char* text, SDL_Color color,SDL_Rect* position){ - SDL_Surface *text_img = TTF_RenderText_Solid(font, text, color); +SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font,trap_t trap, SDL_Color color,SDL_Rect* position,int y,int i){ + SDL_Surface *text_img = TTF_RenderText_Solid(font,trap.text[i], color); SDL_Texture *text_tex = SDL_CreateTextureFromSurface(renderer, text_img); position->x=60; - position->y=30; + position->y=30+y; position->w=text_img->w; position->h=text_img->h; SDL_FreeSurface(text_img); return text_tex; } -/* -void initialiser_text_surf(FontPosition_t fontPos,SDL_Renderer* renderer,trap_t trap){ - SDL_Color color_question={255,255,255,255}; - SDL_Color color_answer={255,0,255,0}; - fontPos.textSurfQ = TTF_RenderText_Solid(fontPos.font,trap.question,color_question); - fontPos.textTextureQ= SDL_CreateTextureFromSurface(renderer,fontPos.textSurfQ); - for (int i = 0; i < 3; i++) - { - fontPos.textSurfR[i] = TTF_RenderText_Solid(fontPos.font,trap.answers[i],color_answer); - fontPos.textTextureR[i]= SDL_CreateTextureFromSurface(renderer,fontPos.textSurfR[i]); - } -}*/ trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3){ trap_t* traps=malloc(4*sizeof(trap_t)); @@ -48,27 +20,23 @@ trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3){ return traps; } -void showTrap(trap_t trap,player_t* player,SDL_Event* event){ - int px=0,py=0; - //int px=fontPos.position[j+1].x,py=fontPos.position[j+1].y; +void showTrap(trap_t trap,player_t* player,SDL_Event* event,int j){ + int y=30; + int px,py; //SDL_Surface* textSurfQ; SDL_Init(SDL_INIT_VIDEO); - SDL_Window *fenetre=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,600,SDL_WINDOW_OPENGL); + SDL_Window *fenetre=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,400,SDL_WINDOW_OPENGL); SDL_Renderer *renderer=SDL_CreateRenderer(fenetre,-1,0); TTF_Init(); TTF_Font* font= TTF_OpenFont("ressources/arial.ttf",24); SDL_Color color={255,255,255,255}; - SDL_Rect* position; - SDL_Rect*position1; - SDL_Rect*position2; - //textSurfQ = TTF_RenderText_Solid(font,trap.question,color); - SDL_Texture* textTextureQ= createText(renderer,font,trap.question,color,position); - SDL_Texture* textTextureR=createText(renderer,font,trap.answers,color,position1); - SDL_Texture* textTextureR1=createText(renderer,font,trap.answers,color,position2); - //SDL_FreeSurface(textSurfQ); - //textSurfQ = TTF_RenderText_Solid(font,trap.answers,color); - //SDL_Texture* textTextureR= SDL_CreateTextureFromSurface(renderer,textSurfQ); - //SDL_FreeSurface(textSurfQ); + SDL_Rect position[4]; + SDL_Texture* textTextureQ[4]; + for(int i=0;i<4;i++){ + textTextureQ[i]= createText(renderer,font,trap,color,&position[i],(i+1)*y,i); + } + px=position[j+1].x; + py=position[j+1].y; TTF_CloseFont(font); bool end=false; while(!end){ @@ -82,7 +50,7 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){ } if (event->type== SDL_MOUSEBUTTONDOWN){ if (event->button.state==SDL_PRESSED){ - if(((event->button.x>=px) && (event->button.x<=( px +TRAPWT)))&&((event->button.y>=py)&&(event->button.y<=(py+TRAPHT)))){ + if(((event->button.x>=px) && (event->button.x<=( px +position[j+1].w)))&&((event->button.y>=py)&&(event->button.y<=(py+position[j+1].h)))){ player->score += 50; end=true; }else{ @@ -92,31 +60,21 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){ } } - SDL_RenderCopy(renderer,textTextureQ,NULL,position); - SDL_RenderCopy(renderer,textTextureR,NULL,position1); - SDL_RenderCopy(renderer,textTextureR,NULL,position2); + for(int i=0;i<4;i++){ + SDL_RenderCopy(renderer,textTextureQ[i],NULL,&position[i]); + } SDL_RenderPresent(renderer); SDL_UpdateWindowSurface(fenetre); } - SDL_DestroyTexture(textTextureQ); - SDL_DestroyTexture(textTextureR); - SDL_DestroyTexture(textTextureR1); + for (int i=0;i<4;i++){ + SDL_DestroyTexture(textTextureQ[i]); + } // Quitter SDL SDL_DestroyRenderer(renderer); SDL_DestroyWindow(fenetre); - - SDL_Quit(); TTF_Quit(); } void clean_trap(trap_t* trap){ free(trap); -} -/* -void clean_FontPosition(FontPosition_t fontPos){ - SDL_FreeSurface(fontPos.textSurfQ); - SDL_DestroyTexture(fontPos.textTextureQ); - SDL_FreeSurface(fontPos.textSurfR); - SDL_DestroyTexture(fontPos.textTextureR[i]); - TTF_CloseFont(fontPos.font); -}*/ +} \ No newline at end of file diff --git a/trap.h b/trap.h index a0f018784b2bbf11c69ea1eda9a36a61173294dc..bb8a10c5743db605191bc6428a1a1cd25f9a1236 100644 --- a/trap.h +++ b/trap.h @@ -1,35 +1,17 @@ #ifndef TRAP_H #define TRAP_H #include "graphics.h" -#include "sdl2-ttf-light.h" #define TRAPHT 60 #define TRAPWT 120 typedef struct trap_s{ - const char* question; - const char* answers; - const char* answers1; - const char* answers2; + const char* text[4]; }trap_t; -typedef struct FontPosition_s{ - SDL_Rect position[4]; - /* - SDL_Surface* textSurfQ; - SDL_Surface* textSurfR[3]; - SDL_Texture* textTextureQ; - SDL_Texture* textTextureR[3]; - TTF_Font* font;*/ -}FontPosition_t; - -FontPosition_t initFontPosition(); -void initialiser_text_surf(FontPosition_t fontPos,SDL_Renderer* renderer,trap_t trap); -SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font, const char* text, SDL_Color color,SDL_Rect* position); +SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font,trap_t trap, SDL_Color color,SDL_Rect* position,int y,int i); trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3); -void showTrap(trap_t trap,player_t* player,SDL_Event* event); +void showTrap(trap_t trap,player_t* player,SDL_Event* event,int j); void clean_trap(trap_t* trap); -void clean_FontPosition(FontPosition_t fontPos); -//void check_Trap(player_t* player,trap_t trap,char** map,int* i,walls_t* walls); #endif \ No newline at end of file