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