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

GameOfLife fonctionnel

parent ff2cdcd9
No related branches found
No related tags found
No related merge requests found
travail_individuel/Belkhiri/GameOfLife/Res/background.png

28.5 KiB

0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 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 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0
No preview for this file type
...@@ -39,17 +39,25 @@ void gestGame(){ ...@@ -39,17 +39,25 @@ void gestGame(){
running = 0; running = 0;
break; break;
case SDL_KEYUP: case SDL_KEYDOWN:
switch (event.key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_x: case SDLK_x:
running = 0; running = 0;
continue; continue;
case SDLK_RIGHT:
updateMap(map);
continue;
case SDLK_s:
writeMap(map, "map.txt");
printf("Saved map to map.txt\n");
default: default:
continue; continue;
} }
break; continue;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT){ if (event.button.button == SDL_BUTTON_LEFT){
...@@ -62,7 +70,7 @@ void gestGame(){ ...@@ -62,7 +70,7 @@ void gestGame(){
map[j][i] = !map[j][i]; map[j][i] = !map[j][i];
} }
} }
break; continue;
default: default:
continue; continue;
......
...@@ -7,7 +7,7 @@ int map[MAPSIZE][MAPSIZE]; ...@@ -7,7 +7,7 @@ int map[MAPSIZE][MAPSIZE];
void initMap(int map[MAPSIZE][MAPSIZE]) { void initMap(int map[MAPSIZE][MAPSIZE]) {
for(int i = 0; i < MAPSIZE; i++){ for(int i = 0; i < MAPSIZE; i++){
for(int j = 0; j < MAPSIZE; j++){ for(int j = 0; j < MAPSIZE; j++){
map[i][j] = rand() % 2; map[i][j] = 0;
} }
} }
} }
...@@ -20,4 +20,16 @@ void printMap(int map[MAPSIZE][MAPSIZE]){ ...@@ -20,4 +20,16 @@ void printMap(int map[MAPSIZE][MAPSIZE]){
} }
printf("\n"); printf("\n");
} }
}
void writeMap(int map[MAPSIZE][MAPSIZE], char* filename){
FILE* f = fopen(filename, "w");
for(int i = 0; i < MAPSIZE; i++){
for(int j = 0; j < MAPSIZE; j++){
fprintf(f, "%d ", map[i][j]);
}
fprintf(f, "\n");
}
fclose(f);
} }
\ No newline at end of file
...@@ -8,5 +8,6 @@ extern int map[MAPSIZE][MAPSIZE]; ...@@ -8,5 +8,6 @@ extern int map[MAPSIZE][MAPSIZE];
void printMap(int map[MAPSIZE][MAPSIZE]); void printMap(int map[MAPSIZE][MAPSIZE]);
void initMap(int map[MAPSIZE][MAPSIZE]); void initMap(int map[MAPSIZE][MAPSIZE]);
void writeMap(int map[MAPSIZE][MAPSIZE], char* filename);
#endif #endif
\ No newline at end of file
...@@ -15,6 +15,9 @@ SDL_Surface * playButtonSurface = NULL; ...@@ -15,6 +15,9 @@ SDL_Surface * playButtonSurface = NULL;
SDL_Texture * columnTexture = NULL; SDL_Texture * columnTexture = NULL;
SDL_Surface * columnSurface = NULL; SDL_Surface * columnSurface = NULL;
SDL_Texture * backgroundTexture = NULL;
SDL_Surface * backgroundSurface = NULL;
void CreateWindow(){ void CreateWindow(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){ if (SDL_Init(SDL_INIT_VIDEO) != 0){
...@@ -52,6 +55,11 @@ void drawBackground(){ ...@@ -52,6 +55,11 @@ void drawBackground(){
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
} }
void drawBackground2(){
SDL_Rect rect = {0, 0, ScreenDimension.w, ScreenDimension.h};
SDL_RenderCopy(renderer, backgroundTexture, NULL, &rect);
}
void drawTitle(){ void drawTitle(){
int titleWidth, titleHeight; int titleWidth, titleHeight;
SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight); SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight);
...@@ -93,7 +101,6 @@ void drawMap(int map[MAPSIZE][MAPSIZE]){ ...@@ -93,7 +101,6 @@ void drawMap(int map[MAPSIZE][MAPSIZE]){
} }
void drawColumns(){ void drawColumns(){
// draw 2 columnTexture, 1 left 1 right
int columnWidth, columnHeight; int columnWidth, columnHeight;
SDL_QueryTexture(columnTexture, NULL, NULL, &columnWidth, &columnHeight); SDL_QueryTexture(columnTexture, NULL, NULL, &columnWidth, &columnHeight);
int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2; int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2;
...@@ -106,6 +113,7 @@ void drawColumns(){ ...@@ -106,6 +113,7 @@ void drawColumns(){
void drawGame(){ void drawGame(){
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
drawBackground(); drawBackground();
drawBackground2();
drawMap(map); drawMap(map);
drawColumns(); drawColumns();
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
...@@ -117,14 +125,17 @@ void MainLoop(){ ...@@ -117,14 +125,17 @@ void MainLoop(){
titleSurface = IMG_Load("Res/title.png"); titleSurface = IMG_Load("Res/title.png");
playButtonSurface = IMG_Load("Res/playButton.png"); playButtonSurface = IMG_Load("Res/playButton.png");
columnSurface = IMG_Load("Res/column.png"); columnSurface = IMG_Load("Res/column.png");
backgroundSurface = IMG_Load("Res/background.png");
titleTexture = SDL_CreateTextureFromSurface(renderer, titleSurface); titleTexture = SDL_CreateTextureFromSurface(renderer, titleSurface);
playButtonTexture = SDL_CreateTextureFromSurface(renderer, playButtonSurface); playButtonTexture = SDL_CreateTextureFromSurface(renderer, playButtonSurface);
columnTexture = SDL_CreateTextureFromSurface(renderer, columnSurface); columnTexture = SDL_CreateTextureFromSurface(renderer, columnSurface);
backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_FreeSurface(titleSurface); SDL_FreeSurface(titleSurface);
SDL_FreeSurface(playButtonSurface); SDL_FreeSurface(playButtonSurface);
SDL_FreeSurface(columnSurface); SDL_FreeSurface(columnSurface);
SDL_FreeSurface(backgroundSurface);
unsigned int a = SDL_GetTicks(); unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks(); unsigned int b = SDL_GetTicks();
...@@ -140,7 +151,6 @@ void MainLoop(){ ...@@ -140,7 +151,6 @@ void MainLoop(){
a = SDL_GetTicks(); a = SDL_GetTicks();
delta = (a - b); delta = (a - b);
if (delta > 1/FPS_TO_GET){ if (delta > 1/FPS_TO_GET){
printf("fps : %f\n", 1000/delta);
b = a; b = a;
switch (game_state){ switch (game_state){
case MENU: case MENU:
......
#include "utility.h"
int surviveRule[NB_RULES] = {0, 0, 1, 1, 0, 0, 0, 0, 0};
int bornRule[NB_RULES] = {0, 0, 0, 1, 0, 0, 0, 0, 0};
int survivingNeighbors(int x, int y){
int count = 0;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i == 0 && j == 0) continue;
if(map[(x+i+MAPSIZE)%MAPSIZE][(y+j+MAPSIZE)%MAPSIZE] == 1) count++;
}
}
return count;
}
void updateMap(){
int newMap[MAPSIZE][MAPSIZE];
for (int i=0; i<MAPSIZE; i++){
for (int j=0; j<MAPSIZE; j++){
int count = survivingNeighbors(j, i);
if (map[j][i] == 1){
if (!surviveRule[count]) newMap[j][i] = 0;
else newMap[j][i] = 1;
}
else{
if (bornRule[count]) newMap[j][i] = 1;
else newMap[j][i] = 0;
}
}
}
for (int i=0; i<MAPSIZE; i++){
for (int j=0; j<MAPSIZE; j++){
map[i][j] = newMap[i][j];
}
}
}
#ifndef _UTILITY_H_
#define _UTILITY_H_
#include "main.h"
#include "map.h"
#define NB_RULES 9
void updateMap();
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment