sdl2-win93 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.dir-locals.el +2 -0
  3. data/.github/workflows/gempush.yml +29 -0
  4. data/.gitignore +14 -0
  5. data/COPYING.txt +165 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +24 -0
  8. data/Makefile +4 -0
  9. data/README.md +36 -0
  10. data/Rakefile +51 -0
  11. data/doc/po/ja.po +10357 -0
  12. data/ext/sdl2_ext/clipboard.c +61 -0
  13. data/ext/sdl2_ext/color.c +103 -0
  14. data/ext/sdl2_ext/color.h +4 -0
  15. data/ext/sdl2_ext/event.c +1298 -0
  16. data/ext/sdl2_ext/extconf.rb +22 -0
  17. data/ext/sdl2_ext/filesystem.c +63 -0
  18. data/ext/sdl2_ext/gamecontroller.c +408 -0
  19. data/ext/sdl2_ext/gamecontroller.c.m4 +408 -0
  20. data/ext/sdl2_ext/gl.c +351 -0
  21. data/ext/sdl2_ext/gl.c.m4 +351 -0
  22. data/ext/sdl2_ext/hint.c +99 -0
  23. data/ext/sdl2_ext/joystick.c +339 -0
  24. data/ext/sdl2_ext/joystick.c.m4 +339 -0
  25. data/ext/sdl2_ext/key.c +1302 -0
  26. data/ext/sdl2_ext/key.c.m4 +833 -0
  27. data/ext/sdl2_ext/main.c +258 -0
  28. data/ext/sdl2_ext/messagebox.c +233 -0
  29. data/ext/sdl2_ext/mixer.c +1205 -0
  30. data/ext/sdl2_ext/mixer.c.m4 +1205 -0
  31. data/ext/sdl2_ext/mouse.c +286 -0
  32. data/ext/sdl2_ext/rubysdl2_internal.h +127 -0
  33. data/ext/sdl2_ext/timer.c +63 -0
  34. data/ext/sdl2_ext/ttf.c +376 -0
  35. data/ext/sdl2_ext/ttf.c.m4 +376 -0
  36. data/ext/sdl2_ext/video.c +4093 -0
  37. data/ext/sdl2_ext/video.c.m4 +3867 -0
  38. data/lib/sdl2.rb +3 -0
  39. data/lib/sdl2/event.rb +55 -0
  40. data/lib/sdl2/version.rb +8 -0
  41. data/sample/chunk_destroy.rb +16 -0
  42. data/sample/gfxprimitives.rb +54 -0
  43. data/sample/icon.bmp +0 -0
  44. data/sample/memory_test/m1.rb +28 -0
  45. data/sample/memory_test/m2.rb +18 -0
  46. data/sample/memory_test/m3.rb +12 -0
  47. data/sample/message_box.rb +33 -0
  48. data/sample/music_player.rb +137 -0
  49. data/sample/playwave.rb +19 -0
  50. data/sample/primitives.rb +32 -0
  51. data/sample/test_clipboard.rb +16 -0
  52. data/sample/test_controller.rb +62 -0
  53. data/sample/test_joystick.rb +53 -0
  54. data/sample/test_keyboard.rb +52 -0
  55. data/sample/test_mouse.rb +50 -0
  56. data/sample/test_surface.rb +13 -0
  57. data/sample/test_ttf.rb +82 -0
  58. data/sample/test_video.rb +59 -0
  59. data/sample/testgl.rb +175 -0
  60. data/sample/testsprite.rb +296 -0
  61. data/sample/testspriteminimal.rb +75 -0
  62. data/sample/timer.rb +11 -0
  63. data/sample/version.rb +12 -0
  64. data/sample/video_info.rb +64 -0
  65. data/sdl2-win93.gemspec +31 -0
  66. metadata +158 -0
@@ -0,0 +1,376 @@
1
+ /* -*- mode: C -*- */
2
+ #ifdef HAVE_SDL_TTF_H
3
+ #include "rubysdl2_internal.h"
4
+ #include <SDL_ttf.h>
5
+ #include <ruby/encoding.h>
6
+
7
+ static VALUE cTTF;
8
+ static VALUE mStyle;
9
+ static VALUE mHinting;
10
+
11
+ #define TTF_ERROR() do { HANDLE_ERROR(SDL_SetError("%s", TTF_GetError())); } while (0)
12
+ #define HANDLE_TTF_ERROR(code) \
13
+ do { if ((code) < 0) { TTF_ERROR(); } } while (0)
14
+
15
+ typedef struct TTF {
16
+ TTF_Font* font;
17
+ } TTF;
18
+
19
+ #define TTF_ATTRIBUTE(attr, capitalized_attr, ruby2c, c2ruby) \
20
+ static VALUE TTF_##attr(VALUE self) \
21
+ { \
22
+ return c2ruby(TTF_Get##capitalized_attr(Get_TTF_Font(self))); \
23
+ } \
24
+ static VALUE TTF_set_##attr(VALUE self, VALUE val) \
25
+ { \
26
+ TTF_Set##capitalized_attr(Get_TTF_Font(self), ruby2c(val)); \
27
+ return Qnil; \
28
+ }
29
+
30
+ #define TTF_ATTRIBUTE_INT(attr, capitalized_attr) \
31
+ TTF_ATTRIBUTE(attr, capitalized_attr, NUM2INT, INT2NUM)
32
+
33
+ #define TTF_ATTR_READER(attr, capitalized_attr, c2ruby) \
34
+ static VALUE TTF_##attr(VALUE self) \
35
+ { \
36
+ return c2ruby(TTF_Font##capitalized_attr(Get_TTF_Font(self))); \
37
+ }
38
+
39
+ static void TTF_free(TTF* f)
40
+ {
41
+ if (rubysdl2_is_active() && f->font)
42
+ TTF_CloseFont(f->font);
43
+ free(f);
44
+ }
45
+
46
+ static VALUE TTF_new(TTF_Font* font)
47
+ {
48
+ TTF* f = ALLOC(TTF);
49
+ f->font = font;
50
+ return Data_Wrap_Struct(cTTF, 0, TTF_free, f);
51
+ }
52
+
53
+ DEFINE_WRAPPER(TTF_Font, TTF, font, cTTF, "SDL2::TTF");
54
+
55
+ /*
56
+ * Document-class: SDL2::TTF
57
+ *
58
+ * This class represents font information.
59
+ *
60
+ * You can render TrueType fonts using [SDL_ttf](https://www.libsdl.org/projects/SDL_ttf/)
61
+ * and this class.
62
+ *
63
+ * @!attribute style
64
+ * Font style.
65
+ * The OR'd bits of the constants of {SDL2::TTF::Style}.
66
+ * @return [Integer]
67
+ *
68
+ * @!attribute outline
69
+ * The outline pixel width of the font.
70
+ * @return [Integer]
71
+ *
72
+ * @!attribute hinting
73
+ * Font hinting.
74
+ * One of the constants of {SDL2::TTF::Hinting}.
75
+ * @return [Integer]
76
+ *
77
+ * @!attribute kerning
78
+ * True if kerning is enabled.
79
+ * @return [Booelan]
80
+ *
81
+ * @!attribute [r] height
82
+ * The maximum pixel height of all glyphs of the font.
83
+ * You can use this height to render text as close together vertically
84
+ * as possible.
85
+ * @return [Integer]
86
+ * @see #line_skip
87
+ *
88
+ * @!attribute [r] ascent
89
+ * The maximum pixel ascent of all glyphs of the font.
90
+ * The distance from the top of the font to the baseline.
91
+ * @return [Integer]
92
+ * @see #descent
93
+ *
94
+ * @!attribute [r] descent
95
+ * The maximum pixel descent of all glyphs of the font.
96
+ * The distance from the bottom of the font to the baseline.
97
+ * @return [Integer]
98
+ * @see #ascent
99
+ *
100
+ * @!attribute [r] line_skip
101
+ * The recommended pixel height of rendered line of text
102
+ * of the font. Normally, this value is larger than {#height}.
103
+ * @return [Integer]
104
+ *
105
+ * @!attribute [r] num_faces
106
+ * The number of faces available in the font.
107
+ * @return [Integer]
108
+ *
109
+ * @!attribute [r] face_family_name
110
+ * The current font face family name of the font.
111
+ * @return [String]
112
+ *
113
+ * @!attribute [r] face_style_name
114
+ * The current font face style name of the font.
115
+ * @return [String]
116
+ *
117
+ */
118
+
119
+ /*
120
+ * Initialize TrueType font rendering submodule.
121
+ *
122
+ * This function must be called before calling other methods/class methods
123
+ * of {TTF}.
124
+ *
125
+ * @return [nil]
126
+ */
127
+ static VALUE TTF_s_init(VALUE self)
128
+ {
129
+ HANDLE_TTF_ERROR(TTF_Init());
130
+ return Qnil;
131
+ }
132
+
133
+ /*
134
+ * Open a font data from file.
135
+ *
136
+ * @overload open(fname, ptsize, index=0)
137
+ * @param fname [String] the path of the font file
138
+ * @param ptsize [Integer] the point size of the font (72DPI).
139
+ * @param index [Integer] the index of the font faces.
140
+ * Some font files have multiple font faces, and you
141
+ * can select one of them using **index**. 0 origin.
142
+ * If a font have only one font face, 0 is the only
143
+ * valid index.
144
+ *
145
+ * @return [SDL2::TTF] opened font information
146
+ * @raise [SDL2::Error] occurs when you fail to open a file.
147
+ *
148
+ */
149
+ static VALUE TTF_s_open(int argc, VALUE* argv, VALUE self)
150
+ {
151
+ TTF_Font* font;
152
+ VALUE fname, ptsize, index;
153
+ rb_scan_args(argc, argv, "21", &fname, &ptsize, &index);
154
+
155
+ font = TTF_OpenFontIndex(StringValueCStr(fname), NUM2INT(ptsize),
156
+ index == Qnil ? 0 : NUM2LONG(index));
157
+ if (!font)
158
+ TTF_ERROR();
159
+
160
+ return TTF_new(font);
161
+ }
162
+
163
+ /*
164
+ * Destroy the font data and release memory.
165
+ *
166
+ * @return [nil]
167
+ */
168
+ static VALUE TTF_destroy(VALUE self)
169
+ {
170
+ TTF* f = Get_TTF(self);
171
+ if (f->font)
172
+ TTF_CloseFont(f->font);
173
+ f->font = NULL;
174
+ return Qnil;
175
+ }
176
+
177
+ TTF_ATTRIBUTE_INT(style, FontStyle);
178
+ TTF_ATTRIBUTE_INT(outline, FontOutline);
179
+ TTF_ATTRIBUTE_INT(hinting, FontHinting);
180
+ TTF_ATTRIBUTE(kerning, FontKerning, RTEST, INT2BOOL);
181
+ TTF_ATTR_READER(height, Height, INT2FIX);
182
+ TTF_ATTR_READER(ascent, Ascent, INT2FIX);
183
+ TTF_ATTR_READER(descent, Descent, INT2FIX);
184
+ TTF_ATTR_READER(line_skip, LineSkip, INT2FIX);
185
+ TTF_ATTR_READER(num_faces, Faces, LONG2NUM);
186
+ TTF_ATTR_READER(face_is_fixed_width_p, FaceIsFixedWidth, INT2BOOL);
187
+ TTF_ATTR_READER(face_family_name, FaceFamilyName, utf8str_new_cstr);
188
+ TTF_ATTR_READER(face_style_name, FaceStyleName, utf8str_new_cstr);
189
+
190
+ /*
191
+ * @overload size_text(text)
192
+ * Calculate the size of rendered surface of **text** using the font.
193
+ *
194
+ * @param text [String] the string to size up
195
+ *
196
+ * @return [[Integer, Integer]] a pair of width and height of the rendered surface
197
+ *
198
+ * @raise [SDL2::Error] It is raised when an error occurs, such as a glyph ins the
199
+ * string not being found.
200
+ */
201
+ static VALUE TTF_size_text(VALUE self, VALUE text)
202
+ {
203
+ int w, h;
204
+ Check_Type(text, T_STRING);
205
+ text = rb_str_export_to_enc(text, rb_utf8_encoding());
206
+ HANDLE_TTF_ERROR(TTF_SizeUTF8(Get_TTF_Font(self), StringValueCStr(text), &w, &h));
207
+ return rb_ary_new3(2, INT2NUM(w), INT2NUM(h));
208
+ }
209
+
210
+ static SDL_Surface* render_solid(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
211
+ {
212
+ return TTF_RenderUTF8_Solid(font, text, fg);
213
+ }
214
+
215
+ static SDL_Surface* render_blended(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
216
+ {
217
+ return TTF_RenderUTF8_Blended(font, text, fg);
218
+ }
219
+
220
+ static VALUE render(SDL_Surface* (*renderer)(TTF_Font*, const char*, SDL_Color, SDL_Color),
221
+ VALUE font, VALUE text, VALUE fg, VALUE bg)
222
+ {
223
+ SDL_Surface* surface;
224
+ text = rb_str_export_to_enc(text, rb_utf8_encoding());
225
+ surface = renderer(Get_TTF_Font(font), StringValueCStr(text),
226
+ Color_to_SDL_Color(fg), Color_to_SDL_Color(bg));
227
+ if (!surface)
228
+ TTF_ERROR();
229
+
230
+ return Surface_new(surface);
231
+ }
232
+
233
+ /*
234
+ * @overload render_solid(text, fg)
235
+ * Render **text** using the font with fg color on a new surface, using
236
+ * *Solid* mode.
237
+ *
238
+ * Solid mode rendering is quick but dirty.
239
+ *
240
+ * @param text [String] the text to render
241
+ * @param fg [[Integer, Integer, Integer]]
242
+ * the color to render. An array of r, g, and b components.
243
+ *
244
+ * @return [SDL2::Surface]
245
+ *
246
+ * @raise [SDL2::Error] raised when the randering fails.
247
+ */
248
+ static VALUE TTF_render_solid(VALUE self, VALUE text, VALUE fg)
249
+ {
250
+ return render(render_solid, self, text, fg, Qnil);
251
+ }
252
+
253
+ /*
254
+ * @overload render_shaded(text, fg, bg)
255
+ * Render **text** using the font with fg color on a new surface, using
256
+ * *Shaded* mode.
257
+ *
258
+ * Shaded mode rendering is slow and nice, but with a solid box filled by
259
+ * the background color.
260
+ *
261
+ * @param text [String] the text to render
262
+ * @param fg [[Integer, Integer, Integer]]
263
+ * the color to render. An array of r, g, and b components.
264
+ * @param bg [[Integer, Integer, Integer]]
265
+ * the background color. An array of r, g, and b components.
266
+ *
267
+ * @return [SDL2::Surface]
268
+ *
269
+ * @raise [SDL2::Error] raised when the randering fails.
270
+ */
271
+ static VALUE TTF_render_shaded(VALUE self, VALUE text, VALUE fg, VALUE bg)
272
+ {
273
+ return render(TTF_RenderUTF8_Shaded, self, text, fg, bg);
274
+ }
275
+
276
+ /*
277
+ * @overload render_blended(text, fg)
278
+ * Render **text** using the font with fg color on a new surface, using
279
+ * *Blended* mode.
280
+ *
281
+ * Blended mode rendering is very slow but very nice.
282
+ * The rendered surface has an alpha channel,
283
+ *
284
+ * @param text [String] the text to render
285
+ * @param fg [[Integer, Integer, Integer]]
286
+ * the color to render. An array of r, g, and b components.
287
+ *
288
+ * @return [SDL2::Surface]
289
+ *
290
+ * @raise [SDL2::Error] raised when the randering fails.
291
+ */
292
+ static VALUE TTF_render_blended(VALUE self, VALUE text, VALUE fg)
293
+ {
294
+ return render(render_blended, self, text, fg, Qnil);
295
+ }
296
+
297
+ /*
298
+ * Document-module: SDL2::TTF::Style
299
+ *
300
+ * Constants represents font styles.
301
+ */
302
+
303
+ /* Document-module: SDL2::TTF::Hinting
304
+ *
305
+ * Constants represents font hinting for FreeType.
306
+ */
307
+ void rubysdl2_init_ttf(void)
308
+ {
309
+ cTTF = rb_define_class_under(mSDL2, "TTF", rb_cObject);
310
+ rb_undef_alloc_func(cTTF);
311
+
312
+ rb_define_singleton_method(cTTF, "init", TTF_s_init, 0);
313
+ rb_define_singleton_method(cTTF, "open", TTF_s_open, -1);
314
+ /* Return true if the font is destroyed by {#destroy}. */
315
+ rb_define_method(cTTF, "destroy?", TTF_destroy_p, 0);
316
+ rb_define_method(cTTF, "destroy", TTF_destroy, 0);
317
+
318
+ #define DEFINE_TTF_ATTRIBUTE(attr) do { \
319
+ rb_define_method(cTTF, #attr, TTF_##attr, 0); \
320
+ rb_define_method(cTTF, #attr "=", TTF_set_##attr, 1); \
321
+ } while (0)
322
+
323
+ DEFINE_TTF_ATTRIBUTE(style);
324
+ DEFINE_TTF_ATTRIBUTE(outline);
325
+ DEFINE_TTF_ATTRIBUTE(hinting);
326
+ DEFINE_TTF_ATTRIBUTE(kerning);
327
+
328
+ #define DEFINE_TTF_ATTR_READER(attr) \
329
+ rb_define_method(cTTF, #attr, TTF_##attr, 0)
330
+
331
+ DEFINE_TTF_ATTR_READER(height);
332
+ DEFINE_TTF_ATTR_READER(ascent);
333
+ DEFINE_TTF_ATTR_READER(descent);
334
+ DEFINE_TTF_ATTR_READER(line_skip);
335
+ DEFINE_TTF_ATTR_READER(num_faces);
336
+ /* Return true if the font is fixed width. */
337
+ rb_define_method(cTTF, "face_is_fixed_width?", TTF_face_is_fixed_width_p, 0);
338
+ DEFINE_TTF_ATTR_READER(face_family_name);
339
+ DEFINE_TTF_ATTR_READER(face_style_name);
340
+
341
+ rb_define_method(cTTF, "size_text", TTF_size_text, 1);
342
+ rb_define_method(cTTF, "render_solid", TTF_render_solid, 2);
343
+ rb_define_method(cTTF, "render_shaded", TTF_render_shaded, 3);
344
+ rb_define_method(cTTF, "render_blended", TTF_render_blended, 2);
345
+
346
+ mStyle = rb_define_module_under(cTTF, "Style");
347
+ /* */
348
+ /* normal style */
349
+ rb_define_const(mStyle, "NORMAL", INT2NUM((TTF_STYLE_NORMAL)));
350
+ /* bold style */
351
+ rb_define_const(mStyle, "BOLD", INT2NUM((TTF_STYLE_BOLD)));
352
+ /* italic style */
353
+ rb_define_const(mStyle, "ITALIC", INT2NUM((TTF_STYLE_ITALIC)));
354
+ /* underline style */
355
+ rb_define_const(mStyle, "UNDERLINE", INT2NUM((TTF_STYLE_UNDERLINE)));
356
+ /* strikethrough style */
357
+ rb_define_const(mStyle, "STRIKETHROUGH", INT2NUM((TTF_STYLE_STRIKETHROUGH)));
358
+
359
+ mHinting = rb_define_module_under(cTTF, "Hinting");
360
+ /* */
361
+ /* normal hinting, default */
362
+ rb_define_const(mHinting, "NORMAL", INT2NUM((TTF_HINTING_NORMAL)));
363
+ /* lighter hinting for non-monochrome modes */
364
+ rb_define_const(mHinting, "LIGHT", INT2NUM((TTF_HINTING_LIGHT)));
365
+ /* strong hinting only used for monochrome output */
366
+ rb_define_const(mHinting, "MONO", INT2NUM((TTF_HINTING_MONO)));
367
+ /* no hinting */
368
+ rb_define_const(mHinting, "NONE", INT2NUM((TTF_HINTING_NONE)));
369
+ }
370
+
371
+ #else /* HAVE_SDL_TTF_H */
372
+ void rubysdl2_init_ttf(void)
373
+ {
374
+ }
375
+ #endif /* HAVE_SDL_TTF_H */
376
+
@@ -0,0 +1,376 @@
1
+ /* -*- mode: C -*- */
2
+ #ifdef HAVE_SDL_TTF_H
3
+ #include "rubysdl2_internal.h"
4
+ #include <SDL_ttf.h>
5
+ #include <ruby/encoding.h>
6
+
7
+ static VALUE cTTF;
8
+ static VALUE mStyle;
9
+ static VALUE mHinting;
10
+
11
+ #define TTF_ERROR() do { HANDLE_ERROR(SDL_SetError("%s", TTF_GetError())); } while (0)
12
+ #define HANDLE_TTF_ERROR(code) \
13
+ do { if ((code) < 0) { TTF_ERROR(); } } while (0)
14
+
15
+ typedef struct TTF {
16
+ TTF_Font* font;
17
+ } TTF;
18
+
19
+ #define TTF_ATTRIBUTE(attr, capitalized_attr, ruby2c, c2ruby) \
20
+ static VALUE TTF_##attr(VALUE self) \
21
+ { \
22
+ return c2ruby(TTF_Get##capitalized_attr(Get_TTF_Font(self))); \
23
+ } \
24
+ static VALUE TTF_set_##attr(VALUE self, VALUE val) \
25
+ { \
26
+ TTF_Set##capitalized_attr(Get_TTF_Font(self), ruby2c(val)); \
27
+ return Qnil; \
28
+ }
29
+
30
+ #define TTF_ATTRIBUTE_INT(attr, capitalized_attr) \
31
+ TTF_ATTRIBUTE(attr, capitalized_attr, NUM2INT, INT2NUM)
32
+
33
+ #define TTF_ATTR_READER(attr, capitalized_attr, c2ruby) \
34
+ static VALUE TTF_##attr(VALUE self) \
35
+ { \
36
+ return c2ruby(TTF_Font##capitalized_attr(Get_TTF_Font(self))); \
37
+ }
38
+
39
+ static void TTF_free(TTF* f)
40
+ {
41
+ if (rubysdl2_is_active() && f->font)
42
+ TTF_CloseFont(f->font);
43
+ free(f);
44
+ }
45
+
46
+ static VALUE TTF_new(TTF_Font* font)
47
+ {
48
+ TTF* f = ALLOC(TTF);
49
+ f->font = font;
50
+ return Data_Wrap_Struct(cTTF, 0, TTF_free, f);
51
+ }
52
+
53
+ DEFINE_WRAPPER(TTF_Font, TTF, font, cTTF, "SDL2::TTF");
54
+
55
+ /*
56
+ * Document-class: SDL2::TTF
57
+ *
58
+ * This class represents font information.
59
+ *
60
+ * You can render TrueType fonts using [SDL_ttf](https://www.libsdl.org/projects/SDL_ttf/)
61
+ * and this class.
62
+ *
63
+ * @!attribute style
64
+ * Font style.
65
+ * The OR'd bits of the constants of {SDL2::TTF::Style}.
66
+ * @return [Integer]
67
+ *
68
+ * @!attribute outline
69
+ * The outline pixel width of the font.
70
+ * @return [Integer]
71
+ *
72
+ * @!attribute hinting
73
+ * Font hinting.
74
+ * One of the constants of {SDL2::TTF::Hinting}.
75
+ * @return [Integer]
76
+ *
77
+ * @!attribute kerning
78
+ * True if kerning is enabled.
79
+ * @return [Booelan]
80
+ *
81
+ * @!attribute [r] height
82
+ * The maximum pixel height of all glyphs of the font.
83
+ * You can use this height to render text as close together vertically
84
+ * as possible.
85
+ * @return [Integer]
86
+ * @see #line_skip
87
+ *
88
+ * @!attribute [r] ascent
89
+ * The maximum pixel ascent of all glyphs of the font.
90
+ * The distance from the top of the font to the baseline.
91
+ * @return [Integer]
92
+ * @see #descent
93
+ *
94
+ * @!attribute [r] descent
95
+ * The maximum pixel descent of all glyphs of the font.
96
+ * The distance from the bottom of the font to the baseline.
97
+ * @return [Integer]
98
+ * @see #ascent
99
+ *
100
+ * @!attribute [r] line_skip
101
+ * The recommended pixel height of rendered line of text
102
+ * of the font. Normally, this value is larger than {#height}.
103
+ * @return [Integer]
104
+ *
105
+ * @!attribute [r] num_faces
106
+ * The number of faces available in the font.
107
+ * @return [Integer]
108
+ *
109
+ * @!attribute [r] face_family_name
110
+ * The current font face family name of the font.
111
+ * @return [String]
112
+ *
113
+ * @!attribute [r] face_style_name
114
+ * The current font face style name of the font.
115
+ * @return [String]
116
+ *
117
+ */
118
+
119
+ /*
120
+ * Initialize TrueType font rendering submodule.
121
+ *
122
+ * This function must be called before calling other methods/class methods
123
+ * of {TTF}.
124
+ *
125
+ * @return [nil]
126
+ */
127
+ static VALUE TTF_s_init(VALUE self)
128
+ {
129
+ HANDLE_TTF_ERROR(TTF_Init());
130
+ return Qnil;
131
+ }
132
+
133
+ /*
134
+ * Open a font data from file.
135
+ *
136
+ * @overload open(fname, ptsize, index=0)
137
+ * @param fname [String] the path of the font file
138
+ * @param ptsize [Integer] the point size of the font (72DPI).
139
+ * @param index [Integer] the index of the font faces.
140
+ * Some font files have multiple font faces, and you
141
+ * can select one of them using **index**. 0 origin.
142
+ * If a font have only one font face, 0 is the only
143
+ * valid index.
144
+ *
145
+ * @return [SDL2::TTF] opened font information
146
+ * @raise [SDL2::Error] occurs when you fail to open a file.
147
+ *
148
+ */
149
+ static VALUE TTF_s_open(int argc, VALUE* argv, VALUE self)
150
+ {
151
+ TTF_Font* font;
152
+ VALUE fname, ptsize, index;
153
+ rb_scan_args(argc, argv, "21", &fname, &ptsize, &index);
154
+
155
+ font = TTF_OpenFontIndex(StringValueCStr(fname), NUM2INT(ptsize),
156
+ index == Qnil ? 0 : NUM2LONG(index));
157
+ if (!font)
158
+ TTF_ERROR();
159
+
160
+ return TTF_new(font);
161
+ }
162
+
163
+ /*
164
+ * Destroy the font data and release memory.
165
+ *
166
+ * @return [nil]
167
+ */
168
+ static VALUE TTF_destroy(VALUE self)
169
+ {
170
+ TTF* f = Get_TTF(self);
171
+ if (f->font)
172
+ TTF_CloseFont(f->font);
173
+ f->font = NULL;
174
+ return Qnil;
175
+ }
176
+
177
+ TTF_ATTRIBUTE_INT(style, FontStyle);
178
+ TTF_ATTRIBUTE_INT(outline, FontOutline);
179
+ TTF_ATTRIBUTE_INT(hinting, FontHinting);
180
+ TTF_ATTRIBUTE(kerning, FontKerning, RTEST, INT2BOOL);
181
+ TTF_ATTR_READER(height, Height, INT2FIX);
182
+ TTF_ATTR_READER(ascent, Ascent, INT2FIX);
183
+ TTF_ATTR_READER(descent, Descent, INT2FIX);
184
+ TTF_ATTR_READER(line_skip, LineSkip, INT2FIX);
185
+ TTF_ATTR_READER(num_faces, Faces, LONG2NUM);
186
+ TTF_ATTR_READER(face_is_fixed_width_p, FaceIsFixedWidth, INT2BOOL);
187
+ TTF_ATTR_READER(face_family_name, FaceFamilyName, utf8str_new_cstr);
188
+ TTF_ATTR_READER(face_style_name, FaceStyleName, utf8str_new_cstr);
189
+
190
+ /*
191
+ * @overload size_text(text)
192
+ * Calculate the size of rendered surface of **text** using the font.
193
+ *
194
+ * @param text [String] the string to size up
195
+ *
196
+ * @return [[Integer, Integer]] a pair of width and height of the rendered surface
197
+ *
198
+ * @raise [SDL2::Error] It is raised when an error occurs, such as a glyph ins the
199
+ * string not being found.
200
+ */
201
+ static VALUE TTF_size_text(VALUE self, VALUE text)
202
+ {
203
+ int w, h;
204
+ Check_Type(text, T_STRING);
205
+ text = rb_str_export_to_enc(text, rb_utf8_encoding());
206
+ HANDLE_TTF_ERROR(TTF_SizeUTF8(Get_TTF_Font(self), StringValueCStr(text), &w, &h));
207
+ return rb_ary_new3(2, INT2NUM(w), INT2NUM(h));
208
+ }
209
+
210
+ static SDL_Surface* render_solid(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
211
+ {
212
+ return TTF_RenderUTF8_Solid(font, text, fg);
213
+ }
214
+
215
+ static SDL_Surface* render_blended(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
216
+ {
217
+ return TTF_RenderUTF8_Blended(font, text, fg);
218
+ }
219
+
220
+ static VALUE render(SDL_Surface* (*renderer)(TTF_Font*, const char*, SDL_Color, SDL_Color),
221
+ VALUE font, VALUE text, VALUE fg, VALUE bg)
222
+ {
223
+ SDL_Surface* surface;
224
+ text = rb_str_export_to_enc(text, rb_utf8_encoding());
225
+ surface = renderer(Get_TTF_Font(font), StringValueCStr(text),
226
+ Color_to_SDL_Color(fg), Color_to_SDL_Color(bg));
227
+ if (!surface)
228
+ TTF_ERROR();
229
+
230
+ return Surface_new(surface);
231
+ }
232
+
233
+ /*
234
+ * @overload render_solid(text, fg)
235
+ * Render **text** using the font with fg color on a new surface, using
236
+ * *Solid* mode.
237
+ *
238
+ * Solid mode rendering is quick but dirty.
239
+ *
240
+ * @param text [String] the text to render
241
+ * @param fg [[Integer, Integer, Integer]]
242
+ * the color to render. An array of r, g, and b components.
243
+ *
244
+ * @return [SDL2::Surface]
245
+ *
246
+ * @raise [SDL2::Error] raised when the randering fails.
247
+ */
248
+ static VALUE TTF_render_solid(VALUE self, VALUE text, VALUE fg)
249
+ {
250
+ return render(render_solid, self, text, fg, Qnil);
251
+ }
252
+
253
+ /*
254
+ * @overload render_shaded(text, fg, bg)
255
+ * Render **text** using the font with fg color on a new surface, using
256
+ * *Shaded* mode.
257
+ *
258
+ * Shaded mode rendering is slow and nice, but with a solid box filled by
259
+ * the background color.
260
+ *
261
+ * @param text [String] the text to render
262
+ * @param fg [[Integer, Integer, Integer]]
263
+ * the color to render. An array of r, g, and b components.
264
+ * @param bg [[Integer, Integer, Integer]]
265
+ * the background color. An array of r, g, and b components.
266
+ *
267
+ * @return [SDL2::Surface]
268
+ *
269
+ * @raise [SDL2::Error] raised when the randering fails.
270
+ */
271
+ static VALUE TTF_render_shaded(VALUE self, VALUE text, VALUE fg, VALUE bg)
272
+ {
273
+ return render(TTF_RenderUTF8_Shaded, self, text, fg, bg);
274
+ }
275
+
276
+ /*
277
+ * @overload render_blended(text, fg)
278
+ * Render **text** using the font with fg color on a new surface, using
279
+ * *Blended* mode.
280
+ *
281
+ * Blended mode rendering is very slow but very nice.
282
+ * The rendered surface has an alpha channel,
283
+ *
284
+ * @param text [String] the text to render
285
+ * @param fg [[Integer, Integer, Integer]]
286
+ * the color to render. An array of r, g, and b components.
287
+ *
288
+ * @return [SDL2::Surface]
289
+ *
290
+ * @raise [SDL2::Error] raised when the randering fails.
291
+ */
292
+ static VALUE TTF_render_blended(VALUE self, VALUE text, VALUE fg)
293
+ {
294
+ return render(render_blended, self, text, fg, Qnil);
295
+ }
296
+
297
+ /*
298
+ * Document-module: SDL2::TTF::Style
299
+ *
300
+ * Constants represents font styles.
301
+ */
302
+
303
+ /* Document-module: SDL2::TTF::Hinting
304
+ *
305
+ * Constants represents font hinting for FreeType.
306
+ */
307
+ void rubysdl2_init_ttf(void)
308
+ {
309
+ cTTF = rb_define_class_under(mSDL2, "TTF", rb_cObject);
310
+ rb_undef_alloc_func(cTTF);
311
+
312
+ rb_define_singleton_method(cTTF, "init", TTF_s_init, 0);
313
+ rb_define_singleton_method(cTTF, "open", TTF_s_open, -1);
314
+ /* Return true if the font is destroyed by {#destroy}. */
315
+ rb_define_method(cTTF, "destroy?", TTF_destroy_p, 0);
316
+ rb_define_method(cTTF, "destroy", TTF_destroy, 0);
317
+
318
+ #define DEFINE_TTF_ATTRIBUTE(attr) do { \
319
+ rb_define_method(cTTF, #attr, TTF_##attr, 0); \
320
+ rb_define_method(cTTF, #attr "=", TTF_set_##attr, 1); \
321
+ } while (0)
322
+
323
+ DEFINE_TTF_ATTRIBUTE(style);
324
+ DEFINE_TTF_ATTRIBUTE(outline);
325
+ DEFINE_TTF_ATTRIBUTE(hinting);
326
+ DEFINE_TTF_ATTRIBUTE(kerning);
327
+
328
+ #define DEFINE_TTF_ATTR_READER(attr) \
329
+ rb_define_method(cTTF, #attr, TTF_##attr, 0)
330
+
331
+ DEFINE_TTF_ATTR_READER(height);
332
+ DEFINE_TTF_ATTR_READER(ascent);
333
+ DEFINE_TTF_ATTR_READER(descent);
334
+ DEFINE_TTF_ATTR_READER(line_skip);
335
+ DEFINE_TTF_ATTR_READER(num_faces);
336
+ /* Return true if the font is fixed width. */
337
+ rb_define_method(cTTF, "face_is_fixed_width?", TTF_face_is_fixed_width_p, 0);
338
+ DEFINE_TTF_ATTR_READER(face_family_name);
339
+ DEFINE_TTF_ATTR_READER(face_style_name);
340
+
341
+ rb_define_method(cTTF, "size_text", TTF_size_text, 1);
342
+ rb_define_method(cTTF, "render_solid", TTF_render_solid, 2);
343
+ rb_define_method(cTTF, "render_shaded", TTF_render_shaded, 3);
344
+ rb_define_method(cTTF, "render_blended", TTF_render_blended, 2);
345
+
346
+ mStyle = rb_define_module_under(cTTF, "Style");
347
+ /* define(`DEFINE_TTF_STYLE_CONST',`rb_define_const(mStyle, "$1", INT2NUM((TTF_STYLE_$1)))') */
348
+ /* normal style */
349
+ DEFINE_TTF_STYLE_CONST(NORMAL);
350
+ /* bold style */
351
+ DEFINE_TTF_STYLE_CONST(BOLD);
352
+ /* italic style */
353
+ DEFINE_TTF_STYLE_CONST(ITALIC);
354
+ /* underline style */
355
+ DEFINE_TTF_STYLE_CONST(UNDERLINE);
356
+ /* strikethrough style */
357
+ DEFINE_TTF_STYLE_CONST(STRIKETHROUGH);
358
+
359
+ mHinting = rb_define_module_under(cTTF, "Hinting");
360
+ /* define(`DEFINE_TTF_HINTING_CONST',`rb_define_const(mHinting, "$1", INT2NUM((TTF_HINTING_$1)))') */
361
+ /* normal hinting, default */
362
+ DEFINE_TTF_HINTING_CONST(NORMAL);
363
+ /* lighter hinting for non-monochrome modes */
364
+ DEFINE_TTF_HINTING_CONST(LIGHT);
365
+ /* strong hinting only used for monochrome output */
366
+ DEFINE_TTF_HINTING_CONST(MONO);
367
+ /* no hinting */
368
+ DEFINE_TTF_HINTING_CONST(NONE);
369
+ }
370
+
371
+ #else /* HAVE_SDL_TTF_H */
372
+ void rubysdl2_init_ttf(void)
373
+ {
374
+ }
375
+ #endif /* HAVE_SDL_TTF_H */
376
+