diff --git a/README.md b/README.md index 86bd12df76309da0927f046e68483e211e928ab3..8cf93b44d61fb3c3f4c9950284db4b63b860d275 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,8 @@ Trello : https://trello.com/b/YGhNeYX3/projet-apprentissage-par-renforcement │ │ ├── x_fenetre │ │ ├── snakes │ │ └── GameOfLife -│ ├── Beret +│ ├── Beret +│ │ ├── snakes │ │ └── x_fenetre │ └── Meyer │ ├── x_fenetre diff --git a/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json b/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json index 17f009151d05d549c7b9e379848b7983402aca52..73d1098f5d406b5126d47235226f8567b396165e 100644 --- a/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json +++ b/travail_individuel/Belkhiri/GameOfLife/.vscode/settings.json @@ -1,5 +1,5 @@ -{ - "files.associations": { - "main.h": "c" - } +{ + "files.associations": { + "main.h": "c" + } } \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/Makefile b/travail_individuel/Belkhiri/GameOfLife/Makefile index 7a37dccc5fa59a8272d8e92a2494d76748d19625..e17f11daea9751b0b718d7a24c09300edcf5fc2b 100644 --- a/travail_individuel/Belkhiri/GameOfLife/Makefile +++ b/travail_individuel/Belkhiri/GameOfLife/Makefile @@ -1,23 +1,23 @@ -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) +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) diff --git a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c index 8b3a2d5894b9d81f214dd27bad46a1caf9cfc43b..33a437b6e791dad599fdf84ce5a45b405a89e1b2 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.c @@ -1,84 +1,84 @@ -#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; +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h index c5c76e37ddffc777052540d5c6668199b00b39c2..2baf6e38d0424c8328f928f0c20e8bb059000c7d 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/gest_event.h @@ -1,11 +1,11 @@ -#ifndef _GEST_EVENT_H_ -#define _GEST_EVENT_H_ - - -#include "main.h" -#include "utility.h" -#include "render.h" - -void *EventLoop(void *arg); - +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/main.c b/travail_individuel/Belkhiri/GameOfLife/src/main.c index 30afa9439497ca314a78ae8417ff7b7e34324ca1..967862f6bd5cd0179ee1ce3294ebd002f7a87617 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/main.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/main.c @@ -1,14 +1,14 @@ -#include "main.h" - -int running; -int game_state; - - -int main(){ - running = 1; - game_state = MENU; - initMap(map); - - MainLoop(); - +#include "main.h" + +int running; +int game_state; + + +int main(){ + running = 1; + game_state = MENU; + initMap(map); + + MainLoop(); + } \ No newline at end of file diff --git a/travail_individuel/Belkhiri/GameOfLife/src/main.h b/travail_individuel/Belkhiri/GameOfLife/src/main.h index 73ac9527544c6acc5af9e8e6cca57e3edda73a1e..7e4bdfa54d1b506290f8c78ecae2cdccc127168f 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/main.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/main.h @@ -1,30 +1,30 @@ -#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; - - +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.c b/travail_individuel/Belkhiri/GameOfLife/src/map.c index cb922b4765c5aed5624acf3bc05775e9abe57ac7..7b60b81c7d34ddeaa96a9e85389b71be4db3498d 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.c @@ -1,23 +1,23 @@ -#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"); - } +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/map.h b/travail_individuel/Belkhiri/GameOfLife/src/map.h index 111b8545fc513e390c2d75b9c9f0fe61dd046b0a..1fca7b95abba057bf77555bb74c3a963c0dde49c 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/map.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/map.h @@ -1,12 +1,12 @@ -#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]); - +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.c b/travail_individuel/Belkhiri/GameOfLife/src/render.c index 9889be58826005c54def13e958b8b02843bfa837..e5ea603cdc53334daff2306484f6695be14f02e4 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/render.c +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.c @@ -1,160 +1,160 @@ -#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)); - } - - } +#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 diff --git a/travail_individuel/Belkhiri/GameOfLife/src/render.h b/travail_individuel/Belkhiri/GameOfLife/src/render.h index ead06c849701218040c65cf14d50d93be83ba1ed..05dfe33ec768ebbfaff1137c704d71afdd9934a7 100644 --- a/travail_individuel/Belkhiri/GameOfLife/src/render.h +++ b/travail_individuel/Belkhiri/GameOfLife/src/render.h @@ -1,18 +1,18 @@ -#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(); - +#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 diff --git a/travail_individuel/Belkhiri/snakes/Makefile b/travail_individuel/Belkhiri/snakes/Makefile index e76de77896acfdf5d8d8e97b8a1c9504c2364375..040996e0bcfbcc7ddce2272c87c890aaf17130a0 100644 --- a/travail_individuel/Belkhiri/snakes/Makefile +++ b/travail_individuel/Belkhiri/snakes/Makefile @@ -1,21 +1,21 @@ -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 *.c) -OBJ=$(SRC:.c=.o) - -all:$(EXEC) - -$(EXEC):$(OBJ) - $(CC) -o $@ $^ $(LDFLAG) - -%.o:%.c - $(CC) -o $@ -c $< $(CFLAG) - -.PHONY:clean - -clean: +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 *.c) +OBJ=$(SRC:.c=.o) + +all:$(EXEC) + +$(EXEC):$(OBJ) + $(CC) -o $@ $^ $(LDFLAG) + +%.o:%.c + $(CC) -o $@ -c $< $(CFLAG) + +.PHONY:clean + +clean: rm -rf $(EXEC) \ No newline at end of file diff --git a/travail_individuel/Belkhiri/snakes/snakes.c b/travail_individuel/Belkhiri/snakes/snakes.c index f2cd98011316231e5ba48287b2a18ac5eae3cef7..8f379b9895b32adf98779ae7f54c7b2d0b267510 100644 --- a/travail_individuel/Belkhiri/snakes/snakes.c +++ b/travail_individuel/Belkhiri/snakes/snakes.c @@ -1,233 +1,233 @@ -#include <SDL2/SDL.h> -#include <math.h> -#include <stdio.h> -#include <string.h> - -#define NB_BLOCS 4 -#define NB_SNAKES 10 - -typedef struct bloc { - int p1[2]; - int p2[2]; - int p3[2]; - int p4[2]; -} bloc_t; - -typedef struct snake { - bloc_t * blocs[NB_BLOCS]; -} snake_t; - - -int running = 1; - -void end_sdl(char ok, - char const* msg, - SDL_Window* window, - SDL_Renderer* renderer) { - char msg_formated[255]; - int l; - - if (!ok) { - strncpy(msg_formated, msg, 250); - l = strlen(msg_formated); - strcpy(msg_formated + l, " : %s\n"); - - SDL_Log(msg_formated, SDL_GetError()); - } - - if (renderer != NULL) { - SDL_DestroyRenderer(renderer); - renderer = NULL; - } - if (window != NULL) { - SDL_DestroyWindow(window); - window= NULL; - } - - SDL_Quit(); - - if (!ok) { - exit(EXIT_FAILURE); - } -} - -void draw_snake(SDL_Renderer* renderer, snake_t * Snake_to_draw) { - for (int i = 0; i < NB_BLOCS; i++) { - SDL_SetRenderDrawColor(renderer, rand() % 255, rand() % 255, rand() % 255, 255); - SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p1[0], Snake_to_draw->blocs[i]->p1[1], Snake_to_draw->blocs[i]->p2[0], Snake_to_draw->blocs[i]->p2[1]); - SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p2[0], Snake_to_draw->blocs[i]->p2[1], Snake_to_draw->blocs[i]->p3[0], Snake_to_draw->blocs[i]->p3[1]); - SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p3[0], Snake_to_draw->blocs[i]->p3[1], Snake_to_draw->blocs[i]->p4[0], Snake_to_draw->blocs[i]->p4[1]); - SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p4[0], Snake_to_draw->blocs[i]->p4[1], Snake_to_draw->blocs[i]->p1[0], Snake_to_draw->blocs[i]->p1[1]); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - } -} - -snake_t * init_snake(int x, int y, int h, int w) { - snake_t * snake = malloc(sizeof(snake_t)); - for (int i = 0; i < NB_BLOCS; i++) { - snake->blocs[i] = malloc(sizeof(bloc_t)); - if (i == 0){ - snake->blocs[i]->p1[0] = x; - snake->blocs[i]->p1[1] = y; - snake->blocs[i]->p2[0] = x; - snake->blocs[i]->p2[1] = y+h; - snake->blocs[i]->p3[0] = x+w; - snake->blocs[i]->p3[1] = y+h; - snake->blocs[i]->p4[0] = x+w; - snake->blocs[i]->p4[1] = y; - } - else { - snake->blocs[i]->p1[0] = snake->blocs[i-1]->p2[0]; - snake->blocs[i]->p1[1] = snake->blocs[i-1]->p2[1]; - snake->blocs[i]->p2[0] = snake->blocs[i]->p1[0]; - snake->blocs[i]->p2[1] = snake->blocs[i]->p1[1] + h/(i+2); - snake->blocs[i]->p4[0] = snake->blocs[i-1]->p3[0]; - snake->blocs[i]->p4[1] = snake->blocs[i-1]->p3[1]; - snake->blocs[i]->p3[0] = snake->blocs[i]->p4[0]; - snake->blocs[i]->p3[1] = snake->blocs[i]->p4[1] + h/(i+2); - } - } - return snake; -} - -void rotate_bloc(float angle, bloc_t * bloc){ - float x1 = bloc->p1[0]; - float y1 = bloc->p1[1]; - float x2 = bloc->p2[0]; - float y2 = bloc->p2[1]; - float x3 = bloc->p3[0]; - float y3 = bloc->p3[1]; - float x4 = bloc->p4[0]; - float y4 = bloc->p4[1]; - // middle is center of rotation - float middle_x = (x1 + x4)/2; - float middle_y = (y1 + y4)/2; - - float x1_new = x1 - middle_x; - float y1_new = y1 - middle_y; - float x2_new = x2 - middle_x; - float y2_new = y2 - middle_y; - float x3_new = x3 - middle_x; - float y3_new = y3 - middle_y; - float x4_new = x4 - middle_x; - float y4_new = y4 - middle_y; - // rotate - x1_new = x1_new * cos(angle) - y1_new * sin(angle); - y1_new = x1_new * sin(angle) + y1_new * cos(angle); - x2_new = x2_new * cos(angle) - y2_new * sin(angle); - y2_new = x2_new * sin(angle) + y2_new * cos(angle); - x3_new = x3_new * cos(angle) - y3_new * sin(angle); - y3_new = x3_new * sin(angle) + y3_new * cos(angle); - x4_new = x4_new * cos(angle) - y4_new * sin(angle); - y4_new = x4_new * sin(angle) + y4_new * cos(angle); - // move back - x1_new = x1_new + middle_x; - y1_new = y1_new + middle_y; - x2_new = x2_new + middle_x; - y2_new = y2_new + middle_y; - x3_new = x3_new + middle_x; - y3_new = y3_new + middle_y; - x4_new = x4_new + middle_x; - y4_new = y4_new + middle_y; - // assign new values - bloc->p1[0] = x1_new; - bloc->p1[1] = y1_new; - bloc->p2[0] = x2_new; - bloc->p2[1] = y2_new; - bloc->p3[0] = x3_new; - bloc->p3[1] = y3_new; - bloc->p4[0] = x4_new; - bloc->p4[1] = y4_new; -} - -float random_angle_radian() { - return (float)rand() / (float)RAND_MAX * 2 * M_PI; -} - -void move_bloc(int x, int y, bloc_t * bloc){ - bloc->p1[0] += x; - bloc->p1[1] += y; - bloc->p2[0] += x; - bloc->p2[1] += y; - bloc->p3[0] += x; - bloc->p3[1] += y; - bloc->p4[0] += x; - bloc->p4[1] += y; -} - -void move_snake(int x, int y, snake_t * snake){ - for (int i = NB_BLOCS-1; i >= 0; i--) { - move_bloc(x, y, snake->blocs[i]); - } -} - -void rotate_snake(float angle, snake_t * snake) { - for (int i = 0; i < NB_BLOCS; i++) { - if (i == 0){ - rotate_bloc(angle, snake->blocs[i]); - } - else { - rotate_bloc(angle, snake->blocs[i]); - snake->blocs[i]->p1[0] = snake->blocs[i-1]->p2[0]; - snake->blocs[i]->p1[1] = snake->blocs[i-1]->p2[1]; - snake->blocs[i]->p4[0] = snake->blocs[i-1]->p3[0]; - snake->blocs[i]->p4[1] = snake->blocs[i-1]->p3[1]; - } - } -} - -int main(int argc, char** argv) { - (void)argc; - (void)argv; - - SDL_Window* window = NULL; - SDL_Renderer* renderer = NULL; - - SDL_DisplayMode screen; - - /*********************************************************************************************************************/ - /* Initialisation de la SDL + gestion de l'échec possible */ - if (SDL_Init(SDL_INIT_VIDEO) != 0) end_sdl(0, "ERROR SDL INIT", window, renderer); - - SDL_GetCurrentDisplayMode(0, &screen); - printf("Résolution écran\n\tw : %d\n\th : %d\n", - screen.w, screen.h); - - /* Création de la fenêtre */ - window = SDL_CreateWindow("Premier dessin", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, screen.w, - screen.h, - SDL_WINDOW_OPENGL); - if (window == NULL) end_sdl(0, "ERROR WINDOW CREATION", window, renderer); - - /* Création du renderer */ - renderer = SDL_CreateRenderer(window, -1, - SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (renderer == NULL) end_sdl(0, "ERROR RENDERER CREATION", window, renderer); - - snake_t * snakeList[NB_SNAKES]; - for (int i = 0; i < NB_SNAKES; i++) { - // random int between 0 and WIDTH - - snakeList[i] = init_snake(rand()%screen.w/2, rand()%screen.h/2, rand()%200, rand()%50); - } - - for (int i=0; i< 50; i++){ - for (int j=0; j<NB_SNAKES; j++){ - float angle = 0.2 * pow(-1, 1 + rand()%2); - rotate_snake(angle, snakeList[j]); - move_snake(rand()%10, rand()%10, snakeList[j]); - draw_snake(renderer, snakeList[j]); - } - SDL_RenderPresent(renderer); - SDL_Delay(100); - SDL_RenderClear(renderer); - } - - - - /* on referme proprement la SDL */ - end_sdl(1, "Normal ending", window, renderer); - return EXIT_SUCCESS; +#include <SDL2/SDL.h> +#include <math.h> +#include <stdio.h> +#include <string.h> + +#define NB_BLOCS 4 +#define NB_SNAKES 10 + +typedef struct bloc { + int p1[2]; + int p2[2]; + int p3[2]; + int p4[2]; +} bloc_t; + +typedef struct snake { + bloc_t * blocs[NB_BLOCS]; +} snake_t; + + +int running = 1; + +void end_sdl(char ok, + char const* msg, + SDL_Window* window, + SDL_Renderer* renderer) { + char msg_formated[255]; + int l; + + if (!ok) { + strncpy(msg_formated, msg, 250); + l = strlen(msg_formated); + strcpy(msg_formated + l, " : %s\n"); + + SDL_Log(msg_formated, SDL_GetError()); + } + + if (renderer != NULL) { + SDL_DestroyRenderer(renderer); + renderer = NULL; + } + if (window != NULL) { + SDL_DestroyWindow(window); + window= NULL; + } + + SDL_Quit(); + + if (!ok) { + exit(EXIT_FAILURE); + } +} + +void draw_snake(SDL_Renderer* renderer, snake_t * Snake_to_draw) { + for (int i = 0; i < NB_BLOCS; i++) { + SDL_SetRenderDrawColor(renderer, rand() % 255, rand() % 255, rand() % 255, 255); + SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p1[0], Snake_to_draw->blocs[i]->p1[1], Snake_to_draw->blocs[i]->p2[0], Snake_to_draw->blocs[i]->p2[1]); + SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p2[0], Snake_to_draw->blocs[i]->p2[1], Snake_to_draw->blocs[i]->p3[0], Snake_to_draw->blocs[i]->p3[1]); + SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p3[0], Snake_to_draw->blocs[i]->p3[1], Snake_to_draw->blocs[i]->p4[0], Snake_to_draw->blocs[i]->p4[1]); + SDL_RenderDrawLine(renderer, Snake_to_draw->blocs[i]->p4[0], Snake_to_draw->blocs[i]->p4[1], Snake_to_draw->blocs[i]->p1[0], Snake_to_draw->blocs[i]->p1[1]); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + } +} + +snake_t * init_snake(int x, int y, int h, int w) { + snake_t * snake = malloc(sizeof(snake_t)); + for (int i = 0; i < NB_BLOCS; i++) { + snake->blocs[i] = malloc(sizeof(bloc_t)); + if (i == 0){ + snake->blocs[i]->p1[0] = x; + snake->blocs[i]->p1[1] = y; + snake->blocs[i]->p2[0] = x; + snake->blocs[i]->p2[1] = y+h; + snake->blocs[i]->p3[0] = x+w; + snake->blocs[i]->p3[1] = y+h; + snake->blocs[i]->p4[0] = x+w; + snake->blocs[i]->p4[1] = y; + } + else { + snake->blocs[i]->p1[0] = snake->blocs[i-1]->p2[0]; + snake->blocs[i]->p1[1] = snake->blocs[i-1]->p2[1]; + snake->blocs[i]->p2[0] = snake->blocs[i]->p1[0]; + snake->blocs[i]->p2[1] = snake->blocs[i]->p1[1] + h/(i+2); + snake->blocs[i]->p4[0] = snake->blocs[i-1]->p3[0]; + snake->blocs[i]->p4[1] = snake->blocs[i-1]->p3[1]; + snake->blocs[i]->p3[0] = snake->blocs[i]->p4[0]; + snake->blocs[i]->p3[1] = snake->blocs[i]->p4[1] + h/(i+2); + } + } + return snake; +} + +void rotate_bloc(float angle, bloc_t * bloc){ + float x1 = bloc->p1[0]; + float y1 = bloc->p1[1]; + float x2 = bloc->p2[0]; + float y2 = bloc->p2[1]; + float x3 = bloc->p3[0]; + float y3 = bloc->p3[1]; + float x4 = bloc->p4[0]; + float y4 = bloc->p4[1]; + // middle is center of rotation + float middle_x = (x1 + x4)/2; + float middle_y = (y1 + y4)/2; + + float x1_new = x1 - middle_x; + float y1_new = y1 - middle_y; + float x2_new = x2 - middle_x; + float y2_new = y2 - middle_y; + float x3_new = x3 - middle_x; + float y3_new = y3 - middle_y; + float x4_new = x4 - middle_x; + float y4_new = y4 - middle_y; + // rotate + x1_new = x1_new * cos(angle) - y1_new * sin(angle); + y1_new = x1_new * sin(angle) + y1_new * cos(angle); + x2_new = x2_new * cos(angle) - y2_new * sin(angle); + y2_new = x2_new * sin(angle) + y2_new * cos(angle); + x3_new = x3_new * cos(angle) - y3_new * sin(angle); + y3_new = x3_new * sin(angle) + y3_new * cos(angle); + x4_new = x4_new * cos(angle) - y4_new * sin(angle); + y4_new = x4_new * sin(angle) + y4_new * cos(angle); + // move back + x1_new = x1_new + middle_x; + y1_new = y1_new + middle_y; + x2_new = x2_new + middle_x; + y2_new = y2_new + middle_y; + x3_new = x3_new + middle_x; + y3_new = y3_new + middle_y; + x4_new = x4_new + middle_x; + y4_new = y4_new + middle_y; + // assign new values + bloc->p1[0] = x1_new; + bloc->p1[1] = y1_new; + bloc->p2[0] = x2_new; + bloc->p2[1] = y2_new; + bloc->p3[0] = x3_new; + bloc->p3[1] = y3_new; + bloc->p4[0] = x4_new; + bloc->p4[1] = y4_new; +} + +float random_angle_radian() { + return (float)rand() / (float)RAND_MAX * 2 * M_PI; +} + +void move_bloc(int x, int y, bloc_t * bloc){ + bloc->p1[0] += x; + bloc->p1[1] += y; + bloc->p2[0] += x; + bloc->p2[1] += y; + bloc->p3[0] += x; + bloc->p3[1] += y; + bloc->p4[0] += x; + bloc->p4[1] += y; +} + +void move_snake(int x, int y, snake_t * snake){ + for (int i = NB_BLOCS-1; i >= 0; i--) { + move_bloc(x, y, snake->blocs[i]); + } +} + +void rotate_snake(float angle, snake_t * snake) { + for (int i = 0; i < NB_BLOCS; i++) { + if (i == 0){ + rotate_bloc(angle, snake->blocs[i]); + } + else { + rotate_bloc(angle, snake->blocs[i]); + snake->blocs[i]->p1[0] = snake->blocs[i-1]->p2[0]; + snake->blocs[i]->p1[1] = snake->blocs[i-1]->p2[1]; + snake->blocs[i]->p4[0] = snake->blocs[i-1]->p3[0]; + snake->blocs[i]->p4[1] = snake->blocs[i-1]->p3[1]; + } + } +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + + SDL_DisplayMode screen; + + /*********************************************************************************************************************/ + /* Initialisation de la SDL + gestion de l'échec possible */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) end_sdl(0, "ERROR SDL INIT", window, renderer); + + SDL_GetCurrentDisplayMode(0, &screen); + printf("Résolution écran\n\tw : %d\n\th : %d\n", + screen.w, screen.h); + + /* Création de la fenêtre */ + window = SDL_CreateWindow("Premier dessin", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, screen.w, + screen.h, + SDL_WINDOW_OPENGL); + if (window == NULL) end_sdl(0, "ERROR WINDOW CREATION", window, renderer); + + /* Création du renderer */ + renderer = SDL_CreateRenderer(window, -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) end_sdl(0, "ERROR RENDERER CREATION", window, renderer); + + snake_t * snakeList[NB_SNAKES]; + for (int i = 0; i < NB_SNAKES; i++) { + // random int between 0 and WIDTH + + snakeList[i] = init_snake(rand()%screen.w/2, rand()%screen.h/2, rand()%200, rand()%50); + } + + for (int i=0; i< 50; i++){ + for (int j=0; j<NB_SNAKES; j++){ + float angle = 0.2 * pow(-1, 1 + rand()%2); + rotate_snake(angle, snakeList[j]); + move_snake(rand()%10, rand()%10, snakeList[j]); + draw_snake(renderer, snakeList[j]); + } + SDL_RenderPresent(renderer); + SDL_Delay(100); + SDL_RenderClear(renderer); + } + + + + /* on referme proprement la SDL */ + end_sdl(1, "Normal ending", window, renderer); + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/travail_individuel/Belkhiri/x_fenetre/Makefile b/travail_individuel/Belkhiri/x_fenetre/Makefile index e76de77896acfdf5d8d8e97b8a1c9504c2364375..040996e0bcfbcc7ddce2272c87c890aaf17130a0 100644 --- a/travail_individuel/Belkhiri/x_fenetre/Makefile +++ b/travail_individuel/Belkhiri/x_fenetre/Makefile @@ -1,21 +1,21 @@ -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 *.c) -OBJ=$(SRC:.c=.o) - -all:$(EXEC) - -$(EXEC):$(OBJ) - $(CC) -o $@ $^ $(LDFLAG) - -%.o:%.c - $(CC) -o $@ -c $< $(CFLAG) - -.PHONY:clean - -clean: +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 *.c) +OBJ=$(SRC:.c=.o) + +all:$(EXEC) + +$(EXEC):$(OBJ) + $(CC) -o $@ $^ $(LDFLAG) + +%.o:%.c + $(CC) -o $@ -c $< $(CFLAG) + +.PHONY:clean + +clean: rm -rf $(EXEC) \ No newline at end of file diff --git a/travail_individuel/Belkhiri/x_fenetre/X.c b/travail_individuel/Belkhiri/x_fenetre/X.c index 2d5a68fbf92fd56439bdb235ff3782c02e99f836..2440bbabbd3c401f7fbca8640da16b1a1ed710bd 100644 --- a/travail_individuel/Belkhiri/x_fenetre/X.c +++ b/travail_individuel/Belkhiri/x_fenetre/X.c @@ -1,100 +1,100 @@ -#include <SDL2/SDL.h> -#include <stdio.h> - - -int main(int argc, char **argv) { - (void)argc; - (void)argv; - - int screen_width = 1079; - int screen_height = 1920; - - SDL_Window * windows[20]; - - int bounce_count = 0; - int amplitude_bounce = 200; - int left_most_position = 0; - int top_most_position = 0; - - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - SDL_Log("Error : SDL initialisation - %s\n", - SDL_GetError()); - exit(EXIT_FAILURE); - } - int pas_y = (screen_height)/20; - int pas_x = (screen_width)/20; - int last_drawy = 0; - - for (int i = 0; i < 10; i++) { - int draw_x = top_most_position + i * pas_x; - int draw_y = left_most_position + i * pas_y; - last_drawy = draw_y; - windows[i] = SDL_CreateWindow("Window", - draw_y , draw_x, - 400, 300, - SDL_WINDOW_SHOWN); - if (windows[i] == NULL) { - SDL_Log("Error : SDL window creation - %s\n", - SDL_GetError()); - exit(EXIT_FAILURE); - } - SDL_Delay(50); - } - - for (int i = 10; i < 20; i++) { - int draw_x = top_most_position + (i-10) * pas_x; - int draw_y = last_drawy - (i-10) * pas_y; - windows[i] = SDL_CreateWindow("Window", - draw_y , draw_x, - 400, 300, - SDL_WINDOW_SHOWN); - if (windows[i] == NULL) { - SDL_Log("Error : SDL window creation - %s\n", - SDL_GetError()); - exit(EXIT_FAILURE); - } - SDL_Delay(50); - } - - SDL_Delay(100); - - while (bounce_count < 50) { - - - for (int i = 0; i < 10; i++) { - int draw_x = top_most_position + i * pas_x; - int draw_y = left_most_position + i * pas_y; - last_drawy = draw_y; - SDL_SetWindowPosition(windows[i], - draw_y , draw_x); - - //SDL_Delay(50); - } - - for (int i = 10; i < 20; i++) { - int draw_x = top_most_position + (i-10) * pas_x; - int draw_y = last_drawy - (i-10) * pas_y; - SDL_SetWindowPosition(windows[i], - draw_y , draw_x); - //SDL_Delay(50); - } - - SDL_Delay(100); - - - bounce_count+=1; - int signe = (bounce_count % 2 == 0) ? (1) : (-1); - left_most_position = left_most_position + amplitude_bounce; - amplitude_bounce = 200 * sin(bounce_count); - } - for (int i = 0; i < 20; i++) { - SDL_DestroyWindow(windows[i]); - } - - - SDL_Delay(2000); - - SDL_Quit(); - - return 0; +#include <SDL2/SDL.h> +#include <stdio.h> + + +int main(int argc, char **argv) { + (void)argc; + (void)argv; + + int screen_width = 1079; + int screen_height = 1920; + + SDL_Window * windows[20]; + + int bounce_count = 0; + int amplitude_bounce = 200; + int left_most_position = 0; + int top_most_position = 0; + + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + SDL_Log("Error : SDL initialisation - %s\n", + SDL_GetError()); + exit(EXIT_FAILURE); + } + int pas_y = (screen_height)/20; + int pas_x = (screen_width)/20; + int last_drawy = 0; + + for (int i = 0; i < 10; i++) { + int draw_x = top_most_position + i * pas_x; + int draw_y = left_most_position + i * pas_y; + last_drawy = draw_y; + windows[i] = SDL_CreateWindow("Window", + draw_y , draw_x, + 400, 300, + SDL_WINDOW_SHOWN); + if (windows[i] == NULL) { + SDL_Log("Error : SDL window creation - %s\n", + SDL_GetError()); + exit(EXIT_FAILURE); + } + SDL_Delay(50); + } + + for (int i = 10; i < 20; i++) { + int draw_x = top_most_position + (i-10) * pas_x; + int draw_y = last_drawy - (i-10) * pas_y; + windows[i] = SDL_CreateWindow("Window", + draw_y , draw_x, + 400, 300, + SDL_WINDOW_SHOWN); + if (windows[i] == NULL) { + SDL_Log("Error : SDL window creation - %s\n", + SDL_GetError()); + exit(EXIT_FAILURE); + } + SDL_Delay(50); + } + + SDL_Delay(100); + + while (bounce_count < 50) { + + + for (int i = 0; i < 10; i++) { + int draw_x = top_most_position + i * pas_x; + int draw_y = left_most_position + i * pas_y; + last_drawy = draw_y; + SDL_SetWindowPosition(windows[i], + draw_y , draw_x); + + //SDL_Delay(50); + } + + for (int i = 10; i < 20; i++) { + int draw_x = top_most_position + (i-10) * pas_x; + int draw_y = last_drawy - (i-10) * pas_y; + SDL_SetWindowPosition(windows[i], + draw_y , draw_x); + //SDL_Delay(50); + } + + SDL_Delay(100); + + + bounce_count+=1; + int signe = (bounce_count % 2 == 0) ? (1) : (-1); + left_most_position = left_most_position + amplitude_bounce; + amplitude_bounce = 200 * sin(bounce_count); + } + for (int i = 0; i < 20; i++) { + SDL_DestroyWindow(windows[i]); + } + + + SDL_Delay(2000); + + SDL_Quit(); + + return 0; } \ No newline at end of file diff --git a/travail_individuel/Beret/snakes/Makefile b/travail_individuel/Beret/snakes/Makefile index 8a82e304f1ceab181d7729d8f1e264013cc7eaf3..4b29748f975107bf39834854c44f88ecc1746b57 100644 --- a/travail_individuel/Beret/snakes/Makefile +++ b/travail_individuel/Beret/snakes/Makefile @@ -1,10 +1,10 @@ -CC=gcc - -main:main.o - $(CC) -o main main.o -lm -lSDL2 - @echo "=> Lancer le programme avec ./main" - -main.o:main.c - $(CC) -c main.c -g -Wall -Wextra -clean: +CC=gcc + +main:main.o + $(CC) -o main main.o -lm -lSDL2 + @echo "=> Lancer le programme avec ./main" + +main.o:main.c + $(CC) -c main.c -g -Wall -Wextra +clean: rm -rf main main.o \ No newline at end of file diff --git a/travail_individuel/Beret/snakes/main.c b/travail_individuel/Beret/snakes/main.c index a04f55a36f086fcc03b66bee8d623cf55ce2a63f..5d06bbd9e9f3fe0da44898b5f711f6ca25c9acdd 100644 --- a/travail_individuel/Beret/snakes/main.c +++ b/travail_individuel/Beret/snakes/main.c @@ -8,16 +8,17 @@ void draw(SDL_Renderer* renderer, int temps) { SDL_Rect rectangle = {0,0,300,200}; - SDL_Rect rectangle2 = {(temps)%(300),150,20,10}; + SDL_Rect rectangle2 = {(temps/2)%(300),150,20,10}; + SDL_Rect rectangle3 = {(temps/3+50)%(300),10,35,15}; + SDL_Rect rectangle4 = {(temps/2+150)%(300),190,50,20}; int oiseauxx[17]={0,1,2,2,2,3,3,4,1,1,0,0,0,-1,-1,-2,-3}; int oiseauxy[17]={0,0,0,1,2,2,3,3,-1,-2,-1,-2,-3,0,1,1,2}; srand( time( NULL ) ); - int saut= rand() % 5; - printf("saut%d\n",saut); + int saut= rand() % 10; srand( time( NULL ) ); int signe= rand() % 2; - printf("signe%d\n",signe); + SDL_SetRenderDrawColor(renderer, @@ -33,7 +34,19 @@ void draw(SDL_Renderer* renderer, int temps) { SDL_RenderFillRect(renderer, &rectangle2); + SDL_SetRenderDrawColor(renderer, + 255, 255, 255, // mode Red, Green, Blue (tous dans 0..255) + 150); // 0 = transparent ; 255 = opaque + + SDL_RenderFillRect(renderer, &rectangle3); + + SDL_SetRenderDrawColor(renderer, + 255, 255, 255, // mode Red, Green, Blue (tous dans 0..255) + 150); // 0 = transparent ; 255 = opaque + SDL_RenderFillRect(renderer, &rectangle4); + + if (signe%2!=0){saut = -saut; } int pt = 0; /* tracer un cercle n'est en fait pas trivial, voilà le résultat sans algo intelligent ... */ while ( pt<16) { @@ -44,8 +57,8 @@ void draw(SDL_Renderer* renderer, int temps) { 255); // opacité = opaque SDL_RenderDrawPoint(renderer, - (200+ temps/2 - oiseauxx[pt])%(300), // coordonnée en x - 100-oiseauxy[pt]); + (200+ temps- oiseauxx[pt])%(300), // coordonnée en x + 100-oiseauxy[pt]+saut/2); SDL_SetRenderDrawColor(renderer, 0, // quantité de Rouge @@ -54,8 +67,17 @@ void draw(SDL_Renderer* renderer, int temps) { 255); // opacité = opaque SDL_RenderDrawPoint(renderer, - (180+ temps/2 - oiseauxx[pt])%(300), // coordonnée en x - 150-oiseauxy[pt]); + (180+ temps - oiseauxx[pt])%(300), // coordonnée en x + 150-oiseauxy[pt]-(1.5*saut)); + SDL_SetRenderDrawColor(renderer, + 0, // quantité de Rouge + 0, // de vert + 0, // de bleu + 255); + // opacité = opaque + SDL_RenderDrawPoint(renderer, + (10+ temps - oiseauxx[pt])%(300), // coordonnée en x + 10-oiseauxy[pt]-(saut)); pt=pt+1;} } diff --git a/travail_individuel/Beret/x_fenetre/main.c b/travail_individuel/Beret/x_fenetre/main.c index c0b0f2e62db5b91406a06c46c8916f51e9fcf975..d4d6373195912d3243d41e6d7a85981431d948df 100644 --- a/travail_individuel/Beret/x_fenetre/main.c +++ b/travail_individuel/Beret/x_fenetre/main.c @@ -1,101 +1,101 @@ -#include <SDL2/SDL.h> -#include <math.h> -#include <stdio.h> -#include <string.h> - -int main(int argc, char **argv) { - (void)argc; - (void)argv; - int x=0; - - - - SDL_Window - *window_1 = NULL,*window_2=NULL, *window_3=NULL,*window_4=NULL; // Future fenêtre de gauche - - /* Initialisation de la SDL + gestion de l'échec possible */ - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - SDL_Log("Error : SDL initialisation - %s\n", - SDL_GetError()); // l'initialisation de la SDL a échoué - exit(EXIT_FAILURE); - } - - SDL_DisplayMode DM; - SDL_GetCurrentDisplayMode(0, &DM); - int Width = DM.w; - - /* Création de la fenêtre de gauche */ - window_1 = SDL_CreateWindow( - "Vague", // codage en utf8, donc accents possibles - x,300 + 100*cos(x/10), - 100,100, - SDL_WINDOW_RESIZABLE); - - if (window_1 == NULL) { - SDL_Log("Error : SDL window 1 creation - %s\n", - SDL_GetError()); // échec de la création de la fenêtre - SDL_Quit(); // On referme la SDL - exit(EXIT_FAILURE); - } - window_2 = SDL_CreateWindow( - "vague2", // codage en utf8, donc accents possibles - x-10,300 + 110*cos((x-10)/10), // à droite de la fenêtre de gauche - 80, 80, // largeur = 500, hauteur = 300 - 0); - - if (window_2 == NULL) { - SDL_Log("Error : SDL window 2 creation - %s\n", - SDL_GetError()); // échec de la création de la deuxième fenêtre - SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite - SDL_Quit(); - exit(EXIT_FAILURE); - } - window_3 = SDL_CreateWindow( - "vague3", // codage en utf8, donc accents possibles - x-20,300 + 130*cos((x-20)/10), // à droite de la fenêtre de gauche - 60, 60, // largeur = 500, hauteur = 300 - 0); - - if (window_3 == NULL) { - SDL_Log("Error : SDL window 3 creation - %s\n", - SDL_GetError()); // échec de la création de la deuxième fenêtre - SDL_DestroyWindow(window_2); - SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite - SDL_Quit(); - exit(EXIT_FAILURE); - } - - window_4 = SDL_CreateWindow( - "vague4", // codage en utf8, donc accents possibles - x-30,300 + 150*cos((x-30)/10), // à droite de la fenêtre de gauche - 40, 40, // largeur = 500, hauteur = 300 - 0); - - if (window_4 == NULL) { - SDL_Log("Error : SDL window 4 creation - %s\n", - SDL_GetError()); // échec de la création de la deuxième fenêtre - SDL_DestroyWindow(window_3); - SDL_DestroyWindow(window_2); - SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite - SDL_Quit(); - exit(EXIT_FAILURE); - } - while (x<Width){ - SDL_SetWindowPosition(window_1,x,300 + 100*cos((x)/10)); - SDL_SetWindowPosition(window_2,x-10,300 + 100*cos((x-10)/10)); - SDL_SetWindowPosition(window_3,x-20,300 + 100*cos((x-20)/10)); - SDL_SetWindowPosition(window_4,x-30,300 + 100*cos((x-30)/10)); - x=x+10; - - SDL_Delay(150); // Pause exprimée en ms - } - - SDL_DestroyWindow(window_4); - SDL_DestroyWindow(window_3); - SDL_DestroyWindow(window_2); - SDL_DestroyWindow(window_1); - - SDL_Quit(); // la SDL - - return 0; +#include <SDL2/SDL.h> +#include <math.h> +#include <stdio.h> +#include <string.h> + +int main(int argc, char **argv) { + (void)argc; + (void)argv; + int x=0; + + + + SDL_Window + *window_1 = NULL,*window_2=NULL, *window_3=NULL,*window_4=NULL; // Future fenêtre de gauche + + /* Initialisation de la SDL + gestion de l'échec possible */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + SDL_Log("Error : SDL initialisation - %s\n", + SDL_GetError()); // l'initialisation de la SDL a échoué + exit(EXIT_FAILURE); + } + + SDL_DisplayMode DM; + SDL_GetCurrentDisplayMode(0, &DM); + int Width = DM.w; + + /* Création de la fenêtre de gauche */ + window_1 = SDL_CreateWindow( + "Vague", // codage en utf8, donc accents possibles + x,300 + 100*cos(x/10), + 100,100, + SDL_WINDOW_RESIZABLE); + + if (window_1 == NULL) { + SDL_Log("Error : SDL window 1 creation - %s\n", + SDL_GetError()); // échec de la création de la fenêtre + SDL_Quit(); // On referme la SDL + exit(EXIT_FAILURE); + } + window_2 = SDL_CreateWindow( + "vague2", // codage en utf8, donc accents possibles + x-10,300 + 110*cos((x-10)/10), // à droite de la fenêtre de gauche + 80, 80, // largeur = 500, hauteur = 300 + 0); + + if (window_2 == NULL) { + SDL_Log("Error : SDL window 2 creation - %s\n", + SDL_GetError()); // échec de la création de la deuxième fenêtre + SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite + SDL_Quit(); + exit(EXIT_FAILURE); + } + window_3 = SDL_CreateWindow( + "vague3", // codage en utf8, donc accents possibles + x-20,300 + 130*cos((x-20)/10), // à droite de la fenêtre de gauche + 60, 60, // largeur = 500, hauteur = 300 + 0); + + if (window_3 == NULL) { + SDL_Log("Error : SDL window 3 creation - %s\n", + SDL_GetError()); // échec de la création de la deuxième fenêtre + SDL_DestroyWindow(window_2); + SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite + SDL_Quit(); + exit(EXIT_FAILURE); + } + + window_4 = SDL_CreateWindow( + "vague4", // codage en utf8, donc accents possibles + x-30,300 + 150*cos((x-30)/10), // à droite de la fenêtre de gauche + 40, 40, // largeur = 500, hauteur = 300 + 0); + + if (window_4 == NULL) { + SDL_Log("Error : SDL window 4 creation - %s\n", + SDL_GetError()); // échec de la création de la deuxième fenêtre + SDL_DestroyWindow(window_3); + SDL_DestroyWindow(window_2); + SDL_DestroyWindow(window_1); // la première fenétre (qui elle a été créée) doit être détruite + SDL_Quit(); + exit(EXIT_FAILURE); + } + while (x<Width){ + SDL_SetWindowPosition(window_1,x,300 + 100*cos((x)/10)); + SDL_SetWindowPosition(window_2,x-10,300 + 100*cos((x-10)/10)); + SDL_SetWindowPosition(window_3,x-20,300 + 100*cos((x-20)/10)); + SDL_SetWindowPosition(window_4,x-30,300 + 100*cos((x-30)/10)); + x=x+10; + + SDL_Delay(150); // Pause exprimée en ms + } + + SDL_DestroyWindow(window_4); + SDL_DestroyWindow(window_3); + SDL_DestroyWindow(window_2); + SDL_DestroyWindow(window_1); + + SDL_Quit(); // la SDL + + return 0; } \ No newline at end of file diff --git a/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json index 4d5b55df0634dd59212f0d153967b2f7af421e22..eaf3851735b9a8228a0079cdb788b7f1b33386f2 100644 --- a/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json +++ b/travail_individuel/Meyer/jeu_de_la_vie/.vscode/c_cpp_properties.json @@ -1,17 +1,17 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**", - "/usr/include/SDL2" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "gnu17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 } \ No newline at end of file diff --git a/travail_individuel/Meyer/jeu_de_la_vie/main.c b/travail_individuel/Meyer/jeu_de_la_vie/main.c index ff7b0b31d23a1f203dc3bc84dc6dc4d09e689d99..229722d5b557c08ef8e357627856cd156fc3fa2e 100644 --- a/travail_individuel/Meyer/jeu_de_la_vie/main.c +++ b/travail_individuel/Meyer/jeu_de_la_vie/main.c @@ -1,209 +1,209 @@ -#include <stdio.h> -#include <stdlib.h> -#include <SDL2/SDL.h> - -#define TAILLE_MONDE_DELIMITE 20 -#define TAILLE_TABLEAU_REGLES 9 - -int survie[TAILLE_TABLEAU_REGLES] = {0, 0, 1, 1, 0, 0, 0, 0, 0}; -int naissance[TAILLE_TABLEAU_REGLES] = {0, 0, 0, 1, 0, 0, 0, 0, 0}; - -SDL_Window *window; -int width = 700; -int height = 700; -SDL_Renderer *renderer; -SDL_Rect rect; -SDL_Event event; -int running = 1; -int tailleAffichage = 30; - -void initSDL2(){ - - SDL_Init(SDL_INIT_VIDEO); - - window = SDL_CreateWindow( - "Jeu de la vie : monde délimité", - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - width, - height, - SDL_WINDOW_RESIZABLE - ); - - if(window == 0){ - fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); - } - - renderer = SDL_CreateRenderer( - window, - -1, - SDL_RENDERER_ACCELERATED - ); - - if (renderer == 0) { - fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); - SDL_DestroyRenderer(renderer); // Attention : on suppose que les NULL sont maintenus !! - renderer = NULL; - } - -} - -void destroySDL2(){ - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); -} - -void initMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ - int i = 0; - int j = 0; - - for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ - for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ - monde[i][j] = 0; - } - } -} - -void afficheMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ - int i = 0; - int j = 0; - - for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ - for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ - switch(monde[i][j]){ - case 0: - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0); - break; - case 1: - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - break; - default: - printf("Problème valeur tableau monde.\n"); - break; - } - rect.x = tailleAffichage*i; - rect.y = tailleAffichage*j; - rect.w = tailleAffichage-2; - rect.h = tailleAffichage-2; - SDL_RenderFillRect(renderer, &rect); - } - } -} - -void afficherEcran(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ - - SDL_SetRenderDrawColor(renderer, 0, 16, 158, 0); - SDL_RenderClear(renderer); - - afficheMonde(monde); - - SDL_RenderPresent(renderer); -} - -void changeCellule(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int clic_x, int clic_y){ - int ligne = clic_y/tailleAffichage; - int colonne = clic_x/tailleAffichage; - if(0 == monde[colonne][ligne]){ - monde[colonne][ligne] = 1; - }else{ - monde[colonne][ligne] = 0; - } -} - -int nombreVoisinsVivants(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int cel_x, int cel_y){ - int nbrVoisinsVivants = 0; - if(cel_x==0 && cel_y==0){ - nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1]; - } else if(cel_x==0 && cel_y==TAILLE_MONDE_DELIMITE-1){ - nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1]; - }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==0 ){ - nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1]; - }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==TAILLE_MONDE_DELIMITE-1){ - nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1]; - }else if(cel_x==0){ - nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1]; - }else if(cel_x==TAILLE_MONDE_DELIMITE-1){ - nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1] + monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1]; - }else if(cel_y==0){ - nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1] + monde[cel_x+1][cel_y]; - }else if(cel_y==TAILLE_MONDE_DELIMITE-1){ - nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y]; - }else{ - nbrVoisinsVivants = monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + - monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1] + monde[cel_x-1][cel_y]; - } - return nbrVoisinsVivants; -} - -void reglesEvolutions(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ - int i = 0; - int j = 0; - int nbrVoisinsVivants = 0; - for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ - for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ - nbrVoisinsVivants = nombreVoisinsVivants(monde, i, j); - if(monde[i][j]==1 && survie[nbrVoisinsVivants]==0){ - monde[i][j] = 0; - }else if(monde[i][j]==0 && naissance[nbrVoisinsVivants]==1){ - monde[i][j] = 1; - } - } - } -} - -int main() { - - int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]; - - initSDL2(); - - initMonde(monde); - - while (running) { - while (SDL_PollEvent(&event)){ - switch(event.type){ - case SDL_WINDOWEVENT: - printf("window event\n"); - switch (event.window.event){ - case SDL_WINDOWEVENT_CLOSE: - printf("appui sur la croix\n"); - break; - case SDL_WINDOWEVENT_SIZE_CHANGED: - width = event.window.data1; - height = event.window.data2; - printf("Size : %d%d\n", width, height); - break; - } - break; - case SDL_MOUSEBUTTONDOWN: - printf("Appui :%d %d\n", event.button.x, event.button.y); - changeCellule(monde, event.button.x, event.button.y); - break; - case SDL_KEYDOWN: - switch(event.key.keysym.sym){ - case SDLK_RIGHT: - printf("goo calcul!\n"); - reglesEvolutions(monde); - break; - case SDLK_s: - printf("stop calcul...\n"); - break; - default: - printf("une touche est tapee\n"); - break; - } - break; - case SDL_QUIT : - printf("on quitte\n"); - running = 0; - break; - } - //parfois on dessine ici - afficherEcran(monde); - } - SDL_Delay(10); - } - destroySDL2(); - return 0; +#include <stdio.h> +#include <stdlib.h> +#include <SDL2/SDL.h> + +#define TAILLE_MONDE_DELIMITE 20 +#define TAILLE_TABLEAU_REGLES 9 + +int survie[TAILLE_TABLEAU_REGLES] = {0, 0, 1, 1, 0, 0, 0, 0, 0}; +int naissance[TAILLE_TABLEAU_REGLES] = {0, 0, 0, 1, 0, 0, 0, 0, 0}; + +SDL_Window *window; +int width = 700; +int height = 700; +SDL_Renderer *renderer; +SDL_Rect rect; +SDL_Event event; +int running = 1; +int tailleAffichage = 30; + +void initSDL2(){ + + SDL_Init(SDL_INIT_VIDEO); + + window = SDL_CreateWindow( + "Jeu de la vie : monde délimité", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_RESIZABLE + ); + + if(window == 0){ + fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); + } + + renderer = SDL_CreateRenderer( + window, + -1, + SDL_RENDERER_ACCELERATED + ); + + if (renderer == 0) { + fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); // Attention : on suppose que les NULL sont maintenus !! + renderer = NULL; + } + +} + +void destroySDL2(){ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +void initMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + monde[i][j] = 0; + } + } +} + +void afficheMonde(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + switch(monde[i][j]){ + case 0: + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0); + break; + case 1: + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + break; + default: + printf("Problème valeur tableau monde.\n"); + break; + } + rect.x = tailleAffichage*i; + rect.y = tailleAffichage*j; + rect.w = tailleAffichage-2; + rect.h = tailleAffichage-2; + SDL_RenderFillRect(renderer, &rect); + } + } +} + +void afficherEcran(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + + SDL_SetRenderDrawColor(renderer, 0, 16, 158, 0); + SDL_RenderClear(renderer); + + afficheMonde(monde); + + SDL_RenderPresent(renderer); +} + +void changeCellule(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int clic_x, int clic_y){ + int ligne = clic_y/tailleAffichage; + int colonne = clic_x/tailleAffichage; + if(0 == monde[colonne][ligne]){ + monde[colonne][ligne] = 1; + }else{ + monde[colonne][ligne] = 0; + } +} + +int nombreVoisinsVivants(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE], int cel_x, int cel_y){ + int nbrVoisinsVivants = 0; + if(cel_x==0 && cel_y==0){ + nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1]; + } else if(cel_x==0 && cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x+1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==0 ){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1 && cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1]; + }else if(cel_x==0){ + nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1]; + }else if(cel_x==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x][cel_y-1] + monde[cel_x-1][cel_y-1] + monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1]; + }else if(cel_y==0){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x+1][cel_y+1] + monde[cel_x+1][cel_y]; + }else if(cel_y==TAILLE_MONDE_DELIMITE-1){ + nbrVoisinsVivants = monde[cel_x-1][cel_y] + monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y]; + }else{ + nbrVoisinsVivants = monde[cel_x-1][cel_y-1] + monde[cel_x][cel_y-1] + monde[cel_x+1][cel_y-1] + monde[cel_x+1][cel_y] + + monde[cel_x+1][cel_y+1] + monde[cel_x][cel_y+1] + monde[cel_x-1][cel_y+1] + monde[cel_x-1][cel_y]; + } + return nbrVoisinsVivants; +} + +void reglesEvolutions(int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]){ + int i = 0; + int j = 0; + int nbrVoisinsVivants = 0; + for(i=0; i<TAILLE_MONDE_DELIMITE; i++){ + for(j=0; j<TAILLE_MONDE_DELIMITE; j++){ + nbrVoisinsVivants = nombreVoisinsVivants(monde, i, j); + if(monde[i][j]==1 && survie[nbrVoisinsVivants]==0){ + monde[i][j] = 0; + }else if(monde[i][j]==0 && naissance[nbrVoisinsVivants]==1){ + monde[i][j] = 1; + } + } + } +} + +int main() { + + int monde[TAILLE_MONDE_DELIMITE][TAILLE_MONDE_DELIMITE]; + + initSDL2(); + + initMonde(monde); + + while (running) { + while (SDL_PollEvent(&event)){ + switch(event.type){ + case SDL_WINDOWEVENT: + printf("window event\n"); + switch (event.window.event){ + case SDL_WINDOWEVENT_CLOSE: + printf("appui sur la croix\n"); + break; + case SDL_WINDOWEVENT_SIZE_CHANGED: + width = event.window.data1; + height = event.window.data2; + printf("Size : %d%d\n", width, height); + break; + } + break; + case SDL_MOUSEBUTTONDOWN: + printf("Appui :%d %d\n", event.button.x, event.button.y); + changeCellule(monde, event.button.x, event.button.y); + break; + case SDL_KEYDOWN: + switch(event.key.keysym.sym){ + case SDLK_RIGHT: + printf("goo calcul!\n"); + reglesEvolutions(monde); + break; + case SDLK_s: + printf("stop calcul...\n"); + break; + default: + printf("une touche est tapee\n"); + break; + } + break; + case SDL_QUIT : + printf("on quitte\n"); + running = 0; + break; + } + //parfois on dessine ici + afficherEcran(monde); + } + SDL_Delay(10); + } + destroySDL2(); + return 0; } \ No newline at end of file diff --git a/travail_individuel/Meyer/jeu_de_la_vie/makefile b/travail_individuel/Meyer/jeu_de_la_vie/makefile index 9e978d1309fe9b051df7c0c3bdd1e125a4aa5612..32231ce37f404a358e71809b3a0e17d18b58628b 100644 --- a/travail_individuel/Meyer/jeu_de_la_vie/makefile +++ b/travail_individuel/Meyer/jeu_de_la_vie/makefile @@ -1,11 +1,11 @@ -CC=gcc - -main:main.o - $(CC) -o main main.o -lm -lSDL2 - @echo "=> Lancer le programme avec ./main" - -main.o:main.c - $(CC) -c main.c -g -Wall -Wextra - -clean: - rm -rf main main.o +CC=gcc + +main:main.o + $(CC) -o main main.o -lm -lSDL2 + @echo "=> Lancer le programme avec ./main" + +main.o:main.c + $(CC) -c main.c -g -Wall -Wextra + +clean: + rm -rf main main.o diff --git a/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json index 4d5b55df0634dd59212f0d153967b2f7af421e22..eaf3851735b9a8228a0079cdb788b7f1b33386f2 100644 --- a/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json +++ b/travail_individuel/Meyer/pave_de_serpents/.vscode/c_cpp_properties.json @@ -1,17 +1,17 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**", - "/usr/include/SDL2" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "gnu17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 } \ No newline at end of file diff --git a/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json b/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json index 236a91b402122022af296ef7d154061e140897b5..5d0c21c6d152de8919c34f7491265c7297e8fea8 100644 --- a/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json +++ b/travail_individuel/Meyer/pave_de_serpents/.vscode/settings.json @@ -1,5 +1,5 @@ -{ - "files.associations": { - "sdl.h": "c" - } +{ + "files.associations": { + "sdl.h": "c" + } } \ No newline at end of file diff --git a/travail_individuel/Meyer/pave_de_serpents/main.c b/travail_individuel/Meyer/pave_de_serpents/main.c index 860bb73398edb294eee2e1acde40fd85f4bacf58..f449df779b659cca2a5d240a92cd2a7132896f75 100644 --- a/travail_individuel/Meyer/pave_de_serpents/main.c +++ b/travail_individuel/Meyer/pave_de_serpents/main.c @@ -1,166 +1,166 @@ -#include <SDL2/SDL.h> -#include <stdio.h> -#include <stdlib.h> -#include <math.h> -#define PI 3.141592654 - -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - - SDL_Window - *window = NULL; - - int window_width = 1000; - int window_height = 700; - int window_x; - int window_y; - - SDL_InitSubSystem(SDL_INIT_VIDEO); - SDL_DisplayMode mode; - int display_width; - int display_height; - - SDL_Renderer *renderer; - SDL_Rect rect; - - int pale_1_x = 300; - int pale_1_y = 300; - - int pale_2_x = 700; - int pale_2_y = 300; - - int pale_3_x = 250; - int pale_3_y = 500; - - if (SDL_GetDesktopDisplayMode(0, &mode) != 0) - { - SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); - return 1; - } - display_width = mode.w; - display_height = mode.h; - - /* Initialisation de la SDL + gestion de l'échec possible */ - if (SDL_Init(SDL_INIT_VIDEO) != 0) - { - SDL_Log("Error : SDL initialisation - %s\n", - SDL_GetError()); // l'initialisation de la SDL a échoué - exit(EXIT_FAILURE); - } - - /* Création de la fenêtre */ - window_x = (display_width - window_width) / 2; - window_y = (display_height - window_height) / 2; - window = SDL_CreateWindow( - "Moulin à serpents", - window_x, window_y, // centrage de la fenêtre - window_width, window_height, // largeur, hauteur - SDL_WINDOW_RESIZABLE); // redimensionnable - - if (window == NULL) - { - SDL_Log("Error : SDL window 1 creation - %s\n", - SDL_GetError()); // échec de la création de la fenêtre - SDL_Quit(); // On referme la SDL - exit(EXIT_FAILURE); - } - - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); /* SDL_RENDERER_SOFTWARE */ - if (renderer == 0) - { - fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); - - SDL_Quit(); // On referme la SDL - exit(EXIT_FAILURE); - } - - int i = 20; - - while(i>0){ - - // dessin - /* couleur de fond */ - SDL_SetRenderDrawColor(renderer, 157, 224, 144, 255); - SDL_RenderClear(renderer); - - //sol - SDL_SetRenderDrawColor(renderer, 49, 150, 29, 255); - rect.w = window_width; - rect.h = window_height/3; - rect.x = 0; - rect.y = window_height - window_height/3; - SDL_RenderFillRect(renderer, &rect); - - /* bloc du moulin */ - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - rect.w = 150; - rect.h = 250; - rect.x = (window_width - rect.w)/2; - rect.y = window_height - rect.h - 150; - SDL_RenderFillRect(renderer, &rect); - - /*triangle moulin*/ - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - rect.w = 150; - rect.h = 50; - rect.x = (window_width - rect.w)/2; - rect.y = window_height - rect.h - 400; - SDL_RenderFillRect(renderer, &rect); - - //pales du moulin - //pale 1 du moulin - int theta = 40; - int px = window_width/2; - int py = window_height - rect.h - 375; - float c = cos(theta), s = sin(theta); - float dx = pale_1_x - px, dy = pale_1_y - py; - pale_1_x = pale_1_x + c * dx - s * dy; - pale_1_y = pale_1_y + s * dx + c * dy; - - SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); - SDL_RenderDrawLine(renderer, - px, - py, - pale_1_x, - pale_1_y); - - //pale 2 du moulin - dx = pale_2_x - px, dy = pale_2_y - py; - pale_2_x = pale_2_x + c * dx - s * dy; - pale_2_y = pale_2_y + s * dx + c * dy; - SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); - SDL_RenderDrawLine(renderer, - px, - py, - pale_2_x, - pale_2_y); - - //pale 3 du moulin - dx = pale_3_x - px, dy = pale_3_y - py; - pale_3_x = pale_3_x + c * dx - s * dy; - pale_3_y = pale_3_y + s * dx + c * dy; - SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); - SDL_RenderDrawLine(renderer, - px, - py, - pale_3_x, - pale_3_y); - - /* afficher à l'ecran */ - SDL_RenderPresent(renderer); - SDL_Delay(1000); - SDL_RenderClear(renderer); - i = i - 2; - } - - SDL_DestroyRenderer(renderer); - - // fermer fenetre - SDL_DestroyWindow(window); // la fenêtre - - SDL_Quit(); // la SDL - - return 0; +#include <SDL2/SDL.h> +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#define PI 3.141592654 + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + SDL_Window + *window = NULL; + + int window_width = 1000; + int window_height = 700; + int window_x; + int window_y; + + SDL_InitSubSystem(SDL_INIT_VIDEO); + SDL_DisplayMode mode; + int display_width; + int display_height; + + SDL_Renderer *renderer; + SDL_Rect rect; + + int pale_1_x = 300; + int pale_1_y = 300; + + int pale_2_x = 700; + int pale_2_y = 300; + + int pale_3_x = 250; + int pale_3_y = 500; + + if (SDL_GetDesktopDisplayMode(0, &mode) != 0) + { + SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); + return 1; + } + display_width = mode.w; + display_height = mode.h; + + /* Initialisation de la SDL + gestion de l'échec possible */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + SDL_Log("Error : SDL initialisation - %s\n", + SDL_GetError()); // l'initialisation de la SDL a échoué + exit(EXIT_FAILURE); + } + + /* Création de la fenêtre */ + window_x = (display_width - window_width) / 2; + window_y = (display_height - window_height) / 2; + window = SDL_CreateWindow( + "Moulin à serpents", + window_x, window_y, // centrage de la fenêtre + window_width, window_height, // largeur, hauteur + SDL_WINDOW_RESIZABLE); // redimensionnable + + if (window == NULL) + { + SDL_Log("Error : SDL window 1 creation - %s\n", + SDL_GetError()); // échec de la création de la fenêtre + SDL_Quit(); // On referme la SDL + exit(EXIT_FAILURE); + } + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); /* SDL_RENDERER_SOFTWARE */ + if (renderer == 0) + { + fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); + + SDL_Quit(); // On referme la SDL + exit(EXIT_FAILURE); + } + + int i = 20; + + while(i>0){ + + // dessin + /* couleur de fond */ + SDL_SetRenderDrawColor(renderer, 157, 224, 144, 255); + SDL_RenderClear(renderer); + + //sol + SDL_SetRenderDrawColor(renderer, 49, 150, 29, 255); + rect.w = window_width; + rect.h = window_height/3; + rect.x = 0; + rect.y = window_height - window_height/3; + SDL_RenderFillRect(renderer, &rect); + + /* bloc du moulin */ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + rect.w = 150; + rect.h = 250; + rect.x = (window_width - rect.w)/2; + rect.y = window_height - rect.h - 150; + SDL_RenderFillRect(renderer, &rect); + + /*triangle moulin*/ + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + rect.w = 150; + rect.h = 50; + rect.x = (window_width - rect.w)/2; + rect.y = window_height - rect.h - 400; + SDL_RenderFillRect(renderer, &rect); + + //pales du moulin + //pale 1 du moulin + int theta = 40; + int px = window_width/2; + int py = window_height - rect.h - 375; + float c = cos(theta), s = sin(theta); + float dx = pale_1_x - px, dy = pale_1_y - py; + pale_1_x = pale_1_x + c * dx - s * dy; + pale_1_y = pale_1_y + s * dx + c * dy; + + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_1_x, + pale_1_y); + + //pale 2 du moulin + dx = pale_2_x - px, dy = pale_2_y - py; + pale_2_x = pale_2_x + c * dx - s * dy; + pale_2_y = pale_2_y + s * dx + c * dy; + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_2_x, + pale_2_y); + + //pale 3 du moulin + dx = pale_3_x - px, dy = pale_3_y - py; + pale_3_x = pale_3_x + c * dx - s * dy; + pale_3_y = pale_3_y + s * dx + c * dy; + SDL_SetRenderDrawColor(renderer, 163, 82, 2, 255); + SDL_RenderDrawLine(renderer, + px, + py, + pale_3_x, + pale_3_y); + + /* afficher à l'ecran */ + SDL_RenderPresent(renderer); + SDL_Delay(1000); + SDL_RenderClear(renderer); + i = i - 2; + } + + SDL_DestroyRenderer(renderer); + + // fermer fenetre + SDL_DestroyWindow(window); // la fenêtre + + SDL_Quit(); // la SDL + + return 0; } \ No newline at end of file diff --git a/travail_individuel/Meyer/pave_de_serpents/makefile b/travail_individuel/Meyer/pave_de_serpents/makefile index 9e978d1309fe9b051df7c0c3bdd1e125a4aa5612..32231ce37f404a358e71809b3a0e17d18b58628b 100644 --- a/travail_individuel/Meyer/pave_de_serpents/makefile +++ b/travail_individuel/Meyer/pave_de_serpents/makefile @@ -1,11 +1,11 @@ -CC=gcc - -main:main.o - $(CC) -o main main.o -lm -lSDL2 - @echo "=> Lancer le programme avec ./main" - -main.o:main.c - $(CC) -c main.c -g -Wall -Wextra - -clean: - rm -rf main main.o +CC=gcc + +main:main.o + $(CC) -o main main.o -lm -lSDL2 + @echo "=> Lancer le programme avec ./main" + +main.o:main.c + $(CC) -c main.c -g -Wall -Wextra + +clean: + rm -rf main main.o diff --git a/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json b/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json index 4d5b55df0634dd59212f0d153967b2f7af421e22..eaf3851735b9a8228a0079cdb788b7f1b33386f2 100644 --- a/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json +++ b/travail_individuel/Meyer/x_fenetre/.vscode/c_cpp_properties.json @@ -1,17 +1,17 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**", - "/usr/include/SDL2" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "gnu17", - "cppStandard": "gnu++14", - "intelliSenseMode": "linux-gcc-x64" - } - ], - "version": 4 +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/SDL2" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 } \ No newline at end of file diff --git a/travail_individuel/Meyer/x_fenetre/.vscode/settings.json b/travail_individuel/Meyer/x_fenetre/.vscode/settings.json index 088a016fe65a59c2cfcbb56823ba698a0d776631..1f4f9926f98c73f157784bd3c0fd2c54d9580235 100644 --- a/travail_individuel/Meyer/x_fenetre/.vscode/settings.json +++ b/travail_individuel/Meyer/x_fenetre/.vscode/settings.json @@ -1,6 +1,6 @@ -{ - "files.associations": { - "main.C": "cpp", - "cmath": "cpp" - } +{ + "files.associations": { + "main.C": "cpp", + "cmath": "cpp" + } } \ No newline at end of file diff --git a/travail_individuel/Meyer/x_fenetre/main.c b/travail_individuel/Meyer/x_fenetre/main.c index ce85e6a5c0db57af11c9c9d06c08e53b34f65bb0..0192e3a0471768be892c6c21765d19829724de54 100644 --- a/travail_individuel/Meyer/x_fenetre/main.c +++ b/travail_individuel/Meyer/x_fenetre/main.c @@ -1,128 +1,128 @@ -#include <SDL2/SDL.h> -#include <stdio.h> -#include <stdlib.h> -#include <time.h> -#include <math.h> - -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - - SDL_Window - *window = NULL; - - int window_width = 400; - int window_height = 300; - int window_x; - int window_y; - - int number_of_stars = 10; - - SDL_InitSubSystem(SDL_INIT_VIDEO); - SDL_DisplayMode mode; - int display_width; - int display_height; - - if (SDL_GetDesktopDisplayMode(0, &mode) != 0) - { - SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); - return 1; - } - display_width = mode.w; - display_height = mode.h; - - /* Initialisation de la SDL + gestion de l'échec possible */ - if (SDL_Init(SDL_INIT_VIDEO) != 0) - { - SDL_Log("Error : SDL initialisation - %s\n", - SDL_GetError()); // l'initialisation de la SDL a échoué - exit(EXIT_FAILURE); - } - - /* Création de la fenêtre */ - window_x = (display_width - window_width) / 2; - window_y = display_height; - window = SDL_CreateWindow( - "Rocket : 3", - window_x, window_y, // centrage de la fenêtre - window_width, window_height, // largeur = 400, hauteur = 300 - SDL_WINDOW_RESIZABLE); // redimensionnable - - if (window == NULL) - { - SDL_Log("Error : SDL window 1 creation - %s\n", - SDL_GetError()); // échec de la création de la fenêtre - SDL_Quit(); // On referme la SDL - exit(EXIT_FAILURE); - } - - // decompte decollage - SDL_Delay(1000); - SDL_SetWindowTitle(window, "Rocket : 2"); - SDL_Delay(1000); - SDL_SetWindowTitle(window, "Rocket : 1"); - SDL_Delay(1000); - SDL_SetWindowTitle(window, "Rocket : FEU!"); - SDL_Delay(1000); - SDL_SetWindowTitle(window, "Rocket"); - - while (window_y > 0) - { - window_width = window_width - 15; - window_x = (display_width - window_width) / 2; - window_y = window_y - 100; - SDL_SetWindowSize(window, window_width, window_height); - SDL_SetWindowPosition(window, window_x, window_y); - SDL_Delay(200); // Pause exprimée en ms - } - - // explosion de la rocket - SDL_DestroyWindow(window); // la fenêtre - - // aleatoire - srand(time(0)); - // creation de [number_of_stars] etoiles dans des directions aleatoires - SDL_Window *window_stars[20]; - int tab_window_x[20]; - int tab_window_y[20]; - int tab_direction[20]; - for (int i = 1; i <= number_of_stars; i++) - { - int width_r = 50 + rand() % 100; - int height_r = 50 + rand() % 50; - tab_window_x[i] = (display_width - width_r) / 2; - tab_window_y[i] = 0; - tab_direction[i] = pow(-1, 1+(rand()%2)); // donne -1 ou 1 - - window_stars[i] = SDL_CreateWindow( - "Étoile", - tab_window_x[i], tab_window_y[i], - width_r, height_r, - SDL_WINDOW_RESIZABLE); - - SDL_Delay(10); // Pause exprimée en ms - } - - // déplacement des etoiles - for (int temps = 0; temps <= 5; temps++) - { - for (int i = 1; i <= number_of_stars; i++) - { - tab_window_y[i] = tab_window_y[i] + 20 + rand()%70; - tab_window_x[i] = tab_window_x[i] + tab_direction[i]*(rand()%200); - SDL_SetWindowPosition(window_stars[i], tab_window_x[i], tab_window_y[i]); - } - SDL_Delay(400); - } - - // destruction des etoiles - for (int i = 1; i <= number_of_stars; i++) - { - SDL_DestroyWindow(window_stars[i]); - } - - SDL_Quit(); // la SDL - - return 0; +#include <SDL2/SDL.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <math.h> + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + SDL_Window + *window = NULL; + + int window_width = 400; + int window_height = 300; + int window_x; + int window_y; + + int number_of_stars = 10; + + SDL_InitSubSystem(SDL_INIT_VIDEO); + SDL_DisplayMode mode; + int display_width; + int display_height; + + if (SDL_GetDesktopDisplayMode(0, &mode) != 0) + { + SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); + return 1; + } + display_width = mode.w; + display_height = mode.h; + + /* Initialisation de la SDL + gestion de l'échec possible */ + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + SDL_Log("Error : SDL initialisation - %s\n", + SDL_GetError()); // l'initialisation de la SDL a échoué + exit(EXIT_FAILURE); + } + + /* Création de la fenêtre */ + window_x = (display_width - window_width) / 2; + window_y = display_height; + window = SDL_CreateWindow( + "Rocket : 3", + window_x, window_y, // centrage de la fenêtre + window_width, window_height, // largeur = 400, hauteur = 300 + SDL_WINDOW_RESIZABLE); // redimensionnable + + if (window == NULL) + { + SDL_Log("Error : SDL window 1 creation - %s\n", + SDL_GetError()); // échec de la création de la fenêtre + SDL_Quit(); // On referme la SDL + exit(EXIT_FAILURE); + } + + // decompte decollage + SDL_Delay(1000); + SDL_SetWindowTitle(window, "Rocket : 2"); + SDL_Delay(1000); + SDL_SetWindowTitle(window, "Rocket : 1"); + SDL_Delay(1000); + SDL_SetWindowTitle(window, "Rocket : FEU!"); + SDL_Delay(1000); + SDL_SetWindowTitle(window, "Rocket"); + + while (window_y > 0) + { + window_width = window_width - 15; + window_x = (display_width - window_width) / 2; + window_y = window_y - 100; + SDL_SetWindowSize(window, window_width, window_height); + SDL_SetWindowPosition(window, window_x, window_y); + SDL_Delay(200); // Pause exprimée en ms + } + + // explosion de la rocket + SDL_DestroyWindow(window); // la fenêtre + + // aleatoire + srand(time(0)); + // creation de [number_of_stars] etoiles dans des directions aleatoires + SDL_Window *window_stars[20]; + int tab_window_x[20]; + int tab_window_y[20]; + int tab_direction[20]; + for (int i = 1; i <= number_of_stars; i++) + { + int width_r = 50 + rand() % 100; + int height_r = 50 + rand() % 50; + tab_window_x[i] = (display_width - width_r) / 2; + tab_window_y[i] = 0; + tab_direction[i] = pow(-1, 1+(rand()%2)); // donne -1 ou 1 + + window_stars[i] = SDL_CreateWindow( + "Étoile", + tab_window_x[i], tab_window_y[i], + width_r, height_r, + SDL_WINDOW_RESIZABLE); + + SDL_Delay(10); // Pause exprimée en ms + } + + // déplacement des etoiles + for (int temps = 0; temps <= 5; temps++) + { + for (int i = 1; i <= number_of_stars; i++) + { + tab_window_y[i] = tab_window_y[i] + 20 + rand()%70; + tab_window_x[i] = tab_window_x[i] + tab_direction[i]*(rand()%200); + SDL_SetWindowPosition(window_stars[i], tab_window_x[i], tab_window_y[i]); + } + SDL_Delay(400); + } + + // destruction des etoiles + for (int i = 1; i <= number_of_stars; i++) + { + SDL_DestroyWindow(window_stars[i]); + } + + SDL_Quit(); // la SDL + + return 0; } \ No newline at end of file diff --git a/travail_individuel/Meyer/x_fenetre/makefile b/travail_individuel/Meyer/x_fenetre/makefile index 9e978d1309fe9b051df7c0c3bdd1e125a4aa5612..32231ce37f404a358e71809b3a0e17d18b58628b 100644 --- a/travail_individuel/Meyer/x_fenetre/makefile +++ b/travail_individuel/Meyer/x_fenetre/makefile @@ -1,11 +1,11 @@ -CC=gcc - -main:main.o - $(CC) -o main main.o -lm -lSDL2 - @echo "=> Lancer le programme avec ./main" - -main.o:main.c - $(CC) -c main.c -g -Wall -Wextra - -clean: - rm -rf main main.o +CC=gcc + +main:main.o + $(CC) -o main main.o -lm -lSDL2 + @echo "=> Lancer le programme avec ./main" + +main.o:main.c + $(CC) -c main.c -g -Wall -Wextra + +clean: + rm -rf main main.o