Commit 82175b90d322151fec75c03cdb68e0d3b0f97071
1 parent
f1721432
.
Showing
1 changed file
with
0 additions
and
239 deletions
Show diff stats
libgraph.c deleted
... | ... | @@ -1,239 +0,0 @@ |
1 | -/**** Bibliotheque graphique ****/ | |
2 | - | |
3 | -/** Fichiers d'inclusion **/ | |
4 | - | |
5 | -#include <SDL/SDL.h> | |
6 | -#include <SDL/SDL_ttf.h> | |
7 | -#include "libgraph.h" | |
8 | - | |
9 | -/** Types **/ | |
10 | - | |
11 | -typedef struct | |
12 | -{ | |
13 | - int r, v, b; | |
14 | -} couleur; | |
15 | - | |
16 | -/** Constantes **/ | |
17 | - | |
18 | -#define BITS_PAR_PIXEL 32 | |
19 | -#define TAILLE_POLICE 20 | |
20 | - | |
21 | -static const couleur couleurs[] = { { 255, 255, 255 }, { 0, 0, 0 }, { 255, 0, 0 }, | |
22 | - { 0, 255, 0 }, { 0, 0, 255 }, { 255, 105, 180 }, | |
23 | - { 150, 150, 150 }, { -1, -1, -1 } }; | |
24 | - | |
25 | -static const char policeDefaut[]="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; | |
26 | - | |
27 | -/** Variables globales **/ | |
28 | - | |
29 | -static SDL_Surface *surface; | |
30 | -TTF_Font* police; | |
31 | - | |
32 | -/** Fonctions **/ | |
33 | - | |
34 | -void choisirPolice(const char *chemin, int taille){ | |
35 | - police=TTF_OpenFont(chemin, taille); | |
36 | -} | |
37 | - | |
38 | -void initialiserTexte() { | |
39 | - TTF_Init(); | |
40 | - choisirPolice(policeDefaut, TAILLE_POLICE); | |
41 | -} | |
42 | - | |
43 | -/* Initialisation de la surface dessinable */ | |
44 | -unsigned char creerSurface (int largeur, int hauteur, char *titre) | |
45 | -{ | |
46 | - SDL_Init (SDL_INIT_VIDEO); | |
47 | - SDL_WM_SetCaption (titre, titre); | |
48 | - surface = SDL_SetVideoMode (largeur, hauteur, BITS_PAR_PIXEL, SDL_DOUBLEBUF); | |
49 | - initialiserTexte(); | |
50 | - | |
51 | - return (surface != NULL && police != NULL); | |
52 | -} | |
53 | - | |
54 | -/* Fermeture de la surface dessinable */ | |
55 | - | |
56 | -void fermerSurface (void) | |
57 | -{ | |
58 | - if (surface != NULL) SDL_FreeSurface (surface); | |
59 | - if (police != NULL) TTF_CloseFont(police); | |
60 | - TTF_Quit(); | |
61 | - SDL_Quit (); | |
62 | -} | |
63 | - | |
64 | -/* Creation d'une couleur */ | |
65 | - | |
66 | -static int creerCouleur (int ncouleur) | |
67 | -{ | |
68 | - couleur c = couleurs[ncouleur]; | |
69 | - return SDL_MapRGB (surface->format, c.r, c.v, c.b); | |
70 | -} | |
71 | - | |
72 | -/* Dessin d'un rectangle plein */ | |
73 | - | |
74 | -void rectanglePlein (int x, int y, int l, int h, int c) | |
75 | -{ | |
76 | - SDL_Rect rectangle = { x, y, l, h }; | |
77 | - SDL_FillRect (surface, &rectangle, creerCouleur (c)); | |
78 | - // SDL_Flip(surface); | |
79 | -} | |
80 | - | |
81 | -/* Manipulation de lutins */ | |
82 | - | |
83 | -static SDL_Surface *lutins[MAX_LUTINS]; | |
84 | -static int lutins_nb = 0; | |
85 | - | |
86 | -int lutinTexte(char* texte, int couleurTexte) { | |
87 | - couleur c=couleurs[couleurTexte]; | |
88 | - SDL_Color couleur={c.r, c.v, c.b}; | |
89 | - SDL_Surface* lutin=TTF_RenderText_Solid(police, texte, couleur); | |
90 | - if (lutin != NULL) | |
91 | - { | |
92 | - lutins[lutins_nb++] = lutin; | |
93 | - return lutins_nb - 1; | |
94 | - } | |
95 | - return -1; | |
96 | -} | |
97 | - | |
98 | -static void configurerLutin (SDL_Surface *lutin, int ncouleur) | |
99 | -{ | |
100 | - couleur c = couleurs[ncouleur]; | |
101 | - int fond = SDL_MapRGB (lutin->format, c.r, c.v, c.b); | |
102 | - SDL_SetColorKey (lutin, SDL_SRCCOLORKEY | SDL_RLEACCEL, fond); | |
103 | -} | |
104 | - | |
105 | -int chargerLutin (char *fichier, int couleur) | |
106 | -{ | |
107 | - if (lutins_nb >= MAX_LUTINS) return -2; | |
108 | - SDL_Surface *lutin = SDL_LoadBMP (fichier); | |
109 | - if (lutin != NULL) | |
110 | - { | |
111 | - lutins[lutins_nb++] = lutin; | |
112 | - if (couleur >= 0) configurerLutin (lutin, couleur); | |
113 | - return lutins_nb - 1; | |
114 | - } | |
115 | - return -1; | |
116 | -} | |
117 | - | |
118 | -void afficherLutin (int lutin, int x, int y) | |
119 | -{ | |
120 | - SDL_Rect position; | |
121 | - position.x = x; | |
122 | - position.y = y; | |
123 | - SDL_BlitSurface (lutins[lutin], NULL, surface, &position); | |
124 | -} | |
125 | - | |
126 | -int creerLutin (int x, int y, int largeur, int hauteur, int couleur) | |
127 | -{ | |
128 | - if (lutins_nb >= MAX_LUTINS) return -2; | |
129 | - int rmask, gmask, bmask, amask; | |
130 | -#if SDL_BYTEORDER == SDL_BIG_ENDIAN | |
131 | - rmask = 0xff000000; | |
132 | - gmask = 0x00ff0000; | |
133 | - bmask = 0x0000ff00; | |
134 | - amask = 0x000000ff; | |
135 | -#else | |
136 | - rmask = 0x000000ff; | |
137 | - gmask = 0x0000ff00; | |
138 | - bmask = 0x00ff0000; | |
139 | - amask = 0xff000000; | |
140 | -#endif | |
141 | - if (couleur < 0) amask = 0x00000000; | |
142 | - SDL_Surface *lutin = | |
143 | - SDL_CreateRGBSurface (0, largeur, hauteur, BITS_PAR_PIXEL, rmask, gmask, bmask, amask); | |
144 | - SDL_Rect fenetre; | |
145 | - fenetre.x = x; | |
146 | - fenetre.y = y; | |
147 | - fenetre.h = hauteur; | |
148 | - fenetre.w = largeur; | |
149 | - SDL_BlitSurface (surface, &fenetre, lutin, NULL); | |
150 | - lutins[lutins_nb++] = lutin; | |
151 | - if (couleur >= 0) configurerLutin (lutin, couleur); | |
152 | - return lutins_nb - 1; | |
153 | -} | |
154 | - | |
155 | -void tailleLutin (int lutin, int *largeur, int *hauteur) | |
156 | -{ | |
157 | - *largeur = lutins[lutin]->w; | |
158 | - *hauteur = lutins[lutin]->h; | |
159 | -} | |
160 | - | |
161 | -int sauverLutin (int lutin, char *nom) { return SDL_SaveBMP (lutins[lutin], nom); } | |
162 | - | |
163 | -/* Manipulation de copie de surface en BMP */ | |
164 | - | |
165 | -int sauverSurface (char *fichier) { return SDL_SaveBMP (surface, fichier); } | |
166 | - | |
167 | -unsigned char chargerSurface (char *fichier) | |
168 | -{ | |
169 | - SDL_Surface *image = SDL_LoadBMP (fichier); | |
170 | - if (image != NULL) | |
171 | - { | |
172 | - SDL_BlitSurface (image, NULL, surface, NULL); | |
173 | - SDL_Flip (surface); | |
174 | - } | |
175 | - return (image != NULL); | |
176 | -} | |
177 | - | |
178 | -void majSurface (void) { SDL_Flip (surface); } | |
179 | - | |
180 | -/* Trouver la couleur d'un pixel */ | |
181 | - | |
182 | -int couleurPixel (int x, int y) | |
183 | -{ | |
184 | - int bpp = surface->format->BytesPerPixel; | |
185 | - Uint32 *p = (Uint32 *)(surface->pixels + y * surface->pitch + x * bpp); | |
186 | - Uint8 r, v, b; | |
187 | - SDL_GetRGB (*p, surface->format, &r, &v, &b); | |
188 | - int i = 0; | |
189 | - while (1) | |
190 | - { | |
191 | - if (couleurs[i].r < 0) break; | |
192 | - if (r == couleurs[i].r && v == couleurs[i].v && b == couleurs[i].b) break; | |
193 | - i++; | |
194 | - } | |
195 | - if (couleurs[i].r < 0) | |
196 | - return -1; | |
197 | - else | |
198 | - return i; | |
199 | -} | |
200 | - | |
201 | -/* Fonction de traitement des événements */ | |
202 | - | |
203 | -void lireEvenement (evenement *evt, char *touche, void **detail) | |
204 | -{ | |
205 | - static SDL_keysym _detail; | |
206 | - SDL_Event event; | |
207 | - while (SDL_PollEvent (&event)) | |
208 | - { | |
209 | - if (event.type == SDL_QUIT) *evt = quitter; | |
210 | - if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) | |
211 | - { | |
212 | - *evt = (event.type == SDL_KEYDOWN) ? toucheBas : toucheHaut; | |
213 | - char *nom = SDL_GetKeyName (event.key.keysym.sym); | |
214 | - if (strlen (nom) == 1 && nom[0] >= 32 && nom[0] < 128) | |
215 | - *touche = nom[0]; | |
216 | - else | |
217 | - *touche = 0; | |
218 | - if (detail != NULL) | |
219 | - { | |
220 | - _detail = event.key.keysym; | |
221 | - *detail = &_detail; | |
222 | - } | |
223 | - break; | |
224 | - } | |
225 | - } | |
226 | -} | |
227 | - | |
228 | -void attendreEvenement (void) | |
229 | -{ | |
230 | - SDL_Event event; | |
231 | - while (SDL_WaitEvent (&event)) switch (event.type) | |
232 | - { | |
233 | - case SDL_QUIT: | |
234 | - exit (0); | |
235 | - case SDL_KEYDOWN: | |
236 | - case SDL_MOUSEBUTTONDOWN: | |
237 | - return; | |
238 | - } | |
239 | -} |