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

modification fait avec Rawan

parent c8c51305
Branches
No related tags found
No related merge requests found
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:
......
data.c 0 → 100644
#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);
data.h 0 → 100644
#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
#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
File added
main 0 → 100644
File added
main.c 0 → 100644
#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
main.o 0 → 100644
File added
#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);
......
map.o 0 → 100644
File added
#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);
}
#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
player.o 0 → 100644
File added
File added
File added
#####################################################
#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######
###########################################
###########################################
###########################################
###########################################
ressources/pavage.bmp

480 KiB

ressources/personnage1.bmp

24.1 KiB

#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment