]> git.jsancho.org Git - gacela.git/blob - src/gacela_FTGL.c
Gacela as Guile modules.
[gacela.git] / src / gacela_FTGL.c
1 /* Gacela, a GNU Guile extension for fast games development
2    Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <libguile.h>
19 #include <FTGL/ftgl.h>
20 #include "gacela_FTGL.h"
21
22 struct font
23 {
24   SCM filename;
25   FTGLfont *font_address;
26 };
27
28 static scm_t_bits font_tag;
29
30 SCM
31 make_font (SCM file, FTGLfont *font_address)
32 {
33   SCM smob;
34   struct font *font;
35
36   font = (struct font *) scm_gc_malloc (sizeof (struct font), "font");
37
38   font->filename = SCM_BOOL_F;
39   font->font_address = NULL;
40
41   SCM_NEWSMOB (smob, font_tag, font);
42
43   font->filename = file;
44   font->font_address = font_address;
45
46   return smob;
47 }
48
49 FTGLfont *
50 get_font_address (SCM font_smob)
51 {
52   struct font *font;
53
54   scm_assert_smob_type (font_tag, font_smob);
55   font = (struct font *) SCM_SMOB_DATA (font_smob);
56   return font->font_address;
57 }
58
59 SCM
60 mark_font (SCM font_smob)
61 {
62   struct font *font = (struct font *) SCM_SMOB_DATA (font_smob);
63
64   scm_gc_mark (font->filename);
65      
66   return SCM_BOOL_F;
67 }
68
69 size_t
70 free_font (SCM font_smob)
71 {
72   struct font *font = (struct font *) SCM_SMOB_DATA (font_smob);
73
74   ftglDestroyFont (font->font_address);
75   scm_gc_free (font, sizeof (struct font), "font");
76
77   return 0;
78 }
79
80 static int
81 print_font (SCM font_smob, SCM port, scm_print_state *pstate)
82 {
83   struct font *font = (struct font *) SCM_SMOB_DATA (font_smob);
84
85   scm_puts ("#<font \"", port);
86   scm_display (font->filename, port);
87   scm_puts ("\">", port);
88
89   /* non-zero means success */
90   return 1;
91 }
92
93
94 SCM
95 gacela_ftglCreateTextureFont (SCM file)
96 {
97   FTGLfont *font_address = ftglCreateTextureFont (scm_to_locale_string (file));
98
99   if (font_address) {
100     return make_font (file, font_address);
101   }
102   else {
103     return SCM_BOOL_F;
104   }
105 }
106
107 SCM
108 gacela_ftglSetFontFaceSize (SCM font, SCM size, SCM res)
109 {
110   return scm_from_int (ftglSetFontFaceSize (get_font_address (font), scm_to_int (size), scm_to_int (res)));
111 }
112
113 SCM
114 gacela_ftglSetFontCharMap (SCM font, SCM encoding)
115 {
116   return scm_from_int (ftglSetFontCharMap (get_font_address (font), scm_to_int (encoding)));
117 }
118
119 SCM
120 gacela_ftglRenderFont (SCM font, SCM string, SCM mode)
121 {
122   ftglRenderFont (get_font_address (font), scm_to_locale_string(string), scm_to_int (mode));
123   return SCM_UNSPECIFIED;
124 }
125
126
127 void*
128 FTGL_register_functions (void* data)
129 {
130   font_tag = scm_make_smob_type ("font", sizeof (struct font));
131   scm_set_smob_mark (font_tag, mark_font);
132   scm_set_smob_free (font_tag, free_font);
133   scm_set_smob_print (font_tag, print_font);
134   //  scm_set_smob_equalp (surface_tag, equalp_surface);
135
136   scm_c_define ("ft_encoding_unicode", scm_from_int (ft_encoding_unicode));
137   scm_c_define ("FTGL_RENDER_ALL", scm_from_int (FTGL_RENDER_ALL));
138
139   scm_c_define_gsubr ("ftglCreateTextureFont", 1, 0, 0, gacela_ftglCreateTextureFont);
140   scm_c_define_gsubr ("ftglSetFontFaceSize", 3, 0, 0, gacela_ftglSetFontFaceSize);
141   scm_c_define_gsubr ("ftglSetFontCharMap", 2, 0, 0, gacela_ftglSetFontCharMap);
142   scm_c_define_gsubr ("ftglRenderFont", 3, 0, 0, gacela_ftglRenderFont);
143
144   return NULL;
145 }