]> git.jsancho.org Git - gacela.git/blob - SDL.c
(no commit message)
[gacela.git] / SDL.c
1 #define max(a, b) ((a > b) ? a : b)
2 #define min(a, b) ((a < b) ? a : b)
3
4 int
5 gacela_SDL_SurfaceFormat (int surface)
6 {
7   SDL_Surface *s = surface;
8
9   return s->format;
10 }
11
12 void
13 gacela_SDL_BlitSurface (int src, int srcrect, int dst, int dstrect)
14 {
15   SDL_BlitSurface (src, srcrect, dst, dstrect);
16 }
17
18 int
19 gacela_SDL_Rect (int x, int y, int w, int h)
20 {
21   SDL_Rect *rect;
22
23   rect = (SDL_Rect *)malloc (sizeof (SDL_Rect));
24   rect->x = x;
25   rect->y = y;
26   rect->w = w;
27   rect->h = h;
28
29   return rect;
30 }
31
32 int
33 gacela_TTF_Init (void)
34 {
35   return TTF_Init ();
36 }
37
38 int
39 gacela_TTF_OpenFont (char *file, int ptsize)
40 {
41   return TTF_OpenFont (file, ptsize);
42 }
43
44 void
45 gacela_TTF_CloseFont (int font)
46 {
47   TTF_CloseFont (font);
48 }
49
50 void
51 gacela_TTF_Quit (void)
52 {
53   TTF_Quit ();
54 }
55
56 int
57 gacela_Mix_OpenAudio (int frequency, int channels, int chunksize)
58 {
59   return Mix_OpenAudio (frequency, MIX_DEFAULT_FORMAT, channels, chunksize);
60 }
61
62 int
63 gacela_Mix_LoadMUS (char *filename)
64 {
65   return Mix_LoadMUS (filename);
66 }
67
68 int
69 gacela_Mix_LoadWAV (char *filename)
70 {
71   return Mix_LoadWAV (filename);
72 }
73
74 int
75 gacela_Mix_PlayChannel (int channel, int chunk, int loops)
76 {
77   return Mix_PlayChannel (channel, chunk, loops);
78 }
79
80 int
81 gacela_Mix_PlayMusic (int music, int loops)
82 {
83   return Mix_PlayMusic (music, loops);
84 }
85
86 int
87 gacela_Mix_PlayingMusic (void)
88 {
89   return Mix_PlayingMusic ();
90 }
91
92 int
93 gacela_Mix_PausedMusic (void)
94 {
95   return Mix_PausedMusic ();
96 }
97
98 void
99 gacela_Mix_PauseMusic (void)
100 {
101   Mix_PauseMusic ();
102 }
103
104 void
105 gacela_Mix_ResumeMusic (void)
106 {
107   Mix_ResumeMusic ();
108 }
109
110 int
111 gacela_Mix_HaltMusic (void)
112 {
113   return Mix_HaltMusic ();
114 }
115
116 void
117 gacela_Mix_FreeChunk (int chunk)
118 {
119   Mix_FreeChunk (chunk);
120 }
121
122 void
123 gacela_Mix_FreeMusic (int music)
124 {
125   Mix_FreeMusic (music);
126 }
127
128 void
129 gacela_Mix_CloseAudio (void)
130 {
131   Mix_CloseAudio ();
132 }
133
134 void
135 gacela_sge_FilledCircle (int surface, int x, int y, int r, int red, int green, int blue)
136 {
137   SDL_Surface *s = surface;
138
139   sge_FilledCircle (s, x, y, r, SDL_MapRGB (s->format, red, green, blue));
140 }
141
142 void
143 gacela_sge_FilledRect (int surface, int x1, int y1, int x2, int y2, int red, int green, int blue)
144 {
145   SDL_Surface *s = surface;
146
147   sge_FilledRect (s, x1, y1, x2, y2, SDL_MapRGB (s->format, red, green, blue));
148 }
149
150 void
151 gacela_free (int pointer)
152 {
153   free (pointer);
154 }
155
156 void
157 apply_surface (int x, int y, int source, int destination, \
158                int cx, int cy, int cw, int ch, int cid)
159 {
160   SDL_Rect offset;
161   SDL_Rect *clip = NULL;
162   SDL_Surface *tmps = source;
163   int tmpw, tmpx, tmpy;
164
165   if (cw != 0 || ch != 0)
166     {
167       clip = (SDL_Rect *)malloc(sizeof(SDL_Rect));
168       if (cid == 0)
169         {
170           clip->x = cx;
171           clip->y = cy;
172         }
173       else
174         {
175           tmpw = tmps->w / cw;
176           if (tmps->w % cw > 0) tmpw++;
177           tmpy = cid / tmpw;
178           tmpx = cid - tmpw*tmpy;
179
180           if (tmpx * cw > tmps->w || tmpy * ch > tmps->h)
181             {
182               clip->x = 0;
183               clip->y = 0;
184             }
185           else
186             {
187               clip->x = tmpx * cw;
188               clip->y = tmpy * ch;
189             }
190           printf ("Id: %d cx: %d cy: %d\n", cid, clip->x, clip->y);
191         }
192       clip->w = cw;
193       clip->h = ch;
194     }
195
196   offset.x = x;
197   offset.y = y;
198   SDL_BlitSurface (source, clip, destination, &offset);
199   free(clip);
200 }
201
202 int
203 render_text (int font, char *text, int red, int green, int blue)
204 {
205   SDL_Color textColor = {red, green, blue};
206   return TTF_RenderText_Solid (font, text, textColor); 
207 }
208
209 int
210 load_image (char *filename, int red, int green, int blue)
211 {
212   SDL_Surface *loadedImage = NULL;
213   SDL_Surface *optimizedImage = NULL;
214
215   loadedImage = IMG_Load (filename);
216   if (loadedImage != NULL)
217     {
218       optimizedImage = SDL_DisplayFormat (loadedImage);
219       SDL_FreeSurface (loadedImage);
220       if (optimizedImage != NULL)
221         {
222           SDL_SetColorKey (optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB (optimizedImage->format, red, green, blue));
223         }
224     }
225   return optimizedImage;
226 }
227
228 void
229 fill_surface (int surface, int red, int green, int blue)
230 {
231   SDL_Surface *s = surface;
232
233   SDL_FillRect (s, &s->clip_rect, SDL_MapRGB (s->format, red, green, blue));
234 }
235
236 int
237 box_collision (int surface1, int x1, int y1, int surface2, int x2, int y2)
238 {
239   SDL_Surface *s1 = surface1;
240   SDL_Surface *s2 = surface2;
241   int left1, left2, bottom1, bottom2;
242   int xstart, xend, ystart, yend;
243   int x, y;
244   
245   left1 = x1 + s1->w - 1;
246   bottom1 = y1 + s1->h - 1;
247   left2 = x2 + s2->w - 1;
248   bottom2 = y2 + s2->h - 1;
249   
250   if ((x1 > left2) || (x2 > left1)) return 0;
251   if ((y1 > bottom2) || (y2 > bottom1)) return 0;
252   return 1;
253 }
254
255 int
256 transparent_pixel (SDL_Surface *surface, int x, int y)
257 {
258   int bpp = surface->format->BytesPerPixel;
259   Uint8 *p;
260   Uint32 pixelcolor;
261
262   if (SDL_MUSTLOCK (surface)) SDL_LockSurface (surface);
263   assert ((x < surface->w) && (y < surface->h));
264
265   p = (Uint8 *)surface->pixels + y*surface->pitch + x*bpp;
266
267   switch (bpp)
268     {
269       case (1):
270         pixelcolor = *p;
271         break;
272
273       case (2):
274         pixelcolor = *(Uint16 *)p;
275         break;
276
277       case (3):
278         if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
279           pixelcolor = p[0] << 16 | p[1] << 8 | p[2];
280         else
281           pixelcolor = p[0] | p[1] << 8 | p[2] << 16;
282         break;
283
284       case (4):
285         pixelcolor = *(Uint32 *)p;
286         break;
287     }
288
289   if (SDL_MUSTLOCK (surface)) SDL_UnlockSurface (surface);
290
291   return (pixelcolor == surface->format->colorkey);
292 }
293
294 int
295 create_SDL_Surface (int screen, int w, int h, int red, int green, int blue)
296 {
297   SDL_Surface *s = screen;
298   SDL_Surface *new = NULL;
299
300   new = SDL_CreateRGBSurface (s->flags, w, h, \
301                               s->format->BitsPerPixel, \
302                               s->format->Rmask, s->format->Gmask, \
303                               s->format->Bmask, s->format->Amask);
304   if (new != NULL)
305     {
306       SDL_SetColorKey (new, SDL_SRCCOLORKEY, SDL_MapRGB (new->format, red, green, blue));
307     }
308
309   return new;
310 }
311
312 int
313 copy_SDL_Surface (int surface)
314 {
315   SDL_Surface *s = surface;
316   
317   return SDL_ConvertSurface (s, s->format, s->flags);
318 }