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

ajout affichage menu, map et gestion des clicks dans GameOfLife

parent 5b570ea9
No related branches found
No related tags found
No related merge requests found
Showing
with 380 additions and 0 deletions
{
"files.associations": {
"main.h": "c"
}
}
\ No newline at end of file
CC=gcc
LDFLAG=$(shell sdl2-config --cflags --libs) -lm -lSDL2_ttf -D_REENTRANT -lSDL2_image -pthread
CFLAG=-Wall $(shell sdl2-config --cflags --libs)
EXEC=run
SRC=$(wildcard src/*.c)
OBJ=$(SRC:.c=.o)
all:$(EXEC)
$(EXEC):$(OBJ)
$(CC) -o $@ $^ $(LDFLAG)
mv $^ bin/
%.o:%.c
$(CC) -o $@ -c $< $(CFLAG)
.PHONY:clean
clean:
rm -rf bin/*.o
rm -rf $(EXEC)
File added
travail_individuel/Belkhiri/GameOfLife/Res/column.png

3.83 KiB

travail_individuel/Belkhiri/GameOfLife/Res/playButton.png

9.39 KiB

travail_individuel/Belkhiri/GameOfLife/Res/title.png

8.42 KiB

File added
#include "gest_event.h"
void gestMenu(){
SDL_Event event;
while (SDL_PollEvent(&event)){
switch(event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_x:
running = 0;
continue;
default:
game_state = GAME;
continue;
}
break;
default:
continue;
}
}
SDL_Delay(5);
}
void gestGame(){
SDL_Event event;
while (SDL_PollEvent(&event)){
switch(event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_x:
running = 0;
continue;
default:
continue;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT){
int x = event.button.x;
int y = event.button.y;
// change value of map[x][y]
if (x < ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2 + MAPSIZE*CELLSIZE && x > ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2){
int i = (x - ScreenDimension.w/2 + MAPSIZE*CELLSIZE/2)/CELLSIZE;
int j = (y)/CELLSIZE;
map[j][i] = !map[j][i];
}
}
break;
default:
continue;
}
}
SDL_Delay(5);
}
void *EventLoop(void *arg){
while(running){
switch(game_state){
case MENU : gestMenu();break;
case GAME : gestGame();break;
default:printf("game state fault");break;
}
}
return NULL;
}
\ No newline at end of file
#ifndef _GEST_EVENT_H_
#define _GEST_EVENT_H_
#include "main.h"
#include "utility.h"
#include "render.h"
void *EventLoop(void *arg);
#endif
\ No newline at end of file
#include "main.h"
int running;
int game_state;
int main(){
running = 1;
game_state = MENU;
initMap(map);
MainLoop();
}
\ No newline at end of file
#ifndef _MAIN_HEADER_
#define _MAIN_HEADER_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <pthread.h>
#include "render.h"
#include "utility.h"
#include "gest_event.h"
#include "map.h"
#define MENU 0
#define GAME 1
extern int running;
extern int game_state;
#endif
\ No newline at end of file
#include "map.h"
int map[MAPSIZE][MAPSIZE];
void initMap(int map[MAPSIZE][MAPSIZE]) {
for(int i = 0; i < MAPSIZE; i++){
for(int j = 0; j < MAPSIZE; j++){
map[i][j] = rand() % 2;
}
}
}
void printMap(int map[MAPSIZE][MAPSIZE]){
for(int i = 0; i < MAPSIZE; i++){
for(int j = 0; j < MAPSIZE; j++){
printf("%d ", map[i][j]);
}
printf("\n");
}
}
\ No newline at end of file
#ifndef _MAP_HEADER_
#define _MAP_HEADER_
#include "main.h"
#define MAPSIZE 20
extern int map[MAPSIZE][MAPSIZE];
void printMap(int map[MAPSIZE][MAPSIZE]);
void initMap(int map[MAPSIZE][MAPSIZE]);
#endif
\ No newline at end of file
#include "render.h"
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_DisplayMode ScreenDimension;
TTF_Font *RobotoFont;
SDL_Texture * titleTexture = NULL;
SDL_Surface * titleSurface = NULL;
SDL_Texture * playButtonTexture = NULL;
SDL_Surface * playButtonSurface = NULL;
SDL_Texture * columnTexture = NULL;
SDL_Surface * columnSurface = NULL;
void CreateWindow(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Couldn't create window.");
exit(EXIT_FAILURE);
}
SDL_GetCurrentDisplayMode(0, &ScreenDimension);
window = SDL_CreateWindow("Game Of Life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, ScreenDimension.w, ScreenDimension.h, SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
if (window == NULL){
printf("Couldn't create window");
exit(EXIT_FAILURE);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
printf("Couldn't create renderer.");
exit(EXIT_FAILURE);
}
if (TTF_Init() == -1)
{
exit(EXIT_FAILURE);
}
RobotoFont = TTF_OpenFont("Res/Roboto-Black.ttf", 50);
}
void drawBackground(){
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect = {0, 0, ScreenDimension.w, ScreenDimension.h};
SDL_RenderFillRect(renderer, &rect);
}
void drawTitle(){
int titleWidth, titleHeight;
SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight);
SDL_Rect titleRect = {ScreenDimension.w/2 - titleWidth/2, ScreenDimension.h/2 - titleHeight, titleWidth, titleHeight};
SDL_RenderCopy(renderer, titleTexture, NULL, &titleRect);
}
void drawPlayButton(){
int titleWidth, titleHeight;
SDL_QueryTexture(titleTexture, NULL, NULL, &titleWidth, &titleHeight);
int playButtonWidth, playButtonHeight;
SDL_QueryTexture(playButtonTexture, NULL, NULL, &playButtonWidth, &playButtonHeight);
playButtonHeight = playButtonHeight / 2;
playButtonWidth = playButtonWidth / 2;
SDL_Rect playButtonRect = {ScreenDimension.w/2 - playButtonWidth/2, ScreenDimension.h/2 + titleHeight, playButtonWidth, playButtonHeight};
SDL_RenderCopy(renderer, playButtonTexture, NULL, &playButtonRect);
}
void drawMenu(){
SDL_RenderClear(renderer);
drawBackground();
drawTitle();
drawPlayButton();
SDL_RenderPresent(renderer);
}
void drawMap(int map[MAPSIZE][MAPSIZE]){
int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2;
for(int i = 0; i < MAPSIZE; i++){
for(int j = 0; j < MAPSIZE; j++){
if(map[i][j] == 1){
SDL_Rect rect = {x_offset + j*CELLSIZE, i*CELLSIZE, CELLSIZE, CELLSIZE};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
}
}
}
void drawColumns(){
// draw 2 columnTexture, 1 left 1 right
int columnWidth, columnHeight;
SDL_QueryTexture(columnTexture, NULL, NULL, &columnWidth, &columnHeight);
int x_offset = ScreenDimension.w/2 - MAPSIZE*CELLSIZE/2;
SDL_Rect leftRect = {0, -50, columnWidth, ScreenDimension.h + 100};
SDL_Rect rightRect = {x_offset + MAPSIZE*CELLSIZE + columnWidth/2, -50, columnWidth, ScreenDimension.h + 100};
SDL_RenderCopy(renderer, columnTexture, NULL, &leftRect);
SDL_RenderCopy(renderer, columnTexture, NULL, &rightRect);
}
void drawGame(){
SDL_RenderClear(renderer);
drawBackground();
drawMap(map);
drawColumns();
SDL_RenderPresent(renderer);
}
void MainLoop(){
CreateWindow();
titleSurface = IMG_Load("Res/title.png");
playButtonSurface = IMG_Load("Res/playButton.png");
columnSurface = IMG_Load("Res/column.png");
titleTexture = SDL_CreateTextureFromSurface(renderer, titleSurface);
playButtonTexture = SDL_CreateTextureFromSurface(renderer, playButtonSurface);
columnTexture = SDL_CreateTextureFromSurface(renderer, columnSurface);
SDL_FreeSurface(titleSurface);
SDL_FreeSurface(playButtonSurface);
SDL_FreeSurface(columnSurface);
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
double delta = 0;
pthread_t eventThread;
if (pthread_create(&eventThread, NULL, EventLoop, NULL) != 0){
printf("Couldn't create thread.");
exit(EXIT_FAILURE);
}
while (running){
a = SDL_GetTicks();
delta = (a - b);
if (delta > 1/FPS_TO_GET){
printf("fps : %f\n", 1000/delta);
b = a;
switch (game_state){
case MENU:
drawMenu();
break;
case GAME:
drawGame();
break;
}
}
else {
// fait dormir le thread pour garder des ressources
usleep(1000 * (1/FPS_TO_GET - delta));
}
}
}
\ No newline at end of file
#ifndef _RENDER_H_
#define _RENDER_H_
#include "main.h"
#include "utility.h"
#include "gest_event.h"
#define FPS_TO_GET 1000/60.0
#define CELLSIZE (ScreenDimension.h/MAPSIZE)
extern SDL_Window *window;
extern SDL_Renderer *renderer;
extern SDL_DisplayMode ScreenDimension;
void MainLoop();
#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