second commit with source files

This commit is contained in:
21801009 2022-06-21 14:19:56 +02:00
parent 7e02db3e88
commit 2859664a32
7 changed files with 739 additions and 0 deletions

2
.gitignore vendored
View File

@ -52,3 +52,5 @@ Module.symvers
Mkfile.old
dkms.conf
# VSCode files
.vscode

61
Makefile Normal file
View File

@ -0,0 +1,61 @@
CC=gcc
CFLAGS=-O2 -Wall `sdl-config --cflags`
LIBS=`sdl-config --libs` -lm -lSDL_ttf -fcommon
DIR=IN100_sept_2010
#Cible generique pour Linux
%: graphics.o %.c
rm -f $@
$(CC) $(CFLAGS) graphics.o $@.c -o $@ $(LIBS)
#Cible generique pour free BSD
.c .o: graphics.o
rm -f $@
$(CC) $(CFLAGS) graphics.o $@.c -o $@ $(LIBS)
graphics.o: graphics.c graphics.h
rm -f police.h
touch police.h
if test -e /usr/include/SDL_ttf.h; then echo "#define SDL_TTF_OK" > police.h; fi
if test -e /usr/include/SDL/SDL_ttf.h; then echo "#define SDL_TTF_OK" > police.h; fi
if test -e /usr/local/include/SDL_ttf.h; then echo "#define SDL_TTF_OK" > police.h; fi
if test -e /usr/local/include/SDL/SDL_ttf.h; then echo "#define SDL_TTF_OK" > police.h; fi
$(CC) $(CFLAGS) -c graphics.c
sans_ttf:
rm -f police.h
touch police.h
$(CC) $(CFLAGS) -c graphics.c
$(CC) $(CFLAGS) graphics.o exemple.c -o exemple $(LIBS)
./exemple
exemple: exemple.c graphics.o
$(CC) $(CFLAGS) graphics.o exemple.c -o exemple $(LIBS)
./exemple
jeu: jeu.c jeu.o
$(CC) $(CFLAGS) -c jeu.c
$(CC) $(CFLAGS) graphics.o jeu.o main.c -o puissance4 $(LIBS)
tar: clean
rm -rf $(DIR)
mkdir $(DIR)
cp exemple.c $(DIR)
cp graphics.c $(DIR)
cp graphics.h $(DIR)
cp couleur.h $(DIR)
cp exo1.3.c $(DIR)
cp Makefile $(DIR)
cp *.ttf $(DIR)
tar cvf $(DIR).tar $(DIR)
rm -rf $(DIR)
clean:
rm -f *core
rm -f *.o
rm -f police.h
rm -f exemple
rm -f *.tar
rm -rf $(DIR)

161
couleur.h Normal file
View File

@ -0,0 +1,161 @@
// 16 couleurs de base en francais
#define argent 0xC0C0C0
#define blanc 0xFFFFFF
#define bleu 0x0000FF
#define bleumarine 0x000080
#define citronvert 0x00FF00
#define cyan 0x00FFFF
#define magenta 0xFF00FF
#define gris 0x808080
#define jaune 0xFFFF00
#define marron 0x800000
#define noir 0x000000
#define rouge 0xFF0000
#define sarcelle 0x008080
#define vert 0x00FF00
#define vertclair 0x008000
#define vertolive 0x808000
#define violet 0x800080
// 140 couleurs en anglais
#define aliceblue 0xF0F8FF
#define antiquewhite 0xFAEBD7
#define aqua 0x00FFFF
#define aquamarine 0x7FFFD4
#define azure 0xF0FFFF
#define beige 0xF5F5DC
#define bisque 0xFFE4C4
#define black 0x000000
#define blanchedalmond 0xFFEBCD
#define blue 0x0000FF
#define blueviolet 0x8A2BE2
#define brown 0xA52A2A
#define burlywood 0xDEB887
#define cadetblue 0x5F9EA0
#define chartreuse 0x7FFF00
#define chocolate 0xD2691E
#define coral 0xFF7F50
#define cornflowerblue 0x6495ED
#define cornsilk 0xFFF8DC
#define crimson 0xDC143C
#define cyan 0x00FFFF
#define darkblue 0x00008B
#define darkcyan 0x008B8B
#define darkgoldenrod 0xB8860B
#define darkgray 0xA9A9A9
#define darkgreen 0x006400
#define darkkhaki 0xBDB76B
#define darkmagenta 0x8B008B
#define darkolivegreen 0x556B2F
#define darkorange 0xFF8C00
#define darkorchid 0x9932CC
#define darkred 0x8B0000
#define darksalmon 0xE9967A
#define darkseagreen 0x8FBC8F
#define darkslateblue 0x483D8B
#define darkslategray 0x2F4F4F
#define darkturquoise 0x00CED1
#define darkviolet 0x9400D3
#define deeppink 0xFF1493
#define deepskyblue 0x00BFFF
#define dimgray 0x696969
#define dodgerblue 0x1E90FF
#define firebrick 0xB22222
#define floralwhite 0xFFFAF0
#define forestgreen 0x228B22
#define fuchsia 0xFF00FF
#define gainsboro 0xDCDCDC
#define ghostwhite 0xF8F8FF
#define gold 0xFFD700
#define goldenrod 0xDAA520
#define gray 0x808080
#define green 0x008000
#define greenyellow 0xADFF2F
#define honeydew 0xF0FFF0
#define hotpink 0xFF69B4
#define indianred 0xCD5C5C
#define indigo 0x4B0082
#define ivory 0xFFFFF0
#define khaki 0xF0E68C
#define lavender 0xE6E6FA
#define lavenderblush 0xFFF0F5
#define lawngreen 0x7CFC00
#define lemonchiffon 0xFFFACD
#define lightblue 0xADD8E6
#define lightcoral 0xF08080
#define lightcyan 0xE0FFFF
#define lightgoldenrodyellow 0xFAFAD2
#define lightgreen 0x90EE90
#define lightgrey 0xD3D3D3
#define lightpink 0xFFB6C1
#define lightsalmon 0xFFA07A
#define lightseagreen 0x20B2AA
#define lightskyblue 0x87CEFA
#define lightslategray 0x778899
#define lightsteelblue 0xB0C4DE
#define lightyellow 0xFFFFE0
#define lime 0x00FF00
#define limegreen 0x32CD32
#define linen 0xFAF0E6
#define magenta 0xFF00FF
#define maroon 0x800000
#define mediumaquamarine 0x66CDAA
#define mediumblue 0x0000CD
#define mediumorchid 0xBA55D3
#define mediumpurple 0x9370DB
#define mediumseagreen 0x3CB371
#define mediumslateblue 0x7B68EE
#define mediumspringgreen 0x00FA9A
#define mediumturquoise 0x48D1CC
#define mediumvioletred 0xC71585
#define midnightblue 0x191970
#define mintcream 0xF5FFFA
#define mistyrose 0xFFE4E1
#define moccasin 0xFFE4B5
#define navajowhite 0xFFDEAD
#define navy 0x000080
#define oldlace 0xFDF5E6
#define olive 0x808000
#define olivedrab 0x6B8E23
#define orange 0xFFA500
#define orangered 0xFF4500
#define orchid 0xDA70D6
#define palegoldenrod 0xEEE8AA
#define palegreen 0x98FB98
#define paleturquoise 0xAFEEEE
#define palevioletred 0xDB7093
#define papayawhip 0xFFEFD5
#define peachpuff 0xFFDAB9
#define peru 0xCD853F
#define pink 0xFFC0CB
#define plum 0xDDA0DD
#define powderblue 0xB0E0E6
#define purple 0x800080
#define red 0xFF0000
#define rosybrown 0xBC8F8F
#define royalblue 0x4169E1
#define saddlebrown 0x8B4513
#define salmon 0xFA8072
#define sandybrown 0xF4A460
#define seagreen 0x2E8B57
#define seashell 0xFFF5EE
#define sienna 0xA0522D
#define silver 0xC0C0C0
#define skyblue 0x87CEEB
#define slateblue 0x6A5ACD
#define slategray 0x708090
#define snow 0xFFFAFA
#define springgreen 0x00FF7F
#define steelblue 0x4682B4
#define tan 0xD2B48C
#define teal 0x008080
#define thistle 0xD8BFD8
#define tomato 0xFF6347
#define turquoise 0x40E0D0
#define violetlight 0xEE82EE
#define wheat 0xF5DEB3
#define white 0xFFFFFF
#define whitesmoke 0xF5F5F5
#define yellow 0xFFFF00
#define yellowgreen 0x9ACD32

452
graphics.c Normal file
View File

@ -0,0 +1,452 @@
#include "graphics.h"
#include "police.h"
#define EN_LOCAL
#ifdef SDL_TTF_OK
#include <SDL_ttf.h>
#include <SDL/SDL_ttf.h>
#define POLICE_NAME "verdana.ttf"
TTF_Font *police[256];
int verdana_ok = 0;
#endif
int WIDTH;
int HEIGHT;
SDL_Surface * SDL_screen;
int __init_graphics_is_already_called = 0;
int SDL_AFFICHE_AUTO = 1;
#define MAX_WIDTH 1920
#define MAX_HEIGHT 1080
void init_graphics(int W, int H) {
if ((W>10) && (W<MAX_WIDTH ))
WIDTH = W;
else WIDTH = 600;
if ((H>10) && (H<MAX_HEIGHT))
HEIGHT = H;
else HEIGHT = 400;
SDL_Init(SDL_INIT_VIDEO);
SDL_screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( SDL_screen == NULL ) {
fprintf(stderr, "Impossible de passer en %dx%d en 32 bits: %s\n", WIDTH, HEIGHT, SDL_GetError());
exit(1);
}
SDL_EnableKeyRepeat(1,0);
SDL_WM_SetCaption("Puissance 4!!",NULL);
__init_graphics_is_already_called = 25;
printf("Fenetre de %d x %d\n",WIDTH,HEIGHT);
#ifdef SDL_TTF_OK
printf("SDL_ttf OK, ");
TTF_Init();
police[10] = TTF_OpenFont(POLICE_NAME, 10);
if (police[10]) {
verdana_ok=1; printf("police %s OK : affichage graphique OK.\n",POLICE_NAME);
}
else {
verdana_ok = 0; printf("police %s absente : affichage dans la console.\n",POLICE_NAME);
}
#else
printf("SDL_ttf absent : affichage dans la console.\n");
#endif
fill_screen(noir);
affiche_auto_on();
affiche_all();
}
void affiche_auto_on () {
SDL_AFFICHE_AUTO = 1;
}
void affiche_auto_off() {
SDL_AFFICHE_AUTO = 0;
}
void affiche_all() {
SDL_Event event;
if (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
exit(0);
if (__init_graphics_is_already_called == 25)
SDL_Flip(SDL_screen);
else {
init_graphics(380,80);
write_text("init_graphics() n'a pas <20>t<EFBFBD> appel<65>e.");
write_text("");
write_text(" Cliquer pour terminer.");
fprintf(stderr,"init_graphics() n'a pas <20>t<EFBFBD> appel<65>e.\n");
fprintf(stderr,"Cliquer pour terminer.\n");
wait_clic();
exit(1);
}
}
void synchro() { affiche_all(); }
COULEUR couleur_RGB(int r, int g, int b) {
COULEUR C;
return ((r%256)<<16) + ((g%256)<<8) + (b%256);
return C;
}
point get_mouse() {
point dep;
dep.x = dep.y = -1;
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
exit(0);
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE : exit(0);
default : break;
}
}
if (event.type == SDL_MOUSEMOTION)
{
dep.x = event.motion.x;
dep.y = event.motion.y;
}
}
return dep;
}
void wait_escape() {
int display = 1;
SDL_Event event;
point p;
p.x = WIDTH/2 - 170;
p.y = 25;
aff_pol("Appuyer sur Echap pour terminer",20,p,gris);
affiche_all();
while (SDL_WaitEvent(&event) && display) {
if (event.type == SDL_QUIT) exit(0);
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE : display=0; break;
default : break;
}
}
}
#ifdef SDL_TTF_OK
int i;
if (verdana_ok) for (i=0;i<256;i++) if (police[i]) TTF_CloseFont(police[i]);
TTF_Quit();
#endif
SDL_Quit();
}
point wait_clic() {
int encore = 1;
point P;
SDL_Event event;
P.x = 0;
P.y = 0;
#ifdef EN_LOCAL
#ifdef SDL_TTF_OK
point E,F;
char S[16];
E.x = WIDTH - 55; E.y = 15;
F.x = WIDTH; F.y = 0;
#endif
#endif
while (SDL_WaitEvent(&event) && encore) {
if ((event.type == SDL_MOUSEBUTTONDOWN) && (event.button.button == SDL_BUTTON_LEFT)) {
encore=0;
P.x = event.button.x;
P.y = HEIGHT-event.button.y;
}
if (event.type == SDL_MOUSEMOTION) {
#ifdef EN_LOCAL
#ifdef SDL_TTF_OK
if (police[10]) {
draw_fill_rectangle(E,F,noir);
sprintf(S,"%4d %4d",event.motion.x,HEIGHT - event.motion.y);
aff_pol(S,10,E,gris);
affiche_all();
}
#endif
#endif
printf("%cEn attente de clic ... %4d %4d %c",13,event.motion.x,HEIGHT - event.motion.y,13);
fflush(stdout);
}
if (event.type == SDL_QUIT) exit(0);
}
#ifdef EN_LOCAL
#ifdef SDL_TTF_OK
aff_pol(S,10,E,noir);
//draw_fill_rectangle(E,F,jaune);
affiche_all();
#endif
#endif
printf("%cClic en %4d %4d \n",13,P.x,P.y);
return P;
}
void fill_screen(COULEUR color) {
int i,j;
for (i=0;i<WIDTH;i++)
for (j=0;j<HEIGHT;j++)
*((COULEUR *)SDL_screen->pixels + (HEIGHT-j-1) * WIDTH + i) = color;
if (SDL_AFFICHE_AUTO)
affiche_all();
}
int dans_ecran(int x, int y) {
if (x<0) return 0;
if (x>=WIDTH) return 0;
if (y<0) return 0;
if (y>=HEIGHT) return 0;
return 1;
}
#define add_pix(x,y,color) if (dans_ecran((x),(y))) *((COULEUR *)SDL_screen->pixels + (HEIGHT-(y)-1) * WIDTH + (x)) = (color)
void draw_pixel(point p, COULEUR color) {
add_pix(p.x,p.y,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_line(point p1, point p2, COULEUR color) {
int xmin, xmax;
int ymin, ymax;
int i,j;
float a,b,ii,jj;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
if (xmin==xmax) for (j=ymin;j<=ymax;j++) add_pix(xmin,j,color);
if (ymin==ymax) for (i=xmin;i<=xmax;i++) add_pix(i,ymin,color);
// La variation la plus grande est en x
if ((xmax-xmin >= ymax-ymin) && (ymax-ymin>0)) {
a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
b = p1.y - a*p1.x;
for (i=xmin;i<=xmax;i++) {
jj = a*i+b;
j = jj;
if (((jj-j) > 0.5) && (j < HEIGHT-1)) j++;
add_pix(i,j,color);
}
}
// La variation la plus grande est en y
if ((ymax-ymin > xmax-xmin) && (xmax-xmin>0)) {
a = (float)(p1.y-p2.y) / ((float)(p1.x-p2.x));
b = p1.y - a*p1.x;
for (j=ymin;j<=ymax;j++) {
ii = (j-b)/a;
i = ii;
if (((ii-i) > 0.5) && (i < WIDTH-1)) i++;
add_pix(i,j,color);
}
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_rectangle(point p1, point p2, COULEUR color) {
int xmin, xmax;
int ymin, ymax;
int i,j;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
for (i=xmin;i<=xmax;i++) add_pix(i,ymin,color);
for (i=xmin;i<=xmax;i++) add_pix(i,ymax,color);
for (j=ymin;j<=ymax;j++) add_pix(xmin,j,color);
for (j=ymin;j<=ymax;j++) add_pix(xmax,j,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_fill_rectangle(point p1, point p2, COULEUR color) {
int xmin, xmax;
int ymin, ymax;
int i,j;
if (p1.x < p2.x) {xmin=p1.x; xmax=p2.x;} else{xmin=p2.x; xmax=p1.x;}
if (p1.y < p2.y) {ymin=p1.y; ymax=p2.y;} else{ymin=p2.y; ymax=p1.y;}
for (i=xmin;i<=xmax;i++) for (j=ymin;j<=ymax;j++) add_pix(i,j,color);
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_circle(point centre, int rayon, COULEUR color) {
point min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x + rayon;
min.y = centre.y - rayon; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++) {
dx = i - centre.x;
dy = sqrt(rr - dx*dx);
j = centre.y + dy;
add_pix(i,j,color);
j = centre.y - dy;
add_pix(i,j,color);
}
for (j=min.y;j<=max.y;j++) {
dy = j - centre.y;
dx = sqrt(rr - dy*dy);
i = centre.x + dx;
add_pix(i,j,color);
i = centre.x - dx;
add_pix(i,j,color);
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_fill_circle(point centre, int rayon, COULEUR color) {
point min, max;
int i,j;
float dx, dy, rr;
min.x = centre.x - rayon; max.x = centre.x + rayon;
min.y = centre.y - rayon; max.y = centre.y + rayon;
rr = rayon*rayon;
for (i=min.x;i<=max.x;i++) {
dx = i - centre.x;
for (j=min.y;j<=max.y;j++) {
dy = j - centre.y;
if (dx*dx + dy*dy <= rr) add_pix(i,j,color);
}
}
if (SDL_AFFICHE_AUTO) affiche_all();
}
void draw_square(point p, int taille, COULEUR color) {
point p1 = {p.x-taille/2, p.y-taille/2}, p2 = {p.x+taille/2, p.y+taille/2};
draw_rectangle(p1, p2, color);
}
void draw_fill_square(point p, int taille, COULEUR color) {
point p1 = {p.x-taille/2, p.y-taille/2}, p2 = {p.x+taille/2, p.y+taille/2};
draw_fill_rectangle(p1, p2, color);
}
void aff_pol(char *a_ecrire, int taille, point p, COULEUR C) {
#ifdef SDL_TTF_OK
int i;
SDL_Color color;
SDL_Surface *texte = NULL;
SDL_Rect position;
static int premiere_fois = 1;
static TTF_Font *police[256];
TTF_Font *pol;
if (premiere_fois) {
TTF_Init(); for (i=0;i<256;i++) police[i] = NULL; premiere_fois = 0;
}
if (taille>=256) pol = TTF_OpenFont(POLICE_NAME, taille);
else {
if (police[taille]==NULL) police[taille] = TTF_OpenFont(POLICE_NAME, taille);
pol = police[taille];
}
SDL_GetRGB(C,SDL_screen->format,&(color.r),&(color.g),&(color.b));
if (pol) texte = TTF_RenderText_Blended(pol, a_ecrire, color); else texte = NULL;
if (texte) {
position.x = p.x;
position.y = HEIGHT - p.y;
SDL_BlitSurface(texte, NULL, SDL_screen, &position);
if (SDL_AFFICHE_AUTO)
affiche_all();
SDL_FreeSurface(texte);
}
else printf("%s\n",a_ecrire);
/*
if (SDL_AFFICHE_AUTO) affiche_all();
if (police) TTF_CloseFont(police);
TTF_Quit();
if (texte) SDL_FreeSurface(texte);
*/
#else
taille = 0; p.x = p.y = 0; C = 0;
printf("%s\n",a_ecrire);
#endif
}
void aff_int(int n, int taille, point p, COULEUR C) {
char s[32];
sprintf(s,"%d",n);
aff_pol(s,taille,p,C);
}
void attendre(int millisecondes) { SDL_Delay(millisecondes); }
float alea_float() {
static int init_alea = 1;
if (init_alea) srand(time(NULL));
init_alea = 0;
return (float)rand() / (float)RAND_MAX;
}
int alea_int(int N) { return (int)(N*alea_float()); }
void write_text(char *a_ecrire) {
#ifdef SDL_TTF_OK
static char s[256];
static int fin = 0;
static int premier = 1;
static point position;
if (premier) {
position.x = 10;
position.y = HEIGHT;
premier = 0;
}
if (verdana_ok)
{
// printf("verd\n");
if (a_ecrire) {
if (fin+strlen(a_ecrire) > 256) {
fprintf(stderr,"Chaine de caract<63>re trop longue\n");
return;
}
strcat(s+fin,a_ecrire);
// printf("a_ecr : #%s#\n",s);
aff_pol(s,20,position,blanc);
fin += strlen(a_ecrire);
s[fin] = '\0';
}
else {
position.y -= 20;
fin = 0;
s[fin] = '\0';
}
}
else {
if (a_ecrire) {
printf("%s",a_ecrire);
fflush(stdout);
}
else printf("\n");
}
#else
if (a_ecrire) {printf("%s",a_ecrire); fflush(stdout);}
else printf("\n");
#endif
}

62
graphics.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <SDL/SDL.h>
#include "couleur.h"
typedef struct point {int x,y;} point;
typedef Uint32 COULEUR;
typedef int BOOL;
#define MINDEP 1
#define TRUE 1
#define True 1
#define true 1
#define FALSE 0
#define False 0
#define false 0
void init_graphics(int W, int H);
void affiche_auto_on();
void affiche_auto_off();
void affiche_all();
void synchro();
COULEUR couleur_RGB(int r, int g, int b);
point get_mouse();
void wait_escape();
point wait_clic();
void fill_screen(COULEUR color);
void draw_pixel(point p, COULEUR color);
void draw_line(point p1, point p2, COULEUR color);
void draw_rectangle(point p1, point p2, COULEUR color);
void draw_fill_rectangle(point p1, point p2, COULEUR color);
void draw_circle(point centre, int rayon, COULEUR color);
void draw_fill_circle(point centre, int rayon, COULEUR color);
void draw_square(point p1, int taille, COULEUR color);
void draw_fill_square(point p1, int taille, COULEUR color);
void aff_pol(char *a_ecrire, int taille, point p, COULEUR C);
void aff_int(int n, int taille, point p, COULEUR C);
void attendre(int millisecondes);
int alea_int(int N);
void write_text(char *a_ecrire);
#endif

1
police.h Normal file
View File

@ -0,0 +1 @@
#define SDL_TTF_OK

BIN
verdana.ttf Normal file

Binary file not shown.