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

nettoyage du main

parent 2aa2a688
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ LIBS = -L /SDL2_ttf/.libs -L /SDL2_image/.libs ...@@ -4,7 +4,7 @@ LIBS = -L /SDL2_ttf/.libs -L /SDL2_image/.libs
LDFLAGS = `sdl2-config --cflags --libs` LDFLAGS = `sdl2-config --cflags --libs`
INCLUDES = -I ./SDL2_ttf -I ./SDL2_image INCLUDES = -I ./SDL2_ttf -I ./SDL2_image
EXEC = main EXEC = main
SRC = main.c player.c map.c textures.c SRC = main.c player.c map.c textures.c data.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
all: $(EXEC) all: $(EXEC)
......
#include "data.h" #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* init_wall(int px,int py,int postx,int posty){
walls_t* walls= malloc(sizeof(walls_t)); walls_t* walls= malloc(sizeof(walls_t));
walls->bricks.w=BRICKW; walls->bricks.w=BRICKW;
...@@ -17,5 +10,5 @@ walls_t* init_wall(int px,int py,int postx,int posty){ ...@@ -17,5 +10,5 @@ walls_t* init_wall(int px,int py,int postx,int posty){
walls->Src.y=BRICKH*py; walls->Src.y=BRICKH*py;
walls->Src.w=BRICKW; walls->Src.w=BRICKW;
walls->Src.h=BRICKH; walls->Src.h=BRICKH;
} return walls;
//void gameOver(char** tab,int nbl,int nbc,base_t* base); }
\ No newline at end of file
...@@ -12,19 +12,10 @@ typedef struct walls_s ...@@ -12,19 +12,10 @@ typedef struct walls_s
{ {
SDL_Rect bricks; SDL_Rect bricks;
SDL_Rect Src; SDL_Rect Src;
SDL_Texture* texture;
}walls_t; }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); walls_t* init_wall(int px,int py,int postx,int posty);
void gameOver(char** tab,int nbl,int nbc,base_t* base);
#endif #endif
\ No newline at end of file
data.o 0 → 100644
File added
#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;
}
#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
No preview for this file type
...@@ -6,10 +6,10 @@ int main() ...@@ -6,10 +6,10 @@ int main()
int nbc = 0; int nbc = 0;
char** map =lire_map("ressources/maze_map.txt"); char** map =lire_map("ressources/maze_map.txt");
taille_map("ressources/maze_map.txt",&nbl,&nbc); taille_map("ressources/maze_map.txt",&nbl,&nbc);
player_t*player = init_player(32,32); player_t*player = init_player(1,1);
SDL_Window* fenetre; // Déclaration de la fenêtre SDL_Window* fenetre; // Déclaration de la fenêtre
bool terminer = false; walls_t* walls;
bool terminer=false;
if(SDL_Init(SDL_INIT_VIDEO) < 0) // Initialisation de la SDL if(SDL_Init(SDL_INIT_VIDEO) < 0) // Initialisation de la SDL
{ {
printf("Erreur d’initialisation de la SDL: %s",SDL_GetError()); printf("Erreur d’initialisation de la SDL: %s",SDL_GetError());
...@@ -30,22 +30,8 @@ int main() ...@@ -30,22 +30,8 @@ int main()
ecran = SDL_CreateRenderer(fenetre, -1, SDL_RENDERER_ACCELERATED); ecran = SDL_CreateRenderer(fenetre, -1, SDL_RENDERER_ACCELERATED);
// Charger l’images // Charger l’images
SDL_Texture* sprite= charger_image("ressources/pavage.bmp",ecran); SDL_Texture* sprite= charger_image("ressources/pavage.bmp",ecran);
//player->texture = charger_image("ressources/personnage1.bmp",ecran); player->texture= charger_image("ressources/personnage.bmp",ecran);
SDL_Event event;
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 // Boucle principale
while(!terminer){ while(!terminer){
while (SDL_PollEvent(&event)){ while (SDL_PollEvent(&event)){
...@@ -60,73 +46,43 @@ int main() ...@@ -60,73 +46,43 @@ int main()
} }
if(event.key.keysym.sym == SDLK_LEFT){ if(event.key.keysym.sym == SDLK_LEFT){
movement(map,player,'q'); // fléche vers la gauche movement(map,player,'q'); // fléche vers la gauche
} change_movement_player(player,1,1);
}
if(event.key.keysym.sym == SDLK_RIGHT){ if(event.key.keysym.sym == SDLK_RIGHT){
movement(map,player,'d'); // fléche vers la droite movement(map,player,'d'); // fléche vers la droite
} change_movement_player(player,1,2);
}
if(event.key.keysym.sym == SDLK_UP){ if(event.key.keysym.sym == SDLK_UP){
movement(map,player,'z');// fléche vers le haut movement(map,player,'z');// fléche vers le haut
change_movement_player(player,1,3);
} }
if(event.key.keysym.sym == SDLK_DOWN){ if(event.key.keysym.sym == SDLK_DOWN){
movement(map,player,'s');// fléche vers le bas movement(map,player,'s');// fléche vers le bas
} change_movement_player(player,1,0);
}
} }
} }
SDL_RenderClear(ecran); SDL_RenderClear(ecran);
SDL_Rect DestR[160];
SDL_Rect DestR_P[6];
SDL_Rect Src;
for (int i=0;i<nbl;i++){ for (int i=0;i<nbl;i++){
for (int j = 0; j< nbc; j++) for (int j = 0; j< nbc; j++)
{ {
if(map[i][j]=='L'){ if(map[i][j]=='L'){
DestR[i].x=8*width; walls=init_wall(j,i,9,3);
DestR[i].y=3*height; SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src));
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]=='#'){ else if(map[i][j]=='#'){
DestR[i].x=9*width; walls=init_wall(j,i,10,3);
DestR[i].y=3*height; SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src));
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'){ else if (map[i][j]=='S'){
DestR[i].x=13*width; walls=init_wall(j,i,13,3);
DestR[i].y=3*height; SDL_RenderCopy(ecran,sprite,&(walls->bricks),&(walls->Src));
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; SDL_RenderCopy(ecran,player->texture,&(player->DestR),&(player->Src));
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_RenderPresent(ecran);
SDL_UpdateWindowSurface(fenetre); SDL_UpdateWindowSurface(fenetre);
} }
......
main.o 0 → 100644
File added
map.o 0 → 100644
File added
...@@ -15,17 +15,24 @@ player_t* init_player(int posc,int posl){ ...@@ -15,17 +15,24 @@ player_t* init_player(int posc,int posl){
player->Src.h=PLAYERH; player->Src.h=PLAYERH;
player->Src.x=PLAYERW*player->x; player->Src.x=PLAYERW*player->x;
player->Src.y=PLAYERH*player->y; player->Src.y=PLAYERH*player->y;
player->lives=3;
player->gameover=0;
return player; return player;
} }
void change_movement_player(player_t* 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.x=PLAYERW*posc;
player->DestR.y=PLAYERH*posl; 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;
} }
/* void gamrover(player_t* player,char** tab, int nbc,int nbl){
void gameOver(char** tab,int nbl,int nbc,player_t* player){
for (int i = 0; i<nbl ; i++) for (int i = 0; i<nbl ; i++)
{ {
for (int j = 0; j<nbc; j++) for (int j = 0; j<nbc; j++)
...@@ -38,7 +45,7 @@ void gameOver(char** tab,int nbl,int nbc,player_t* player){ ...@@ -38,7 +45,7 @@ void gameOver(char** tab,int nbl,int nbc,player_t* player){
} }
} }
} }
*/
bool handle_movement_up(char** tab,player_t*player) 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'){
......
...@@ -14,7 +14,10 @@ typedef struct player_s ...@@ -14,7 +14,10 @@ typedef struct player_s
int score; int score;
SDL_Rect DestR; SDL_Rect DestR;
SDL_Rect Src; SDL_Rect Src;
SDL_Texture* texture;
int speed; int speed;
int lives;
int gameover;
}player_t; }player_t;
player_t* init_player(int posc,int posl); player_t* init_player(int posc,int posl);
...@@ -24,6 +27,7 @@ bool handle_movement_down(char** tab,player_t*player); ...@@ -24,6 +27,7 @@ bool handle_movement_down(char** tab,player_t*player);
bool handle_movement_left(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_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);
void gamrover(player_t* player,char** tab, int nbc,int nbl);
void free_player(player_t* player); void free_player(player_t* player);
#endif #endif
\ No newline at end of file
player.o 0 → 100644
File added
ressources/personnage.bmp

64.1 KiB

ressources/personnage1.bmp

24.1 KiB

File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment