-
Notifications
You must be signed in to change notification settings - Fork 0
/
affichage_sdl.c
173 lines (144 loc) · 4.46 KB
/
affichage_sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "affichage_sdl.h"
/** Dessine un pixel
* screen : Surface cible
* x, y : Coordonnées du point
* color : Couleur du pixel à dessiner
**/
void setPixel(SDL_Surface *screen, int x, int y, Uint32 color)
{
if(x < 0 || y < 0 || x >= screen->w || y >= screen->h)
return;
*((Uint32*)(screen->pixels) + x + y * screen->w) = color;
}
void drawSquare(SDL_Surface *screen, int coteBloc, int hBlocs, int vBlocs)
{
SDL_Rect blitrect = {hBlocs*coteBloc, vBlocs*coteBloc,0,0};
SDL_Surface *carre = NULL;
Uint32 color = SDL_MapRGB(screen->format, 255,255,255);
// TODO Calculer width et length avant
carre = SDL_CreateRGBSurface(0,coteBloc,coteBloc,32, 0, 0, 0,255);
SDL_FillRect(carre, NULL, color);
SDL_BlitSurface(carre,NULL, screen, &blitrect);
SDL_FreeSurface(carre);
}
void drawSquareAbsolute(SDL_Surface *screen, int coteBloc, int x, int y)
{
SDL_Rect blitrect = {x, y,0,0};
SDL_Surface *carre = NULL;
Uint32 color = SDL_MapRGB(screen->format, 255,255,255);
// TODO Calculer width et length avant
carre = SDL_CreateRGBSurface(0,coteBloc,coteBloc,32, 0, 0, 0,255);
SDL_FillRect(carre, NULL, color);
SDL_BlitSurface(carre,NULL, screen, &blitrect);
SDL_FreeSurface(carre);
}
/** Dessine la grille de jeu
* screen : Surface qui accueille la grille
* coteBloc : largeur d'une maille de la grille
* x, y : Bord supérieur gauche de la grille
* w, h : Nombre de cases en largeur/hauteur
**/
void drawGrid(SDL_Surface *screen, int coteBloc, int x, int y, int w, int h)
{
int i, j;
/// Parcours case par case puis pixel par pixel
for(i = x; i < x + w*coteBloc+1; i += coteBloc)
{
for(j = y; j < y + h*coteBloc; j++)
{
setPixel(screen, i,j, SDL_MapRGB(screen->format, 100,100,100));
}
}
for(j = y; j < y + h*coteBloc+1; j += coteBloc)
{
for(i = x; i < x + w*coteBloc; i++)
{
setPixel(screen, i,j, SDL_MapRGB(screen->format, 100,100,100));
}
}
}
/* Initialise la SDL
* ptr_screen : Pointeur sur le pointeur de la SDL_Surface
* ptr_police : Pointeur sur la police de caractère utilisée
* On récupère un pointeur sur un pointeur pour modifier le pointeur
* original
*/
void initSDL(SDL_Surface **ptr_screen, TTF_Font **ptr_police, int w, int h)
{
/*************************** Initialisation SDL ***************************/
atexit(SDL_Quit);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Erreur à l'initialisation de la SDL : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
// Modifie la valeur du pointeur 'screen'
*ptr_screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
if (*ptr_screen == NULL) {
fprintf(stderr, "Impossible d'activer le mode graphique : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if(TTF_Init() == -1)
{
fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
exit(EXIT_FAILURE);
}
*ptr_police = TTF_OpenFont("Font.ttf", 30);
SDL_WM_SetCaption("Conway's Game of Life !", NULL);
/*************************** Initialisation SDL ***************************/
}
/** Dessine la surface de jeu sur screen
* screen : Surface où dessiner
* sim : Surface à dessiner
* grid : Dessine la grille si vrai
**/
void drawGameMatrix(SDL_Surface *screen, Simulation *sim, int grid)
{
int x = 0, y = 0, i, j;
int xCoin = sim->dispX - 0.5*sim->largeurCase*sim->nbCasesX;
int yCoin = sim->dispY - 0.5*sim->largeurCase*sim->nbCasesY;
for(i = 0; i < sim->nbCasesX; i++)
{
for(j = 0; j < sim->nbCasesY; j++)
{
if(sim->buffers[sim->actualBuffer][i][j])
{
x = xCoin + i*sim->largeurCase;
y = yCoin + j*sim->largeurCase;
drawSquareAbsolute(screen, sim->largeurCase, x, y);
}
}
}
if(grid)
{
drawGrid(screen, sim->largeurCase, xCoin, yCoin, sim->nbCasesX, sim->nbCasesY);
drawGrid(screen, sim->nbCasesX*sim->largeurCase/sqrt(sim->nbThread), xCoin, yCoin, sqrt(sim->nbThread), sqrt(sim->nbCasesY));
}
}
Fps initFps()
{
Fps fps;
fps.goal = 600;
fps.ticksBetweenFrames = 1000/fps.goal;
fps.nextTick = 0;
fps.real = fps.goal;
fps.realCpt = 0;
fps.lastSecondTicks = 0;
return fps;
}
void tempoFps(Fps *fps)
{
// Si on doit attendre avant la prochaine frame
if(fps->nextTick > SDL_GetTicks())
SDL_Delay(fps->nextTick - SDL_GetTicks());
// On calcule le tick où la prochaine frame va s'afficher
fps->nextTick = SDL_GetTicks() + fps->ticksBetweenFrames;
}
void computeTrueFps(Fps *fps)
{
if(SDL_GetTicks() >= fps->lastSecondTicks+1000)
{
fps->real = fps->realCpt;
fps->realCpt = 0;
fps->lastSecondTicks = SDL_GetTicks();
}
}