Skip to content
Snippets Groups Projects
Commit 9874bef7 authored by Taha Belkhiri's avatar Taha Belkhiri
Browse files

ajout d'une map 20x20, et affichage de l'herbe et des arbres

parent 81e51fa6
No related branches found
No related tags found
No related merge requests found
travail_de_groupe/chef_oeuvre/Res/grass.png

313 B

travail_de_groupe/chef_oeuvre/Res/tree.png

1.11 KiB

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
\ No newline at end of file
No preview for this file type
......@@ -34,7 +34,7 @@ void *EventLoop(void *arg){
while(running){
switch(game_state){
case MENU : gestMenu();break;
//case GAME : gestGame();break;
case GAME : gestMenu();break;
default:printf("game state fault");break;
}
}
......
......@@ -7,6 +7,7 @@ int game_state;
int main(){
running = 1;
game_state = GAME;
readMapFromFile("map.txt");
MainLoop();
......
......@@ -16,6 +16,7 @@
#include "render.h"
#include "gest_event.h"
#include "map.h"
#define MENU 0
......
#include "map.h"
int map[MAPSIZE][MAPSIZE];
void readMapFromFile(char * filename){
FILE * fp;
int i, j;
fp = fopen(filename, "r");
if(fp == NULL){
printf("Error opening file\n");
exit(1);
}
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
fscanf(fp, "%d", &map[i][j]);
}
}
fclose(fp);
}
\ No newline at end of file
#ifndef MAP_HEADER_
#define MAP_HEADER_
#include "main.h"
#define MAPSIZE 20
extern int map[MAPSIZE][MAPSIZE];
void readMapFromFile(char * filename);
#endif
\ No newline at end of file
......@@ -4,9 +4,15 @@ SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *RobotoFont;
SDL_DisplayMode ScreenDimension;
SDL_Surface * grassSurface;
SDL_Texture * grassTexture;
SDL_Surface * treeSurface;
SDL_Texture * treeTexture;
void CreateWindow(){
SDL_DisplayMode ScreenDimension;
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Couldn't create window.");
......@@ -38,10 +44,44 @@ void CreateWindow(){
}
void drawMap(){
int i, j;
SDL_Rect rect;
rect.h = CELLSIZE;
rect.w = CELLSIZE;
rect.x = (ScreenDimension.w - (MAPSIZE * CELLSIZE)) / 2; // centers the drawing
rect.y = 0;
for(i = 0; i < MAPSIZE; i++){
for(j = 0; j < MAPSIZE; j++){
SDL_RenderCopy(renderer, grassTexture, NULL, &rect);
if(map[i][j] == 1){
SDL_RenderCopy(renderer, treeTexture, NULL, &rect);
}
rect.x += CELLSIZE;
}
rect.x = (ScreenDimension.w - (MAPSIZE * CELLSIZE)) / 2;
rect.y += CELLSIZE;
}
}
void drawGame(){
SDL_RenderClear(renderer);
drawMap();
SDL_RenderPresent(renderer);
}
void MainLoop(){
CreateWindow();
grassSurface = IMG_Load("Res/grass.png");
grassTexture = SDL_CreateTextureFromSurface(renderer, grassSurface);
treeSurface = IMG_Load("Res/tree.png");
treeTexture = SDL_CreateTextureFromSurface(renderer, treeSurface);
SDL_FreeSurface(grassSurface);
SDL_FreeSurface(treeSurface);
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
double delta = 0;
......@@ -62,7 +102,7 @@ void MainLoop(){
//Menu();
break;
case GAME:
//Game();
drawGame();
break;
}
}
......
......@@ -5,6 +5,7 @@
#include "gest_event.h"
#define FPS_TO_GET 60
#define CELLSIZE (ScreenDimension.h / MAPSIZE)
extern SDL_Window *window;
extern SDL_Renderer *renderer;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment