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 "gacela_SDL.h"
28 SDL_Surface *surface_address;
31 static scm_t_bits surface_tag;
34 make_surface (SCM file, SDL_Surface *surface_address)
37 struct surface *surface;
39 surface = (struct surface *) scm_gc_malloc (sizeof (struct surface), "surface");
41 surface->filename = SCM_BOOL_F;
42 surface->surface_address = NULL;
44 SCM_NEWSMOB (smob, surface_tag, surface);
46 surface->filename = file;
47 surface->surface_address = surface_address;
53 get_surface_address (SCM surface_smob)
55 struct surface *surface;
57 scm_assert_smob_type (surface_tag, surface_smob);
58 surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
59 return surface->surface_address;
63 get_surface_width (SCM surface_smob)
65 SDL_Surface *surface = get_surface_address (surface_smob);
67 return scm_from_int (surface->w);
71 get_surface_height (SCM surface_smob)
73 SDL_Surface *surface = get_surface_address (surface_smob);
75 return scm_from_int (surface->h);
79 mark_surface (SCM surface_smob)
81 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
83 scm_gc_mark (surface->filename);
89 free_surface (SCM surface_smob)
91 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
93 SDL_FreeSurface (surface->surface_address);
94 scm_gc_free (surface, sizeof (struct surface), "surface");
100 print_surface (SCM surface_smob, SCM port, scm_print_state *pstate)
102 struct surface *surface = (struct surface *) SCM_SMOB_DATA (surface_smob);
104 scm_puts ("#<surface \"", port);
105 scm_display (surface->filename, port);
106 scm_puts ("\">", port);
108 /* non-zero means success */
114 gacela_SDL_Init (SCM flags)
116 return scm_from_int (SDL_Init (scm_to_int (flags)));
120 gacela_SDL_Quit (void)
123 return SCM_UNSPECIFIED;
127 gacela_SDL_SetVideoMode (SCM width, SCM height, SCM bpp, SCM flags)
129 SDL_Surface *screen = SDL_SetVideoMode (scm_to_int (width), scm_to_int (height), \
130 scm_to_int (bpp), scm_to_int (flags));
133 return make_surface (scm_from_locale_string ("screen"), screen);
136 return SCM_UNSPECIFIED;
141 gacela_SDL_WM_SetCaption (SCM title, SCM icon)
143 SDL_WM_SetCaption (scm_to_locale_string(title), scm_to_locale_string(icon));
144 return SCM_UNSPECIFIED;
148 gacela_SDL_Flip (SCM screen)
150 return scm_from_int (SDL_Flip (get_surface_address (screen)));
154 gacela_SDL_Delay (SCM ms)
156 SDL_Delay ((int)scm_to_double (ms));
157 return SCM_UNSPECIFIED;
161 gacela_SDL_GetTicks (void)
163 return scm_from_int (SDL_GetTicks ());
167 gacela_SDL_DisplayFormat (SCM surface)
169 return scm_from_int ((int)SDL_DisplayFormat (get_surface_address (surface)));
173 gacela_SDL_MapRGB (SCM format, SCM r, SCM g, SCM b)
175 return scm_from_int (SDL_MapRGB ((SDL_PixelFormat *)scm_to_int (format), scm_to_int (r), scm_to_int (g), scm_to_int (b)));
179 gacela_SDL_SetColorKey (SCM surface, SCM flag, SCM key)
181 return scm_from_int (SDL_SetColorKey (get_surface_address (surface), scm_to_int (flag), scm_to_int (key)));
185 gacela_SDL_LoadBMP (SCM file)
187 SDL_Surface *image = SDL_LoadBMP (scm_to_locale_string (file));
190 return make_surface (file, image);
193 return SCM_UNSPECIFIED;
198 gacela_IMG_Load (SCM filename)
200 SDL_Surface *image = IMG_Load (scm_to_locale_string (filename));
203 return make_surface (filename, image);
206 return SCM_UNSPECIFIED;
211 gacela_SDL_GetVideoInfo (void)
213 const SDL_VideoInfo *info;
216 info = SDL_GetVideoInfo ();
217 vi = scm_list_n (SCM_UNDEFINED);
219 vi = scm_cons (scm_cons (scm_from_locale_symbol ("blit_hw"), scm_from_int (info->blit_hw)), vi);
220 vi = scm_cons (scm_cons (scm_from_locale_symbol ("hw_available"), scm_from_int (info->hw_available)), vi);
226 gacela_SDL_GL_SetAttribute (SCM attr, SCM value)
228 return scm_from_int (SDL_GL_SetAttribute (scm_to_int (attr), scm_to_int (value)));
232 gacela_SDL_PollEvent (void)
237 event = scm_list_n (SCM_UNDEFINED);
239 if (SDL_PollEvent (&sdl_event)) {
240 switch (sdl_event.type) {
243 event = scm_cons (scm_cons (scm_from_locale_symbol ("key.keysym.sym"), scm_from_int (sdl_event.key.keysym.sym)), event);
246 event = scm_cons (scm_cons (scm_from_locale_symbol ("type"), scm_from_int (sdl_event.type)), event);
253 gacela_SDL_GL_SwapBuffers (void)
255 SDL_GL_SwapBuffers ();
256 return SCM_UNSPECIFIED;
260 gacela_SDL_EnableKeyRepeat (SCM delay, SCM interval)
262 return scm_from_int (SDL_EnableKeyRepeat (scm_to_int (delay), scm_to_int (interval)));
266 gacela_Mix_OpenAudio (SCM frequency, SCM format, SCM channels, SCM chunksize)
268 return scm_from_int (Mix_OpenAudio (scm_to_int (frequency), scm_to_int (format), scm_to_int (channels), scm_to_int (chunksize)));
272 gacela_Mix_LoadMUS (SCM file)
274 return scm_from_int ((int)Mix_LoadMUS (scm_to_locale_string (file)));
278 gacela_Mix_LoadWAV (SCM file)
280 return scm_from_int ((int)Mix_LoadWAV (scm_to_locale_string (file)));
284 gacela_Mix_PlayChannel (SCM channel, SCM chunk, SCM loops)
286 return scm_from_int (Mix_PlayChannel (scm_to_int (channel), (Mix_Chunk *)scm_to_int (chunk), scm_to_int (loops)));
290 gacela_Mix_PlayMusic (SCM music, SCM loops)
292 return scm_from_int (Mix_PlayMusic ((Mix_Music *)scm_to_int (music), scm_to_int (loops)));
296 gacela_Mix_PlayingMusic (void)
298 return scm_from_int (Mix_PlayingMusic ());
302 gacela_Mix_PausedMusic (void)
304 return scm_from_int (Mix_PausedMusic ());
308 gacela_Mix_PauseMusic (void)
311 return SCM_UNSPECIFIED;
315 gacela_Mix_ResumeMusic (void)
318 return SCM_UNSPECIFIED;
322 gacela_Mix_HaltMusic (void)
324 return scm_from_int (Mix_HaltMusic ());
328 gacela_Mix_FreeMusic (SCM music)
330 Mix_FreeMusic ((Mix_Music *)scm_to_int (music));
331 return SCM_UNSPECIFIED;
335 gacela_Mix_FreeChunk (SCM chunk)
337 Mix_FreeChunk ((Mix_Chunk *)scm_to_int (chunk));
338 return SCM_UNSPECIFIED;
342 gacela_Mix_CloseAudio (void)
345 return SCM_UNSPECIFIED;
350 SDL_register_functions (void* data)
352 surface_tag = scm_make_smob_type ("surface", sizeof (struct surface));
353 scm_set_smob_mark (surface_tag, mark_surface);
354 scm_set_smob_free (surface_tag, free_surface);
355 scm_set_smob_print (surface_tag, print_surface);
356 scm_c_define_gsubr ("surface-w", 1, 0, 0, get_surface_width);
357 scm_c_define_gsubr ("surface-h", 1, 0, 0, get_surface_height);
359 scm_c_define ("SDL_INIT_TIMER", scm_from_int (SDL_INIT_TIMER));
360 scm_c_define ("SDL_INIT_AUDIO", scm_from_int (SDL_INIT_AUDIO));
361 scm_c_define ("SDL_INIT_VIDEO", scm_from_int (SDL_INIT_VIDEO));
362 scm_c_define ("SDL_INIT_CDROM", scm_from_int (SDL_INIT_CDROM));
363 scm_c_define ("SDL_INIT_JOYSTICK", scm_from_int (SDL_INIT_JOYSTICK));
364 scm_c_define ("SDL_INIT_NOPARACHUTE", scm_from_int (SDL_INIT_NOPARACHUTE));
365 scm_c_define ("SDL_INIT_EVENTTHREAD", scm_from_int (SDL_INIT_EVENTTHREAD));
366 scm_c_define ("SDL_INIT_EVERYTHING", scm_from_int (SDL_INIT_EVERYTHING));
368 scm_c_define ("SDL_SWSURFACE", scm_from_int (SDL_SWSURFACE));
369 scm_c_define ("SDL_HWSURFACE", scm_from_int (SDL_HWSURFACE));
370 scm_c_define ("SDL_ASYNCBLIT", scm_from_int (SDL_ASYNCBLIT));
372 scm_c_define ("SDL_ANYFORMAT", scm_from_int (SDL_ANYFORMAT));
373 scm_c_define ("SDL_HWPALETTE", scm_from_int (SDL_HWPALETTE));
374 scm_c_define ("SDL_DOUBLEBUF", scm_from_int (SDL_DOUBLEBUF));
375 scm_c_define ("SDL_FULLSCREEN", scm_from_int (SDL_FULLSCREEN));
376 scm_c_define ("SDL_OPENGL", scm_from_int (SDL_OPENGL));
377 scm_c_define ("SDL_OPENGLBLIT", scm_from_int (SDL_OPENGLBLIT));
378 scm_c_define ("SDL_RESIZABLE", scm_from_int (SDL_RESIZABLE));
379 scm_c_define ("SDL_NOFRAME", scm_from_int (SDL_NOFRAME));
381 scm_c_define ("SDL_HWACCEL", scm_from_int (SDL_HWACCEL));
382 scm_c_define ("SDL_SRCCOLORKEY", scm_from_int (SDL_SRCCOLORKEY));
384 scm_c_define ("SDL_GL_DOUBLEBUFFER", scm_from_int (SDL_GL_DOUBLEBUFFER));
386 scm_c_define ("SDL_DEFAULT_REPEAT_DELAY", scm_from_int (SDL_DEFAULT_REPEAT_DELAY));
387 scm_c_define ("SDL_DEFAULT_REPEAT_INTERVAL", scm_from_int (SDL_DEFAULT_REPEAT_INTERVAL));
389 scm_c_define ("SDL_LIL_ENDIAN", scm_from_int (SDL_LIL_ENDIAN));
390 scm_c_define ("SDL_BIG_ENDIAN", scm_from_int (SDL_BIG_ENDIAN));
391 scm_c_define ("SDL_BYTEORDER", scm_from_int (SDL_BYTEORDER));
393 scm_c_define ("MIX_DEFAULT_FORMAT", scm_from_int (MIX_DEFAULT_FORMAT));
395 scm_c_define ("SDL_NOEVENT", scm_from_int (SDL_NOEVENT));
396 scm_c_define ("SDL_ACTIVEEVENT", scm_from_int (SDL_ACTIVEEVENT));
397 scm_c_define ("SDL_KEYDOWN", scm_from_int (SDL_KEYDOWN));
398 scm_c_define ("SDL_KEYUP", scm_from_int (SDL_KEYUP));
399 scm_c_define ("SDL_MOUSEMOTION", scm_from_int (SDL_MOUSEMOTION));
400 scm_c_define ("SDL_MOUSEBUTTONDOWN", scm_from_int (SDL_MOUSEBUTTONDOWN));
401 scm_c_define ("SDL_MOUSEBUTTONUP", scm_from_int (SDL_MOUSEBUTTONUP));
402 scm_c_define ("SDL_JOYAXISMOTION", scm_from_int (SDL_JOYAXISMOTION));
403 scm_c_define ("SDL_JOYBALLMOTION", scm_from_int (SDL_JOYBALLMOTION));
404 scm_c_define ("SDL_JOYHATMOTION", scm_from_int (SDL_JOYHATMOTION));
405 scm_c_define ("SDL_JOYBUTTONDOWN", scm_from_int (SDL_JOYBUTTONDOWN));
406 scm_c_define ("SDL_JOYBUTTONUP", scm_from_int (SDL_JOYBUTTONUP));
407 scm_c_define ("SDL_QUIT", scm_from_int (SDL_QUIT));
408 scm_c_define ("SDL_SYSWMEVENT", scm_from_int (SDL_SYSWMEVENT));
409 scm_c_define ("SDL_EVENT_RESERVEDA", scm_from_int (SDL_EVENT_RESERVEDA));
410 scm_c_define ("SDL_EVENT_RESERVEDB", scm_from_int (SDL_EVENT_RESERVEDB));
411 scm_c_define ("SDL_VIDEORESIZE", scm_from_int (SDL_VIDEORESIZE));
412 scm_c_define ("SDL_VIDEOEXPOSE", scm_from_int (SDL_VIDEOEXPOSE));
413 scm_c_define ("SDL_EVENT_RESERVED2", scm_from_int (SDL_EVENT_RESERVED2));
414 scm_c_define ("SDL_EVENT_RESERVED3", scm_from_int (SDL_EVENT_RESERVED3));
415 scm_c_define ("SDL_EVENT_RESERVED4", scm_from_int (SDL_EVENT_RESERVED4));
416 scm_c_define ("SDL_EVENT_RESERVED5", scm_from_int (SDL_EVENT_RESERVED5));
417 scm_c_define ("SDL_EVENT_RESERVED6", scm_from_int (SDL_EVENT_RESERVED6));
418 scm_c_define ("SDL_EVENT_RESERVED7", scm_from_int (SDL_EVENT_RESERVED7));
419 scm_c_define ("SDL_USEREVENT", scm_from_int (SDL_USEREVENT));
420 scm_c_define ("SDL_NUMEVENTS", scm_from_int (SDL_NUMEVENTS));
422 scm_c_define_gsubr ("SDL_Init", 1, 0, 0, gacela_SDL_Init);
423 scm_c_define_gsubr ("SDL_Quit", 0, 0, 0, gacela_SDL_Quit);
424 scm_c_define_gsubr ("SDL_SetVideoMode", 4, 0, 0, gacela_SDL_SetVideoMode);
425 scm_c_define_gsubr ("SDL_WM_SetCaption", 2, 0, 0, gacela_SDL_WM_SetCaption);
426 scm_c_define_gsubr ("SDL_Flip", 1, 0, 0, gacela_SDL_Flip);
427 scm_c_define_gsubr ("SDL_Delay", 1, 0, 0, gacela_SDL_Delay);
428 scm_c_define_gsubr ("SDL_GetTicks", 0, 0, 0, gacela_SDL_GetTicks);
429 scm_c_define_gsubr ("SDL_DisplayFormat", 1, 0, 0, gacela_SDL_DisplayFormat);
430 scm_c_define_gsubr ("SDL_MapRGB", 4, 0, 0, gacela_SDL_MapRGB);
431 scm_c_define_gsubr ("SDL_SetColorKey", 3, 0, 0, gacela_SDL_SetColorKey);
432 scm_c_define_gsubr ("SDL_LoadBMP", 1, 0, 0, gacela_SDL_LoadBMP);
433 scm_c_define_gsubr ("IMG_Load", 1, 0, 0, gacela_IMG_Load);
434 scm_c_define_gsubr ("SDL_GetVideoInfo", 0, 0, 0, gacela_SDL_GetVideoInfo);
435 scm_c_define_gsubr ("SDL_GL_SetAttribute", 2, 0, 0, gacela_SDL_GL_SetAttribute);
436 scm_c_define_gsubr ("SDL_PollEvent", 0, 0, 0, gacela_SDL_PollEvent);
437 scm_c_define_gsubr ("SDL_GL_SwapBuffers", 0, 0, 0, gacela_SDL_GL_SwapBuffers);
438 scm_c_define_gsubr ("SDL_EnableKeyRepeat", 2, 0, 0, gacela_SDL_EnableKeyRepeat);
439 scm_c_define_gsubr ("Mix_OpenAudio", 4, 0, 0, gacela_Mix_OpenAudio);
440 scm_c_define_gsubr ("Mix_LoadMUS", 1, 0, 0, gacela_Mix_LoadMUS);
441 scm_c_define_gsubr ("Mix_LoadWAV", 1, 0, 0, gacela_Mix_LoadWAV);
442 scm_c_define_gsubr ("Mix_PlayChannel", 3, 0, 0, gacela_Mix_PlayChannel);
443 scm_c_define_gsubr ("Mix_PlayMusic", 2, 0, 0, gacela_Mix_PlayMusic);
444 scm_c_define_gsubr ("Mix_PlayingMusic", 0, 0, 0, gacela_Mix_PlayingMusic);
445 scm_c_define_gsubr ("Mix_PausedMusic", 0, 0, 0, gacela_Mix_PausedMusic);
446 scm_c_define_gsubr ("Mix_PauseMusic", 0, 0, 0, gacela_Mix_PauseMusic);
447 scm_c_define_gsubr ("Mix_ResumeMusic", 0, 0, 0, gacela_Mix_ResumeMusic);
448 scm_c_define_gsubr ("Mix_HaltMusic", 0, 0, 0, gacela_Mix_HaltMusic);
449 scm_c_define_gsubr ("Mix_FreeMusic", 1, 0, 0, gacela_Mix_FreeMusic);
450 scm_c_define_gsubr ("Mix_FreeChunk", 1, 0, 0, gacela_Mix_FreeChunk);
451 scm_c_define_gsubr ("Mix_CloseAudio", 0, 0, 0, gacela_Mix_CloseAudio);