rubysdl 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/LICENSE +505 -0
  2. data/MANIFEST +81 -0
  3. data/NEWS.en +144 -0
  4. data/NEWS.ja +151 -0
  5. data/README.en +117 -0
  6. data/README.ja +166 -0
  7. data/SDL_kanji.c +403 -0
  8. data/SDL_kanji.h +48 -0
  9. data/depend +18 -0
  10. data/doc/Makefile +18 -0
  11. data/doc/cdrom.rsd +431 -0
  12. data/doc/collision.rsd +162 -0
  13. data/doc/event.rsd +1487 -0
  14. data/doc/font.rsd +839 -0
  15. data/doc/general.rsd +49 -0
  16. data/doc/init.rsd +175 -0
  17. data/doc/joystick.rsd +387 -0
  18. data/doc/mixer.rsd +837 -0
  19. data/doc/mpeg.rsd +595 -0
  20. data/doc/rsd.rb +125 -0
  21. data/doc/sdlskk.rsd +496 -0
  22. data/doc/time.rsd +45 -0
  23. data/doc/video.rsd +2499 -0
  24. data/doc/wm.rsd +113 -0
  25. data/extconf.rb +92 -0
  26. data/lib/rubysdl_aliases.rb +431 -0
  27. data/lib/sdl.rb +271 -0
  28. data/rubysdl.h +109 -0
  29. data/rubysdl_cdrom.c +176 -0
  30. data/rubysdl_const_list.txt +280 -0
  31. data/rubysdl_doc.en.rd +2180 -0
  32. data/rubysdl_doc_old.rd +2402 -0
  33. data/rubysdl_event.c +346 -0
  34. data/rubysdl_event2.c +417 -0
  35. data/rubysdl_event_key.c +356 -0
  36. data/rubysdl_image.c +53 -0
  37. data/rubysdl_joystick.c +156 -0
  38. data/rubysdl_kanji.c +135 -0
  39. data/rubysdl_main.c +202 -0
  40. data/rubysdl_mixer.c +422 -0
  41. data/rubysdl_mouse.c +96 -0
  42. data/rubysdl_opengl.c +63 -0
  43. data/rubysdl_pixel.c +133 -0
  44. data/rubysdl_ref.html +5550 -0
  45. data/rubysdl_ref.rd +6163 -0
  46. data/rubysdl_rwops.c +90 -0
  47. data/rubysdl_sdlskk.c +312 -0
  48. data/rubysdl_sge_video.c +622 -0
  49. data/rubysdl_smpeg.c +341 -0
  50. data/rubysdl_time.c +36 -0
  51. data/rubysdl_ttf.c +324 -0
  52. data/rubysdl_video.c +749 -0
  53. data/rubysdl_wm.c +71 -0
  54. data/sample/aadraw.rb +24 -0
  55. data/sample/alpha.rb +27 -0
  56. data/sample/alphadraw.rb +25 -0
  57. data/sample/bfont.rb +24 -0
  58. data/sample/cdrom.rb +17 -0
  59. data/sample/collision.rb +97 -0
  60. data/sample/cursor.bmp +0 -0
  61. data/sample/cursor.rb +22 -0
  62. data/sample/ellipses.rb +35 -0
  63. data/sample/event2.rb +32 -0
  64. data/sample/font.bmp +0 -0
  65. data/sample/font.rb +25 -0
  66. data/sample/fpstimer.rb +175 -0
  67. data/sample/icon.bmp +0 -0
  68. data/sample/joy2.rb +81 -0
  69. data/sample/kanji.rb +36 -0
  70. data/sample/movesp.rb +93 -0
  71. data/sample/playmod.rb +14 -0
  72. data/sample/plaympeg.rb +48 -0
  73. data/sample/playwave.rb +16 -0
  74. data/sample/randrect.rb +40 -0
  75. data/sample/sample.ttf +0 -0
  76. data/sample/sdlskk.rb +70 -0
  77. data/sample/sgetest.rb +31 -0
  78. data/sample/stetris.rb +275 -0
  79. data/sample/testgl.rb +166 -0
  80. data/sample/testsprite.rb +68 -0
  81. data/sample/transformblit.rb +41 -0
  82. metadata +121 -0
@@ -0,0 +1,341 @@
1
+ /*
2
+ Ruby/SDL Ruby extension library for SDL
3
+
4
+ Copyright (C) 2001-2007 Ohbayashi Ippei
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ */
20
+ #ifdef HAVE_SMPEG
21
+ #include "rubysdl.h"
22
+ #include "smpeg/smpeg.h"
23
+
24
+ #ifdef HAVE_SDL_MIXER
25
+ #include "SDL_mixer.h"
26
+ #endif
27
+
28
+ static SMPEG_Filter* filters[3];
29
+ #define NULL_FILTER 0
30
+ #define BILINEAR_FILTER 1
31
+ #define DEBLOCKING_FILTER 2
32
+ #define NUM_FILTERS 3
33
+
34
+ static void setInfoToSMPEGInfo(VALUE obj,SMPEG_Info info)
35
+ {
36
+ rb_iv_set(obj,"@has_audio",BOOL(info.has_audio));
37
+ rb_iv_set(obj,"@has_video",BOOL(info.has_video));
38
+ rb_iv_set(obj,"@width",INT2NUM(info.width));
39
+ rb_iv_set(obj,"@height",INT2NUM(info.height));
40
+ rb_iv_set(obj,"@current_frame",INT2NUM(info.current_frame));
41
+ rb_iv_set(obj,"@current_fps",INT2NUM(info.current_fps));
42
+ rb_iv_set(obj,"@audio_string",rb_str_new2(info.audio_string));
43
+ rb_iv_set(obj,"@audio_current_frame",INT2NUM(info.audio_current_frame));
44
+ rb_iv_set(obj,"@current_offset",UINT2NUM(info.current_offset));
45
+ rb_iv_set(obj,"@total_size",UINT2NUM(info.total_size));
46
+ rb_iv_set(obj,"@current_time",UINT2NUM(info.current_time));
47
+ rb_iv_set(obj,"@total_time",UINT2NUM(info.total_time));
48
+ }
49
+
50
+ static VALUE smpeg_load(VALUE class,VALUE filename)
51
+ {
52
+ SMPEG *mpeg;
53
+ VALUE obj;
54
+ char error_msg[2048];
55
+
56
+ mpeg = SMPEG_new(GETCSTR(filename),NULL,0);
57
+ if( SMPEG_error(mpeg) ){
58
+ snprintf(error_msg,sizeof(error_msg),"Couldn't load %s: %s",
59
+ GETCSTR(filename),SMPEG_error(mpeg));
60
+ SMPEG_delete(mpeg);
61
+ rb_raise(eSDLError,"%s",error_msg);
62
+ }
63
+
64
+ obj = Data_Wrap_Struct(cMPEG,0,SMPEG_delete,mpeg);
65
+ rb_iv_set(obj,"enable_audio",Qtrue);
66
+ return obj;
67
+ }
68
+
69
+ static VALUE smpeg_getInfo(VALUE obj,VALUE infoObj)
70
+ {
71
+ SMPEG *mpeg;
72
+ SMPEG_Info info;
73
+
74
+ if( !rb_obj_is_kind_of(infoObj,cMPEGInfo) )
75
+ rb_raise(rb_eArgError,"type mismatch(expect SDL::MPEG::Info)");
76
+ Data_Get_Struct(obj,SMPEG,mpeg);
77
+
78
+ SMPEG_getinfo(mpeg,&info);
79
+ setInfoToSMPEGInfo(infoObj,info);
80
+
81
+ return Qnil;
82
+ }
83
+
84
+ static VALUE smpeg_enableAudio(VALUE obj,VALUE enable)
85
+ {
86
+ SMPEG *mpeg;
87
+ Data_Get_Struct(obj,SMPEG,mpeg);
88
+ rb_iv_set(obj,"enable_audio",enable);
89
+ return Qnil;
90
+ }
91
+
92
+ static VALUE smpeg_enableVideo(VALUE obj,VALUE enable)
93
+ {
94
+ SMPEG *mpeg;
95
+ Data_Get_Struct(obj,SMPEG,mpeg);
96
+ SMPEG_enablevideo(mpeg,RTEST(enable));
97
+ return Qnil;
98
+ }
99
+
100
+ static VALUE smpeg_status(VALUE obj)
101
+ {
102
+ SMPEG *mpeg;
103
+ Data_Get_Struct(obj,SMPEG,mpeg);
104
+ return INT2FIX(SMPEG_status(mpeg));
105
+ }
106
+
107
+ static VALUE smpeg_setVolume(VALUE obj,VALUE volume)
108
+ {
109
+ SMPEG *mpeg;
110
+ Data_Get_Struct(obj,SMPEG,mpeg);
111
+ SMPEG_setvolume(mpeg,NUM2INT(volume));
112
+ return Qnil;
113
+ }
114
+
115
+ static VALUE smpeg_setDisplay(VALUE obj,VALUE dst)
116
+ {
117
+ SMPEG *mpeg;
118
+ SDL_Surface *surface;
119
+ if( !rb_obj_is_kind_of(dst,cSurface) )
120
+ rb_raise(rb_eArgError,"type mismatchi(expect Surface)");
121
+
122
+ Data_Get_Struct(obj,SMPEG,mpeg);
123
+ Data_Get_Struct(dst,SDL_Surface,surface);
124
+
125
+ SMPEG_setdisplay(mpeg,surface,NULL,NULL);
126
+ rb_iv_set(obj,"surface",dst);
127
+ return Qnil;
128
+ }
129
+
130
+ static VALUE smpeg_setLoop(VALUE obj,VALUE repeat)
131
+ {
132
+ SMPEG *mpeg;
133
+ Data_Get_Struct(obj,SMPEG,mpeg);
134
+ SMPEG_loop(mpeg,RTEST(repeat));
135
+ return Qnil;
136
+ }
137
+
138
+ static VALUE smpeg_scaleXY(VALUE obj,VALUE w,VALUE h)
139
+ {
140
+ SMPEG *mpeg;
141
+ Data_Get_Struct(obj,SMPEG,mpeg);
142
+ SMPEG_scaleXY(mpeg,NUM2INT(w),NUM2INT(h));
143
+ return Qnil;
144
+ }
145
+
146
+ static VALUE smpeg_scale(VALUE obj,VALUE scale)
147
+ {
148
+ SMPEG *mpeg;
149
+ Data_Get_Struct(obj,SMPEG,mpeg);
150
+ SMPEG_scale(mpeg,NUM2INT(scale));
151
+ return Qnil;
152
+ }
153
+
154
+ static VALUE smpeg_move(VALUE obj,VALUE x,VALUE y)
155
+ {
156
+ SMPEG *mpeg;
157
+ Data_Get_Struct(obj,SMPEG,mpeg);
158
+ SMPEG_move(mpeg,NUM2INT(x),NUM2INT(y));
159
+ return Qnil;
160
+ }
161
+
162
+ static VALUE smpeg_setDisplayRegion(VALUE obj,VALUE x,VALUE y,VALUE w,
163
+ VALUE h)
164
+ {
165
+ SMPEG *mpeg;
166
+ Data_Get_Struct(obj,SMPEG,mpeg);
167
+ SMPEG_setdisplayregion(mpeg,NUM2INT(x),NUM2INT(y),NUM2INT(w),NUM2INT(h));
168
+ return Qnil;
169
+ }
170
+
171
+ static VALUE smpeg_play(VALUE obj)
172
+ {
173
+ SMPEG *mpeg;
174
+ int use_audio;
175
+
176
+ Data_Get_Struct(obj,SMPEG,mpeg);
177
+
178
+ #if HAVE_SDL_MIXER
179
+ use_audio = RTEST(rb_iv_get(obj,"enable_audio")) &&
180
+ Mix_QuerySpec( NULL, NULL, NULL );
181
+
182
+ if( use_audio ){
183
+ SDL_AudioSpec audiofmt;
184
+ Uint16 format;
185
+ int freq, channels;
186
+
187
+ SMPEG_enableaudio(mpeg, 0);
188
+ /* Tell SMPEG what the audio format is */
189
+ Mix_QuerySpec(&freq, &format, &channels);
190
+ audiofmt.format = format;
191
+ audiofmt.freq = freq;
192
+ audiofmt.channels = channels;
193
+ SMPEG_actualSpec(mpeg, &audiofmt);
194
+
195
+ /* Hook in the MPEG music mixer */
196
+ Mix_HookMusic(NULL,NULL);
197
+ Mix_HookMusic(SMPEG_playAudioSDL, mpeg);
198
+ SMPEG_enableaudio(mpeg, 1);
199
+ }
200
+ #else
201
+ SMPEG_enableaudio(mpeg, RTEST(rb_iv_get(obj,"enable_audio")));
202
+ #endif
203
+
204
+ SMPEG_play(mpeg);
205
+ return Qnil;
206
+ }
207
+
208
+ static VALUE smpeg_pause(VALUE obj)
209
+ {
210
+ SMPEG *mpeg;
211
+ Data_Get_Struct(obj,SMPEG,mpeg);
212
+ SMPEG_pause(mpeg);
213
+ return Qnil;
214
+ }
215
+
216
+ static VALUE smpeg_stop(VALUE obj)
217
+ {
218
+ SMPEG *mpeg;
219
+ Data_Get_Struct(obj,SMPEG,mpeg);
220
+ SMPEG_stop(mpeg);
221
+ Mix_HookMusic(NULL,NULL);
222
+ return Qnil;
223
+ }
224
+
225
+ static VALUE smpeg_rewind(VALUE obj)
226
+ {
227
+ SMPEG *mpeg;
228
+ Data_Get_Struct(obj,SMPEG,mpeg);
229
+ SMPEG_rewind(mpeg);
230
+ return Qnil;
231
+ }
232
+
233
+ static VALUE smpeg_seek(VALUE obj,VALUE bytes)
234
+ {
235
+ SMPEG *mpeg;
236
+ Data_Get_Struct(obj,SMPEG,mpeg);
237
+ SMPEG_seek(mpeg,NUM2INT(bytes));
238
+ return Qnil;
239
+ }
240
+
241
+ static VALUE smpeg_skip(VALUE obj,VALUE seconds)
242
+ {
243
+ SMPEG *mpeg;
244
+ Data_Get_Struct(obj,SMPEG,mpeg);
245
+ SMPEG_skip(mpeg,NUM2DBL(seconds));
246
+ return Qnil;
247
+ }
248
+
249
+ static VALUE smpeg_renderFrame(VALUE obj,VALUE framenum)
250
+ {
251
+ SMPEG *mpeg;
252
+ Data_Get_Struct(obj,SMPEG,mpeg);
253
+ SMPEG_renderFrame(mpeg,NUM2INT(framenum));
254
+ return Qnil;
255
+ }
256
+
257
+ static VALUE smpeg_renderFinal(VALUE obj,VALUE dst, VALUE x, VALUE y)
258
+ {
259
+ SMPEG *mpeg;
260
+ SDL_Surface *surface;
261
+ if( !rb_obj_is_kind_of(dst,cSurface) )
262
+ rb_raise(rb_eArgError,"type mismatchi(expect Surface)");
263
+
264
+ Data_Get_Struct(obj,SMPEG,mpeg);
265
+ Data_Get_Struct(dst,SDL_Surface,surface);
266
+
267
+ SMPEG_renderFinal(mpeg, surface, NUM2INT(x), NUM2INT(y));
268
+ return Qnil;
269
+ }
270
+
271
+ static VALUE smpeg_setFilter(VALUE obj,VALUE filter)
272
+ {
273
+ SMPEG *mpeg;
274
+
275
+ Data_Get_Struct(obj,SMPEG,mpeg);
276
+ if( (NUM2INT(filter)<0) || (NUM2INT(filter)>=NUM_FILTERS) )
277
+ rb_raise(eSDLError,"There isn't that filter");
278
+ SMPEG_filter(mpeg,filters[NUM2INT(filter)]);
279
+ return Qnil;
280
+ }
281
+
282
+ static void defineConstForSMPEG()
283
+ {
284
+ rb_define_const(cMPEG,"ERROR",INT2FIX(SMPEG_ERROR));
285
+ rb_define_const(cMPEG,"STOPPED",INT2FIX(SMPEG_STOPPED));
286
+ rb_define_const(cMPEG,"PLAYING",INT2FIX(SMPEG_PLAYING));
287
+ rb_define_const(cMPEG,"NULL_FILTER",INT2FIX(NULL_FILTER));
288
+ rb_define_const(cMPEG,"BILINEAR_FILTER",INT2FIX(BILINEAR_FILTER));
289
+ rb_define_const(cMPEG,"DEBLOCKING_FILTER",INT2FIX(DEBLOCKING_FILTER));
290
+ }
291
+
292
+ void init_smpeg()
293
+ {
294
+ cMPEG = rb_define_class_under(mSDL,"MPEG",rb_cObject);
295
+ cMPEGInfo = rb_define_class_under(cMPEG,"Info",rb_cObject);
296
+
297
+ filters[NULL_FILTER] = SMPEGfilter_null();
298
+ filters[BILINEAR_FILTER] = SMPEGfilter_bilinear();
299
+ filters[DEBLOCKING_FILTER] = SMPEGfilter_deblocking();
300
+
301
+ defineConstForSMPEG();
302
+
303
+ rb_define_attr(cMPEGInfo,"has_audio",1,0);
304
+ rb_define_attr(cMPEGInfo,"has_video",1,0);
305
+ rb_define_attr(cMPEGInfo,"width",1,0);
306
+ rb_define_attr(cMPEGInfo,"height",1,0);
307
+ rb_define_attr(cMPEGInfo,"current_frame",1,0);
308
+ rb_define_attr(cMPEGInfo,"current_fps",1,0);
309
+ rb_define_attr(cMPEGInfo,"audio_string",1,0);
310
+ rb_define_attr(cMPEGInfo,"audio_current_frame",1,0);
311
+ rb_define_attr(cMPEGInfo,"current_offset",1,0);
312
+ rb_define_attr(cMPEGInfo,"total_size",1,0);
313
+ rb_define_attr(cMPEGInfo,"current_time",1,0);
314
+ rb_define_attr(cMPEGInfo,"total_time",1,0);
315
+
316
+ rb_define_singleton_method(cMPEG,"load",smpeg_load,1);
317
+ rb_define_singleton_method(cMPEG,"new",smpeg_load,1);
318
+
319
+ rb_define_method(cMPEG,"info",smpeg_getInfo,1);
320
+ rb_define_method(cMPEG,"enableAudio",smpeg_enableAudio,1);
321
+ rb_define_method(cMPEG,"enableVideo",smpeg_enableVideo,1);
322
+ rb_define_method(cMPEG,"status",smpeg_status,0);
323
+ rb_define_method(cMPEG,"setVolume",smpeg_setVolume,1);
324
+ rb_define_method(cMPEG,"setDisplay",smpeg_setDisplay,1);
325
+ rb_define_method(cMPEG,"setLoop",smpeg_setLoop,1);
326
+ rb_define_method(cMPEG,"scaleXY",smpeg_scaleXY,2);
327
+ rb_define_method(cMPEG,"scale",smpeg_scale,1);
328
+ rb_define_method(cMPEG,"move",smpeg_move,1);
329
+ rb_define_method(cMPEG,"setDisplayRegion",smpeg_setDisplayRegion,4);
330
+ rb_define_method(cMPEG,"play",smpeg_play,0);
331
+ rb_define_method(cMPEG,"pause",smpeg_pause,0);
332
+ rb_define_method(cMPEG,"stop",smpeg_stop,0);
333
+ rb_define_method(cMPEG,"rewind",smpeg_rewind,0);
334
+ rb_define_method(cMPEG,"seek",smpeg_seek,1);
335
+ rb_define_method(cMPEG,"skip",smpeg_skip,1);
336
+ rb_define_method(cMPEG,"renderFrame",smpeg_renderFrame,1);
337
+ rb_define_method(cMPEG,"renderFinal",smpeg_renderFinal,3);
338
+ rb_define_method(cMPEG,"setFilter",smpeg_setFilter,1);
339
+
340
+ }
341
+ #endif /* HAVE_SMPEG */
@@ -0,0 +1,36 @@
1
+ /*
2
+ Ruby/SDL Ruby extension library for SDL
3
+
4
+ Copyright (C) 2001-2007 Ohbayashi Ippei
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ */
20
+ #include "rubysdl.h"
21
+
22
+ static VALUE sdl_getTicks(VALUE mod)
23
+ {
24
+ return UINT2NUM(SDL_GetTicks());
25
+ }
26
+ static VALUE sdl_delay(VALUE mod,VALUE ms)
27
+ {
28
+ SDL_Delay(NUM2UINT(ms));
29
+ return Qnil;
30
+ }
31
+
32
+ void init_time()
33
+ {
34
+ rb_define_module_function(mSDL,"getTicks",sdl_getTicks,0);
35
+ rb_define_module_function(mSDL,"delay",sdl_delay,1);
36
+ }
@@ -0,0 +1,324 @@
1
+ /*
2
+ Ruby/SDL Ruby extension library for SDL
3
+
4
+ Copyright (C) 2001-2007 Ohbayashi Ippei
5
+
6
+ This library is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU Lesser General Public
8
+ License as published by the Free Software Foundation; either
9
+ version 2.1 of the License, or (at your option) any later version.
10
+
11
+ This library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public
17
+ License along with this library; if not, write to the Free Software
18
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ */
20
+ #ifdef HAVE_SDL_TTF
21
+ #include "rubysdl.h"
22
+ #include <SDL_ttf.h>
23
+
24
+ typedef SDL_Surface* (*RenderFunc)(TTF_Font *,const char *,SDL_Color,SDL_Color);
25
+
26
+ static int ttf_initialized=0;
27
+ static int ttf_finalized=0;
28
+
29
+ static void ttf_closeFont(TTF_Font *font)
30
+ {
31
+ if( !ttf_finalized )
32
+ TTF_CloseFont(font);
33
+ }
34
+ static VALUE sdl_ttf_init(VALUE class)
35
+ {
36
+ if( TTF_Init()== -1 )
37
+ rb_raise(eSDLError,"Couldn't initialize TTF engine: %s",TTF_GetError());
38
+ ttf_initialized=1;
39
+ return Qnil;
40
+ }
41
+ static VALUE sdl_ttf_wasInit(VALUE class)
42
+ {
43
+ return BOOL(TTF_WasInit());
44
+ }
45
+
46
+ static VALUE sdl_ttf_open(int argc, VALUE *argv, VALUE class)
47
+ {
48
+ TTF_Font *font;
49
+ VALUE filename, size, index;
50
+ rb_scan_args( argc, argv, "21", &filename, &size, &index );
51
+ if( NIL_P(index) )
52
+ font=TTF_OpenFont( GETCSTR(filename),NUM2INT(size) );
53
+ else
54
+ #ifdef HAVE_TTF_OPENFONTINDEX
55
+ font=TTF_OpenFontIndex( GETCSTR(filename),NUM2INT(size),NUM2INT(index) );
56
+ #else
57
+ if( index != 0)
58
+ rb_raise(rb_eRuntimeError,"Not supported for selecting indivisual font face by SDL_ttf. The feature is in SDL_ttf 2.0.4 or later.");
59
+ else
60
+ font=TTF_OpenFont( GETCSTR(filename),NUM2INT(size) );
61
+ #endif
62
+ if( font==NULL )
63
+ rb_raise(eSDLError,"Couldn't open font %s: %s",GETCSTR(filename),
64
+ TTF_GetError());
65
+ return Data_Wrap_Struct(class,0,ttf_closeFont,font);
66
+ }
67
+ static VALUE sdl_ttf_getFontStyle(VALUE obj)
68
+ {
69
+ TTF_Font *font;
70
+ Data_Get_Struct(obj,TTF_Font,font);
71
+ return INT2FIX( TTF_GetFontStyle(font) );
72
+ }
73
+ static VALUE sdl_ttf_setFontStyle(VALUE obj,VALUE style)
74
+ {
75
+ TTF_Font *font;
76
+ Data_Get_Struct(obj,TTF_Font,font);
77
+ TTF_SetFontStyle(font,NUM2UINT(style));
78
+ return Qnil;
79
+ }
80
+ static VALUE sdl_ttf_getFontFaces(VALUE obj)
81
+ {
82
+ #ifdef TTF_FONTFACES
83
+ TTF_Font *font;
84
+ Data_Get_Struct(obj,TTF_Font,font);
85
+ return UINT2NUM( TTF_FontFaces(font) );
86
+ #else
87
+ rb_raise(rb_eRuntimeError,"Not supported. The feature is in SDL_ttf 2.0.4 or later.");
88
+ #endif
89
+ }
90
+ static VALUE sdl_ttf_FontFaceIsFixedWidth(VALUE obj)
91
+ {
92
+ #ifdef TTF_FONTISFIXEDWIDTH
93
+ TTF_Font *font;
94
+ Data_Get_Struct(obj,TTF_Font,font);
95
+ if( TTF_FontFaceIsFixedWidth(font) )
96
+ return Qtrue;
97
+ else
98
+ return Qfalse;
99
+ #else
100
+ rb_raise(rb_eRuntimeError,"Not supported. The feature is in SDL_ttf 2.0.4 or later.");
101
+ #endif
102
+ }
103
+ static VALUE sdl_ttf_FontFaceFamilyName(VALUE obj)
104
+ {
105
+ #ifdef TTF_FONTFACEFAMILYNAME
106
+ TTF_Font *font;
107
+ char* name;
108
+
109
+ Data_Get_Struct(obj,TTF_Font,font);
110
+ name = TTF_FontFaceFamilyName(font);
111
+ if(name == NULL)
112
+ return Qnil;
113
+ else
114
+ return rb_str_new2(name);
115
+ #else
116
+ rb_raise(rb_eRuntimeError,"Not supported. The feature is in SDL_ttf 2.0.4 or later.");
117
+ #endif
118
+ }
119
+ static VALUE sdl_ttf_FontFaceStyleName(VALUE obj)
120
+ {
121
+ #ifdef TTF_FONTFACESTYLENAME
122
+ TTF_Font *font;
123
+ char* name;
124
+
125
+ Data_Get_Struct(obj,TTF_Font,font);
126
+ name = TTF_FontFaceStyleName(font);
127
+ if(name == NULL)
128
+ return Qnil;
129
+ else
130
+ return rb_str_new2( (const char *) );
131
+ #else
132
+ rb_raise(rb_eRuntimeError,"Not supported. The feature is in SDL_ttf 2.0.4 or later.");
133
+ #endif
134
+ }
135
+ static VALUE sdl_ttf_sizeText(VALUE obj,VALUE text)
136
+ {
137
+ TTF_Font *font;
138
+ int w,h;
139
+ Data_Get_Struct(obj,TTF_Font,font);
140
+ TTF_SizeUTF8(font,GETCSTR(text),&w,&h);
141
+ return rb_ary_new3(2,INT2FIX(w),INT2FIX(h));
142
+ }
143
+
144
+ static VALUE sdl_ttf_fontHeight(VALUE obj)
145
+ {
146
+ TTF_Font *font;
147
+ Data_Get_Struct(obj,TTF_Font,font);
148
+ return INT2FIX(TTF_FontHeight(font));
149
+ }
150
+
151
+ static VALUE sdl_ttf_fontAscent(VALUE obj)
152
+ {
153
+ TTF_Font *font;
154
+ Data_Get_Struct(obj,TTF_Font,font);
155
+ return INT2FIX(TTF_FontAscent(font));
156
+ }
157
+
158
+ static VALUE sdl_ttf_fontDescent(VALUE obj)
159
+ {
160
+ TTF_Font *font;
161
+ Data_Get_Struct(obj,TTF_Font,font);
162
+ return INT2FIX(TTF_FontDescent(font));
163
+ }
164
+
165
+ static VALUE sdl_ttf_fontLineSkip(VALUE obj)
166
+ {
167
+ TTF_Font *font;
168
+ Data_Get_Struct(obj,TTF_Font,font);
169
+ return INT2FIX(TTF_FontLineSkip(font));
170
+ }
171
+
172
+ static VALUE ttf_draw(VALUE obj,VALUE dest,VALUE text,VALUE x,
173
+ VALUE y,VALUE fgr,VALUE fgg,VALUE fgb,
174
+ VALUE bgr,VALUE bgg,VALUE bgb,RenderFunc render)
175
+ {
176
+ TTF_Font *font;
177
+ SDL_Surface *destSurface, *tmpSurface;
178
+ SDL_Color fg,bg;
179
+ SDL_Rect destRect;
180
+ int result;
181
+ char *ctext=GETCSTR(text);
182
+ /* If text=="" , TTF_RenderUTF8_Solid() and etc fail to render */
183
+ if( ctext[0]=='\0' )return INT2FIX(0);
184
+
185
+ if( !rb_obj_is_kind_of( dest,cSurface ) )
186
+ rb_raise( rb_eArgError,"type mismatch(expect Surface)");
187
+ Data_Get_Struct(obj,TTF_Font,font);
188
+ Data_Get_Struct(dest,SDL_Surface,destSurface);
189
+ fg.r=NUM2UINT(fgr); fg.g=NUM2UINT(fgg); fg.b=NUM2UINT(fgb);
190
+ bg.r=NUM2UINT(bgr); bg.g=NUM2UINT(bgg); bg.b=NUM2UINT(bgb);
191
+ SetRect(destRect,x,y,1,1);
192
+
193
+ tmpSurface=render(font,ctext,fg,bg);
194
+ if( tmpSurface==NULL )
195
+ rb_raise(eSDLError,"Text Render fail: %s",TTF_GetError());
196
+
197
+ result=SDL_BlitSurface(tmpSurface,NULL,destSurface,&destRect);
198
+ SDL_FreeSurface(tmpSurface);
199
+ if( result == -1 ){
200
+ rb_raise(eSDLError,"SDL_BlitSurface fail: %s",SDL_GetError());
201
+ }
202
+ return INT2NUM(result);
203
+ }
204
+
205
+
206
+ static SDL_Surface* ttf_wrap_RenderUTF8_Solid(TTF_Font *font,
207
+ const char *text,
208
+ SDL_Color fg,
209
+ SDL_Color bg)
210
+ {
211
+ return TTF_RenderUTF8_Solid(font,text,fg);
212
+ }
213
+
214
+ static VALUE sdl_ttf_drawSolidUTF8(VALUE obj,VALUE dest,VALUE text,VALUE x,
215
+ VALUE y,VALUE r,VALUE g,VALUE b)
216
+ {
217
+ return ttf_draw(obj,dest,text,x,y,r,g,b,1,1,1,ttf_wrap_RenderUTF8_Solid);
218
+ }
219
+ static SDL_Surface* ttf_wrap_RenderUTF8_Blended(TTF_Font *font,
220
+ const char *text,
221
+ SDL_Color fg,
222
+ SDL_Color bg)
223
+ {
224
+ return TTF_RenderUTF8_Blended(font,text,fg);
225
+ }
226
+
227
+ static VALUE sdl_ttf_drawBlendedUTF8(VALUE obj,VALUE dest,VALUE text,VALUE x,
228
+ VALUE y,VALUE r,VALUE g,VALUE b)
229
+ {
230
+ return ttf_draw(obj,dest,text,x,y,r,g,b,1,1,1,ttf_wrap_RenderUTF8_Blended);
231
+ }
232
+
233
+ static VALUE sdl_ttf_drawShadedUTF8(VALUE obj,VALUE dest, VALUE text,VALUE x,
234
+ VALUE y,VALUE fgr,VALUE fgg,VALUE fgb,
235
+ VALUE bgr,VALUE bgg,VALUE bgb)
236
+ {
237
+ return ttf_draw(obj,dest,text,x,y,fgr,fgg,fgb,bgr,bgg,bgb,
238
+ TTF_RenderUTF8_Shaded);
239
+ }
240
+
241
+ static VALUE ttf_render(VALUE obj,VALUE text,VALUE fgr,VALUE fgg,VALUE fgb,
242
+ VALUE bgr,VALUE bgg,VALUE bgb,RenderFunc render)
243
+ {
244
+ TTF_Font *font;
245
+ SDL_Surface *surface;
246
+ SDL_Color fg,bg;
247
+
248
+ Data_Get_Struct(obj,TTF_Font,font);
249
+ fg.r=NUM2UINT(fgr); fg.g=NUM2UINT(fgg); fg.b=NUM2UINT(fgb);
250
+ bg.r=NUM2UINT(bgr); bg.g=NUM2UINT(bgg); bg.b=NUM2UINT(bgb);
251
+
252
+ surface = render( font, GETCSTR(text), fg, bg );
253
+
254
+ if( surface == NULL )
255
+ return Qnil;
256
+
257
+ return Data_Wrap_Struct(cSurface,0,sdl_freeSurface,surface);
258
+ }
259
+
260
+ static VALUE sdl_ttf_renderSolidUTF8(VALUE obj,VALUE text,VALUE r,
261
+ VALUE g,VALUE b)
262
+ {
263
+ return ttf_render(obj,text,r,g,b,1,1,1,ttf_wrap_RenderUTF8_Solid);
264
+ }
265
+
266
+ static VALUE sdl_ttf_renderBlendedUTF8(VALUE obj,VALUE text,VALUE r,
267
+ VALUE g,VALUE b)
268
+ {
269
+ return ttf_render(obj,text,r,g,b,1,1,1,ttf_wrap_RenderUTF8_Blended);
270
+ }
271
+
272
+ static VALUE sdl_ttf_renderShadedUTF8(VALUE obj,VALUE text,
273
+ VALUE fgr,VALUE fgg,VALUE fgb,
274
+ VALUE bgr,VALUE bgg,VALUE bgb)
275
+ {
276
+ return ttf_render(obj,text,fgr,fgg,fgb,bgr,bgg,bgb,TTF_RenderUTF8_Shaded);
277
+ }
278
+
279
+ static void defineConstForTTF()
280
+ {
281
+ rb_define_const(cTTF,"STYLE_NORMAL",UINT2NUM(TTF_STYLE_NORMAL));
282
+ rb_define_const(cTTF,"STYLE_BOLD",UINT2NUM(TTF_STYLE_BOLD));
283
+ rb_define_const(cTTF,"STYLE_ITALIC",UINT2NUM(TTF_STYLE_ITALIC));
284
+ rb_define_const(cTTF,"STYLE_UNDERLINE",UINT2NUM(TTF_STYLE_UNDERLINE));
285
+ }
286
+ void init_ttf()
287
+ {
288
+ cTTF=rb_define_class_under(mSDL,"TTF",rb_cObject);
289
+ rb_define_singleton_method(cTTF,"init",sdl_ttf_init,0);
290
+ rb_define_singleton_method(cTTF,"init?",sdl_ttf_wasInit,0);
291
+ rb_define_singleton_method(cTTF,"open",sdl_ttf_open,-1);
292
+
293
+ rb_define_method(cTTF,"style",sdl_ttf_getFontStyle,0);
294
+ rb_define_method(cTTF,"style=",sdl_ttf_setFontStyle,1);
295
+ rb_define_method(cTTF,"textSize",sdl_ttf_sizeText,1);
296
+ rb_define_method(cTTF,"faces",sdl_ttf_getFontFaces,0);
297
+ rb_define_method(cTTF,"fixedWidth?",sdl_ttf_FontFaceIsFixedWidth,0);
298
+ rb_define_method(cTTF,"familyName",sdl_ttf_FontFaceFamilyName,0);
299
+ rb_define_method(cTTF,"styleName",sdl_ttf_FontFaceStyleName,0);
300
+
301
+ rb_define_method(cTTF,"height",sdl_ttf_fontHeight,0);
302
+ rb_define_method(cTTF,"ascent",sdl_ttf_fontAscent,0);
303
+ rb_define_method(cTTF,"descent",sdl_ttf_fontDescent,0);
304
+ rb_define_method(cTTF,"lineSkip",sdl_ttf_fontLineSkip,0);
305
+
306
+ rb_define_method(cTTF,"drawSolidUTF8",sdl_ttf_drawSolidUTF8,7);
307
+ rb_define_method(cTTF,"drawBlendedUTF8",sdl_ttf_drawBlendedUTF8,7);
308
+ rb_define_method(cTTF,"drawShadedUTF8",sdl_ttf_drawShadedUTF8,10);
309
+
310
+ rb_define_method(cTTF,"renderSolidUTF8",sdl_ttf_renderSolidUTF8,4);
311
+ rb_define_method(cTTF,"renderBlendedUTF8",sdl_ttf_renderBlendedUTF8,4);
312
+ rb_define_method(cTTF,"renderShadedUTF8",sdl_ttf_renderShadedUTF8,7);
313
+
314
+ defineConstForTTF();
315
+ }
316
+ void quit_ttf()
317
+ {
318
+ if(ttf_initialized){
319
+ TTF_Quit();
320
+ ttf_finalized=1;
321
+ }
322
+ }
323
+
324
+ #endif /* HAVE_SDL_TTF */