Skip to content
Snippets Groups Projects
Commit ff9b52c5 authored by IKHRICHI Soumaya's avatar IKHRICHI Soumaya
Browse files

modification dans le trap.c fait avec Rawan

parent 68575b6e
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
int main() 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 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"}; //trap_t trap4={"Who won the world cup in 2006","italy"};
int nbl = 0,nbc = 0,i=0; int nbl = 0,nbc = 0,i=0;
char** map =lire_map("ressources/maze_map.txt");//lire le fichier char** map =lire_map("ressources/maze_map.txt");//lire le fichier
...@@ -18,12 +18,11 @@ int main() ...@@ -18,12 +18,11 @@ int main()
init_sdl(&fenetre,&ecran,SCREENW,SCREENH); init_sdl(&fenetre,&ecran,SCREENW,SCREENH);
// Charger l’images // Charger l’images
apply_texture(walls,ecran); apply_texture(walls,ecran);
FontPosition_t fontPos=initFontPosition();
SDL_Event event; SDL_Event event;
// Boucle principale // Boucle principale
while(!player->gameover){ while(!player->gameover){
if (map[player->y][player->x]=='T'){ if (map[player->y][player->x]=='T'){
showTrap(trap1,player,&event); showTrap(trap1,player,&event,0);
map[player->y][player->x]='L'; map[player->y][player->x]='L';
} }
handle_events(&event,walls,player,map); handle_events(&event,walls,player,map);
......
#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);
}
#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
#include "trap.h" #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_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, text, color); SDL_Surface *text_img = TTF_RenderText_Solid(font,trap.text[i], color);
SDL_Texture *text_tex = SDL_CreateTextureFromSurface(renderer, text_img); SDL_Texture *text_tex = SDL_CreateTextureFromSurface(renderer, text_img);
position->x=60; position->x=60;
position->y=30; position->y=30+y;
position->w=text_img->w; position->w=text_img->w;
position->h=text_img->h; position->h=text_img->h;
SDL_FreeSurface(text_img); SDL_FreeSurface(text_img);
return text_tex; 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* init_trap(trap_t trap1,trap_t trap2,trap_t trap3){
trap_t* traps=malloc(4*sizeof(trap_t)); 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){ ...@@ -48,27 +20,23 @@ trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3){
return traps; return traps;
} }
void showTrap(trap_t trap,player_t* player,SDL_Event* event){ void showTrap(trap_t trap,player_t* player,SDL_Event* event,int j){
int px=0,py=0; int y=30;
//int px=fontPos.position[j+1].x,py=fontPos.position[j+1].y; int px,py;
//SDL_Surface* textSurfQ; //SDL_Surface* textSurfQ;
SDL_Init(SDL_INIT_VIDEO); 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); SDL_Renderer *renderer=SDL_CreateRenderer(fenetre,-1,0);
TTF_Init(); TTF_Init();
TTF_Font* font= TTF_OpenFont("ressources/arial.ttf",24); TTF_Font* font= TTF_OpenFont("ressources/arial.ttf",24);
SDL_Color color={255,255,255,255}; SDL_Color color={255,255,255,255};
SDL_Rect* position; SDL_Rect position[4];
SDL_Rect*position1; SDL_Texture* textTextureQ[4];
SDL_Rect*position2; for(int i=0;i<4;i++){
//textSurfQ = TTF_RenderText_Solid(font,trap.question,color); textTextureQ[i]= createText(renderer,font,trap,color,&position[i],(i+1)*y,i);
SDL_Texture* textTextureQ= createText(renderer,font,trap.question,color,position); }
SDL_Texture* textTextureR=createText(renderer,font,trap.answers,color,position1); px=position[j+1].x;
SDL_Texture* textTextureR1=createText(renderer,font,trap.answers,color,position2); py=position[j+1].y;
//SDL_FreeSurface(textSurfQ);
//textSurfQ = TTF_RenderText_Solid(font,trap.answers,color);
//SDL_Texture* textTextureR= SDL_CreateTextureFromSurface(renderer,textSurfQ);
//SDL_FreeSurface(textSurfQ);
TTF_CloseFont(font); TTF_CloseFont(font);
bool end=false; bool end=false;
while(!end){ while(!end){
...@@ -82,7 +50,7 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){ ...@@ -82,7 +50,7 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){
} }
if (event->type== SDL_MOUSEBUTTONDOWN){ if (event->type== SDL_MOUSEBUTTONDOWN){
if (event->button.state==SDL_PRESSED){ 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; player->score += 50;
end=true; end=true;
}else{ }else{
...@@ -92,31 +60,21 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){ ...@@ -92,31 +60,21 @@ void showTrap(trap_t trap,player_t* player,SDL_Event* event){
} }
} }
SDL_RenderCopy(renderer,textTextureQ,NULL,position); for(int i=0;i<4;i++){
SDL_RenderCopy(renderer,textTextureR,NULL,position1); SDL_RenderCopy(renderer,textTextureQ[i],NULL,&position[i]);
SDL_RenderCopy(renderer,textTextureR,NULL,position2); }
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(fenetre); SDL_UpdateWindowSurface(fenetre);
} }
SDL_DestroyTexture(textTextureQ); for (int i=0;i<4;i++){
SDL_DestroyTexture(textTextureR); SDL_DestroyTexture(textTextureQ[i]);
SDL_DestroyTexture(textTextureR1); }
// Quitter SDL // Quitter SDL
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(fenetre); SDL_DestroyWindow(fenetre);
SDL_Quit();
TTF_Quit(); TTF_Quit();
} }
void clean_trap(trap_t* trap){ void clean_trap(trap_t* trap){
free(trap); free(trap);
} }
/* \ No newline at end of file
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);
}*/
#ifndef TRAP_H #ifndef TRAP_H
#define TRAP_H #define TRAP_H
#include "graphics.h" #include "graphics.h"
#include "sdl2-ttf-light.h"
#define TRAPHT 60 #define TRAPHT 60
#define TRAPWT 120 #define TRAPWT 120
typedef struct trap_s{ typedef struct trap_s{
const char* question; const char* text[4];
const char* answers;
const char* answers1;
const char* answers2;
}trap_t; }trap_t;
typedef struct FontPosition_s{ SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font,trap_t trap, SDL_Color color,SDL_Rect* position,int y,int i);
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);
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3); 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_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 #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment