SDLRuby 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/Rakefile +12 -0
- data/lib/SDLRuby/audio.rb +65 -0
- data/lib/SDLRuby/cursor.rb +46 -0
- data/lib/SDLRuby/display.rb +79 -0
- data/lib/SDLRuby/event/accessor.rb +127 -0
- data/lib/SDLRuby/event/type.rb +75 -0
- data/lib/SDLRuby/event.rb +80 -0
- data/lib/SDLRuby/font.rb +227 -0
- data/lib/SDLRuby/hint.rb +36 -0
- data/lib/SDLRuby/image/image.rb +23 -0
- data/lib/SDLRuby/image/include/SDL_image.h.rb +61 -0
- data/lib/SDLRuby/image.rb +69 -0
- data/lib/SDLRuby/keyboard.rb +58 -0
- data/lib/SDLRuby/mixer/channel.rb +43 -0
- data/lib/SDLRuby/mixer/chunk.rb +49 -0
- data/lib/SDLRuby/mixer/controller.rb +44 -0
- data/lib/SDLRuby/mixer/include/SDL_mixer.h.rb +95 -0
- data/lib/SDLRuby/mixer/sdl_mixer.rb +60 -0
- data/lib/SDLRuby/mixer.rb +87 -0
- data/lib/SDLRuby/mouse.rb +52 -0
- data/lib/SDLRuby/music.rb +158 -0
- data/lib/SDLRuby/palette.rb +49 -0
- data/lib/SDLRuby/pixel_formatter.rb +67 -0
- data/lib/SDLRuby/rect.rb +11 -0
- data/lib/SDLRuby/renderer/accessor.rb +70 -0
- data/lib/SDLRuby/renderer/drawer.rb +107 -0
- data/lib/SDLRuby/renderer/renderer_info.rb +27 -0
- data/lib/SDLRuby/renderer.rb +101 -0
- data/lib/SDLRuby/rw_ops/rw_object.rb +128 -0
- data/lib/SDLRuby/rw_ops.rb +216 -0
- data/lib/SDLRuby/sdl/include/SDL.h.rb +33 -0
- data/lib/SDLRuby/sdl/include/SDL_audio.h.rb +49 -0
- data/lib/SDLRuby/sdl/include/SDL_blendmode.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_clipboard.h.rb +10 -0
- data/lib/SDLRuby/sdl/include/SDL_config.h.rb +13 -0
- data/lib/SDLRuby/sdl/include/SDL_cpuinfo.h.rb +27 -0
- data/lib/SDLRuby/sdl/include/SDL_endian.h.rb +3 -0
- data/lib/SDLRuby/sdl/include/SDL_error.h.rb +9 -0
- data/lib/SDLRuby/sdl/include/SDL_events.h.rb +32 -0
- data/lib/SDLRuby/sdl/include/SDL_filesystem.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_gamecontroller.h.rb +66 -0
- data/lib/SDLRuby/sdl/include/SDL_gesture.h.rb +12 -0
- data/lib/SDLRuby/sdl/include/SDL_guid.h.rb +7 -0
- data/lib/SDLRuby/sdl/include/SDL_hints.h.rb +14 -0
- data/lib/SDLRuby/sdl/include/SDL_joystick.h.rb +69 -0
- data/lib/SDLRuby/sdl/include/SDL_keyboard.h.rb +26 -0
- data/lib/SDLRuby/sdl/include/SDL_keycode.h.rb +7 -0
- data/lib/SDLRuby/sdl/include/SDL_locale.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_main.h.rb +3 -0
- data/lib/SDLRuby/sdl/include/SDL_messagebox.h.rb +10 -0
- data/lib/SDLRuby/sdl/include/SDL_misc.h.rb +5 -0
- data/lib/SDLRuby/sdl/include/SDL_mouse.h.rb +26 -0
- data/lib/SDLRuby/sdl/include/SDL_pixels.h.rb +25 -0
- data/lib/SDLRuby/sdl/include/SDL_platform.h.rb +3 -0
- data/lib/SDLRuby/sdl/include/SDL_power.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_rect.h.rb +17 -0
- data/lib/SDLRuby/sdl/include/SDL_render.h.rb +94 -0
- data/lib/SDLRuby/sdl/include/SDL_revision.h.rb +2 -0
- data/lib/SDLRuby/sdl/include/SDL_rwops.h.rb +34 -0
- data/lib/SDLRuby/sdl/include/SDL_scancode.h.rb +5 -0
- data/lib/SDLRuby/sdl/include/SDL_sensor.h.rb +25 -0
- data/lib/SDLRuby/sdl/include/SDL_shape.h.rb +13 -0
- data/lib/SDLRuby/sdl/include/SDL_stdinc.h.rb +24 -0
- data/lib/SDLRuby/sdl/include/SDL_surface.h.rb +37 -0
- data/lib/SDLRuby/sdl/include/SDL_system.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_syswm.h.rb +16 -0
- data/lib/SDLRuby/sdl/include/SDL_timer.h.rb +13 -0
- data/lib/SDLRuby/sdl/include/SDL_touch.h.rb +15 -0
- data/lib/SDLRuby/sdl/include/SDL_types.h.rb +3 -0
- data/lib/SDLRuby/sdl/include/SDL_version.h.rb +6 -0
- data/lib/SDLRuby/sdl/include/SDL_video.h.rb +117 -0
- data/lib/SDLRuby/sdl/sdl.rb +55 -0
- data/lib/SDLRuby/sdl/sdl_audio.rb +75 -0
- data/lib/SDLRuby/sdl/sdl_blendmode.rb +10 -0
- data/lib/SDLRuby/sdl/sdl_error.rb +5 -0
- data/lib/SDLRuby/sdl/sdl_events.rb +470 -0
- data/lib/SDLRuby/sdl/sdl_hints.rb +9 -0
- data/lib/SDLRuby/sdl/sdl_keyboard.rb +12 -0
- data/lib/SDLRuby/sdl/sdl_locale.rb +8 -0
- data/lib/SDLRuby/sdl/sdl_message_box.rb +7 -0
- data/lib/SDLRuby/sdl/sdl_mouse.rb +32 -0
- data/lib/SDLRuby/sdl/sdl_palette.rb +12 -0
- data/lib/SDLRuby/sdl/sdl_pixels.rb +138 -0
- data/lib/SDLRuby/sdl/sdl_rect.rb +11 -0
- data/lib/SDLRuby/sdl/sdl_render.rb +35 -0
- data/lib/SDLRuby/sdl/sdl_rw_ops.rb +29 -0
- data/lib/SDLRuby/sdl/sdl_scancode.rb +255 -0
- data/lib/SDLRuby/sdl/sdl_surface.rb +27 -0
- data/lib/SDLRuby/sdl/sdl_version.rb +5 -0
- data/lib/SDLRuby/sdl/sdl_video.rb +104 -0
- data/lib/SDLRuby/sdl.rb +192 -0
- data/lib/SDLRuby/sdl_error.rb +8 -0
- data/lib/SDLRuby/surface/pixel_format.rb +51 -0
- data/lib/SDLRuby/surface/surface_renderer.rb +26 -0
- data/lib/SDLRuby/surface.rb +179 -0
- data/lib/SDLRuby/text_input.rb +38 -0
- data/lib/SDLRuby/texture.rb +93 -0
- data/lib/SDLRuby/ttf/include/SDL_ttf.h.rb +54 -0
- data/lib/SDLRuby/ttf/ttf.rb +41 -0
- data/lib/SDLRuby/ttf.rb +54 -0
- data/lib/SDLRuby/version.rb +5 -0
- data/lib/SDLRuby/window/accessor.rb +125 -0
- data/lib/SDLRuby/window/controller.rb +53 -0
- data/lib/SDLRuby/window/display_info.rb +48 -0
- data/lib/SDLRuby/window/flag.rb +42 -0
- data/lib/SDLRuby/window/hit_tester.rb +61 -0
- data/lib/SDLRuby/window/input.rb +68 -0
- data/lib/SDLRuby/window/shaper.rb +68 -0
- data/lib/SDLRuby/window/surfacer.rb +37 -0
- data/lib/SDLRuby/window/window_renderer.rb +31 -0
- data/lib/SDLRuby/window.rb +90 -0
- data/lib/SDLRuby.rb +16 -0
- data/sig/SDLRuby/event.rbs +104 -0
- data/sig/SDLRuby/pixel_formmater.rbs +24 -0
- data/sig/SDLRuby/rect.rbs +5 -0
- data/sig/SDLRuby/sdl.rbs +606 -0
- data/sig/SDLRuby.rbs +4 -0
- metadata +168 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
module SDLRuby
|
2
|
+
module Renderer
|
3
|
+
module Drawer
|
4
|
+
include Fiddle, SDL
|
5
|
+
|
6
|
+
def draw_copy(texture,
|
7
|
+
angle: nil, center: nil, dst_rect:nil, flip: nil, src_rect: nil)
|
8
|
+
|
9
|
+
sr = src_rect && Rect[*src_rect]
|
10
|
+
dr = dst_rect && Rect[*dst_rect]
|
11
|
+
a = angle || 0
|
12
|
+
pt = if center
|
13
|
+
SDL_Point.malloc(RUBY_FREE).tap { |pt| pt.x, pt.y = *center }
|
14
|
+
end
|
15
|
+
fl = flip || 0 #=SDL_FLIP_NONE
|
16
|
+
|
17
|
+
err = SDL.SDL_RenderCopyEx(renderer, texture, sr, dr, a, pt, fl)
|
18
|
+
raise SDLError if err < 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def draw_fill(*f)
|
22
|
+
size = f.count
|
23
|
+
count = size / 4
|
24
|
+
raise ArgumentError if count.zero? || (size % 4).nonzero?
|
25
|
+
ptr = f.pack("f*")
|
26
|
+
|
27
|
+
err = SDL.SDL_RenderFillRectsF(renderer, ptr, count)
|
28
|
+
raise SDLError if err < 0
|
29
|
+
end
|
30
|
+
|
31
|
+
def draw_line(*f)
|
32
|
+
size = f.count
|
33
|
+
count = size / 2
|
34
|
+
raise ArgumentError if count.zero? || (size % 2).nonzero?
|
35
|
+
ptr = f.pack("f*")
|
36
|
+
|
37
|
+
err = SDL.SDL_RenderDrawLinesF(renderer, ptr, count)
|
38
|
+
raise SDLError if err < 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def draw_point(*f)
|
42
|
+
size = f.count
|
43
|
+
count = size / 2
|
44
|
+
raise ArgumentError if count.zero? || (size % 2).nonzero?
|
45
|
+
ptr = f.pack("f*")
|
46
|
+
|
47
|
+
err = SDL.SDL_RenderDrawPointsF(renderer, ptr, count)
|
48
|
+
raise SDLError if err < 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def draw_rect(*f)
|
52
|
+
size = f.count
|
53
|
+
count = size / 4
|
54
|
+
raise ArgumentError if count.zero? || (size % 4).nonzero?
|
55
|
+
ptr = f.pack("f*")
|
56
|
+
|
57
|
+
err = SDL.SDL_RenderDrawRectsF(renderer, ptr, count)
|
58
|
+
raise SDLError if err < 0
|
59
|
+
end
|
60
|
+
|
61
|
+
def render_blend_mode
|
62
|
+
mode = SDL.tmp_value("int")
|
63
|
+
err = SDL.SDL_GetRenderDrawBlendMode(renderer, mode)
|
64
|
+
raise SDLError if err < 0
|
65
|
+
|
66
|
+
mode.value
|
67
|
+
end
|
68
|
+
|
69
|
+
def render_blend_mode=(mode)
|
70
|
+
err = int SDL_SetRenderDrawBlendMode(renderer, mode)
|
71
|
+
raise SDLError if err < 0
|
72
|
+
end
|
73
|
+
|
74
|
+
def render_clear
|
75
|
+
err = SDL.SDL_RenderClear(renderer)
|
76
|
+
raise SDLError if err < 0
|
77
|
+
end
|
78
|
+
|
79
|
+
def render_clip
|
80
|
+
r = Rect[]
|
81
|
+
SDL.SDL_RenderGetClipRect(renderer, r)
|
82
|
+
r.to_a
|
83
|
+
end
|
84
|
+
|
85
|
+
def render_clip=(rect)
|
86
|
+
err = SDL.SDL_RenderSetClipRect(renderer, Rect[*rect])
|
87
|
+
raise SDLError if err < 0
|
88
|
+
end
|
89
|
+
|
90
|
+
def render_clipped? = SDL.SDL_RenderIsClipEnabled(renderer) != 0
|
91
|
+
|
92
|
+
def render_color
|
93
|
+
r, g, b, a = SDL.tmp_value("Uint8", "Uint8", "Uint8", "Uint8")
|
94
|
+
err = SDL.SDL_GetRenderDrawColor(renderer, r, g, b, a)
|
95
|
+
raise SDLError if err < 0
|
96
|
+
|
97
|
+
[r.value, g.value, b.value, a.value]
|
98
|
+
end
|
99
|
+
|
100
|
+
def render_color=(obj)
|
101
|
+
r, g, b, a = *obj, SDL_ALPHA_OPAQUE
|
102
|
+
err = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a)
|
103
|
+
raise SDLError if err < 0
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module SDLRuby
|
2
|
+
module Renderer
|
3
|
+
class RendererInfo
|
4
|
+
include SDL
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@st = SDL_RendererInfo.malloc(Fiddle::RUBY_FREE)
|
9
|
+
end
|
10
|
+
|
11
|
+
def_delegators :@st,
|
12
|
+
:flags, :to_ptr, :max_texture_height, :max_texture_width
|
13
|
+
|
14
|
+
def accelerated? = flags & SDL_RENDERER_ACCELERATED != 0
|
15
|
+
|
16
|
+
def renderer_formats = @st.texture_formats[0, @st.num_texture_formats]
|
17
|
+
|
18
|
+
def renderer_name = @st.name.to_s
|
19
|
+
|
20
|
+
def software_renderer? = flags & SDL_RENDERER_SOFTWARE != 0
|
21
|
+
|
22
|
+
def max_texture_size = [@st.max_texture_height, @st.max_texture_width]
|
23
|
+
|
24
|
+
def vsync? = flags & SDL_RENDERER_PRESENTVSYNC != 0
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative 'renderer/accessor'
|
2
|
+
require_relative 'renderer/drawer'
|
3
|
+
require_relative 'renderer/renderer_info'
|
4
|
+
|
5
|
+
module SDLRuby
|
6
|
+
module Renderer
|
7
|
+
include Accessor, Drawer, SDL
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def drivers
|
12
|
+
num = SDL.SDL_GetNumRenderDrivers
|
13
|
+
raise SDLError if num < 0
|
14
|
+
|
15
|
+
num.times.map do |idx|
|
16
|
+
st = RendererInfo.new
|
17
|
+
err = SDL.SDL_GetRenderDriverInfo(idx, st)
|
18
|
+
raise SDLError if err < 0
|
19
|
+
|
20
|
+
st.renderer_name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def driver_info(name)
|
25
|
+
idx = drivers.index(name)
|
26
|
+
return unless idx
|
27
|
+
|
28
|
+
st = RendererInfo.new
|
29
|
+
err = SDL.SDL_GetRenderDriverInfo(idx, st)
|
30
|
+
raise SDLError if err < 0
|
31
|
+
|
32
|
+
st
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private def renderer_info
|
37
|
+
st = RendererInfo.new
|
38
|
+
err = SDL.SDL_GetRendererInfo(renderer, st)
|
39
|
+
raise SDLError if err < 0
|
40
|
+
|
41
|
+
@renderer_info ||= st
|
42
|
+
end
|
43
|
+
|
44
|
+
def_delegators :renderer_info,
|
45
|
+
:renderer_name, :software_renderer?, :max_texture_size
|
46
|
+
|
47
|
+
def create_texture(w, h, format, access: nil)
|
48
|
+
ac = access || SDL_TEXTUREACCESS_TARGET
|
49
|
+
ptr = SDL.SDL_CreateTexture(renderer, format, ac, w, h)
|
50
|
+
raise SDLError if ptr.null?
|
51
|
+
Texture.new(ptr)
|
52
|
+
end
|
53
|
+
|
54
|
+
def import_texture(surface)
|
55
|
+
ptr = SDL.SDL_CreateTextureFromSurface(renderer, surface)
|
56
|
+
raise SDLError if ptr.null?
|
57
|
+
Texture.new(ptr)
|
58
|
+
end
|
59
|
+
|
60
|
+
def load_texture(file, type: nil)
|
61
|
+
if String === file
|
62
|
+
rw = SDL.SDL_RWFromFile(file, "rb")
|
63
|
+
free = 1
|
64
|
+
elsif file.respond_to?(:to_path)
|
65
|
+
rw = SDL.SDL_RWFromFile(file.to_path, "rb")
|
66
|
+
free = 1
|
67
|
+
else
|
68
|
+
rw = RWOps[file]
|
69
|
+
free = 0
|
70
|
+
end
|
71
|
+
|
72
|
+
ptr = Image.IMG_LoadTextureTyped_RW(renderer, rw, free, type)
|
73
|
+
raise SDLError if ptr.null?
|
74
|
+
Texture.new(ptr)
|
75
|
+
end
|
76
|
+
|
77
|
+
def present = SDL.SDL_RenderPresent(renderer)
|
78
|
+
|
79
|
+
def default_texture? = SDL.SDL_GetRenderTarget(renderer).null?
|
80
|
+
|
81
|
+
# 値がnilの時はデフォルトのレンダーターゲット
|
82
|
+
#
|
83
|
+
def target_texture=(texture)
|
84
|
+
err = SDL.SDL_SetRenderTarget(renderer, texture)
|
85
|
+
raise SDLError if err < 0
|
86
|
+
end
|
87
|
+
|
88
|
+
def vsync?
|
89
|
+
st = RendererInfo.new
|
90
|
+
err = SDL.SDL_GetRendererInfo(renderer, st)
|
91
|
+
raise SDLError if err < 0
|
92
|
+
|
93
|
+
st.vsync?
|
94
|
+
end
|
95
|
+
|
96
|
+
def vsync=(b)
|
97
|
+
err = SDL.SDL_RenderSetVSync(renderer, b ? 1 : 0)
|
98
|
+
raise SDLError if err != 0
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
module SDLRuby
|
2
|
+
class RWOps
|
3
|
+
class RWObject
|
4
|
+
include Fiddle, SDL
|
5
|
+
|
6
|
+
# size_t (*read) (SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
|
7
|
+
#
|
8
|
+
class ReadClosure < Fiddle::Closure
|
9
|
+
include Fiddle
|
10
|
+
|
11
|
+
def initialize(obj)
|
12
|
+
super(TYPE_SIZE_T, [TYPE_VOIDP, TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T])
|
13
|
+
@obj = obj
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(_, ptr, size, maxnum)
|
17
|
+
len = size * maxnum
|
18
|
+
return 0 if len.zero?
|
19
|
+
|
20
|
+
s = @obj.read(len)
|
21
|
+
return 0 if s.nil? || s.empty? # EOF
|
22
|
+
|
23
|
+
ptr[0, s.size] = s
|
24
|
+
s.size / size
|
25
|
+
rescue => e
|
26
|
+
SDL.last_error_message = e.full_message
|
27
|
+
0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Sint64 (*seek) (SDL_RWops *context, Sint64 offset, int whence)
|
32
|
+
#
|
33
|
+
class SeekClosure < Fiddle::Closure
|
34
|
+
include Fiddle
|
35
|
+
|
36
|
+
def initialize(obj)
|
37
|
+
super(TYPE_INT64_T, [TYPE_VOIDP, TYPE_INT64_T, TYPE_INT])
|
38
|
+
@obj = obj
|
39
|
+
end
|
40
|
+
|
41
|
+
def call(_, offset, whence)
|
42
|
+
@obj.seek(offset, whence)
|
43
|
+
@obj.tell
|
44
|
+
rescue => e
|
45
|
+
SDL.last_error_message = e.full_message
|
46
|
+
-1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Sint64 (*size) (SDL_RWops *context)
|
51
|
+
#
|
52
|
+
class SizeClosure < Fiddle::Closure
|
53
|
+
include Fiddle
|
54
|
+
|
55
|
+
def initialize(obj)
|
56
|
+
super(TYPE_INT64_T, [TYPE_VOIDP])
|
57
|
+
@obj = obj
|
58
|
+
end
|
59
|
+
|
60
|
+
def call(_)
|
61
|
+
@obj.size
|
62
|
+
rescue => e
|
63
|
+
raise e if $DEBUG
|
64
|
+
SDL.last_error_message = e.full_message
|
65
|
+
-1
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# size_t (*write) (SDL_RWops *context, const void *ptr, size_t size, size_t num)
|
70
|
+
#
|
71
|
+
class WriteClosure < Fiddle::Closure
|
72
|
+
include Fiddle
|
73
|
+
|
74
|
+
def initialize(obj)
|
75
|
+
super(TYPE_SIZE_T, [TYPE_VOIDP, TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T])
|
76
|
+
@obj = obj
|
77
|
+
end
|
78
|
+
|
79
|
+
def call(_, ptr, size, num)
|
80
|
+
@obj.write(ptr.to_str(size * num)) / size
|
81
|
+
rescue => e
|
82
|
+
SDL.last_error_message = e.full_message
|
83
|
+
0
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# closeにはC関数を設定する必要がある。
|
88
|
+
# クロージャ―を設定するとGCによる回収の際に
|
89
|
+
# 該当のクロージャ―が既に回収されているためコアダンプする。
|
90
|
+
# また、NULLに設定するとSDL_RWclose関数の呼び出し時にコアダンプする。
|
91
|
+
# SDLをハックしてmem_close関数を割り当てた。
|
92
|
+
# この関数は内部でSDL_FreeRW関数を呼び出すだけである。
|
93
|
+
#
|
94
|
+
# int (*close) (SDL_RWops *context)
|
95
|
+
#
|
96
|
+
MEM_CLOSE = module_eval do
|
97
|
+
_ptr = Pointer.malloc(1, RUBY_FREE)
|
98
|
+
rw = SDL.SDL_RWFromMem(_ptr, _ptr.size)
|
99
|
+
raise SDLError if rw.null?
|
100
|
+
st = SDL_RWops.new(rw)
|
101
|
+
|
102
|
+
st.close.freeze
|
103
|
+
end
|
104
|
+
|
105
|
+
# 引数 obj に与えたオブジェクトは SDL から close を呼び出されてもクローズしない。
|
106
|
+
# このインスタンスを単独で扱う場合は使い終わったらclose関数を呼び出す必要がある。
|
107
|
+
# そうしなければポインターは開放されずメモリーリークする。
|
108
|
+
#
|
109
|
+
def initialize(obj)
|
110
|
+
ptr = SDL.SDL_AllocRW
|
111
|
+
raise SDLError if ptr.null?
|
112
|
+
|
113
|
+
# SDLではSDL_RWopsポインターの開放はclose関数から行うように実装されている。
|
114
|
+
# これと同じ仕様とするため、このインスタンスではfreeの設定を行わない。
|
115
|
+
#
|
116
|
+
@st = SDL_RWops.new(ptr)
|
117
|
+
|
118
|
+
@st.close = MEM_CLOSE
|
119
|
+
@st.read = @read = ReadClosure.new(obj)
|
120
|
+
@st.seek = @seek = SeekClosure.new(obj)
|
121
|
+
@st.size = @size = SizeClosure.new(obj)
|
122
|
+
@st.write = @write = WriteClosure.new(obj)
|
123
|
+
end
|
124
|
+
|
125
|
+
def to_ptr = @st.to_ptr
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,216 @@
|
|
1
|
+
require_relative 'rw_ops/rw_object'
|
2
|
+
|
3
|
+
module SDLRuby
|
4
|
+
class RWOps
|
5
|
+
include Fiddle, SDL
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def [](src, autoclose: nil, readonly: nil)
|
9
|
+
case src
|
10
|
+
when RWOps
|
11
|
+
return src
|
12
|
+
when String
|
13
|
+
size = src.bytesize
|
14
|
+
ptr = if readonly
|
15
|
+
SDL.SDL_RWFromConstMem(src, size)
|
16
|
+
else
|
17
|
+
SDL.SDL_RWFromMem(src, size)
|
18
|
+
end
|
19
|
+
raise SDLError if ptr.null?
|
20
|
+
when Fiddle::Pointer
|
21
|
+
size = src.size
|
22
|
+
ptr = if readonly
|
23
|
+
SDL.SDL_RWFromConstMem(src, size)
|
24
|
+
else
|
25
|
+
SDL.SDL_RWFromMem(src, size)
|
26
|
+
end
|
27
|
+
raise SDLError if ptr.null?
|
28
|
+
else
|
29
|
+
src = RWObject.new(src)
|
30
|
+
ptr = src.to_ptr
|
31
|
+
end
|
32
|
+
|
33
|
+
obj = allocate
|
34
|
+
obj.__send__(:initialize, ptr, src)
|
35
|
+
obj
|
36
|
+
end
|
37
|
+
|
38
|
+
def new(file, mode = "rb")
|
39
|
+
ptr = SDL.SDL_RWFromFile(file, mode)
|
40
|
+
raise SDLError if ptr.null?
|
41
|
+
|
42
|
+
rw = super(ptr)
|
43
|
+
|
44
|
+
if block_given?
|
45
|
+
obj = yield(rw)
|
46
|
+
rw.close
|
47
|
+
obj
|
48
|
+
else
|
49
|
+
rw
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
alias open new
|
54
|
+
end
|
55
|
+
|
56
|
+
def initialize(ptr, obj = nil)
|
57
|
+
@st = SDL_RWops.new(ptr)
|
58
|
+
@obj = obj
|
59
|
+
|
60
|
+
# ポインターへC実装のfree関数を与えるため、終了時のGC処理でコアダンプしない。
|
61
|
+
#
|
62
|
+
@st.to_ptr.free = @st.close
|
63
|
+
|
64
|
+
# close関数の差し替えをすることで、SDLがcloseを行ってもRuby側に伝達できる。
|
65
|
+
# int (*close) (SDL_RWops *context)
|
66
|
+
#
|
67
|
+
st = @st
|
68
|
+
@st.close = @close = Closure::BlockCaller.new(
|
69
|
+
TYPE_INT, [TYPE_VOIDP]
|
70
|
+
) do |context|
|
71
|
+
# call_free はclose関数の戻り値が渡されないためcloseの失敗は分からない。
|
72
|
+
st.to_ptr.call_free
|
73
|
+
|
74
|
+
if $DEBUG
|
75
|
+
warn "SDL_RWops(0x#{st.to_i.to_s(16)}) closed."
|
76
|
+
end
|
77
|
+
|
78
|
+
0
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def close
|
83
|
+
unless closed?
|
84
|
+
err = SDL.SDL_RWclose(self)
|
85
|
+
|
86
|
+
raise SDLError if err != 0
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def closed? = @st.to_ptr.freed?
|
91
|
+
|
92
|
+
def eof?
|
93
|
+
raise IOError, "closed" if closed?
|
94
|
+
|
95
|
+
s = SDL.SDL_RWsize(self)
|
96
|
+
raise SDLError if s < 0
|
97
|
+
|
98
|
+
t = SDL.SDL_RWtell(self)
|
99
|
+
raise SDLError if t < 0
|
100
|
+
|
101
|
+
s == t
|
102
|
+
end
|
103
|
+
|
104
|
+
def read(length = nil, buffer = nil)
|
105
|
+
raise IOError, "closed" if closed?
|
106
|
+
|
107
|
+
len = if length.nil?
|
108
|
+
size - tell
|
109
|
+
else
|
110
|
+
length.to_i
|
111
|
+
end
|
112
|
+
|
113
|
+
if len < 0
|
114
|
+
raise ArgumentError, "length"
|
115
|
+
elsif len > 0 && eof?
|
116
|
+
return nil
|
117
|
+
end
|
118
|
+
|
119
|
+
case buffer
|
120
|
+
when nil
|
121
|
+
return "" if len == 0
|
122
|
+
|
123
|
+
buffer = "\x00" * len
|
124
|
+
buf_size = len
|
125
|
+
when String
|
126
|
+
return buffer.clear if len == 0
|
127
|
+
|
128
|
+
buffer = buffer
|
129
|
+
buf_size = buffer.bytesize
|
130
|
+
when Fiddle::Pointer
|
131
|
+
if len == 0
|
132
|
+
buffer.size = 0
|
133
|
+
return buffer
|
134
|
+
end
|
135
|
+
|
136
|
+
buffer = buffer
|
137
|
+
buf_size = buffer.size
|
138
|
+
else
|
139
|
+
raise TypeError, "buffer"
|
140
|
+
end
|
141
|
+
|
142
|
+
if len > buf_size
|
143
|
+
raise ArgumentError, "length is greater than buffer size."
|
144
|
+
end
|
145
|
+
|
146
|
+
n = SDL.SDL_RWread(self, buffer, 1, len)
|
147
|
+
raise SDLError if n == 0
|
148
|
+
|
149
|
+
if String === buffer
|
150
|
+
buffer.slice!(n, buffer.bytesize)
|
151
|
+
else
|
152
|
+
buffer.size = n
|
153
|
+
end
|
154
|
+
|
155
|
+
buffer
|
156
|
+
end
|
157
|
+
|
158
|
+
def seek(offset, whence = IO::SEEK_SET)
|
159
|
+
raise IOError, "closed" if closed?
|
160
|
+
|
161
|
+
n = SDL.SDL_RWseek(self, offset, whence)
|
162
|
+
raise SDLError if n < 0
|
163
|
+
n
|
164
|
+
end
|
165
|
+
|
166
|
+
alias pos= seek
|
167
|
+
|
168
|
+
def size
|
169
|
+
raise IOError, "closed" if closed?
|
170
|
+
|
171
|
+
n = SDL.SDL_RWsize(self)
|
172
|
+
raise SDLError if n < 0
|
173
|
+
n
|
174
|
+
end
|
175
|
+
|
176
|
+
def tell
|
177
|
+
raise IOError, "closed" if closed?
|
178
|
+
|
179
|
+
n = SDL.SDL_RWtell(self)
|
180
|
+
raise SDLError if n < 0
|
181
|
+
n
|
182
|
+
end
|
183
|
+
|
184
|
+
alias pos tell
|
185
|
+
|
186
|
+
def to_ptr
|
187
|
+
raise SDLError, "released" if closed?
|
188
|
+
@st.to_ptr
|
189
|
+
end
|
190
|
+
|
191
|
+
def type
|
192
|
+
raise SDLError, "released" if closed?
|
193
|
+
@st.type
|
194
|
+
end
|
195
|
+
|
196
|
+
def write(obj)
|
197
|
+
raise IOError, "closed" if closed?
|
198
|
+
|
199
|
+
case obj
|
200
|
+
when String
|
201
|
+
ptr = obj
|
202
|
+
len = obj.bytesize
|
203
|
+
when Fiddle::Pointer
|
204
|
+
ptr = obj
|
205
|
+
len = obj.size
|
206
|
+
else
|
207
|
+
ptr = obj.to_s
|
208
|
+
len = ptr.bytesize
|
209
|
+
end
|
210
|
+
|
211
|
+
n = SDL.SDL_RWwrite(self, ptr, 1, len)
|
212
|
+
raise SDLError if n < len
|
213
|
+
n
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SDLRuby::SDL
|
2
|
+
require_relative 'SDL_main.h.rb'
|
3
|
+
require_relative 'SDL_stdinc.h.rb'
|
4
|
+
require_relative 'SDL_audio.h.rb'
|
5
|
+
require_relative 'SDL_clipboard.h.rb'
|
6
|
+
require_relative 'SDL_cpuinfo.h.rb'
|
7
|
+
require_relative 'SDL_endian.h.rb'
|
8
|
+
require_relative 'SDL_error.h.rb'
|
9
|
+
require_relative 'SDL_events.h.rb'
|
10
|
+
require_relative 'SDL_filesystem.h.rb'
|
11
|
+
require_relative 'SDL_gamecontroller.h.rb'
|
12
|
+
require_relative 'SDL_guid.h.rb'
|
13
|
+
require_relative 'SDL_hints.h.rb'
|
14
|
+
require_relative 'SDL_joystick.h.rb'
|
15
|
+
require_relative 'SDL_messagebox.h.rb'
|
16
|
+
require_relative 'SDL_power.h.rb'
|
17
|
+
require_relative 'SDL_render.h.rb'
|
18
|
+
require_relative 'SDL_rwops.h.rb'
|
19
|
+
require_relative 'SDL_sensor.h.rb'
|
20
|
+
require_relative 'SDL_shape.h.rb'
|
21
|
+
require_relative 'SDL_system.h.rb'
|
22
|
+
require_relative 'SDL_timer.h.rb'
|
23
|
+
require_relative 'SDL_version.h.rb'
|
24
|
+
require_relative 'SDL_video.h.rb'
|
25
|
+
require_relative 'SDL_locale.h.rb'
|
26
|
+
require_relative 'SDL_misc.h.rb'
|
27
|
+
|
28
|
+
extern "int SDL_Init(Uint32 flags)"
|
29
|
+
extern "int SDL_InitSubSystem(Uint32 flags)"
|
30
|
+
extern "void SDL_QuitSubSystem(Uint32 flags)"
|
31
|
+
extern "Uint32 SDL_WasInit(Uint32 flags)"
|
32
|
+
extern "void SDL_Quit(void)"
|
33
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module SDLRuby::SDL
|
2
|
+
require_relative 'SDL_stdinc.h.rb'
|
3
|
+
require_relative 'SDL_error.h.rb'
|
4
|
+
require_relative 'SDL_endian.h.rb'
|
5
|
+
require_relative 'SDL_rwops.h.rb'
|
6
|
+
|
7
|
+
typealias "SDL_AudioFormat", "Uint16"
|
8
|
+
typealias "SDL_AudioDeviceID", "Uint32"
|
9
|
+
typealias "SDL_AudioStream", "struct _SDL_AudioStream"
|
10
|
+
typealias "SDL_AudioStatus", "int"
|
11
|
+
extern "int SDL_GetNumAudioDrivers(void)"
|
12
|
+
extern "const char * SDL_GetAudioDriver(int index)"
|
13
|
+
extern "int SDL_AudioInit(const char *driver_name)"
|
14
|
+
extern "void SDL_AudioQuit(void)"
|
15
|
+
extern "const char * SDL_GetCurrentAudioDriver(void)"
|
16
|
+
extern "int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)"
|
17
|
+
extern "int SDL_GetNumAudioDevices(int iscapture)"
|
18
|
+
extern "const char * SDL_GetAudioDeviceName(int index, int iscapture)"
|
19
|
+
extern "int SDL_GetAudioDeviceSpec(int index, int iscapture, SDL_AudioSpec *spec)"
|
20
|
+
extern "int SDL_GetDefaultAudioInfo(char **name, SDL_AudioSpec *spec, int iscapture)"
|
21
|
+
extern "SDL_AudioDeviceID SDL_OpenAudioDevice( const char *device, int iscapture, const SDL_AudioSpec *desired, SDL_AudioSpec *obtained, int allowed_changes)"
|
22
|
+
extern "SDL_AudioStatus SDL_GetAudioStatus(void)"
|
23
|
+
extern "SDL_AudioStatus SDL_GetAudioDeviceStatus(SDL_AudioDeviceID dev)"
|
24
|
+
extern "void SDL_PauseAudio(int pause_on)"
|
25
|
+
extern "void SDL_PauseAudioDevice(SDL_AudioDeviceID dev, int pause_on)"
|
26
|
+
extern "SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)"
|
27
|
+
extern "void SDL_FreeWAV(Uint8 *audio_buf)"
|
28
|
+
extern "int SDL_BuildAudioCVT(SDL_AudioCVT *cvt, SDL_AudioFormat src_format, Uint8 src_channels, int src_rate, SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)"
|
29
|
+
extern "int SDL_ConvertAudio(SDL_AudioCVT *cvt)"
|
30
|
+
extern "SDL_AudioStream * SDL_NewAudioStream(const SDL_AudioFormat src_format, const Uint8 src_channels, const int src_rate, const SDL_AudioFormat dst_format, const Uint8 dst_channels, const int dst_rate)"
|
31
|
+
extern "int SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, int len)"
|
32
|
+
extern "int SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, int len)"
|
33
|
+
extern "int SDL_AudioStreamAvailable(SDL_AudioStream *stream)"
|
34
|
+
extern "int SDL_AudioStreamFlush(SDL_AudioStream *stream)"
|
35
|
+
extern "void SDL_AudioStreamClear(SDL_AudioStream *stream)"
|
36
|
+
extern "void SDL_FreeAudioStream(SDL_AudioStream *stream)"
|
37
|
+
extern "void SDL_MixAudio(Uint8 *dst, const Uint8 *src, Uint32 len, int volume)"
|
38
|
+
extern "void SDL_MixAudioFormat(Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)"
|
39
|
+
extern "int SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len)"
|
40
|
+
extern "Uint32 SDL_DequeueAudio(SDL_AudioDeviceID dev, void *data, Uint32 len)"
|
41
|
+
extern "Uint32 SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev)"
|
42
|
+
extern "void SDL_ClearQueuedAudio(SDL_AudioDeviceID dev)"
|
43
|
+
extern "void SDL_LockAudio(void)"
|
44
|
+
extern "void SDL_LockAudioDevice(SDL_AudioDeviceID dev)"
|
45
|
+
extern "void SDL_UnlockAudio(void)"
|
46
|
+
extern "void SDL_UnlockAudioDevice(SDL_AudioDeviceID dev)"
|
47
|
+
extern "void SDL_CloseAudio(void)"
|
48
|
+
extern "void SDL_CloseAudioDevice(SDL_AudioDeviceID dev)"
|
49
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
module SDLRuby::SDL
|
2
|
+
typealias "SDL_BlendMode", "int"
|
3
|
+
typealias "SDL_BlendOperation", "int"
|
4
|
+
typealias "SDL_BlendFactor", "int"
|
5
|
+
extern "SDL_BlendMode SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor, SDL_BlendOperation colorOperation, SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor, SDL_BlendOperation alphaOperation)"
|
6
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module SDLRuby::SDL
|
2
|
+
require_relative 'SDL_stdinc.h.rb'
|
3
|
+
|
4
|
+
extern "int SDL_SetClipboardText(const char *text)"
|
5
|
+
extern "char * SDL_GetClipboardText(void)"
|
6
|
+
extern "SDL_bool SDL_HasClipboardText(void)"
|
7
|
+
extern "int SDL_SetPrimarySelectionText(const char *text)"
|
8
|
+
extern "char * SDL_GetPrimarySelectionText(void)"
|
9
|
+
extern "SDL_bool SDL_HasPrimarySelectionText(void)"
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SDLRuby::SDL
|
2
|
+
require_relative 'SDL_platform.h.rb'
|
3
|
+
typealias "int8_t", "signed __int8"
|
4
|
+
typealias "uint8_t", "unsigned __int8"
|
5
|
+
typealias "int16_t", "signed __int16"
|
6
|
+
typealias "uint16_t", "unsigned __int16"
|
7
|
+
typealias "int32_t", "signed __int32"
|
8
|
+
typealias "uint32_t", "unsigned __int32"
|
9
|
+
typealias "int64_t", "signed __int64"
|
10
|
+
typealias "uint64_t", "unsigned __int64"
|
11
|
+
typealias "uintptr_t", "unsigned __int64"
|
12
|
+
typealias "uintptr_t", "unsigned int"
|
13
|
+
end
|