graphics 1.0.0b6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/History.rdoc +88 -0
- data/Manifest.txt +2 -0
- data/Rakefile +7 -8
- data/examples/boid.rb +44 -63
- data/examples/bounce.rb +4 -4
- data/examples/canvas.rb +17 -16
- data/examples/collision.rb +1 -1
- data/examples/demo.rb +1 -1
- data/examples/editor.rb +1 -1
- data/examples/fluid.rb +6 -6
- data/examples/fluid2.rb +22 -9
- data/examples/gol.rb +1 -1
- data/examples/gol2.rb +14 -22
- data/examples/math.rb +1 -1
- data/examples/pi_polygon.rb +8 -3
- data/examples/radar.rb +3 -3
- data/examples/rainbow_fluid.rb +4 -3
- data/examples/tank.rb +7 -3
- data/examples/tank2.rb +3 -3
- data/examples/targeting.rb +3 -3
- data/examples/vants.rb +13 -4
- data/examples/walker.rb +1 -1
- data/examples/walker2.rb +1 -1
- data/examples/zombies.rb +13 -7
- data/ext/sdl/extconf.rb +12 -20
- data/ext/sdl/sdl.c +619 -360
- data/ext/sdl/sge/Makefile +36 -11
- data/ext/sdl/sge/Makefile.conf +15 -9
- data/ext/sdl/sge/sge_bm_text.cpp +7 -6
- data/ext/sdl/sge/sge_collision.cpp +3 -1
- data/ext/sdl/sge/sge_config.h +0 -2
- data/ext/sdl/sge/sge_internal.h +0 -8
- data/ext/sdl/sge/sge_primitives.cpp +0 -11
- data/ext/sdl/sge/sge_primitives.h +0 -3
- data/ext/sdl/sge/sge_rotation.cpp +1 -1
- data/ext/sdl/sge/sge_shape.cpp +18 -9
- data/ext/sdl/sge/sge_surface.cpp +10 -4
- data/ext/sdl/sge/sge_textpp.cpp +17 -13
- data/graphics_setup.sh +43 -13
- data/lib/graphics.rb +1 -1
- data/lib/graphics/body.rb +8 -0
- data/lib/graphics/decorators.rb +15 -3
- data/lib/graphics/extensions.rb +1 -1
- data/lib/graphics/rainbows.rb +17 -25
- data/lib/graphics/simulation.rb +265 -106
- data/lib/graphics/v.rb +8 -1
- data/resources/sounds/attribution.txt +2 -0
- data/resources/sounds/bullet.wav +0 -0
- data/test/test_graphics.rb +232 -107
- metadata +37 -43
- metadata.gz.sig +1 -2
data/examples/walker.rb
CHANGED
data/examples/walker2.rb
CHANGED
data/examples/zombies.rb
CHANGED
@@ -74,7 +74,7 @@ class Person < Entity
|
|
74
74
|
|
75
75
|
INFECT_STEPS = 50.0 # must be a float
|
76
76
|
|
77
|
-
NORMAL_COLOR = :
|
77
|
+
NORMAL_COLOR = :black
|
78
78
|
FREAKD_COLOR = :yellow
|
79
79
|
|
80
80
|
attr_accessor :state, :infect, :speed
|
@@ -171,7 +171,7 @@ end
|
|
171
171
|
|
172
172
|
class Hunter < Person
|
173
173
|
COUNT = 6
|
174
|
-
COLOR = :
|
174
|
+
COLOR = :red
|
175
175
|
|
176
176
|
def color
|
177
177
|
if @infect then
|
@@ -187,7 +187,7 @@ class Hunter < Person
|
|
187
187
|
limit_bounds
|
188
188
|
|
189
189
|
baddies = sim.zombie + sim.person.select(&:infect)
|
190
|
-
nearest = baddies.
|
190
|
+
nearest = baddies.min_by { |z| self.distance_from_squared z }
|
191
191
|
|
192
192
|
return unless nearest
|
193
193
|
|
@@ -195,10 +195,10 @@ class Hunter < Person
|
|
195
195
|
if Person === nearest then
|
196
196
|
nearest.kill
|
197
197
|
else
|
198
|
-
if
|
198
|
+
if 9 =~ 10 then # Hunter has a 1 in 10 chance of dying
|
199
199
|
nearest.kill
|
200
200
|
else
|
201
|
-
self.state
|
201
|
+
self.state = INFECT
|
202
202
|
self.infect = INFECT_STEPS.to_i
|
203
203
|
end
|
204
204
|
end
|
@@ -217,7 +217,7 @@ end
|
|
217
217
|
|
218
218
|
class Zombie < Entity
|
219
219
|
COUNT = 5
|
220
|
-
ZOMBIE_COLOR = :
|
220
|
+
ZOMBIE_COLOR = :green50
|
221
221
|
|
222
222
|
def self.from_person p, sim
|
223
223
|
z = new sim
|
@@ -261,13 +261,15 @@ class Zombie < Entity
|
|
261
261
|
end
|
262
262
|
|
263
263
|
class ZombieGame < Graphics::Simulation
|
264
|
+
include WhiteBackground
|
265
|
+
|
264
266
|
attr_accessor :person, :zombie
|
265
267
|
attr_accessor :part_p, :part_z
|
266
268
|
attr_accessor :scale, :partitions
|
267
269
|
attr_accessor :start
|
268
270
|
|
269
271
|
def initialize
|
270
|
-
super 512, 512,
|
272
|
+
super 512, 512, "Zombie Epidemic Simulator"
|
271
273
|
self.scale = 2
|
272
274
|
self.partitions = 64
|
273
275
|
|
@@ -374,6 +376,10 @@ class ZombieGame < Graphics::Simulation
|
|
374
376
|
def max
|
375
377
|
@max ||= w / scale
|
376
378
|
end
|
379
|
+
|
380
|
+
def inspect
|
381
|
+
"ZombieGame"
|
382
|
+
end
|
377
383
|
end
|
378
384
|
|
379
385
|
ZombieGame.new.run
|
data/ext/sdl/extconf.rb
CHANGED
@@ -5,27 +5,19 @@ $INCFLAGS << " -I$(srcdir)/sge"
|
|
5
5
|
|
6
6
|
$srcs = Dir.glob("#{$srcdir}/{,sge/}*.c{,pp}")
|
7
7
|
|
8
|
-
sdl_config = with_config "
|
9
|
-
$CPPFLAGS +=
|
8
|
+
sdl_config = with_config "sdl2-config", "sdl2-config"
|
9
|
+
$CPPFLAGS += `#{sdl_config} --cflags`.chomp
|
10
|
+
if ENV["STRICT"] then # this isn't right because it is at Makefile creation time
|
11
|
+
$CPPFLAGS += " "
|
12
|
+
$CPPFLAGS += %w[-Werror -Wall
|
13
|
+
-Wimplicit-function-declaration
|
14
|
+
-Wundefined-internal].join(" ")
|
15
|
+
end
|
10
16
|
$LOCAL_LIBS += " " + `#{sdl_config} --libs`.chomp
|
11
17
|
|
12
|
-
have_library("
|
13
|
-
have_library("
|
14
|
-
have_library("
|
15
|
-
|
16
|
-
|
17
|
-
# have_func "TTF_OpenFontIndex"
|
18
|
-
# have_func "TTF_FontFaces"
|
19
|
-
# have_func "TTF_FontFaceIsFixedWidth"
|
20
|
-
# have_func "TTF_FontFaceFamilyName"
|
21
|
-
# have_func "TTF_FontFaceStyleName"
|
22
|
-
# have_func "Mix_LoadMUS_RW"
|
23
|
-
# have_func "rb_thread_blocking_region"
|
24
|
-
# have_func "rb_thread_call_without_gvl" if have_header "ruby/thread.h"
|
25
|
-
|
26
|
-
# if have_func("rb_enc_str_new") && have_func("rb_str_export_to_enc")
|
27
|
-
# $CPPFLAGS += " -D ENABLE_M17N"
|
28
|
-
# $CPPFLAGS += " -D ENABLE_M17N_FILESYSTEM" if enable_config "m17n-filesystem", false
|
29
|
-
# end
|
18
|
+
have_library("SDL2_mixer", "Mix_OpenAudio") or abort "Need sdl2_mixer"
|
19
|
+
have_library("SDL2_image", "IMG_Load") or abort "Need sdl2_image"
|
20
|
+
have_library("SDL2_ttf", "TTF_Init") or abort "Need sdl2_ttf"
|
21
|
+
have_library("SDL2_gfx", "hlineColor") or abort "Need sdl2_gfx"
|
30
22
|
|
31
23
|
create_makefile "sdl/sdl"
|
data/ext/sdl/sdl.c
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
#include <ruby/intern.h>
|
4
4
|
#include <SDL_ttf.h>
|
5
5
|
#include <SDL_image.h>
|
6
|
-
#include <
|
6
|
+
#include <SDL2_gfxPrimitives.h>
|
7
|
+
#include <SDL2_rotozoom.h>
|
8
|
+
#include <sge/sge_collision.h>
|
7
9
|
#include <SDL_mixer.h>
|
8
10
|
|
9
11
|
// https://github.com/google/protobuf/blob/master/ruby/ext/google/protobuf_c/defs.c
|
@@ -33,6 +35,12 @@
|
|
33
35
|
#define DEFINE_SELF(type, var, rb_var) \
|
34
36
|
SDL_##type* var = ruby_to_##type(rb_var)
|
35
37
|
|
38
|
+
#define DEFINE_SELF0(type, var, rb_var) \
|
39
|
+
SDL_##type* var = RTEST(rb_var) ? ruby_to_##type(rb_var) : NULL;
|
40
|
+
|
41
|
+
#define SET_SELF(type, var, rb_var) \
|
42
|
+
var = ruby_to_##type(rb_var)
|
43
|
+
|
36
44
|
#define NUM2SINT32(n) (Sint32)NUM2INT(n)
|
37
45
|
#define NUM2SINT16(n) (Sint16)NUM2INT(n)
|
38
46
|
#define NUM2UINT32(n) (Uint32)NUM2UINT(n)
|
@@ -45,31 +53,15 @@
|
|
45
53
|
|
46
54
|
#define UNUSED(x) (void)(x)
|
47
55
|
|
48
|
-
#define
|
56
|
+
#define FAILURE(s) rb_raise(eSDLError, "%s failed: %s", (s), SDL_GetError())
|
57
|
+
#define AUDIO_FAILURE(s) rb_raise(eSDLError, "%s failed: %s", (s), Mix_GetError())
|
58
|
+
#define TTF_FAILURE(s) rb_raise(eSDLError, "%s failed: %s", (s), TTF_GetError())
|
49
59
|
|
50
|
-
#define
|
51
|
-
#define AUDIO_FAILURE(s) rb_raise(eSDLError, "%s failed: %s", (s), Mix_GetError());
|
52
|
-
|
53
|
-
#ifdef rb_intern
|
54
|
-
#undef rb_intern // HACK -- clang warns about recursive macros
|
55
|
-
#endif
|
60
|
+
#define SHOULD_BLEND(a) (a) != 0xff
|
56
61
|
|
57
62
|
#define DEFINE_ID(name) static ID id_iv_##name
|
58
63
|
#define INIT_ID(name) id_iv_##name = rb_intern("@"#name)
|
59
64
|
|
60
|
-
#define DEFINE_DRAW6(type, name, func) \
|
61
|
-
static VALUE Surface_##name(VALUE s, VALUE x, VALUE y, VALUE w, VALUE h, VALUE c) { \
|
62
|
-
return _draw_func_##type(&func, s, x, y, w, h, c); \
|
63
|
-
}
|
64
|
-
|
65
|
-
#define DEFINE_SXYRC(name, func) \
|
66
|
-
static VALUE Surface_##name(VALUE s, VALUE x, VALUE y, VALUE r, VALUE c) { \
|
67
|
-
return _draw_func_sxyrc(&func, s, x, y, r, c); \
|
68
|
-
}
|
69
|
-
|
70
|
-
#define DEFINE_SXYXYC(name, func) DEFINE_DRAW6(sxyxyc, name, func)
|
71
|
-
#define DEFINE_SXYWHC(name, func) DEFINE_DRAW6(sxywhc, name, func)
|
72
|
-
|
73
65
|
static VALUE cEvent;
|
74
66
|
static VALUE cEventKeydown;
|
75
67
|
static VALUE cEventKeyup;
|
@@ -79,22 +71,27 @@ static VALUE cEventMouseup;
|
|
79
71
|
static VALUE cEventQuit;
|
80
72
|
static VALUE cScreen;
|
81
73
|
static VALUE eSDLError;
|
82
|
-
static VALUE eSDLMem;
|
83
74
|
static VALUE mKey;
|
84
75
|
static VALUE mSDL;
|
85
|
-
static VALUE mWM;
|
86
76
|
static VALUE mMouse;
|
87
77
|
|
88
78
|
typedef TTF_Font SDL_TTFFont;
|
89
79
|
typedef Mix_Chunk SDL_Audio;
|
90
80
|
typedef sge_cdata SDL_CollisionMap;
|
91
81
|
|
82
|
+
static ID id_H;
|
83
|
+
static ID id_W;
|
84
|
+
|
85
|
+
DEFINE_ID(surface);
|
86
|
+
DEFINE_ID(format);
|
87
|
+
DEFINE_ID(renderer);
|
88
|
+
DEFINE_ID(window);
|
89
|
+
DEFINE_ID(texture);
|
92
90
|
DEFINE_ID(button);
|
93
91
|
DEFINE_ID(mod);
|
94
92
|
DEFINE_ID(press);
|
95
93
|
DEFINE_ID(state);
|
96
94
|
DEFINE_ID(sym);
|
97
|
-
DEFINE_ID(unicode);
|
98
95
|
DEFINE_ID(x);
|
99
96
|
DEFINE_ID(xrel);
|
100
97
|
DEFINE_ID(y);
|
@@ -105,42 +102,30 @@ DEFINE_CLASS(Surface, "SDL::Surface")
|
|
105
102
|
DEFINE_CLASS(CollisionMap, "SDL::CollisionMap")
|
106
103
|
DEFINE_CLASS(PixelFormat, "SDL::PixelFormat")
|
107
104
|
DEFINE_CLASS_0(TTFFont, "SDL::TTFFont")
|
105
|
+
DEFINE_CLASS_0(Renderer, "SDL::Renderer") // TODO: I kinda want these hidden
|
106
|
+
DEFINE_CLASS_0(Window, "SDL::Window") // TODO: I kinda want these hidden
|
107
|
+
DEFINE_CLASS_0(Texture, "SDL::Texture") // TODO: I kinda want these hidden
|
108
108
|
|
109
|
+
#define SDL_NUMEVENTS 0xFFFF // HACK
|
109
110
|
typedef VALUE (*event_creator)(SDL_Event *);
|
110
111
|
static event_creator event_creators[SDL_NUMEVENTS];
|
111
112
|
|
112
113
|
static int is_quit = 0;
|
114
|
+
static int key_state_len = 0;
|
113
115
|
static Uint8* key_state = NULL;
|
114
|
-
static
|
116
|
+
static SDL_Keymod mod_state;
|
115
117
|
|
116
118
|
void Init_sdl(void);
|
117
119
|
|
118
120
|
//// Misc / Utility functions:
|
119
121
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
if (rb_obj_is_kind_of(color, rb_cArray)) {
|
124
|
-
switch (RARRAY_LEN(color)) {
|
125
|
-
case 3:
|
126
|
-
return SDL_MapRGB(format,
|
127
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 0)),
|
128
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 1)),
|
129
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 2)));
|
130
|
-
case 4:
|
131
|
-
return SDL_MapRGBA(format,
|
132
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 0)),
|
133
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 1)),
|
134
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 2)),
|
135
|
-
(Uint8)FIX2UINT(rb_ary_entry(color, 3)));
|
136
|
-
default:
|
137
|
-
rb_raise(rb_eArgError, "type mismatch:color array needs 3 or 4 elements");
|
138
|
-
}
|
139
|
-
} else {
|
140
|
-
return NUM2UINT(color);
|
141
|
-
}
|
122
|
+
static void rb_const_reset(VALUE mod, ID id, VALUE val) { // avoids warnings
|
123
|
+
rb_const_remove(mod, id);
|
124
|
+
rb_const_set(mod, id, val);
|
142
125
|
}
|
143
126
|
|
127
|
+
#define VALUE2COLOR(c) NUM2UINT(c)
|
128
|
+
|
144
129
|
//// SDL methods:
|
145
130
|
|
146
131
|
static VALUE sdl_s_init(VALUE mod, VALUE flags) {
|
@@ -151,6 +136,15 @@ static VALUE sdl_s_init(VALUE mod, VALUE flags) {
|
|
151
136
|
if (TTF_Init())
|
152
137
|
rb_raise(eSDLError, "TTF_Init error: %s", TTF_GetError());
|
153
138
|
|
139
|
+
SDL_Rect r;
|
140
|
+
if (SDL_GetDisplayBounds(0, &r) != 0) {
|
141
|
+
rb_raise(eSDLError, "Failure calling SDL_GetDisplayBounds()");
|
142
|
+
return 1;
|
143
|
+
}
|
144
|
+
|
145
|
+
rb_const_reset(cScreen, id_W, UINT2NUM(r.w));
|
146
|
+
rb_const_reset(cScreen, id_H, UINT2NUM(r.h));
|
147
|
+
|
154
148
|
return Qnil;
|
155
149
|
}
|
156
150
|
|
@@ -205,7 +199,7 @@ static VALUE Audio_s_load(VALUE self, VALUE path) {
|
|
205
199
|
Mix_Chunk *chunk = Mix_LoadWAV(RSTRING_PTR(path));
|
206
200
|
|
207
201
|
if (!chunk)
|
208
|
-
|
202
|
+
AUDIO_FAILURE("Audio.load");
|
209
203
|
|
210
204
|
return TypedData_Wrap_Struct(cAudio, &_Audio_type, chunk);
|
211
205
|
}
|
@@ -225,14 +219,7 @@ static void _CollisionMap_free(void* p) {
|
|
225
219
|
if (is_quit) return;
|
226
220
|
if (!p) return;
|
227
221
|
|
228
|
-
|
229
|
-
|
230
|
-
if (format->palette) {
|
231
|
-
free(format->palette->colors);
|
232
|
-
free(format->palette);
|
233
|
-
}
|
234
|
-
|
235
|
-
free(format);
|
222
|
+
sge_destroy_cmap(p);
|
236
223
|
}
|
237
224
|
|
238
225
|
static void _CollisionMap_mark(void* p) {
|
@@ -279,7 +266,6 @@ static VALUE __new_key_event(VALUE klass, SDL_Event *event) {
|
|
279
266
|
rb_ivar_set(obj, id_iv_press, INT2BOOL(event->key.state == SDL_PRESSED)); // TODO: nuke?
|
280
267
|
rb_ivar_set(obj, id_iv_sym, INT2FIX(event->key.keysym.sym));
|
281
268
|
rb_ivar_set(obj, id_iv_mod, UINT2NUM(event->key.keysym.mod));
|
282
|
-
rb_ivar_set(obj, id_iv_unicode, UINT2NUM(event->key.keysym.unicode));
|
283
269
|
return obj;
|
284
270
|
}
|
285
271
|
|
@@ -325,24 +311,27 @@ static VALUE Event__mouseup(SDL_Event *event) {
|
|
325
311
|
|
326
312
|
//// SDL::Key methods:
|
327
313
|
|
328
|
-
static VALUE Key_s_press_p(VALUE mod, VALUE
|
314
|
+
static VALUE Key_s_press_p(VALUE mod, VALUE keycode_) {
|
329
315
|
UNUSED(mod);
|
330
|
-
int sym = NUM2INT(keysym);
|
331
|
-
|
332
|
-
if (SDLK_FIRST >= sym || sym >= SDLK_LAST)
|
333
|
-
rb_raise(eSDLError, "%d is out of key", sym);
|
334
316
|
|
335
317
|
if (!key_state)
|
336
318
|
rb_raise(eSDLError,
|
337
319
|
"You should call SDL::Key#scan before calling SDL::Key#press?");
|
338
320
|
|
339
|
-
|
321
|
+
SDL_Keycode keycode = NUM2INT(keycode_);
|
322
|
+
SDL_Scancode scancode = SDL_GetScancodeFromKey(keycode);
|
323
|
+
|
324
|
+
if (0 >= scancode || scancode >= key_state_len)
|
325
|
+
rb_raise(eSDLError, "%d (%d) is out of bounds: %d",
|
326
|
+
keycode, scancode, key_state_len);
|
327
|
+
|
328
|
+
return INT2BOOL(key_state[scancode]);
|
340
329
|
}
|
341
330
|
|
342
331
|
static VALUE Key_s_scan(VALUE mod) {
|
343
332
|
UNUSED(mod);
|
344
333
|
|
345
|
-
key_state =
|
334
|
+
key_state = (Uint8 *) SDL_GetKeyboardState(&key_state_len);
|
346
335
|
mod_state = SDL_GetModState();
|
347
336
|
|
348
337
|
return Qnil;
|
@@ -371,14 +360,7 @@ static void _PixelFormat_free(void* p) {
|
|
371
360
|
if (is_quit) return;
|
372
361
|
if (!p) return;
|
373
362
|
|
374
|
-
|
375
|
-
|
376
|
-
if (format->palette) {
|
377
|
-
free(format->palette->colors);
|
378
|
-
free(format->palette);
|
379
|
-
}
|
380
|
-
|
381
|
-
free(format);
|
363
|
+
SDL_FreeFormat(p);
|
382
364
|
}
|
383
365
|
|
384
366
|
static void _PixelFormat_mark(void* p) {
|
@@ -397,57 +379,75 @@ static VALUE PixelFormat_map_rgba(VALUE self, VALUE r, VALUE g, VALUE b, VALUE a
|
|
397
379
|
NUM2UINT8(b), NUM2UINT8(a)));
|
398
380
|
}
|
399
381
|
|
400
|
-
static VALUE
|
382
|
+
static VALUE PixelFormat_get_rgba(VALUE self, VALUE pixel) {
|
401
383
|
DEFINE_SELF(PixelFormat, format, self);
|
402
|
-
Uint8 r, g, b;
|
384
|
+
Uint8 r, g, b, a;
|
403
385
|
|
404
|
-
|
386
|
+
SDL_GetRGBA(NUM2UINT(pixel), format, &r, &g, &b, &a);
|
405
387
|
|
406
|
-
return rb_ary_new3(
|
388
|
+
return rb_ary_new3(4, UINT2NUM(r), UINT2NUM(g), UINT2NUM(b), UINT2NUM(a));
|
407
389
|
}
|
408
390
|
|
409
|
-
|
410
|
-
DEFINE_SELF(PixelFormat, format, self);
|
391
|
+
//// SDL::Screen methods:
|
411
392
|
|
412
|
-
|
413
|
-
|
393
|
+
static VALUE Screen_s_open(VALUE klass, VALUE w_, VALUE h_, VALUE bpp_, VALUE flags_) {
|
394
|
+
UNUSED(klass);
|
414
395
|
|
415
|
-
|
416
|
-
|
396
|
+
int w = NUM2INT(w_);
|
397
|
+
int h = NUM2INT(h_);
|
398
|
+
int bpp = NUM2INT(bpp_);
|
399
|
+
Uint32 flags = NUM2UINT32(flags_);
|
417
400
|
|
418
|
-
|
419
|
-
}
|
401
|
+
if (!bpp) bpp = 32; // TODO: remove bpp option and always be 32?
|
420
402
|
|
421
|
-
|
403
|
+
SDL_Window *window =
|
404
|
+
SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
405
|
+
w, h,
|
406
|
+
flags);
|
407
|
+
if (!window) FAILURE("Screen.open(CreateWindow)");
|
422
408
|
|
423
|
-
|
424
|
-
|
425
|
-
|
409
|
+
SDL_Renderer *renderer =
|
410
|
+
SDL_CreateRenderer(window, -1,
|
411
|
+
SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
|
412
|
+
if (!renderer) FAILURE("Screen.open(CreateRenderer)");
|
413
|
+
|
414
|
+
// bumps the refcount and returns the same thing
|
415
|
+
SDL_PixelFormat *format = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA32);
|
416
|
+
if (!format)
|
417
|
+
rb_raise(eSDLError, "SDL_AllocFormat freaked out.");
|
426
418
|
|
427
|
-
|
419
|
+
VALUE vrenderer = TypedData_Wrap_Struct(cRenderer, &_Renderer_type, renderer);
|
420
|
+
VALUE vwindow = TypedData_Wrap_Struct(cWindow, &_Window_type, window);
|
421
|
+
VALUE vformat = TypedData_Wrap_Struct(cPixelFormat, &_PixelFormat_type, format);
|
428
422
|
|
429
|
-
|
430
|
-
|
431
|
-
NUM2INT(w), NUM2INT(h), NUM2INT(bpp), SDL_GetError());
|
423
|
+
rb_ivar_set(vrenderer, id_iv_window, vwindow);
|
424
|
+
rb_ivar_set(vrenderer, id_iv_format, vformat);
|
432
425
|
|
433
|
-
return
|
426
|
+
return vrenderer;
|
434
427
|
}
|
435
428
|
|
436
|
-
static VALUE
|
437
|
-
DEFINE_SELF(
|
429
|
+
static VALUE Renderer_new_texture(VALUE self) {
|
430
|
+
DEFINE_SELF(Renderer, renderer, self);
|
438
431
|
|
439
|
-
|
440
|
-
|
432
|
+
int w, h;
|
433
|
+
if (SDL_GetRendererOutputSize(renderer, &w, &h))
|
434
|
+
FAILURE("Renderer#new_texture(GetRendererOutputSize");
|
441
435
|
|
442
|
-
|
436
|
+
SDL_Texture *texture = SDL_CreateTexture(renderer,
|
437
|
+
SDL_PIXELFORMAT_RGBA32,
|
438
|
+
SDL_TEXTUREACCESS_TARGET,
|
439
|
+
w, h);
|
440
|
+
if (!texture)
|
441
|
+
FAILURE("Renderer#new_texture(CreateTexture)");
|
442
|
+
|
443
|
+
return TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);
|
443
444
|
}
|
444
445
|
|
445
|
-
static VALUE Screen_update(VALUE self, VALUE x, VALUE y, VALUE w, VALUE h) {
|
446
|
-
DEFINE_SELF(Surface, surface, self);
|
447
446
|
|
448
|
-
|
449
|
-
|
450
|
-
|
447
|
+
static VALUE Renderer_present(VALUE self) {
|
448
|
+
DEFINE_SELF(Renderer, renderer, self);
|
449
|
+
|
450
|
+
SDL_RenderPresent(renderer);
|
451
451
|
|
452
452
|
return Qnil;
|
453
453
|
}
|
@@ -467,45 +467,6 @@ static size_t _Surface_memsize(const void *p) {
|
|
467
467
|
return p ? sizeof(struct SDL_Surface) : 0;
|
468
468
|
}
|
469
469
|
|
470
|
-
static VALUE Surface_s_blit(VALUE self,
|
471
|
-
VALUE src, VALUE srcX, VALUE srcY, VALUE srcW, VALUE srcH,
|
472
|
-
VALUE dst, VALUE dstX, VALUE dstY) {
|
473
|
-
UNUSED(self);
|
474
|
-
DEFINE_SELF(Surface, src_surface, src);
|
475
|
-
DEFINE_SELF(Surface, dst_surface, dst);
|
476
|
-
|
477
|
-
SDL_Rect src_rect = NewRect(srcX, srcY, srcW, srcH);
|
478
|
-
SDL_Rect dst_rect = NewRect(dstX, dstY, srcW, srcH);
|
479
|
-
SDL_Rect *sr = ZERO_RECT(src_rect) ? NULL : &src_rect;
|
480
|
-
SDL_Rect *dr = ZERO_RECT(dst_rect) ? NULL : &dst_rect;
|
481
|
-
int result = SDL_BlitSurface(src_surface, sr, dst_surface, dr);
|
482
|
-
|
483
|
-
switch (result) {
|
484
|
-
case -1:
|
485
|
-
FAILURE("SDL::Surface.blit");
|
486
|
-
case -2:
|
487
|
-
rb_raise(eSDLMem, "SDL::Surface lost video memory");
|
488
|
-
}
|
489
|
-
|
490
|
-
return INT2NUM(result);
|
491
|
-
}
|
492
|
-
|
493
|
-
static VALUE Surface_s_new(VALUE self, VALUE w, VALUE h, VALUE pf) {
|
494
|
-
UNUSED(self);
|
495
|
-
SDL_Surface* surface;
|
496
|
-
|
497
|
-
DEFINE_SELF(PixelFormat, format, pf);
|
498
|
-
|
499
|
-
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, NUM2INT(w), NUM2INT(h),
|
500
|
-
format->BitsPerPixel,
|
501
|
-
format->Rmask, format->Gmask,
|
502
|
-
format->Bmask, format->Amask);
|
503
|
-
if (!surface)
|
504
|
-
FAILURE("Surface.new");
|
505
|
-
|
506
|
-
return TypedData_Wrap_Struct(cSurface, &_Surface_type, surface);
|
507
|
-
}
|
508
|
-
|
509
470
|
static VALUE Surface_s_load(VALUE klass, VALUE path) {
|
510
471
|
UNUSED(klass);
|
511
472
|
SDL_Surface *surface;
|
@@ -522,129 +483,232 @@ static VALUE Surface_s_load(VALUE klass, VALUE path) {
|
|
522
483
|
return TypedData_Wrap_Struct(cSurface, &_Surface_type, surface);
|
523
484
|
}
|
524
485
|
|
525
|
-
|
526
|
-
|
486
|
+
#define DEFINE_WRAP12(name) \
|
487
|
+
void wrap_##name(SDL_Surface* a, \
|
488
|
+
Sint16 b, Sint16 c, Sint16 d, Sint16 e, \
|
489
|
+
Sint16 f, Sint16 g, Sint16 h, Sint16 i, \
|
490
|
+
int j, \
|
491
|
+
Uint32 k, Uint8 l) { name(a, b, c, d, e, f, g, h, i, j, k); }
|
492
|
+
#define DEFINE_WRAP7(name) \
|
493
|
+
void wrap_##name(SDL_Surface* a, \
|
494
|
+
Sint16 b, Sint16 c, Sint16 d, Sint16 e, \
|
495
|
+
Uint32 f, Uint8 g) { name(a, b, c, d, e, f); }
|
496
|
+
#define DEFINE_WRAP6(name) \
|
497
|
+
void wrap_##name(SDL_Surface* a, \
|
498
|
+
Sint16 b, Sint16 c, Sint16 d, \
|
499
|
+
Uint32 e, Uint8 f) { name(a, b, c, d, e); }
|
500
|
+
|
501
|
+
#define IDX1(a) !!(a)
|
502
|
+
#define IDX2(a, b) (!!(a))<<1 | !!(b)
|
503
|
+
#define IDX3(a, b, c) (!!(a))<<2 | (!!(b))<<1 | !!(c)
|
504
|
+
|
505
|
+
typedef int (*f_rxyxyc)(SDL_Renderer*,
|
506
|
+
Sint16, Sint16, Sint16, Sint16,
|
507
|
+
Uint32);
|
508
|
+
typedef int (*f_rxyrc)(SDL_Renderer*,
|
509
|
+
Sint16, Sint16, Sint16,
|
510
|
+
Uint32);
|
511
|
+
|
512
|
+
// TODO: ? maybe ?
|
513
|
+
// int hlineColor (SDL_Renderer *renderer, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color)
|
514
|
+
// int vlineColor (SDL_Renderer *renderer, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color)
|
515
|
+
// int roundedRectangleColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color)
|
516
|
+
// int roundedBoxColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color)
|
517
|
+
// int thickLineColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint32 color)
|
518
|
+
// int arcColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color)
|
519
|
+
// int pieColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color)
|
520
|
+
// int filledPieColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color)
|
521
|
+
// int trigonColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
|
522
|
+
// int aatrigonColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
|
523
|
+
// int filledTrigonColor (SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color)
|
524
|
+
// int polygonColor (SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color)
|
525
|
+
// int aapolygonColor (SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color)
|
526
|
+
// int filledPolygonColor (SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color)
|
527
|
+
// int texturedPolygon (SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, SDL_Surface *texture, int texture_dx, int texture_dy)
|
528
|
+
// void gfxPrimitivesSetFont (const void *fontdata, Uint32 cw, Uint32 ch)
|
529
|
+
// void gfxPrimitivesSetFontRotation(Uint32 rotation)
|
530
|
+
// int characterColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, char c, Uint32 color)
|
531
|
+
// int stringColor (SDL_Renderer *renderer, Sint16 x, Sint16 y, const char *s, Uint32 color)
|
532
|
+
|
533
|
+
static VALUE Renderer_draw_bezier(VALUE self,
|
534
|
+
VALUE xs_,
|
535
|
+
VALUE ys_,
|
536
|
+
VALUE steps_,
|
537
|
+
VALUE color_) {
|
538
|
+
DEFINE_SELF(Renderer, renderer, self);
|
539
|
+
|
540
|
+
int xlen = RARRAY_LENINT(xs_);
|
541
|
+
int ylen = RARRAY_LENINT(ys_);
|
542
|
+
|
543
|
+
if (xlen != ylen)
|
544
|
+
rb_raise(rb_eArgError, "xs & ys are different length");
|
545
|
+
|
546
|
+
Sint16 *xs = malloc(xlen*sizeof(Sint16));
|
547
|
+
|
548
|
+
for (int i = 0; i < xlen; i++) {
|
549
|
+
xs[i] = NUM2SINT16(RARRAY_AREF(xs_, i));
|
550
|
+
}
|
551
|
+
|
552
|
+
Sint16 *ys = malloc(ylen*sizeof(Sint16));
|
553
|
+
for (int i = 0; i < ylen; i++) {
|
554
|
+
ys[i] = NUM2SINT16(RARRAY_AREF(ys_, i));
|
555
|
+
}
|
556
|
+
|
557
|
+
int steps = NUM2INT(steps_);
|
558
|
+
Uint32 color = VALUE2COLOR(color_);
|
527
559
|
|
528
|
-
if (
|
529
|
-
|
530
|
-
|
531
|
-
|
560
|
+
if (bezierColor(renderer, xs, ys, xlen, steps, color))
|
561
|
+
FAILURE("draw_bezier");
|
562
|
+
|
563
|
+
free(xs);
|
564
|
+
free(ys);
|
532
565
|
|
533
566
|
return Qnil;
|
534
567
|
}
|
535
568
|
|
536
|
-
|
537
|
-
|
569
|
+
int aafilledCircleColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color) {
|
570
|
+
int result = 0;
|
571
|
+
result |= filledCircleColor(renderer, x, y, rad, color);
|
572
|
+
result |= aacircleColor(renderer, x, y, rad, color);
|
573
|
+
return result;
|
574
|
+
}
|
538
575
|
|
539
|
-
static
|
540
|
-
|
576
|
+
static f_rxyrc f_circle[] = { &circleColor,
|
577
|
+
&filledCircleColor,
|
578
|
+
&aacircleColor,
|
579
|
+
&aafilledCircleColor };
|
541
580
|
|
542
|
-
|
543
|
-
|
581
|
+
static VALUE Renderer_draw_circle(VALUE self,
|
582
|
+
VALUE x, VALUE y,
|
583
|
+
VALUE r,
|
584
|
+
VALUE c,
|
585
|
+
VALUE aa, VALUE f) {
|
586
|
+
DEFINE_SELF(Renderer, renderer, self);
|
544
587
|
|
545
|
-
|
546
|
-
y1 = NUM2SINT16(y);
|
547
|
-
r_ = NUM2SINT16(r);
|
548
|
-
color = VALUE2COLOR(c, surface->format);
|
588
|
+
Uint8 idx = IDX2(RTEST(aa), RTEST(f));
|
549
589
|
|
550
|
-
|
590
|
+
f_circle[idx](renderer,
|
591
|
+
NUM2SINT16(x), NUM2SINT16(y),
|
592
|
+
NUM2SINT16(r),
|
593
|
+
NUM2UINT(c));
|
551
594
|
|
552
595
|
return Qnil;
|
553
596
|
}
|
554
597
|
|
555
|
-
|
556
|
-
|
598
|
+
int aafilledEllipseColor(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color) {
|
599
|
+
int result = 0;
|
600
|
+
result |= filledEllipseColor(renderer, x, y, rx, ry, color);
|
601
|
+
result |= aaellipseColor(renderer, x, y, rx, ry, color);
|
602
|
+
return result;
|
603
|
+
}
|
604
|
+
|
605
|
+
static f_rxyxyc f_ellipse[] = { &ellipseColor,
|
606
|
+
&filledEllipseColor,
|
607
|
+
&aaellipseColor,
|
608
|
+
&aafilledEllipseColor };
|
557
609
|
|
558
|
-
|
559
|
-
|
610
|
+
static VALUE Renderer_draw_ellipse(VALUE self,
|
611
|
+
VALUE x, VALUE y,
|
612
|
+
VALUE rx, VALUE ry,
|
613
|
+
VALUE c,
|
614
|
+
VALUE aa, VALUE f) {
|
615
|
+
DEFINE_SELF(Renderer, renderer, self);
|
560
616
|
|
561
|
-
|
562
|
-
y1 = NUM2SINT16(y);
|
563
|
-
x2 = x1 + NUM2SINT16(w);
|
564
|
-
y2 = y1 + NUM2SINT16(h);
|
565
|
-
color = VALUE2COLOR(c, surface->format);
|
617
|
+
Uint8 idx = IDX2(RTEST(aa), RTEST(f));
|
566
618
|
|
567
|
-
|
619
|
+
f_ellipse[idx](renderer,
|
620
|
+
NUM2SINT16(x),
|
621
|
+
NUM2SINT16(y),
|
622
|
+
NUM2SINT16(rx),
|
623
|
+
NUM2SINT16(ry),
|
624
|
+
NUM2UINT(c));
|
568
625
|
|
569
626
|
return Qnil;
|
570
627
|
}
|
571
628
|
|
572
|
-
static
|
573
|
-
|
629
|
+
static f_rxyxyc f_line[] = { &lineColor,
|
630
|
+
&aalineColor };
|
574
631
|
|
575
|
-
|
576
|
-
|
632
|
+
static VALUE Renderer_draw_line(VALUE self,
|
633
|
+
VALUE x1, VALUE y1,
|
634
|
+
VALUE x2, VALUE y2,
|
635
|
+
VALUE c,
|
636
|
+
VALUE aa) {
|
637
|
+
DEFINE_SELF(Renderer, renderer, self);
|
577
638
|
|
578
|
-
|
579
|
-
y1 = NUM2SINT16(y);
|
580
|
-
r1 = NUM2SINT16(w);
|
581
|
-
r2 = NUM2SINT16(h);
|
582
|
-
color = VALUE2COLOR(c, surface->format);
|
639
|
+
Uint8 idx = IDX1(RTEST(aa));
|
583
640
|
|
584
|
-
|
641
|
+
f_line[idx](renderer,
|
642
|
+
NUM2SINT16(x1),
|
643
|
+
NUM2SINT16(y1),
|
644
|
+
NUM2SINT16(x2),
|
645
|
+
NUM2SINT16(y2),
|
646
|
+
VALUE2COLOR(c));
|
585
647
|
|
586
648
|
return Qnil;
|
587
649
|
}
|
588
650
|
|
589
|
-
static
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
651
|
+
static f_rxyxyc f_rect[] = { &rectangleColor,
|
652
|
+
&boxColor };
|
653
|
+
|
654
|
+
static VALUE Renderer_draw_rect(VALUE self,
|
655
|
+
VALUE x_, VALUE y_,
|
656
|
+
VALUE w_, VALUE h_,
|
657
|
+
VALUE c,
|
658
|
+
VALUE f) {
|
659
|
+
DEFINE_SELF(Renderer, renderer, self);
|
596
660
|
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
661
|
+
Sint16 x1 = NUM2SINT16(x_);
|
662
|
+
Sint16 y1 = NUM2SINT16(y_);
|
663
|
+
Sint16 x2 = NUM2SINT16(w_) + x1;
|
664
|
+
Sint16 y2 = NUM2SINT16(h_) + y1;
|
665
|
+
Uint8 idx = IDX1(RTEST(f));
|
666
|
+
|
667
|
+
f_rect[idx](renderer, x1, y1, x2, y2, NUM2UINT(c));
|
604
668
|
|
605
669
|
return Qnil;
|
606
670
|
}
|
607
671
|
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
DEFINE_SXYXYC(fill_ellipse, sge_FilledEllipse)
|
612
|
-
DEFINE_SXYXYC(draw_ellipse, sge_Ellipse)
|
613
|
-
DEFINE_SXYWHC(draw_rect, sge_Rect)
|
614
|
-
DEFINE_SXYWHC(fill_rect, sge_FilledRect)
|
672
|
+
static VALUE Renderer_clear(VALUE self, VALUE color) {
|
673
|
+
DEFINE_SELF(Renderer, renderer, self);
|
674
|
+
DEFINE_SELF(PixelFormat, format, rb_ivar_get(self, id_iv_format));
|
615
675
|
|
616
|
-
|
617
|
-
|
676
|
+
Uint8 r, g, b, a;
|
677
|
+
SDL_GetRGBA(NUM2UINT(color), format, &r, &g, &b, &a);
|
618
678
|
|
619
|
-
|
679
|
+
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
620
680
|
|
621
|
-
if (
|
622
|
-
FAILURE("
|
681
|
+
if (SDL_RenderClear(renderer))
|
682
|
+
FAILURE("Renderer#clear");
|
623
683
|
|
624
684
|
return Qnil;
|
625
685
|
}
|
626
686
|
|
627
|
-
static VALUE
|
628
|
-
DEFINE_SELF(
|
629
|
-
|
630
|
-
return UINT2NUM(surface->flags);
|
631
|
-
}
|
632
|
-
|
633
|
-
static VALUE Surface_set_alpha(VALUE self, VALUE flag, VALUE alpha) {
|
634
|
-
DEFINE_SELF(Surface, surface, self);
|
687
|
+
static VALUE Renderer_copy_texture(VALUE self, VALUE texture_) {
|
688
|
+
DEFINE_SELF(Renderer, renderer, self);
|
689
|
+
DEFINE_SELF(Texture, texture, texture_);
|
635
690
|
|
636
|
-
if (
|
637
|
-
FAILURE("
|
691
|
+
if (SDL_RenderCopy(renderer, texture, NULL, NULL))
|
692
|
+
FAILURE("Renderer#copy_texture");
|
638
693
|
|
639
694
|
return Qnil;
|
640
695
|
}
|
641
696
|
|
697
|
+
static VALUE Renderer_fast_rect(VALUE self,
|
698
|
+
VALUE x, VALUE y,
|
699
|
+
VALUE w, VALUE h,
|
700
|
+
VALUE color) {
|
701
|
+
return Renderer_draw_rect(self, x, y, w, h, color, Qtrue);
|
702
|
+
}
|
703
|
+
|
642
704
|
static VALUE Surface_format(VALUE self) {
|
643
705
|
DEFINE_SELF(Surface, surface, self);
|
644
706
|
SDL_PixelFormat* format;
|
645
707
|
SDL_Palette* palette;
|
646
708
|
SDL_Palette* src = surface->format->palette;
|
647
709
|
|
710
|
+
// TODO: remove this or drop down to SDL_AllocFormat only
|
711
|
+
|
648
712
|
if (src) {
|
649
713
|
palette = ALLOC(SDL_Palette);
|
650
714
|
palette->ncolors = src->ncolors;
|
@@ -662,6 +726,26 @@ static VALUE Surface_format(VALUE self) {
|
|
662
726
|
return ret;
|
663
727
|
}
|
664
728
|
|
729
|
+
static VALUE Renderer_h(VALUE self) {
|
730
|
+
DEFINE_SELF(Renderer, renderer, self);
|
731
|
+
|
732
|
+
int w, h;
|
733
|
+
|
734
|
+
SDL_GetRendererOutputSize(renderer, &w, &h);
|
735
|
+
|
736
|
+
return INT2NUM(h);
|
737
|
+
}
|
738
|
+
|
739
|
+
static VALUE Renderer_w(VALUE self) {
|
740
|
+
DEFINE_SELF(Renderer, renderer, self);
|
741
|
+
|
742
|
+
int w, h;
|
743
|
+
|
744
|
+
SDL_GetRendererOutputSize(renderer, &w, &h);
|
745
|
+
|
746
|
+
return INT2NUM(w);
|
747
|
+
}
|
748
|
+
|
665
749
|
static VALUE Surface_h(VALUE self) {
|
666
750
|
DEFINE_SELF(Surface, surface, self);
|
667
751
|
|
@@ -669,27 +753,41 @@ static VALUE Surface_h(VALUE self) {
|
|
669
753
|
}
|
670
754
|
|
671
755
|
static VALUE Surface_index(VALUE self, VALUE x, VALUE y) {
|
672
|
-
|
673
|
-
|
674
|
-
return UINT2NUM(sge_GetPixel(surface,
|
675
|
-
NUM2SINT16(x), NUM2SINT16(y)));
|
756
|
+
rb_raise(eSDLError, "Reading the canvas isn't currently supported");
|
757
|
+
return Qnil;
|
676
758
|
}
|
677
759
|
|
678
|
-
static VALUE
|
679
|
-
DEFINE_SELF(
|
760
|
+
static VALUE Renderer_index_eq(VALUE self, VALUE x, VALUE y, VALUE color) {
|
761
|
+
DEFINE_SELF(Renderer, renderer, self);
|
680
762
|
|
681
|
-
|
682
|
-
NUM2SINT16(x), NUM2SINT16(y),
|
683
|
-
VALUE2COLOR(color, surface->format));
|
763
|
+
pixelColor(renderer, NUM2SINT16(x), NUM2SINT16(y), VALUE2COLOR(color));
|
684
764
|
|
685
765
|
return Qnil;
|
686
766
|
}
|
687
767
|
|
768
|
+
static SDL_Surface *pixel = NULL;
|
769
|
+
|
770
|
+
static VALUE Renderer_index(VALUE self, VALUE x, VALUE y) {
|
771
|
+
DEFINE_SELF(Renderer, renderer, self);
|
772
|
+
|
773
|
+
SDL_Rect pixel_rect = { NUM2SINT16(x), NUM2SINT16(y), 1, 1 };
|
774
|
+
|
775
|
+
if (!pixel)
|
776
|
+
pixel = SDL_CreateRGBSurfaceWithFormat(0, 1, 1, 32,
|
777
|
+
SDL_PIXELFORMAT_RGBA32);
|
778
|
+
|
779
|
+
if (SDL_RenderReadPixels(renderer, &pixel_rect, SDL_PIXELFORMAT_RGBA32,
|
780
|
+
pixel->pixels,
|
781
|
+
pixel->pitch))
|
782
|
+
FAILURE("Renderer#[]");
|
783
|
+
|
784
|
+
return UINT2NUM(((Uint32*)pixel->pixels)[0]);
|
785
|
+
}
|
786
|
+
|
688
787
|
static VALUE Surface_make_collision_map(VALUE self) {
|
689
788
|
DEFINE_SELF(Surface, surface, self);
|
690
789
|
|
691
790
|
sge_cdata * cdata = sge_make_cmap(surface);
|
692
|
-
|
693
791
|
if (!cdata)
|
694
792
|
FAILURE("Surface#make_collision_map");
|
695
793
|
|
@@ -702,35 +800,205 @@ static VALUE Surface_w(VALUE self) {
|
|
702
800
|
return INT2NUM(surface->w);
|
703
801
|
}
|
704
802
|
|
705
|
-
|
706
|
-
|
803
|
+
// TODO: maybe remove? I dunno... could be nice for pre-rendering?
|
804
|
+
static VALUE Surface_transform(VALUE self, VALUE angle,
|
805
|
+
VALUE xscale, VALUE yscale,
|
806
|
+
VALUE flags) {
|
707
807
|
DEFINE_SELF(Surface, surface, self);
|
708
808
|
|
709
|
-
SDL_Surface *result =
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
809
|
+
SDL_Surface *result = rotozoomSurfaceXY(surface,
|
810
|
+
NUM2FLT(angle),
|
811
|
+
NUM2FLT(xscale),
|
812
|
+
NUM2FLT(yscale),
|
813
|
+
SMOOTHING_ON);
|
814
|
+
|
715
815
|
if (!result)
|
716
816
|
FAILURE("Surface#transform");
|
717
817
|
|
718
|
-
|
719
|
-
|
720
|
-
surface->format->colorkey) < 0)
|
721
|
-
FAILURE("Surface#transform(set_color_key)");
|
818
|
+
return TypedData_Wrap_Struct(cSurface, &_Surface_type, result);
|
819
|
+
}
|
722
820
|
|
821
|
+
static VALUE Renderer_blit(VALUE self, VALUE src_,
|
822
|
+
VALUE x_, VALUE y_,
|
823
|
+
VALUE a_,
|
824
|
+
VALUE ws_, VALUE hs_,
|
825
|
+
VALUE center_) {
|
826
|
+
DEFINE_SELF(Renderer, renderer, self);
|
827
|
+
DEFINE_SELF(Surface, src, src_);
|
723
828
|
|
724
|
-
|
725
|
-
|
829
|
+
int x = NUM2SINT16(x_);
|
830
|
+
int y = NUM2SINT16(y_);
|
831
|
+
double a = RTEST(a_) ? -NUM2DBL(a_) : 0.0;
|
832
|
+
float ws = RTEST(ws_) ? NUM2FLT(ws_) : 1.0;
|
833
|
+
float hs = RTEST(hs_) ? NUM2FLT(hs_) : 1.0;
|
726
834
|
|
727
|
-
|
835
|
+
SDL_Texture* texture;
|
836
|
+
VALUE vtexture = rb_attr_get(src_, id_iv_texture);
|
837
|
+
|
838
|
+
if (RTEST(vtexture)) {
|
839
|
+
SET_SELF(Texture, texture, vtexture);
|
840
|
+
} else {
|
841
|
+
texture = SDL_CreateTextureFromSurface(renderer, src);
|
842
|
+
if (!texture)
|
843
|
+
FAILURE("_blit(SDL_CreateTextureFromSurface)");
|
844
|
+
|
845
|
+
VALUE vtexture = TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);
|
846
|
+
|
847
|
+
rb_ivar_set(src_, id_iv_texture, vtexture);
|
848
|
+
}
|
849
|
+
|
850
|
+
int w, h;
|
851
|
+
if (SDL_QueryTexture(texture, NULL, NULL, &w, &h))
|
852
|
+
FAILURE("_blit(SDL_QueryTexture)");
|
853
|
+
|
854
|
+
SDL_Rect dst_rect = { x, y, w*ws, h*hs };
|
855
|
+
|
856
|
+
if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND))
|
857
|
+
FAILURE("Renderer#blit(SetTextureBlendMode)");
|
858
|
+
|
859
|
+
if (RTEST(a_) || RTEST(ws_) || RTEST(hs_)) {
|
860
|
+
SDL_Point bottom_left = { 0, h-1 };
|
861
|
+
SDL_Point *point = (RTEST(center_) ? NULL : &bottom_left);
|
862
|
+
|
863
|
+
if (SDL_RenderCopyEx(renderer, texture, NULL, &dst_rect,
|
864
|
+
a, point, SDL_FLIP_NONE))
|
865
|
+
FAILURE("_blit(SDL_RenderCopyEx)");
|
866
|
+
} else {
|
867
|
+
if (SDL_RenderCopy(renderer, texture, NULL, &dst_rect))
|
868
|
+
FAILURE("_blit(SDL_RenderCopyEx)");
|
869
|
+
}
|
870
|
+
|
871
|
+
return Qnil;
|
728
872
|
}
|
729
873
|
|
730
|
-
static VALUE
|
731
|
-
|
874
|
+
static VALUE Renderer_sprite(VALUE self, VALUE w_, VALUE h_) {
|
875
|
+
UNUSED(self);
|
876
|
+
|
877
|
+
int w = NUM2INT(w_);
|
878
|
+
int h = NUM2INT(h_);
|
879
|
+
int bpp = 32;
|
880
|
+
|
881
|
+
SDL_Surface *surface =
|
882
|
+
SDL_CreateRGBSurfaceWithFormat(0, w, h, bpp, SDL_PIXELFORMAT_RGBA32);
|
883
|
+
if (!surface) FAILURE("Surface#sprite(CreateRGBSurfaceWithFormat)");
|
884
|
+
|
885
|
+
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(surface);
|
886
|
+
if (!renderer) FAILURE("Surface#sprite(CreateSoftwareRenderer)");
|
887
|
+
|
888
|
+
// bumps the refcount and returns the same thing
|
889
|
+
SDL_PixelFormat *format = SDL_AllocFormat(surface->format->format);
|
890
|
+
if (format != surface->format)
|
891
|
+
rb_raise(eSDLError, "SDL_AllocFormat freaked out. %p vs %p",
|
892
|
+
format,
|
893
|
+
surface->format);
|
894
|
+
|
895
|
+
VALUE vrenderer = TypedData_Wrap_Struct(cRenderer, &_Renderer_type, renderer);
|
896
|
+
VALUE vsurface = TypedData_Wrap_Struct(cSurface, &_Surface_type, surface);
|
897
|
+
VALUE vformat = TypedData_Wrap_Struct(cPixelFormat, &_PixelFormat_type, format);
|
898
|
+
|
899
|
+
rb_ivar_set(vrenderer, id_iv_surface, vsurface);
|
900
|
+
rb_ivar_set(vrenderer, id_iv_format, vformat);
|
901
|
+
|
902
|
+
return vrenderer;
|
903
|
+
}
|
904
|
+
|
905
|
+
static VALUE Renderer_save(VALUE self, VALUE path) {
|
906
|
+
DEFINE_SELF(Renderer, renderer, self);
|
907
|
+
|
908
|
+
int w, h;
|
909
|
+
SDL_GetRendererOutputSize(renderer, &w, &h);
|
910
|
+
|
911
|
+
SDL_Surface *sshot = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32,
|
912
|
+
SDL_PIXELFORMAT_RGBA32);
|
913
|
+
if (SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_RGBA32,
|
914
|
+
sshot->pixels,
|
915
|
+
sshot->pitch))
|
916
|
+
FAILURE("Renderer#save");
|
917
|
+
int ret = IMG_SavePNG(sshot, RSTRING_PTR(path));
|
918
|
+
SDL_FreeSurface(sshot);
|
919
|
+
|
920
|
+
return INT2NUM(ret);
|
921
|
+
}
|
922
|
+
|
923
|
+
//// SDL::Renderer methods:
|
924
|
+
|
925
|
+
static void _Renderer_free(void* renderer) {
|
926
|
+
if (is_quit) return;
|
927
|
+
if (renderer) SDL_DestroyRenderer(renderer);
|
928
|
+
}
|
929
|
+
|
930
|
+
static void _Renderer_mark(void* renderer) {
|
931
|
+
UNUSED(renderer);
|
932
|
+
}
|
933
|
+
|
934
|
+
static VALUE Renderer_target(VALUE self) {
|
935
|
+
DEFINE_SELF(Renderer, renderer, self);
|
936
|
+
|
937
|
+
SDL_Texture* texture = SDL_GetRenderTarget(renderer);
|
938
|
+
|
939
|
+
if (!texture)
|
940
|
+
FAILURE("Renderer#target");
|
941
|
+
|
942
|
+
return TypedData_Wrap_Struct(cTexture, &_Texture_type, texture);
|
943
|
+
}
|
944
|
+
|
945
|
+
static VALUE Renderer_target_eq(VALUE self, VALUE texture_) {
|
946
|
+
DEFINE_SELF(Renderer, renderer, self);
|
947
|
+
DEFINE_SELF0(Texture, texture, texture_);
|
948
|
+
|
949
|
+
if (SDL_SetRenderTarget(renderer, texture))
|
950
|
+
FAILURE("Renderer#target=");
|
951
|
+
|
952
|
+
return texture_;
|
953
|
+
}
|
954
|
+
|
955
|
+
//// SDL::Texture methods:
|
956
|
+
|
957
|
+
static void _Texture_free(void* texture) {
|
958
|
+
if (is_quit) return;
|
959
|
+
if (texture) SDL_DestroyTexture(texture);
|
960
|
+
}
|
732
961
|
|
733
|
-
|
962
|
+
static void _Texture_mark(void* texture) {
|
963
|
+
UNUSED(texture);
|
964
|
+
}
|
965
|
+
|
966
|
+
//// SDL::Window methods:
|
967
|
+
|
968
|
+
static void _Window_free(void* Window) {
|
969
|
+
if (is_quit) return;
|
970
|
+
if (Window) SDL_DestroyWindow(Window);
|
971
|
+
}
|
972
|
+
|
973
|
+
static void _Window_mark(void* Window) {
|
974
|
+
UNUSED(Window);
|
975
|
+
}
|
976
|
+
|
977
|
+
static VALUE Window_title_eq(VALUE self, VALUE title) {
|
978
|
+
DEFINE_SELF(Window, window, self);
|
979
|
+
|
980
|
+
ExportStringValue(title);
|
981
|
+
|
982
|
+
SDL_SetWindowTitle(window, StringValueCStr(title));
|
983
|
+
|
984
|
+
return Qnil;
|
985
|
+
}
|
986
|
+
|
987
|
+
static VALUE Window_title(VALUE self) {
|
988
|
+
DEFINE_SELF(Window, window, self);
|
989
|
+
|
990
|
+
const char* title = SDL_GetWindowTitle(window);
|
991
|
+
|
992
|
+
return rb_str_new_cstr(title);
|
993
|
+
}
|
994
|
+
|
995
|
+
static VALUE Window_update(VALUE self) {
|
996
|
+
DEFINE_SELF(Window, window, self);
|
997
|
+
|
998
|
+
if (SDL_UpdateWindowSurface(window))
|
999
|
+
FAILURE("Window#update");
|
1000
|
+
|
1001
|
+
return Qnil;
|
734
1002
|
}
|
735
1003
|
|
736
1004
|
//// SDL::TTFFont methods:
|
@@ -765,47 +1033,47 @@ static VALUE Font_height(VALUE self) {
|
|
765
1033
|
|
766
1034
|
static VALUE Font_render(VALUE self, VALUE dst, VALUE text, VALUE c) {
|
767
1035
|
DEFINE_SELF(TTFFont, font, self);
|
768
|
-
DEFINE_SELF(
|
1036
|
+
DEFINE_SELF(PixelFormat, format, rb_ivar_get(dst, id_iv_format));
|
769
1037
|
|
770
1038
|
SDL_Surface *result;
|
1039
|
+
SDL_Color fg;
|
771
1040
|
|
772
|
-
|
1041
|
+
SDL_GetRGBA(VALUE2COLOR(c), format,
|
1042
|
+
&(fg.r), &(fg.g), &(fg.b), &(fg.a));
|
773
1043
|
|
774
1044
|
ExportStringValue(text);
|
775
1045
|
result = TTF_RenderUTF8_Blended(font, StringValueCStr(text), fg);
|
776
1046
|
|
777
|
-
if (result)
|
778
|
-
|
1047
|
+
if (!result)
|
1048
|
+
TTF_FAILURE("Font.render");
|
1049
|
+
|
1050
|
+
return TypedData_Wrap_Struct(cSurface, &_Surface_type, result);
|
779
1051
|
}
|
780
1052
|
|
781
1053
|
static VALUE Font_draw(VALUE self, VALUE dst, VALUE text, VALUE x, VALUE y, VALUE c) {
|
782
1054
|
VALUE img = Font_render(self, dst, text, c);
|
783
|
-
|
784
|
-
|
785
|
-
Surface_s_blit(cSurface, img, zero, zero, zero, zero, dst, x, y);
|
786
|
-
|
787
|
-
return Qnil;
|
1055
|
+
return Renderer_blit(dst, img, x, y, Qnil, Qnil, Qnil, Qnil);
|
788
1056
|
}
|
789
1057
|
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
UNUSED(mod);
|
794
|
-
ExportStringValue(title);
|
795
|
-
ExportStringValue(icon);
|
1058
|
+
static VALUE Font_text_size(VALUE self, VALUE text) {
|
1059
|
+
DEFINE_SELF(TTFFont, font, self);
|
1060
|
+
int w = 1, h = 2, result;
|
796
1061
|
|
797
|
-
|
1062
|
+
ExportStringValue(text);
|
1063
|
+
result = TTF_SizeText(font, StringValueCStr(text), &w, &h);
|
798
1064
|
|
799
|
-
|
1065
|
+
if (!result) {
|
1066
|
+
return rb_ary_new_from_args(2, INT2FIX(w), INT2FIX(h));
|
1067
|
+
} else {
|
1068
|
+
FAILURE("SDL::TTF#text_size");
|
1069
|
+
}
|
800
1070
|
}
|
801
1071
|
|
802
1072
|
// The Rest...
|
803
1073
|
|
804
1074
|
void Init_sdl() {
|
805
|
-
|
806
1075
|
mSDL = rb_define_module("SDL");
|
807
1076
|
mKey = rb_define_module_under(mSDL, "Key");
|
808
|
-
mWM = rb_define_module_under(mSDL, "WM");
|
809
1077
|
mMouse = rb_define_module_under(mSDL, "Mouse");
|
810
1078
|
|
811
1079
|
cAudio = rb_define_class_under(mSDL, "Audio", rb_cData);
|
@@ -816,17 +1084,19 @@ void Init_sdl() {
|
|
816
1084
|
cTTFFont = rb_define_class_under(mSDL, "TTF", rb_cData); // TODO: Font
|
817
1085
|
|
818
1086
|
cScreen = rb_define_class_under(mSDL, "Screen", cSurface);
|
1087
|
+
cRenderer = rb_define_class_under(mSDL, "Renderer", rb_cData);
|
1088
|
+
cWindow = rb_define_class_under(mSDL, "Window", rb_cData);
|
1089
|
+
cTexture = rb_define_class_under(mSDL, "Texture", rb_cData);
|
819
1090
|
|
820
|
-
cEventQuit
|
1091
|
+
cEventQuit = rb_define_class_under(cEvent, "Quit", cEvent);
|
821
1092
|
cEventKeydown = rb_define_class_under(cEvent, "Keydown", cEvent);
|
822
|
-
cEventKeyup = rb_define_class_under(cEvent, "Keyup",
|
1093
|
+
cEventKeyup = rb_define_class_under(cEvent, "Keyup", cEvent);
|
823
1094
|
|
824
1095
|
cEventMousemove = rb_define_class_under(cEvent, "Mousemove", cEvent);
|
825
1096
|
cEventMousedown = rb_define_class_under(cEvent, "Mousedown", cEvent);
|
826
1097
|
cEventMouseup = rb_define_class_under(cEvent, "Mouseup", cEvent);
|
827
1098
|
|
828
1099
|
eSDLError = rb_define_class_under(mSDL, "Error", rb_eStandardError);
|
829
|
-
eSDLMem = rb_define_class_under(cSurface, "VideoMemoryLost", rb_eStandardError);
|
830
1100
|
|
831
1101
|
//// SDL methods:
|
832
1102
|
|
@@ -849,7 +1119,6 @@ void Init_sdl() {
|
|
849
1119
|
rb_define_attr(cEventKeydown, "press", 1, 1);
|
850
1120
|
rb_define_attr(cEventKeydown, "sym", 1, 1);
|
851
1121
|
rb_define_attr(cEventKeydown, "mod", 1, 1);
|
852
|
-
rb_define_attr(cEventKeydown, "unicode", 1, 1);
|
853
1122
|
|
854
1123
|
rb_define_attr(cEventKeyup, "press", 1, 1); // TODO: refactor, possibly subclass
|
855
1124
|
rb_define_attr(cEventKeyup, "sym", 1, 1);
|
@@ -882,62 +1151,74 @@ void Init_sdl() {
|
|
882
1151
|
rb_define_module_function(mMouse, "state", Mouse_s_state, 0);
|
883
1152
|
|
884
1153
|
//// SDL::PixelFormat methods:
|
1154
|
+
//// TODO: phase these out... move to renderer or top of SDL
|
885
1155
|
|
886
|
-
rb_define_method(cPixelFormat, "
|
1156
|
+
rb_define_method(cPixelFormat, "get_rgba", PixelFormat_get_rgba, 1);
|
887
1157
|
rb_define_method(cPixelFormat, "map_rgba", PixelFormat_map_rgba, 4);
|
888
|
-
rb_define_method(cPixelFormat, "colorkey", PixelFormat_colorkey, 0);
|
889
|
-
rb_define_method(cPixelFormat, "alpha", PixelFormat_alpha, 0);
|
890
1158
|
|
891
1159
|
//// SDL::Screen methods:
|
1160
|
+
//// TODO: phase these out entirely?
|
892
1161
|
|
893
1162
|
rb_define_singleton_method(cScreen, "open", Screen_s_open, 4);
|
894
|
-
|
895
|
-
|
1163
|
+
|
1164
|
+
id_W = rb_intern("W");
|
1165
|
+
id_H = rb_intern("H");
|
1166
|
+
rb_const_set(cScreen, id_W, Qnil);
|
1167
|
+
rb_const_set(cScreen, id_H, Qnil);
|
1168
|
+
|
1169
|
+
//// SDL::Window methods:
|
1170
|
+
|
1171
|
+
// TODO: move to top renderer?
|
1172
|
+
rb_define_method(cWindow, "title", Window_title, 0);
|
1173
|
+
rb_define_method(cWindow, "title=", Window_title_eq, 1);
|
1174
|
+
rb_define_method(cWindow, "update", Window_update, 0);
|
1175
|
+
|
1176
|
+
//// SDL::Renderer methods:
|
1177
|
+
|
1178
|
+
rb_define_method(cRenderer, "[]", Renderer_index, 2);
|
1179
|
+
rb_define_method(cRenderer, "[]=", Renderer_index_eq, 3);
|
1180
|
+
rb_define_method(cRenderer, "blit", Renderer_blit, 7);
|
1181
|
+
rb_define_method(cRenderer, "clear", Renderer_clear, 1);
|
1182
|
+
rb_define_method(cRenderer, "copy_texture", Renderer_copy_texture, 1);
|
1183
|
+
rb_define_method(cRenderer, "draw_bezier", Renderer_draw_bezier, 4);
|
1184
|
+
rb_define_method(cRenderer, "draw_circle", Renderer_draw_circle, 6);
|
1185
|
+
rb_define_method(cRenderer, "draw_ellipse", Renderer_draw_ellipse, 7);
|
1186
|
+
rb_define_method(cRenderer, "draw_line", Renderer_draw_line, 6);
|
1187
|
+
rb_define_method(cRenderer, "draw_rect", Renderer_draw_rect, 6);
|
1188
|
+
rb_define_method(cRenderer, "fast_rect", Renderer_fast_rect, 5);
|
1189
|
+
rb_define_method(cRenderer, "h", Renderer_h, 0);
|
1190
|
+
rb_define_method(cRenderer, "new_texture", Renderer_new_texture, 0);
|
1191
|
+
rb_define_method(cRenderer, "present", Renderer_present, 0);
|
1192
|
+
rb_define_method(cRenderer, "save", Renderer_save, 1);
|
1193
|
+
rb_define_method(cRenderer, "sprite", Renderer_sprite, 2);
|
1194
|
+
rb_define_method(cRenderer, "target", Renderer_target, 0);
|
1195
|
+
rb_define_method(cRenderer, "target=", Renderer_target_eq, 1);
|
1196
|
+
rb_define_method(cRenderer, "w", Renderer_w, 0);
|
896
1197
|
|
897
1198
|
//// SDL::Surface methods:
|
898
1199
|
|
899
|
-
rb_define_singleton_method(cSurface, "blit", Surface_s_blit, 8);
|
900
|
-
rb_define_singleton_method(cSurface, "new", Surface_s_new, 3);
|
901
1200
|
rb_define_singleton_method(cSurface, "load", Surface_s_load, 1);
|
902
|
-
|
903
|
-
rb_define_method(cSurface, "
|
904
|
-
rb_define_method(cSurface, "
|
905
|
-
rb_define_method(cSurface, "
|
906
|
-
rb_define_method(cSurface, "
|
907
|
-
rb_define_method(cSurface, "
|
908
|
-
|
909
|
-
|
910
|
-
rb_define_method(cSurface, "fill_rect", Surface_fill_rect, 5);
|
911
|
-
rb_define_method(cSurface, "fast_rect", Surface_fast_rect, 5);
|
912
|
-
rb_define_method(cSurface, "format", Surface_format, 0);
|
913
|
-
rb_define_method(cSurface, "h", Surface_h, 0);
|
1201
|
+
|
1202
|
+
rb_define_method(cSurface, "h", Surface_h, 0);
|
1203
|
+
rb_define_method(cSurface, "[]", Surface_index, 2);
|
1204
|
+
rb_define_method(cSurface, "format", Surface_format, 0);
|
1205
|
+
rb_define_method(cSurface, "transform", Surface_transform, 4);
|
1206
|
+
rb_define_method(cSurface, "w", Surface_w, 0);
|
1207
|
+
|
1208
|
+
// TODO: reimplement and jettison SGE
|
914
1209
|
rb_define_method(cSurface, "make_collision_map", Surface_make_collision_map, 0);
|
915
|
-
rb_define_method(cSurface, "w", Surface_w, 0);
|
916
|
-
rb_define_method(cSurface, "[]", Surface_index, 2);
|
917
|
-
rb_define_method(cSurface, "[]=", Surface_index_equals, 3);
|
918
|
-
rb_define_method(cSurface, "transform", Surface_transform, 5);
|
919
|
-
rb_define_method(cSurface, "save", Surface_save, 1);
|
920
|
-
rb_define_method(cSurface, "flags", Surface_flags, 0);
|
921
|
-
rb_define_method(cSurface, "set_alpha", Surface_set_alpha, 2);
|
922
1210
|
|
923
1211
|
//// SDL::TTFFont methods:
|
924
1212
|
|
925
1213
|
rb_define_singleton_method(cTTFFont, "open", Font_s_open, 2);
|
926
1214
|
|
927
|
-
rb_define_method(cTTFFont, "height",
|
928
|
-
rb_define_method(cTTFFont, "render",
|
929
|
-
rb_define_method(cTTFFont, "draw",
|
930
|
-
|
931
|
-
//// SDL::WM methods:
|
932
|
-
|
933
|
-
rb_define_module_function(mWM, "set_caption", WM_s_set_caption, 2);
|
1215
|
+
rb_define_method(cTTFFont, "height", Font_height, 0);
|
1216
|
+
rb_define_method(cTTFFont, "render", Font_render, 3);
|
1217
|
+
rb_define_method(cTTFFont, "draw", Font_draw, 5);
|
1218
|
+
rb_define_method(cTTFFont, "text_size", Font_text_size, 1);
|
934
1219
|
|
935
1220
|
//// Other Init Actions:
|
936
1221
|
|
937
|
-
sge_Lock_ON();
|
938
|
-
sge_Update_OFF();
|
939
|
-
SDL_EnableUNICODE(1);
|
940
|
-
|
941
1222
|
for (int i=0; i < SDL_NUMEVENTS; ++i)
|
942
1223
|
event_creators[i] = Event__null;
|
943
1224
|
|
@@ -951,42 +1232,49 @@ void Init_sdl() {
|
|
951
1232
|
// event_creators[SDL_SYSWMEVENT] = Event__syswm;
|
952
1233
|
// event_creators[SDL_VIDEORESIZE] = Event__videoresize;
|
953
1234
|
|
1235
|
+
// TODO: maybe pause/unpause automatically instead of chewing CPU?
|
1236
|
+
// SDL_APP_DIDENTERBACKGROUND
|
1237
|
+
// SDL_APP_DIDENTERFOREGROUND
|
1238
|
+
|
954
1239
|
rb_set_end_proc(sdl__quit, 0);
|
955
1240
|
|
956
1241
|
//// Simple Mapped Constants:
|
957
1242
|
|
1243
|
+
INIT_ID(surface);
|
1244
|
+
INIT_ID(format);
|
1245
|
+
INIT_ID(renderer);
|
1246
|
+
INIT_ID(window);
|
1247
|
+
INIT_ID(texture);
|
958
1248
|
INIT_ID(button);
|
959
1249
|
INIT_ID(mod);
|
960
1250
|
INIT_ID(press);
|
961
1251
|
INIT_ID(state);
|
962
1252
|
INIT_ID(sym);
|
963
|
-
INIT_ID(unicode);
|
964
1253
|
INIT_ID(x);
|
965
1254
|
INIT_ID(xrel);
|
966
1255
|
INIT_ID(y);
|
967
1256
|
INIT_ID(yrel);
|
968
1257
|
|
969
1258
|
#define DC(n) rb_define_const(mSDL, #n, UINT2NUM(SDL_##n))
|
970
|
-
DC(DOUBLEBUF);
|
971
|
-
DC(HWSURFACE);
|
972
1259
|
DC(INIT_EVERYTHING);
|
973
|
-
DC(INIT_VIDEO);
|
974
|
-
DC(
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
1260
|
+
DC(INIT_VIDEO); // TODO: phase out? it's in the tests...
|
1261
|
+
DC(TRUE);
|
1262
|
+
|
1263
|
+
#define DW(n) rb_define_const(mSDL, #n, UINT2NUM(SDL_WINDOW_##n))
|
1264
|
+
DW(FULLSCREEN);
|
1265
|
+
DW(OPENGL);
|
1266
|
+
DW(SHOWN);
|
1267
|
+
DW(HIDDEN);
|
1268
|
+
DW(BORDERLESS);
|
1269
|
+
DW(RESIZABLE);
|
1270
|
+
DW(MINIMIZED);
|
1271
|
+
DW(MAXIMIZED);
|
1272
|
+
DW(INPUT_GRABBED);
|
1273
|
+
DW(INPUT_FOCUS);
|
1274
|
+
DW(MOUSE_FOCUS);
|
1275
|
+
DW(FULLSCREEN_DESKTOP);
|
1276
|
+
DW(FOREIGN);
|
1277
|
+
DW(ALLOW_HIGHDPI);
|
990
1278
|
|
991
1279
|
//// Keyboard Constants
|
992
1280
|
|
@@ -995,9 +1283,8 @@ void Init_sdl() {
|
|
995
1283
|
#define DK(n) _KEY(#n, SDLK_##n)
|
996
1284
|
#define DKP(n) _KEY("K"#n, SDLK_##n)
|
997
1285
|
#define DM(n) _KEY("MOD_"#n, KMOD_##n)
|
998
|
-
#define DR(n) _KEY("DEFAULT_REPEAT_"#n, SDL_DEFAULT_REPEAT_##n)
|
999
1286
|
|
1000
|
-
DK(UNKNOWN);
|
1287
|
+
DK(UNKNOWN); DK(BACKSPACE); DK(TAB); DK(CLEAR);
|
1001
1288
|
DK(RETURN); DK(PAUSE); DK(ESCAPE); DK(SPACE); DK(EXCLAIM);
|
1002
1289
|
DK(QUOTEDBL); DK(HASH); DK(DOLLAR); DK(AMPERSAND); DK(QUOTE);
|
1003
1290
|
DK(LEFTPAREN); DK(RIGHTPAREN); DK(ASTERISK); DK(PLUS); DK(COMMA);
|
@@ -1017,31 +1304,9 @@ void Init_sdl() {
|
|
1017
1304
|
KEY("U", u); KEY("V", v); KEY("W", w); KEY("X", x); KEY("Y", y);
|
1018
1305
|
KEY("Z", z);
|
1019
1306
|
|
1020
|
-
// International keyboard syms
|
1021
|
-
DK(WORLD_0); DK(WORLD_1); DK(WORLD_2); DK(WORLD_3); DK(WORLD_4);
|
1022
|
-
DK(WORLD_5); DK(WORLD_6); DK(WORLD_7); DK(WORLD_8); DK(WORLD_9);
|
1023
|
-
DK(WORLD_10); DK(WORLD_11); DK(WORLD_12); DK(WORLD_13); DK(WORLD_14);
|
1024
|
-
DK(WORLD_15); DK(WORLD_16); DK(WORLD_17); DK(WORLD_18); DK(WORLD_19);
|
1025
|
-
DK(WORLD_20); DK(WORLD_21); DK(WORLD_22); DK(WORLD_23); DK(WORLD_24);
|
1026
|
-
DK(WORLD_25); DK(WORLD_26); DK(WORLD_27); DK(WORLD_28); DK(WORLD_29);
|
1027
|
-
DK(WORLD_30); DK(WORLD_31); DK(WORLD_32); DK(WORLD_33); DK(WORLD_34);
|
1028
|
-
DK(WORLD_35); DK(WORLD_36); DK(WORLD_37); DK(WORLD_38); DK(WORLD_39);
|
1029
|
-
DK(WORLD_40); DK(WORLD_41); DK(WORLD_42); DK(WORLD_43); DK(WORLD_44);
|
1030
|
-
DK(WORLD_45); DK(WORLD_46); DK(WORLD_47); DK(WORLD_48); DK(WORLD_49);
|
1031
|
-
DK(WORLD_50); DK(WORLD_51); DK(WORLD_52); DK(WORLD_53); DK(WORLD_54);
|
1032
|
-
DK(WORLD_55); DK(WORLD_56); DK(WORLD_57); DK(WORLD_58); DK(WORLD_59);
|
1033
|
-
DK(WORLD_60); DK(WORLD_61); DK(WORLD_62); DK(WORLD_63); DK(WORLD_64);
|
1034
|
-
DK(WORLD_65); DK(WORLD_66); DK(WORLD_67); DK(WORLD_68); DK(WORLD_69);
|
1035
|
-
DK(WORLD_70); DK(WORLD_71); DK(WORLD_72); DK(WORLD_73); DK(WORLD_74);
|
1036
|
-
DK(WORLD_75); DK(WORLD_76); DK(WORLD_77); DK(WORLD_78); DK(WORLD_79);
|
1037
|
-
DK(WORLD_80); DK(WORLD_81); DK(WORLD_82); DK(WORLD_83); DK(WORLD_84);
|
1038
|
-
DK(WORLD_85); DK(WORLD_86); DK(WORLD_87); DK(WORLD_88); DK(WORLD_89);
|
1039
|
-
DK(WORLD_90); DK(WORLD_91); DK(WORLD_92); DK(WORLD_93); DK(WORLD_94);
|
1040
|
-
DK(WORLD_95);
|
1041
|
-
|
1042
1307
|
// Numeric keypad
|
1043
|
-
DK(
|
1044
|
-
DK(
|
1308
|
+
DK(KP_0); DK(KP_1); DK(KP_2); DK(KP_3); DK(KP_4);
|
1309
|
+
DK(KP_5); DK(KP_6); DK(KP_7); DK(KP_8); DK(KP_9);
|
1045
1310
|
|
1046
1311
|
DK(KP_PERIOD); DK(KP_DIVIDE); DK(KP_MULTIPLY); DK(KP_MINUS);
|
1047
1312
|
DK(KP_PLUS); DK(KP_ENTER); DK(KP_EQUALS);
|
@@ -1055,20 +1320,14 @@ void Init_sdl() {
|
|
1055
1320
|
DK(F10); DK(F11); DK(F12); DK(F13); DK(F14); DK(F15);
|
1056
1321
|
|
1057
1322
|
// Key state modifier keys
|
1058
|
-
DK(
|
1059
|
-
DK(LCTRL); DK(RALT); DK(LALT); DK(
|
1060
|
-
DK(MODE);
|
1323
|
+
DK(CAPSLOCK); DK(SCROLLLOCK); DK(RSHIFT); DK(LSHIFT); DK(RCTRL);
|
1324
|
+
DK(LCTRL); DK(RALT); DK(LALT); DK(RGUI); DK(LGUI); DK(MODE);
|
1061
1325
|
|
1062
1326
|
// Miscellaneous function keys
|
1063
|
-
DK(HELP); DK(
|
1064
|
-
DK(MENU); DK(POWER); DK(EURO); DK(LAST);
|
1327
|
+
DK(HELP); DK(SYSREQ); DK(MENU); DK(POWER);
|
1065
1328
|
|
1066
1329
|
// key mods
|
1067
1330
|
DM(NONE); DM(LSHIFT); DM(RSHIFT); DM(LCTRL); DM(RCTRL); DM(LALT); DM(RALT);
|
1068
|
-
DM(
|
1069
|
-
DM(SHIFT); DM(ALT); DM(
|
1070
|
-
|
1071
|
-
// key repeat constants
|
1072
|
-
DR(DELAY);
|
1073
|
-
DR(INTERVAL);
|
1331
|
+
DM(LGUI); DM(RGUI); DM(NUM); DM(CAPS); DM(MODE); DM(RESERVED); DM(CTRL);
|
1332
|
+
DM(SHIFT); DM(ALT); DM(GUI);
|
1074
1333
|
}
|