ruby-sdl2 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/README.md +5 -6
- data/Rakefile +13 -0
- data/clipboard.c +24 -0
- data/event.c +216 -5
- data/filesystem.c +27 -0
- data/gamecontroller.c +244 -30
- data/gamecontroller.c.m4 +408 -0
- data/gl.c.m4 +159 -9
- data/hint.c +51 -8
- data/key.c.m4 +6 -0
- data/lib/sdl2/version.rb +2 -2
- data/messagebox.c +40 -38
- data/mixer.c +138 -21
- data/mixer.c.m4 +1196 -0
- data/mouse.c +5 -5
- data/sample/message_box.rb +5 -5
- data/sample/primitives.rb +1 -1
- data/sample/test_video.rb +3 -3
- data/sample/testgl.rb +1 -1
- data/sample/testsprite.rb +5 -5
- data/ttf.c +193 -14
- data/ttf.c.m4 +375 -0
- data/video.c.m4 +465 -122
- metadata +6 -3
data/mouse.c
CHANGED
@@ -9,7 +9,7 @@ static VALUE cState;
|
|
9
9
|
/*
|
10
10
|
* Document-module: SDL2::Mouse
|
11
11
|
*
|
12
|
-
* This module
|
12
|
+
* This module has mouse input handling functions.
|
13
13
|
*
|
14
14
|
*/
|
15
15
|
|
@@ -27,7 +27,7 @@ static VALUE cState;
|
|
27
27
|
*
|
28
28
|
* You can get a mouse state with {SDL2::Mouse.state}.
|
29
29
|
*
|
30
|
-
*
|
30
|
+
* @!attribute [r] x
|
31
31
|
* the x coordinate of the mouse cursor.
|
32
32
|
*
|
33
33
|
* For {SDL2::Mouse.state}, this attribute means the x coordinate
|
@@ -37,7 +37,7 @@ static VALUE cState;
|
|
37
37
|
*
|
38
38
|
* @return [Integer]
|
39
39
|
*
|
40
|
-
*
|
40
|
+
* @!attribute [r] y
|
41
41
|
* the y coordinate of the mouse cursor
|
42
42
|
*
|
43
43
|
* For {SDL2::Mouse.state}, this attribute means the y coordinate
|
@@ -47,7 +47,7 @@ static VALUE cState;
|
|
47
47
|
*
|
48
48
|
* @return [Integer]
|
49
49
|
*
|
50
|
-
*
|
50
|
+
* @!attribute [r] button_bits
|
51
51
|
* button bits
|
52
52
|
* @return [Integer]
|
53
53
|
* @see #pressed?
|
@@ -122,7 +122,7 @@ static VALUE Mouse_s_relative_mode_p(VALUE self)
|
|
122
122
|
*
|
123
123
|
* @note This function will flush any pending mouse motion.
|
124
124
|
*
|
125
|
-
* @return [
|
125
|
+
* @return [Boolean]
|
126
126
|
* @see .relative_mode?
|
127
127
|
*/
|
128
128
|
static VALUE Mouse_s_set_relative_mode(VALUE self, VALUE enabled)
|
data/sample/message_box.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
require 'sdl2'
|
3
3
|
|
4
|
-
SDL2.
|
5
|
-
|
4
|
+
SDL2::MessageBox.show_simple_box(SDL2::MessageBox::ERROR, "Error",
|
5
|
+
"This is the error message box", nil)
|
6
6
|
|
7
|
-
button = SDL2.
|
7
|
+
button = SDL2::MessageBox.show(flags: SDL2::MessageBox::WARNING,
|
8
8
|
window: nil,
|
9
9
|
title: "警告ウインドウ",
|
10
10
|
message: "ここに警告文が出ます",
|
@@ -12,11 +12,11 @@ button = SDL2.show_message_box(flags: SDL2::MESSAGEBOX_WARNING,
|
|
12
12
|
id: 0,
|
13
13
|
text: "いいえ",
|
14
14
|
},
|
15
|
-
{flags: SDL2::
|
15
|
+
{flags: SDL2::MessageBox::BUTTON_RETURNKEY_DEFAULT,
|
16
16
|
id: 1,
|
17
17
|
text: "はい",
|
18
18
|
},
|
19
|
-
{flags: SDL2::
|
19
|
+
{flags: SDL2::MessageBox::BUTTON_ESCAPEKEY_DEFAULT,
|
20
20
|
id: 2,
|
21
21
|
text: "キャンセル",
|
22
22
|
},
|
data/sample/primitives.rb
CHANGED
@@ -14,7 +14,7 @@ renderer.draw_point(200, 200)
|
|
14
14
|
renderer.draw_color = [0, 255, 255]
|
15
15
|
renderer.draw_rect(SDL2::Rect.new(500, 20, 40, 60))
|
16
16
|
renderer.fill_rect(SDL2::Rect.new(20, 400, 60, 40))
|
17
|
-
renderer.draw_blend_mode = SDL2::
|
17
|
+
renderer.draw_blend_mode = SDL2::BlendMode::ADD
|
18
18
|
renderer.draw_color = [255, 0, 0]
|
19
19
|
renderer.draw_rect(SDL2::Rect.new(40, 420, 60, 40))
|
20
20
|
|
data/sample/test_video.rb
CHANGED
@@ -14,14 +14,14 @@ rect = SDL2::Rect.new(48, 128, 32, 32)
|
|
14
14
|
renderer.copy(texture, nil, rect)
|
15
15
|
renderer.copy_ex(texture, nil, SDL2::Rect[128, 256, 48, 48], 45, nil,
|
16
16
|
SDL2::Renderer::FLIP_NONE)
|
17
|
-
texture.blend_mode = SDL2::
|
17
|
+
texture.blend_mode = SDL2::BlendMode::ADD
|
18
18
|
renderer.copy(texture, nil, SDL2::Rect[128, 294, 48, 48])
|
19
19
|
|
20
|
-
texture.blend_mode = SDL2::
|
20
|
+
texture.blend_mode = SDL2::BlendMode::NONE
|
21
21
|
texture.color_mod = [128, 128, 255]
|
22
22
|
renderer.copy(texture, nil, SDL2::Rect[300, 420, 48, 48])
|
23
23
|
|
24
|
-
texture.blend_mode = SDL2::
|
24
|
+
texture.blend_mode = SDL2::BlendMode::NONE
|
25
25
|
texture.color_mod = [255, 255, 255]
|
26
26
|
|
27
27
|
if renderer.support_render_target?
|
data/sample/testgl.rb
CHANGED
@@ -15,7 +15,7 @@ SDL2::GL.set_attribute(SDL2::GL::BLUE_SIZE, 8)
|
|
15
15
|
SDL2::GL.set_attribute(SDL2::GL::ALPHA_SIZE, 8)
|
16
16
|
SDL2::GL.set_attribute(SDL2::GL::DOUBLEBUFFER, 1)
|
17
17
|
|
18
|
-
window = SDL2::Window.create("testgl", 0, 0, WINDOW_W, WINDOW_H, SDL2::Window::OPENGL)
|
18
|
+
window = SDL2::Window.create("testgl", 0, 0, WINDOW_W, WINDOW_H, SDL2::Window::Flags::OPENGL)
|
19
19
|
context = SDL2::GL::Context.create(window)
|
20
20
|
|
21
21
|
printf("OpenGL version %d.%d\n",
|
data/sample/testsprite.rb
CHANGED
@@ -147,7 +147,7 @@ class App
|
|
147
147
|
@spritepath = "icon.bmp"
|
148
148
|
@renderer_flags = 0
|
149
149
|
@num_sprites = 100
|
150
|
-
@blend_mode = SDL2::
|
150
|
+
@blend_mode = SDL2::BlendMode::BLEND
|
151
151
|
@cycle = Cycle.new(false, false, rand(255), rand(255), [1,-1].sample, [1,-1].sample)
|
152
152
|
@use_color_key = true
|
153
153
|
end
|
@@ -203,13 +203,13 @@ class App
|
|
203
203
|
opts.on("--blend MODE", "none|blend|add|mod"){|blend_mode|
|
204
204
|
@blend_mode = case blend_mode
|
205
205
|
when "none"
|
206
|
-
SDL2::
|
206
|
+
SDL2::BlendMode::NONE
|
207
207
|
when "blend"
|
208
|
-
SDL2::
|
208
|
+
SDL2::BlendMode::BLEND
|
209
209
|
when "add"
|
210
|
-
SDL2::
|
210
|
+
SDL2::BlendMode::ADD
|
211
211
|
when "mod"
|
212
|
-
SDL2::
|
212
|
+
SDL2::BlendMode::MOD
|
213
213
|
end
|
214
214
|
}
|
215
215
|
|
data/ttf.c
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
/* -*- mode: C -*- */
|
1
2
|
#ifdef HAVE_SDL_TTF_H
|
2
3
|
#include "rubysdl2_internal.h"
|
3
4
|
#include <SDL_ttf.h>
|
4
5
|
#include <ruby/encoding.h>
|
5
6
|
|
6
|
-
VALUE cTTF;
|
7
|
+
static VALUE cTTF;
|
8
|
+
static VALUE mStyle;
|
9
|
+
static VALUE mHinting;
|
7
10
|
|
8
11
|
#define TTF_ERROR() do { HANDLE_ERROR(SDL_SetError(TTF_GetError())); } while (0)
|
9
12
|
#define HANDLE_TTF_ERROR(code) \
|
@@ -49,12 +52,100 @@ static VALUE TTF_new(TTF_Font* font)
|
|
49
52
|
|
50
53
|
DEFINE_WRAPPER(TTF_Font, TTF, font, cTTF, "SDL2::TTF");
|
51
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
|
+
*/
|
52
127
|
static VALUE TTF_s_init(VALUE self)
|
53
128
|
{
|
54
129
|
HANDLE_TTF_ERROR(TTF_Init());
|
55
130
|
return Qnil;
|
56
131
|
}
|
57
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
|
+
*/
|
58
149
|
static VALUE TTF_s_open(int argc, VALUE* argv, VALUE self)
|
59
150
|
{
|
60
151
|
TTF_Font* font;
|
@@ -69,6 +160,11 @@ static VALUE TTF_s_open(int argc, VALUE* argv, VALUE self)
|
|
69
160
|
return TTF_new(font);
|
70
161
|
}
|
71
162
|
|
163
|
+
/*
|
164
|
+
* Destroy the font data and release memory.
|
165
|
+
*
|
166
|
+
* @return [nil]
|
167
|
+
*/
|
72
168
|
static VALUE TTF_destroy(VALUE self)
|
73
169
|
{
|
74
170
|
TTF* f = Get_TTF(self);
|
@@ -91,6 +187,17 @@ TTF_ATTR_READER(face_is_fixed_width_p, FaceIsFixedWidth, INT2BOOL);
|
|
91
187
|
TTF_ATTR_READER(face_family_name, FaceFamilyName, utf8str_new_cstr);
|
92
188
|
TTF_ATTR_READER(face_style_name, FaceStyleName, utf8str_new_cstr);
|
93
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
|
+
*/
|
94
201
|
static VALUE TTF_size_text(VALUE self, VALUE text)
|
95
202
|
{
|
96
203
|
int w, h;
|
@@ -122,21 +229,80 @@ static VALUE render(SDL_Surface* (*renderer)(TTF_Font*, const char*, SDL_Color,
|
|
122
229
|
return Surface_new(surface);
|
123
230
|
}
|
124
231
|
|
232
|
+
/*
|
233
|
+
* @overload render_solid(text, fg)
|
234
|
+
* Render **text** using the font with fg color on a new surface, using
|
235
|
+
* *Solid* mode.
|
236
|
+
*
|
237
|
+
* Solid mode rendering is quick but dirty.
|
238
|
+
*
|
239
|
+
* @param text [String] the text to render
|
240
|
+
* @param fg [[Integer, Integer, Integer]]
|
241
|
+
* the color to render. An array of r, g, and b components.
|
242
|
+
*
|
243
|
+
* @return [SDL2::Surface]
|
244
|
+
*
|
245
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
246
|
+
*/
|
125
247
|
static VALUE TTF_render_solid(VALUE self, VALUE text, VALUE fg)
|
126
248
|
{
|
127
249
|
return render(render_solid, self, text, fg, Qnil);
|
128
250
|
}
|
129
251
|
|
252
|
+
/*
|
253
|
+
* @overload render_shaded(text, fg, bg)
|
254
|
+
* Render **text** using the font with fg color on a new surface, using
|
255
|
+
* *Shaded* mode.
|
256
|
+
*
|
257
|
+
* Shaded mode rendering is slow and nice, but with a solid box filled by
|
258
|
+
* the background color.
|
259
|
+
*
|
260
|
+
* @param text [String] the text to render
|
261
|
+
* @param fg [[Integer, Integer, Integer]]
|
262
|
+
* the color to render. An array of r, g, and b components.
|
263
|
+
* @param bg [[Integer, Integer, Integer]]
|
264
|
+
* the background color. An array of r, g, and b components.
|
265
|
+
*
|
266
|
+
* @return [SDL2::Surface]
|
267
|
+
*
|
268
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
269
|
+
*/
|
130
270
|
static VALUE TTF_render_shaded(VALUE self, VALUE text, VALUE fg, VALUE bg)
|
131
271
|
{
|
132
272
|
return render(TTF_RenderUTF8_Shaded, self, text, fg, bg);
|
133
273
|
}
|
134
274
|
|
275
|
+
/*
|
276
|
+
* @overload render_blended(text, fg)
|
277
|
+
* Render **text** using the font with fg color on a new surface, using
|
278
|
+
* *Blended* mode.
|
279
|
+
*
|
280
|
+
* Blended mode rendering is very slow but very nice.
|
281
|
+
* The rendered surface has an alpha channel,
|
282
|
+
*
|
283
|
+
* @param text [String] the text to render
|
284
|
+
* @param fg [[Integer, Integer, Integer]]
|
285
|
+
* the color to render. An array of r, g, and b components.
|
286
|
+
*
|
287
|
+
* @return [SDL2::Surface]
|
288
|
+
*
|
289
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
290
|
+
*/
|
135
291
|
static VALUE TTF_render_blended(VALUE self, VALUE text, VALUE fg)
|
136
292
|
{
|
137
293
|
return render(render_blended, self, text, fg, Qnil);
|
138
294
|
}
|
139
295
|
|
296
|
+
/*
|
297
|
+
* Document-module: SDL2::TTF::Style
|
298
|
+
*
|
299
|
+
* Constants represents font styles.
|
300
|
+
*/
|
301
|
+
|
302
|
+
/* Document-module: SDL2::TTF::Hinting
|
303
|
+
*
|
304
|
+
* Constants represents font hinting for FreeType.
|
305
|
+
*/
|
140
306
|
void rubysdl2_init_ttf(void)
|
141
307
|
{
|
142
308
|
cTTF = rb_define_class_under(mSDL2, "TTF", rb_cObject);
|
@@ -144,6 +310,7 @@ void rubysdl2_init_ttf(void)
|
|
144
310
|
|
145
311
|
rb_define_singleton_method(cTTF, "init", TTF_s_init, 0);
|
146
312
|
rb_define_singleton_method(cTTF, "open", TTF_s_open, -1);
|
313
|
+
/* Return true if the font is destroyed by {#destroy}. */
|
147
314
|
rb_define_method(cTTF, "destroy?", TTF_destroy_p, 0);
|
148
315
|
rb_define_method(cTTF, "destroy", TTF_destroy, 0);
|
149
316
|
|
@@ -156,7 +323,7 @@ void rubysdl2_init_ttf(void)
|
|
156
323
|
DEFINE_TTF_ATTRIBUTE(outline);
|
157
324
|
DEFINE_TTF_ATTRIBUTE(hinting);
|
158
325
|
DEFINE_TTF_ATTRIBUTE(kerning);
|
159
|
-
|
326
|
+
|
160
327
|
#define DEFINE_TTF_ATTR_READER(attr) \
|
161
328
|
rb_define_method(cTTF, #attr, TTF_##attr, 0)
|
162
329
|
|
@@ -165,6 +332,7 @@ void rubysdl2_init_ttf(void)
|
|
165
332
|
DEFINE_TTF_ATTR_READER(descent);
|
166
333
|
DEFINE_TTF_ATTR_READER(line_skip);
|
167
334
|
DEFINE_TTF_ATTR_READER(num_faces);
|
335
|
+
/* Return true if the font is fixed width. */
|
168
336
|
rb_define_method(cTTF, "face_is_fixed_width?", TTF_face_is_fixed_width_p, 0);
|
169
337
|
DEFINE_TTF_ATTR_READER(face_family_name);
|
170
338
|
DEFINE_TTF_ATTR_READER(face_style_name);
|
@@ -174,18 +342,29 @@ void rubysdl2_init_ttf(void)
|
|
174
342
|
rb_define_method(cTTF, "render_shaded", TTF_render_shaded, 3);
|
175
343
|
rb_define_method(cTTF, "render_blended", TTF_render_blended, 2);
|
176
344
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
345
|
+
mStyle = rb_define_module_under(cTTF, "Style");
|
346
|
+
/* */
|
347
|
+
/* normal style */
|
348
|
+
rb_define_const(mStyle, "NORMAL", INT2NUM((TTF_STYLE_NORMAL)));
|
349
|
+
/* bold style */
|
350
|
+
rb_define_const(mStyle, "BOLD", INT2NUM((TTF_STYLE_BOLD)));
|
351
|
+
/* italic style */
|
352
|
+
rb_define_const(mStyle, "ITALIC", INT2NUM((TTF_STYLE_ITALIC)));
|
353
|
+
/* underline style */
|
354
|
+
rb_define_const(mStyle, "UNDERLINE", INT2NUM((TTF_STYLE_UNDERLINE)));
|
355
|
+
/* strikethrough style */
|
356
|
+
rb_define_const(mStyle, "STRIKETHROUGH", INT2NUM((TTF_STYLE_STRIKETHROUGH)));
|
357
|
+
|
358
|
+
mHinting = rb_define_module_under(cTTF, "Hinting");
|
359
|
+
/* */
|
360
|
+
/* normal hinting, default */
|
361
|
+
rb_define_const(mHinting, "NORMAL", INT2NUM((TTF_HINTING_NORMAL)));
|
362
|
+
/* lighter hinting for non-monochrome modes */
|
363
|
+
rb_define_const(mHinting, "LIGHT", INT2NUM((TTF_HINTING_LIGHT)));
|
364
|
+
/* strong hinting only used for monochrome output */
|
365
|
+
rb_define_const(mHinting, "MONO", INT2NUM((TTF_HINTING_MONO)));
|
366
|
+
/* no hinting */
|
367
|
+
rb_define_const(mHinting, "NONE", INT2NUM((TTF_HINTING_NONE)));
|
189
368
|
}
|
190
369
|
|
191
370
|
#else /* HAVE_SDL_TTF_H */
|
data/ttf.c.m4
ADDED
@@ -0,0 +1,375 @@
|
|
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(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
|
+
text = rb_str_export_to_enc(text, rb_utf8_encoding());
|
205
|
+
HANDLE_TTF_ERROR(TTF_SizeUTF8(Get_TTF_Font(self), StringValueCStr(text), &w, &h));
|
206
|
+
return rb_ary_new3(2, INT2NUM(w), INT2NUM(h));
|
207
|
+
}
|
208
|
+
|
209
|
+
static SDL_Surface* render_solid(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
|
210
|
+
{
|
211
|
+
return TTF_RenderUTF8_Solid(font, text, fg);
|
212
|
+
}
|
213
|
+
|
214
|
+
static SDL_Surface* render_blended(TTF_Font* font, const char* text, SDL_Color fg, SDL_Color bg)
|
215
|
+
{
|
216
|
+
return TTF_RenderUTF8_Blended(font, text, fg);
|
217
|
+
}
|
218
|
+
|
219
|
+
static VALUE render(SDL_Surface* (*renderer)(TTF_Font*, const char*, SDL_Color, SDL_Color),
|
220
|
+
VALUE font, VALUE text, VALUE fg, VALUE bg)
|
221
|
+
{
|
222
|
+
SDL_Surface* surface;
|
223
|
+
text = rb_str_export_to_enc(text, rb_utf8_encoding());
|
224
|
+
surface = renderer(Get_TTF_Font(font), StringValueCStr(text),
|
225
|
+
Array_to_SDL_Color(fg), Array_to_SDL_Color(bg));
|
226
|
+
if (!surface)
|
227
|
+
TTF_ERROR();
|
228
|
+
|
229
|
+
return Surface_new(surface);
|
230
|
+
}
|
231
|
+
|
232
|
+
/*
|
233
|
+
* @overload render_solid(text, fg)
|
234
|
+
* Render **text** using the font with fg color on a new surface, using
|
235
|
+
* *Solid* mode.
|
236
|
+
*
|
237
|
+
* Solid mode rendering is quick but dirty.
|
238
|
+
*
|
239
|
+
* @param text [String] the text to render
|
240
|
+
* @param fg [[Integer, Integer, Integer]]
|
241
|
+
* the color to render. An array of r, g, and b components.
|
242
|
+
*
|
243
|
+
* @return [SDL2::Surface]
|
244
|
+
*
|
245
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
246
|
+
*/
|
247
|
+
static VALUE TTF_render_solid(VALUE self, VALUE text, VALUE fg)
|
248
|
+
{
|
249
|
+
return render(render_solid, self, text, fg, Qnil);
|
250
|
+
}
|
251
|
+
|
252
|
+
/*
|
253
|
+
* @overload render_shaded(text, fg, bg)
|
254
|
+
* Render **text** using the font with fg color on a new surface, using
|
255
|
+
* *Shaded* mode.
|
256
|
+
*
|
257
|
+
* Shaded mode rendering is slow and nice, but with a solid box filled by
|
258
|
+
* the background color.
|
259
|
+
*
|
260
|
+
* @param text [String] the text to render
|
261
|
+
* @param fg [[Integer, Integer, Integer]]
|
262
|
+
* the color to render. An array of r, g, and b components.
|
263
|
+
* @param bg [[Integer, Integer, Integer]]
|
264
|
+
* the background color. An array of r, g, and b components.
|
265
|
+
*
|
266
|
+
* @return [SDL2::Surface]
|
267
|
+
*
|
268
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
269
|
+
*/
|
270
|
+
static VALUE TTF_render_shaded(VALUE self, VALUE text, VALUE fg, VALUE bg)
|
271
|
+
{
|
272
|
+
return render(TTF_RenderUTF8_Shaded, self, text, fg, bg);
|
273
|
+
}
|
274
|
+
|
275
|
+
/*
|
276
|
+
* @overload render_blended(text, fg)
|
277
|
+
* Render **text** using the font with fg color on a new surface, using
|
278
|
+
* *Blended* mode.
|
279
|
+
*
|
280
|
+
* Blended mode rendering is very slow but very nice.
|
281
|
+
* The rendered surface has an alpha channel,
|
282
|
+
*
|
283
|
+
* @param text [String] the text to render
|
284
|
+
* @param fg [[Integer, Integer, Integer]]
|
285
|
+
* the color to render. An array of r, g, and b components.
|
286
|
+
*
|
287
|
+
* @return [SDL2::Surface]
|
288
|
+
*
|
289
|
+
* @raise [SDL2::Error] raised when the randering fails.
|
290
|
+
*/
|
291
|
+
static VALUE TTF_render_blended(VALUE self, VALUE text, VALUE fg)
|
292
|
+
{
|
293
|
+
return render(render_blended, self, text, fg, Qnil);
|
294
|
+
}
|
295
|
+
|
296
|
+
/*
|
297
|
+
* Document-module: SDL2::TTF::Style
|
298
|
+
*
|
299
|
+
* Constants represents font styles.
|
300
|
+
*/
|
301
|
+
|
302
|
+
/* Document-module: SDL2::TTF::Hinting
|
303
|
+
*
|
304
|
+
* Constants represents font hinting for FreeType.
|
305
|
+
*/
|
306
|
+
void rubysdl2_init_ttf(void)
|
307
|
+
{
|
308
|
+
cTTF = rb_define_class_under(mSDL2, "TTF", rb_cObject);
|
309
|
+
rb_undef_alloc_func(cTTF);
|
310
|
+
|
311
|
+
rb_define_singleton_method(cTTF, "init", TTF_s_init, 0);
|
312
|
+
rb_define_singleton_method(cTTF, "open", TTF_s_open, -1);
|
313
|
+
/* Return true if the font is destroyed by {#destroy}. */
|
314
|
+
rb_define_method(cTTF, "destroy?", TTF_destroy_p, 0);
|
315
|
+
rb_define_method(cTTF, "destroy", TTF_destroy, 0);
|
316
|
+
|
317
|
+
#define DEFINE_TTF_ATTRIBUTE(attr) do { \
|
318
|
+
rb_define_method(cTTF, #attr, TTF_##attr, 0); \
|
319
|
+
rb_define_method(cTTF, #attr "=", TTF_set_##attr, 1); \
|
320
|
+
} while (0)
|
321
|
+
|
322
|
+
DEFINE_TTF_ATTRIBUTE(style);
|
323
|
+
DEFINE_TTF_ATTRIBUTE(outline);
|
324
|
+
DEFINE_TTF_ATTRIBUTE(hinting);
|
325
|
+
DEFINE_TTF_ATTRIBUTE(kerning);
|
326
|
+
|
327
|
+
#define DEFINE_TTF_ATTR_READER(attr) \
|
328
|
+
rb_define_method(cTTF, #attr, TTF_##attr, 0)
|
329
|
+
|
330
|
+
DEFINE_TTF_ATTR_READER(height);
|
331
|
+
DEFINE_TTF_ATTR_READER(ascent);
|
332
|
+
DEFINE_TTF_ATTR_READER(descent);
|
333
|
+
DEFINE_TTF_ATTR_READER(line_skip);
|
334
|
+
DEFINE_TTF_ATTR_READER(num_faces);
|
335
|
+
/* Return true if the font is fixed width. */
|
336
|
+
rb_define_method(cTTF, "face_is_fixed_width?", TTF_face_is_fixed_width_p, 0);
|
337
|
+
DEFINE_TTF_ATTR_READER(face_family_name);
|
338
|
+
DEFINE_TTF_ATTR_READER(face_style_name);
|
339
|
+
|
340
|
+
rb_define_method(cTTF, "size_text", TTF_size_text, 1);
|
341
|
+
rb_define_method(cTTF, "render_solid", TTF_render_solid, 2);
|
342
|
+
rb_define_method(cTTF, "render_shaded", TTF_render_shaded, 3);
|
343
|
+
rb_define_method(cTTF, "render_blended", TTF_render_blended, 2);
|
344
|
+
|
345
|
+
mStyle = rb_define_module_under(cTTF, "Style");
|
346
|
+
/* define(`DEFINE_TTF_STYLE_CONST',`rb_define_const(mStyle, "$1", INT2NUM((TTF_STYLE_$1)))') */
|
347
|
+
/* normal style */
|
348
|
+
DEFINE_TTF_STYLE_CONST(NORMAL);
|
349
|
+
/* bold style */
|
350
|
+
DEFINE_TTF_STYLE_CONST(BOLD);
|
351
|
+
/* italic style */
|
352
|
+
DEFINE_TTF_STYLE_CONST(ITALIC);
|
353
|
+
/* underline style */
|
354
|
+
DEFINE_TTF_STYLE_CONST(UNDERLINE);
|
355
|
+
/* strikethrough style */
|
356
|
+
DEFINE_TTF_STYLE_CONST(STRIKETHROUGH);
|
357
|
+
|
358
|
+
mHinting = rb_define_module_under(cTTF, "Hinting");
|
359
|
+
/* define(`DEFINE_TTF_HINTING_CONST',`rb_define_const(mHinting, "$1", INT2NUM((TTF_HINTING_$1)))') */
|
360
|
+
/* normal hinting, default */
|
361
|
+
DEFINE_TTF_HINTING_CONST(NORMAL);
|
362
|
+
/* lighter hinting for non-monochrome modes */
|
363
|
+
DEFINE_TTF_HINTING_CONST(LIGHT);
|
364
|
+
/* strong hinting only used for monochrome output */
|
365
|
+
DEFINE_TTF_HINTING_CONST(MONO);
|
366
|
+
/* no hinting */
|
367
|
+
DEFINE_TTF_HINTING_CONST(NONE);
|
368
|
+
}
|
369
|
+
|
370
|
+
#else /* HAVE_SDL_TTF_H */
|
371
|
+
void rubysdl2_init_ttf(void)
|
372
|
+
{
|
373
|
+
}
|
374
|
+
#endif /* HAVE_SDL_TTF_H */
|
375
|
+
|