Commit 2c91890f authored by Valentin MEUNIER's avatar Valentin MEUNIER
Browse files

dossier

parent 3581b5d2
/*SDL_ttf.c*/
/*15 juin 2021*/
/*gcc SDL_ttf.c -o prog -Wall -Wextra -lSDL2 -lSDL2_ttf -lm -g*/
#include <stdio.h>
#include <string.h>
#include<SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include<stdlib.h>
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);
if (window != NULL) SDL_DestroyWindow(window);
SDL_Quit();
if (!ok) {
exit(EXIT_FAILURE);
}
}
int main ()
{
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Window *window;
int width = 1900;
int height = 1000;
window = SDL_CreateWindow("SDL2 Programme 0.1", 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());
/* on peut aussi utiliser SDL_Log() */
}
SDL_SetWindowTitle(window, "Hello");
SDL_Renderer *renderer;
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());
}
if (TTF_Init() < 0) end_sdl(0, "Couldn't initialize SDL TTF", window, renderer);
TTF_Font* font = NULL;
font = TTF_OpenFont("angelina.ttf", 65);
if (font == NULL) end_sdl(0, "Can't load font", window, renderer);
TTF_SetFontStyle(font, TTF_STYLE_ITALIC | TTF_STYLE_BOLD);
SDL_Color color = {200, 0, 40, 255};
SDL_Surface* text_surface = NULL;
text_surface = TTF_RenderText_Blended(font, "Hello World !", color);
if (text_surface == NULL) end_sdl(0, "Can't create text surface", window, renderer);
SDL_Texture* text_texture = NULL;
text_texture = SDL_CreateTextureFromSurface(renderer, text_surface);
if (text_texture == NULL) end_sdl(0, "Can't create texture from surface", window, renderer);
SDL_FreeSurface(text_surface);
SDL_Rect pos = {10,10, 0, 0};
SDL_QueryTexture(text_texture, NULL, NULL, &pos.w, &pos.h);
SDL_RenderCopy(renderer, text_texture, NULL, &pos);
SDL_DestroyTexture(text_texture);
SDL_Event event;
int running=1;
while (running)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
width = event.window.data1;
height = event.window.data2;
break;
case SDL_WINDOWEVENT_EXPOSED:
break;
}
break;
case SDL_QUIT:
running = 0;
break;
}
}
SDL_RenderPresent(renderer);
SDL_Delay(150);
}
end_sdl(1, "Normal ending", window, renderer);
}
/*X_fenetre.c*/
/*14 juin 2021*/
/*gcc X_fentre.c -o X_fenetre -lSDL2 -Wall -g -Wextra*/
#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main()
{
SDL_Window** tab =malloc (10*(sizeof(SDL_Window *)));
int width=1920/5, height=1080/5;
int x=0;
int y=0;
for (int i=0;i<5;i++)
{
tab[i] = SDL_CreateWindow("window", x,y,width, height,SDL_WINDOW_RESIZABLE);
x+=width;
y+=height;
if (tab[i] == 0)
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
}
SDL_Delay(300);
}
x=0;
y=800;
for (int i=5;i<10;i++)
{
tab[i] = SDL_CreateWindow("window", x,y,width, height,SDL_WINDOW_RESIZABLE);
x+=width;
y-=height;
if (tab[i] == 0)
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
}
SDL_Delay(300);
}
SDL_Delay(2000);
for (int i=0;i<10;i++)
{
SDL_DestroyWindow(tab[i]);
SDL_Delay(300);
}
free(tab);
SDL_Quit();
}
/*Jeu_de_la_vie.c*/
/*15 juin 2021*/
/*gcc jeu_de_la_vie.c -o prog -Wall -Wextra -lSDL2 -lm -g*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include<SDL2/SDL.h>
#include<stdlib.h>
#include<time.h>
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);
if (window != NULL) SDL_DestroyWindow(window);
SDL_Quit();
if (!ok) {
exit(EXIT_FAILURE);
}
}
int** init_grille(int x,int y)
{
int ** grille=malloc(x*sizeof(int *));
if (grille==NULL)
{
printf("Erreur d'allocation du int**\n");
}
for (int i=0; i<x;i++)
{
grille[i]=malloc(y*sizeof(int));
if (grille[i]==NULL)
{
printf("Erreur d'allocation du int* numero %d\n",i);
}
}
return grille;
}
void liberer(int **grille,int x)
{
for (int i=0; i<x;i++)
free(grille[i]);
free(grille);
}
int modulo(int a, int b)
{
if (a>=0 && a<b)
return a;
if (a>=b)
return modulo(a-b,b);
if (a<=0)
return modulo(a+b,b);
}
int * survie(int ** tab, int x, int y,int pos_x,int pos_y)
{
int * survie=malloc(9*sizeof(int));
if (survie==NULL)
printf("Erreur d'allocation du int* survie\n");
survie[0]=tab[modulo((pos_x+1),x)][modulo((pos_y+1),y)];
survie[1]=tab[modulo((pos_x+1),x)][modulo((pos_y),y)];
survie[2]=tab[modulo((pos_x+1),x)][modulo((pos_y-1),y)];
survie[3]=tab[modulo((pos_x),x)][modulo((pos_y+1),y)];
survie[4]=tab[modulo((pos_x),x)][modulo((pos_y-1),y)];
survie[5]=tab[modulo((pos_x-1),x)][modulo((pos_y+1),y)];
survie[6]=tab[modulo((pos_x-1),x)][modulo((pos_y-1),y)];
survie[7]=tab[modulo((pos_x-1),x)][modulo((pos_y),y)];
survie[8]=tab[modulo((pos_x),x)][modulo((pos_y),y)];
return survie;
}
int vie_mort(int * survie)
{
int total=0;
int vie=0;
for (int i=0; i<8; i++)
total+=survie[i];
if (total<=1 && total>=4)
vie=0;
else if (total==3)
vie=1;
else if (total==2 && survie[9]==1)
vie=1;
free(survie);
return vie;
}
void t_tplusun(int ** tab_t, int x, int y,int ** tab_tplusun)
{
int i,j;
for (i=0;i<x;i++)
{
for (j=0;j<y;j++)
{
tab_tplusun[i][j]=vie_mort(survie(tab_t,x,y,i,j));
}
}
}
void init_tab(int** tab,int x,int y)
{
int aleat,i,j;
srand(time(0));
for (i=0;i<x;i++)
{
for (j=0;j<y;j++)
{
aleat=(int)(((float)rand()/(float)RAND_MAX)*2);
tab[i][j]=aleat;
}
}
}
void afficherEcran(SDL_Renderer *renderer,int** tab, int x,int y){
SDL_Rect rect;
int i,j;
SDL_SetRenderDrawColor(renderer, 100, 0, 0, 0);
SDL_RenderClear(renderer);
for (i=0;i<x;i++){
for (j=0;j<y;j++){
if (tab[i][j]==1){
//cellule vivante donc couleur noire
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
}
else{
//cellule morte, couleur blanche
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
}
rect.x=i*10;
rect.y=j*10;
rect.w=rect.h=10;
SDL_RenderFillRect(renderer,&rect);
}
}
SDL_RenderPresent(renderer);
}
int main ()
{
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Window *window;
int width = 1900;
int height = 1000;
int taille = 0;
int ** tab1=NULL;
int ** tab2=NULL;
tab1=init_grille(width/10, height/10);
tab2=init_grille(width/10, height/10);
init_tab(tab1,width/10,height/10);
window = SDL_CreateWindow("SDL2 Programme 0.1", 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());
/* on peut aussi utiliser SDL_Log() */
}
SDL_SetWindowTitle(window, "Jeu_de_la_vie");
SDL_Renderer *renderer;
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_Event event;
int running=1;
while (running)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
width = event.window.data1;
height = event.window.data2;
break;
case SDL_WINDOWEVENT_EXPOSED:
break;
}
break;
case SDL_QUIT:
running = 0;
break;
}
}
t_tplusun(tab1,width/10,height/10,tab2);
afficherEcran(renderer,tab2,width/10,height/10);
SDL_Delay(150);
for (int i=0;i<width/10;i++)
{
for (int j=0;j<height/10;j++)
tab1[i][j]=tab2[i][j];
}
}
end_sdl(1, "Normal ending", window, renderer);
liberer(tab1,width/10);
liberer(tab2,width/10);
}
/*Jeu de la vie interactif*/
/*15 juin 2021*/
/*gcc jeu_de_la_vie_interactif.c -o prog -Wall -Wextra -lSDL2 -lm -g*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include<SDL2/SDL.h>
#include<stdlib.h>
#include<time.h>
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);
if (window != NULL) SDL_DestroyWindow(window);
SDL_Quit();
if (!ok) {
exit(EXIT_FAILURE);
}
}
int** init_grille(int x,int y)
{
int ** grille=malloc(x*sizeof(int *));
if (grille==NULL)
{
printf("Erreur d'allocation du int**\n");
}
for (int i=0; i<x;i++)
{
grille[i]=malloc(y*sizeof(int));
if (grille[i]==NULL)
{
printf("Erreur d'allocation du int* numero %d\n",i);
}
}
return grille;
}
void liberer(int **grille,int x)
{
for (int i=0; i<x;i++)
free(grille[i]);
free(grille);
}
int modulo(int a, int b)
{
if (a>=0 && a<b)
return a;
else if (a>=b)
return modulo(a-b,b);
else
return modulo(a+b,b);
}
int * survie(int ** tab, int x, int y,int pos_x,int pos_y)
{
int * survie=malloc(9*sizeof(int));
if (survie==NULL)
printf("Erreur d'allocation du int* survie\n");
survie[0]=tab[modulo((pos_x+1),x)][modulo((pos_y+1),y)];
survie[1]=tab[modulo((pos_x+1),x)][modulo((pos_y),y)];
survie[2]=tab[modulo((pos_x+1),x)][modulo((pos_y-1),y)];
survie[3]=tab[modulo((pos_x),x)][modulo((pos_y+1),y)];
survie[4]=tab[modulo((pos_x),x)][modulo((pos_y-1),y)];
survie[5]=tab[modulo((pos_x-1),x)][modulo((pos_y+1),y)];
survie[6]=tab[modulo((pos_x-1),x)][modulo((pos_y-1),y)];
survie[7]=tab[modulo((pos_x-1),x)][modulo((pos_y),y)];
survie[8]=tab[modulo((pos_x),x)][modulo((pos_y),y)];
return survie;
}
int vie_mort(int * survie)
{
int total=0;
int vie=0;
for (int i=0; i<8; i++)
total+=survie[i];
if (total<=1 && total>=4)
vie=0;
else if (total==3)
vie=1;
else if (total==2 && survie[9]==1)
vie=1;
free(survie);
return vie;
}
void t_tplusun(int ** tab_t, int x, int y,int ** tab_tplusun)
{
int i,j;
for (i=0;i<x;i++)
{
for (j=0;j<y;j++)
{
tab_tplusun[i][j]=vie_mort(survie(tab_t,x,y,i,j));
}
}
}
void init_tab(int** tab,int x,int y)
{
int aleat,i,j;
srand(time(0));
for (i=0;i<x;i++)
{
for (j=0;j<y;j++)
{
aleat=(int)(((float)rand()/(float)RAND_MAX)*2);
tab[i][j]=aleat;
}
}
}
void afficherEcran(SDL_Renderer *renderer,int** tab, int x,int y){
SDL_Rect rect;
int i,j;
SDL_SetRenderDrawColor(renderer, 100, 0, 0, 0);
SDL_RenderClear(renderer);
for (i=0;i<x;i++){
for (j=0;j<y;j++){
if (tab[i][j]==1){
//cellule vivante donc couleur noire
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
}
else{
//cellule morte, couleur blanche
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
}
rect.x=i*10;
rect.y=j*10;
rect.w=rect.h=10;
SDL_RenderFillRect(renderer,&rect);
}
}
SDL_RenderPresent(renderer);
}
int main ()
{
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
return EXIT_FAILURE;
}
SDL_Window *window;
int width = 1900;
int height = 1000;
int ** tab1=NULL;
int ** tab2=NULL;
tab1=init_grille(width/10, height/10);
tab2=init_grille(width/10, height/10);
init_tab(tab1,width/10,height/10);
window = SDL_CreateWindow("SDL2 Programme 0.1", 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());