1 /* Gacela, a GNU Guile extension for fast games development
2 Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <SDL/SDL_events.h>
21 #include <SDL/SDL_image.h>
22 #include <SDL/SDL_mixer.h>
23 #include <SDL/SDL_rotozoom.h>
24 #include "gacela_SDL.h"
29 SDL_Surface *surface_address;
32 static scm_t_bits surface_tag;
35 make_surface (SCM file, SDL_Surface *surface_address)
38 struct surface *surface;
40 surface = (struct surface *) scm_gc_malloc (sizeof (struct surface), "surface");
42 surface->filename = SCM_BOOL_F;
43 surface->surface_address = NULL;
45 SCM_NEWSMOB (smob, surface_tag, surface);
47 surface->filename = file;
48 surface->surface_address = surface_address;
54 get_surface_address (SCM surface_smob)
56 struct surface *surface;
58 scm_assert_smob_type (surface_tag, surface_smob);
59 surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
60 return surface->surface_address;
64 get_surface_filename (SCM surface_smob)
66 struct surface *surface;
68 scm_assert_smob_type (surface_tag, surface_smob);
69 surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
70 return surface->filename;
74 get_surface_width (SCM surface_smob)
76 SDL_Surface *surface = get_surface_address (surface_smob);
78 return scm_from_int (surface->w);
82 get_surface_height (SCM surface_smob)
84 SDL_Surface *surface = get_surface_address (surface_smob);
86 return scm_from_int (surface->h);
90 get_surface_pixels (SCM surface_smob)
92 SDL_Surface *surface = get_surface_address (surface_smob);
94 return scm_from_int (surface->pixels);
98 get_surface_format_BytesPerPixel (SCM surface_smob)
100 SDL_Surface *surface = get_surface_address (surface_smob);
102 return scm_from_int (surface->format->BytesPerPixel);
106 mark_surface (SCM surface_smob)
108 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
110 scm_gc_mark (surface->filename);
116 free_surface (SCM surface_smob)
118 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
120 SDL_FreeSurface (surface->surface_address);
121 scm_gc_free (surface, sizeof (struct surface), "surface");
127 print_surface (SCM surface_smob, SCM port, scm_print_state *pstate)
129 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
131 scm_puts ("#<surface \"", port);
132 scm_display (surface->filename, port);
133 scm_puts ("\">", port);
135 /* non-zero means success */
141 gacela_SDL_Init (SCM flags)
143 return scm_from_int (SDL_Init (scm_to_int (flags)));
147 gacela_SDL_Quit (void)
150 return SCM_UNSPECIFIED;
154 gacela_SDL_SetVideoMode (SCM width, SCM height, SCM bpp, SCM flags)
156 SDL_Surface *screen = SDL_SetVideoMode (scm_to_int (width), scm_to_int (height), \
157 scm_to_int (bpp), scm_to_int (flags));
160 return make_surface (scm_from_locale_string ("screen"), screen);
168 gacela_SDL_FreeSurface (SCM surface)
170 return scm_from_int (free_surface (surface));
174 gacela_SDL_WM_SetCaption (SCM title, SCM icon)
176 SDL_WM_SetCaption (scm_to_locale_string(title), scm_to_locale_string(icon));
177 return SCM_UNSPECIFIED;
181 gacela_SDL_Flip (SCM screen)
183 return scm_from_int (SDL_Flip (get_surface_address (screen)));
187 gacela_SDL_Delay (SCM ms)
189 SDL_Delay ((int)scm_to_double (ms));
190 return SCM_UNSPECIFIED;
194 gacela_SDL_GetTicks (void)
196 return scm_from_int (SDL_GetTicks ());
200 gacela_SDL_DisplayFormat (SCM surface)
202 return scm_from_int ((int)SDL_DisplayFormat (get_surface_address (surface)));
206 gacela_SDL_MapRGB (SCM format, SCM r, SCM g, SCM b)
208 return scm_from_int (SDL_MapRGB ((SDL_PixelFormat *)scm_to_int (format), scm_to_int (r), scm_to_int (g), scm_to_int (b)));
212 gacela_SDL_SetColorKey (SCM surface, SCM flag, SCM key)
214 return scm_from_int (SDL_SetColorKey (get_surface_address (surface), scm_to_int (flag), scm_to_int (key)));
218 gacela_SDL_SetAlpha (SCM surface, SCM flag, SCM alpha)
220 return scm_from_int (SDL_SetAlpha (get_surface_address (surface), scm_to_int (flag), scm_to_int (alpha)));
224 gacela_SDL_LoadBMP (SCM file)
226 SDL_Surface *image = SDL_LoadBMP (scm_to_locale_string (file));
229 return make_surface (file, image);
237 gacela_IMG_Load (SCM filename)
239 SDL_Surface *image = IMG_Load (scm_to_locale_string (filename));
242 return make_surface (filename, image);
250 gacela_SDL_GetVideoInfo (void)
252 const SDL_VideoInfo *info;
255 info = SDL_GetVideoInfo ();
256 vi = scm_list_n (SCM_UNDEFINED);
258 vi = scm_cons (scm_cons (scm_from_locale_symbol ("blit_hw"), scm_from_int (info->blit_hw)), vi);
259 vi = scm_cons (scm_cons (scm_from_locale_symbol ("hw_available"), scm_from_int (info->hw_available)), vi);
265 gacela_SDL_GL_SetAttribute (SCM attr, SCM value)
267 return scm_from_int (SDL_GL_SetAttribute (scm_to_int (attr), scm_to_int (value)));
271 gacela_SDL_PollEvent (void)
276 event = scm_list_n (SCM_UNDEFINED);
278 if (SDL_PollEvent (&sdl_event)) {
279 switch (sdl_event.type) {
282 event = scm_cons (scm_cons (scm_from_locale_symbol ("key.keysym.sym"), scm_from_int (sdl_event.key.keysym.sym)), event);
285 event = scm_cons (scm_cons (scm_from_locale_symbol ("type"), scm_from_int (sdl_event.type)), event);
292 gacela_SDL_GL_SwapBuffers (void)
294 SDL_GL_SwapBuffers ();
295 return SCM_UNSPECIFIED;
299 gacela_SDL_EnableKeyRepeat (SCM delay, SCM interval)
301 return scm_from_int (SDL_EnableKeyRepeat (scm_to_int (delay), scm_to_int (interval)));
305 gacela_zoomSurface (SCM src, SCM zoomx, SCM zoomy, SCM smooth)
307 SDL_Surface *image = zoomSurface (get_surface_address (src), scm_to_double (zoomx), scm_to_double (zoomy), scm_to_int (smooth));
310 return make_surface (get_surface_filename (src), image);
318 gacela_Mix_OpenAudio (SCM frequency, SCM format, SCM channels, SCM chunksize)
320 return scm_from_int (Mix_OpenAudio (scm_to_int (frequency), scm_to_int (format), scm_to_int (channels), scm_to_int (chunksize)));
324 gacela_Mix_LoadMUS (SCM file)
326 return scm_from_int ((int)Mix_LoadMUS (scm_to_locale_string (file)));
330 gacela_Mix_LoadWAV (SCM file)
332 return scm_from_int ((int)Mix_LoadWAV (scm_to_locale_string (file)));
336 gacela_Mix_PlayChannel (SCM channel, SCM chunk, SCM loops)
338 return scm_from_int (Mix_PlayChannel (scm_to_int (channel), (Mix_Chunk *)scm_to_int (chunk), scm_to_int (loops)));
342 gacela_Mix_PlayMusic (SCM music, SCM loops)
344 return scm_from_int (Mix_PlayMusic ((Mix_Music *)scm_to_int (music), scm_to_int (loops)));
348 gacela_Mix_PlayingMusic (void)
350 return scm_from_int (Mix_PlayingMusic ());
354 gacela_Mix_PausedMusic (void)
356 return scm_from_int (Mix_PausedMusic ());
360 gacela_Mix_PauseMusic (void)
363 return SCM_UNSPECIFIED;
367 gacela_Mix_ResumeMusic (void)
370 return SCM_UNSPECIFIED;
374 gacela_Mix_HaltMusic (void)
376 return scm_from_int (Mix_HaltMusic ());
380 gacela_Mix_FreeMusic (SCM music)
382 Mix_FreeMusic ((Mix_Music *)scm_to_int (music));
383 return SCM_UNSPECIFIED;
387 gacela_Mix_FreeChunk (SCM chunk)
389 Mix_FreeChunk ((Mix_Chunk *)scm_to_int (chunk));
390 return SCM_UNSPECIFIED;
394 gacela_Mix_CloseAudio (void)
397 return SCM_UNSPECIFIED;
402 SDL_register_functions (void* data)
404 surface_tag = scm_make_smob_type ("surface", sizeof (struct surface));
405 scm_set_smob_mark (surface_tag, mark_surface);
406 scm_set_smob_free (surface_tag, free_surface);
407 scm_set_smob_print (surface_tag, print_surface);
408 scm_c_define_gsubr ("surface-file", 1, 0, 0, get_surface_filename);
409 scm_c_define_gsubr ("surface-w", 1, 0, 0, get_surface_width);
410 scm_c_define_gsubr ("surface-h", 1, 0, 0, get_surface_height);
411 scm_c_define_gsubr ("surface-pixels", 1, 0, 0, get_surface_pixels);
412 scm_c_define_gsubr ("surface-format-BytesPerPixel", 1, 0, 0, get_surface_format_BytesPerPixel);
414 scm_c_define ("SDL_INIT_TIMER", scm_from_int (SDL_INIT_TIMER));
415 scm_c_define ("SDL_INIT_AUDIO", scm_from_int (SDL_INIT_AUDIO));
416 scm_c_define ("SDL_INIT_VIDEO", scm_from_int (SDL_INIT_VIDEO));
417 scm_c_define ("SDL_INIT_CDROM", scm_from_int (SDL_INIT_CDROM));
418 scm_c_define ("SDL_INIT_JOYSTICK", scm_from_int (SDL_INIT_JOYSTICK));
419 scm_c_define ("SDL_INIT_NOPARACHUTE", scm_from_int (SDL_INIT_NOPARACHUTE));
420 scm_c_define ("SDL_INIT_EVENTTHREAD", scm_from_int (SDL_INIT_EVENTTHREAD));
421 scm_c_define ("SDL_INIT_EVERYTHING", scm_from_int (SDL_INIT_EVERYTHING));
423 scm_c_define ("SDL_SWSURFACE", scm_from_int (SDL_SWSURFACE));
424 scm_c_define ("SDL_HWSURFACE", scm_from_int (SDL_HWSURFACE));
425 scm_c_define ("SDL_ASYNCBLIT", scm_from_int (SDL_ASYNCBLIT));
427 scm_c_define ("SDL_ANYFORMAT", scm_from_int (SDL_ANYFORMAT));
428 scm_c_define ("SDL_HWPALETTE", scm_from_int (SDL_HWPALETTE));
429 scm_c_define ("SDL_DOUBLEBUF", scm_from_int (SDL_DOUBLEBUF));
430 scm_c_define ("SDL_FULLSCREEN", scm_from_int (SDL_FULLSCREEN));
431 scm_c_define ("SDL_OPENGL", scm_from_int (SDL_OPENGL));
432 scm_c_define ("SDL_OPENGLBLIT", scm_from_int (SDL_OPENGLBLIT));
433 scm_c_define ("SDL_RESIZABLE", scm_from_int (SDL_RESIZABLE));
434 scm_c_define ("SDL_NOFRAME", scm_from_int (SDL_NOFRAME));
436 scm_c_define ("SDL_HWACCEL", scm_from_int (SDL_HWACCEL));
437 scm_c_define ("SDL_SRCCOLORKEY", scm_from_int (SDL_SRCCOLORKEY));
439 scm_c_define ("SDL_GL_DOUBLEBUFFER", scm_from_int (SDL_GL_DOUBLEBUFFER));
441 scm_c_define ("SDL_DEFAULT_REPEAT_DELAY", scm_from_int (SDL_DEFAULT_REPEAT_DELAY));
442 scm_c_define ("SDL_DEFAULT_REPEAT_INTERVAL", scm_from_int (SDL_DEFAULT_REPEAT_INTERVAL));
444 scm_c_define ("SDL_LIL_ENDIAN", scm_from_int (SDL_LIL_ENDIAN));
445 scm_c_define ("SDL_BIG_ENDIAN", scm_from_int (SDL_BIG_ENDIAN));
446 scm_c_define ("SDL_BYTEORDER", scm_from_int (SDL_BYTEORDER));
448 scm_c_define ("MIX_DEFAULT_FORMAT", scm_from_int (MIX_DEFAULT_FORMAT));
450 scm_c_define ("SDL_NOEVENT", scm_from_int (SDL_NOEVENT));
451 scm_c_define ("SDL_ACTIVEEVENT", scm_from_int (SDL_ACTIVEEVENT));
452 scm_c_define ("SDL_KEYDOWN", scm_from_int (SDL_KEYDOWN));
453 scm_c_define ("SDL_KEYUP", scm_from_int (SDL_KEYUP));
454 scm_c_define ("SDL_MOUSEMOTION", scm_from_int (SDL_MOUSEMOTION));
455 scm_c_define ("SDL_MOUSEBUTTONDOWN", scm_from_int (SDL_MOUSEBUTTONDOWN));
456 scm_c_define ("SDL_MOUSEBUTTONUP", scm_from_int (SDL_MOUSEBUTTONUP));
457 scm_c_define ("SDL_JOYAXISMOTION", scm_from_int (SDL_JOYAXISMOTION));
458 scm_c_define ("SDL_JOYBALLMOTION", scm_from_int (SDL_JOYBALLMOTION));
459 scm_c_define ("SDL_JOYHATMOTION", scm_from_int (SDL_JOYHATMOTION));
460 scm_c_define ("SDL_JOYBUTTONDOWN", scm_from_int (SDL_JOYBUTTONDOWN));
461 scm_c_define ("SDL_JOYBUTTONUP", scm_from_int (SDL_JOYBUTTONUP));
462 scm_c_define ("SDL_QUIT", scm_from_int (SDL_QUIT));
463 scm_c_define ("SDL_SYSWMEVENT", scm_from_int (SDL_SYSWMEVENT));
464 scm_c_define ("SDL_EVENT_RESERVEDA", scm_from_int (SDL_EVENT_RESERVEDA));
465 scm_c_define ("SDL_EVENT_RESERVEDB", scm_from_int (SDL_EVENT_RESERVEDB));
466 scm_c_define ("SDL_VIDEORESIZE", scm_from_int (SDL_VIDEORESIZE));
467 scm_c_define ("SDL_VIDEOEXPOSE", scm_from_int (SDL_VIDEOEXPOSE));
468 scm_c_define ("SDL_EVENT_RESERVED2", scm_from_int (SDL_EVENT_RESERVED2));
469 scm_c_define ("SDL_EVENT_RESERVED3", scm_from_int (SDL_EVENT_RESERVED3));
470 scm_c_define ("SDL_EVENT_RESERVED4", scm_from_int (SDL_EVENT_RESERVED4));
471 scm_c_define ("SDL_EVENT_RESERVED5", scm_from_int (SDL_EVENT_RESERVED5));
472 scm_c_define ("SDL_EVENT_RESERVED6", scm_from_int (SDL_EVENT_RESERVED6));
473 scm_c_define ("SDL_EVENT_RESERVED7", scm_from_int (SDL_EVENT_RESERVED7));
474 scm_c_define ("SDL_USEREVENT", scm_from_int (SDL_USEREVENT));
475 scm_c_define ("SDL_NUMEVENTS", scm_from_int (SDL_NUMEVENTS));
477 scm_c_define_gsubr ("SDL_Init", 1, 0, 0, gacela_SDL_Init);
478 scm_c_define_gsubr ("SDL_Quit", 0, 0, 0, gacela_SDL_Quit);
479 scm_c_define_gsubr ("SDL_SetVideoMode", 4, 0, 0, gacela_SDL_SetVideoMode);
480 scm_c_define_gsubr ("SDL_FreeSurface", 1, 0, 0, gacela_SDL_FreeSurface);
481 scm_c_define_gsubr ("SDL_WM_SetCaption", 2, 0, 0, gacela_SDL_WM_SetCaption);
482 scm_c_define_gsubr ("SDL_Flip", 1, 0, 0, gacela_SDL_Flip);
483 scm_c_define_gsubr ("SDL_Delay", 1, 0, 0, gacela_SDL_Delay);
484 scm_c_define_gsubr ("SDL_GetTicks", 0, 0, 0, gacela_SDL_GetTicks);
485 scm_c_define_gsubr ("SDL_DisplayFormat", 1, 0, 0, gacela_SDL_DisplayFormat);
486 scm_c_define_gsubr ("SDL_MapRGB", 4, 0, 0, gacela_SDL_MapRGB);
487 scm_c_define_gsubr ("SDL_SetColorKey", 3, 0, 0, gacela_SDL_SetColorKey);
488 scm_c_define_gsubr ("SDL_SetAlpha", 3, 0, 0, gacela_SDL_SetAlpha);
489 scm_c_define_gsubr ("SDL_LoadBMP", 1, 0, 0, gacela_SDL_LoadBMP);
490 scm_c_define_gsubr ("IMG_Load", 1, 0, 0, gacela_IMG_Load);
491 scm_c_define_gsubr ("SDL_GetVideoInfo", 0, 0, 0, gacela_SDL_GetVideoInfo);
492 scm_c_define_gsubr ("SDL_GL_SetAttribute", 2, 0, 0, gacela_SDL_GL_SetAttribute);
493 scm_c_define_gsubr ("SDL_PollEvent", 0, 0, 0, gacela_SDL_PollEvent);
494 scm_c_define_gsubr ("SDL_GL_SwapBuffers", 0, 0, 0, gacela_SDL_GL_SwapBuffers);
495 scm_c_define_gsubr ("SDL_EnableKeyRepeat", 2, 0, 0, gacela_SDL_EnableKeyRepeat);
496 scm_c_define_gsubr ("zoomSurface", 4, 0, 0, gacela_zoomSurface);
497 scm_c_define_gsubr ("Mix_OpenAudio", 4, 0, 0, gacela_Mix_OpenAudio);
498 scm_c_define_gsubr ("Mix_LoadMUS", 1, 0, 0, gacela_Mix_LoadMUS);
499 scm_c_define_gsubr ("Mix_LoadWAV", 1, 0, 0, gacela_Mix_LoadWAV);
500 scm_c_define_gsubr ("Mix_PlayChannel", 3, 0, 0, gacela_Mix_PlayChannel);
501 scm_c_define_gsubr ("Mix_PlayMusic", 2, 0, 0, gacela_Mix_PlayMusic);
502 scm_c_define_gsubr ("Mix_PlayingMusic", 0, 0, 0, gacela_Mix_PlayingMusic);
503 scm_c_define_gsubr ("Mix_PausedMusic", 0, 0, 0, gacela_Mix_PausedMusic);
504 scm_c_define_gsubr ("Mix_PauseMusic", 0, 0, 0, gacela_Mix_PauseMusic);
505 scm_c_define_gsubr ("Mix_ResumeMusic", 0, 0, 0, gacela_Mix_ResumeMusic);
506 scm_c_define_gsubr ("Mix_HaltMusic", 0, 0, 0, gacela_Mix_HaltMusic);
507 scm_c_define_gsubr ("Mix_FreeMusic", 1, 0, 0, gacela_Mix_FreeMusic);
508 scm_c_define_gsubr ("Mix_FreeChunk", 1, 0, 0, gacela_Mix_FreeChunk);
509 scm_c_define_gsubr ("Mix_CloseAudio", 0, 0, 0, gacela_Mix_CloseAudio);