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

Ajout squelette du chef d'oeuvre

parent a9f2d27e
Branches
No related tags found
No related merge requests found
{
"files.associations": {
"render.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)
\ No newline at end of file
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_ESCAPE:
running = 0;
continue;
default:
continue;
}
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 "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 = GAME;
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 "gest_event.h"
#define MENU 0
#define GAME 1
extern int running;
extern int game_state;
#endif
\ No newline at end of file
#include "render.h"
SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *RobotoFont;
void CreateWindow(){
SDL_DisplayMode ScreenDimension;
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 MainLoop(){
CreateWindow();
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) / 1000.0;
if (delta > 1/FPS_TO_GET){
b = a;
switch (game_state){
case MENU:
//Menu();
break;
case GAME:
//Game();
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 "gest_event.h"
#define FPS_TO_GET 60
extern SDL_Window *window;
extern SDL_Renderer *renderer;
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