SDLRuby 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,216 +1,91 @@
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
1
+ require 'stringio'
2
+ require_relative 'rw/operational'
3
+ require_relative 'rw_ops/rw_object'
4
+
5
+ module SDLRuby
6
+ class RWOps
7
+ include Fiddle, RW::Operational, SDL
8
+
9
+ class << self
10
+ def [](obj)
11
+ return obj if obj.kind_of?(RWOps)
12
+
13
+ case obj
14
+ in Fiddle::Pointer
15
+ ptr = if obj.frozen?
16
+ SDL.SDL_RWFromConstMem(obj, obj.size)
17
+ else
18
+ SDL.SDL_RWFromMem(obj, obj.size)
19
+ end
20
+ raise SDLError if ptr.null?
21
+ in String
22
+ obj = RWObject.new(StringIO.new(obj))
23
+ ptr = obj.to_ptr
24
+ else
25
+ obj = RWObject.new(obj)
26
+ ptr = obj.to_ptr
27
+ end
28
+
29
+ rw = allocate
30
+ rw.__send__(:initialize, ptr, obj)
31
+ rw
32
+ end
33
+
34
+ def new(file, mode = "rb")
35
+ path = if file.instance_of?(String)
36
+ file
37
+ elsif file.respond_to?(:to_path)
38
+ file.to_path
39
+ end
40
+ ptr = SDL.SDL_RWFromFile(path, mode)
41
+ raise SDLError if ptr.null?
42
+
43
+ rw = super(ptr)
44
+
45
+ if block_given?
46
+ begin
47
+ yield(rw)
48
+ ensure
49
+ rw.close
50
+ end
51
+ else
52
+ rw
53
+ end
54
+ end
55
+
56
+ alias open new
57
+ end
58
+
59
+ def initialize(ptr, obj = nil)
60
+ @obj = obj
61
+
62
+ @st = SDL_RWops.new(ptr)
63
+
64
+ # ポインターへC実装のfree関数を与えるため、終了時のGC処理でコアダンプしない。
65
+ #
66
+ @st.to_ptr.free = @st["close"]
67
+
68
+ # close関数の差し替えをすることで、SDLがcloseを行ってもRuby側に伝達できる。
69
+ # int (*close) (SDL_RWops *context)
70
+ #
71
+ @st["close"] = @close = Closure::BlockCaller.new(TYPE_INT, [TYPE_VOIDP]) do |_|
72
+ # call_free はclose関数の戻り値が渡されないためcloseの失敗は分からない。
73
+ to_ptr.call_free
74
+
75
+ if $DEBUG
76
+ warn format("SDL_RWops(0x%016x) closed", to_ptr.to_int)
77
+ end
78
+
79
+ 0
80
+ end
81
+ end
82
+
83
+ def initialize_copy(other)
84
+ raise SDLError, "closed stream" if closed?
85
+
86
+ super
87
+ end
88
+
89
+ def to_ptr = @st.to_ptr
90
+ end
91
+ end
@@ -12,76 +12,76 @@ module SDLRuby::SDL
12
12
  end
13
13
 
14
14
  SDLK_UNKNOWN = 0
15
- SDLK_RETURN = "\r"
16
- SDLK_ESCAPE = "\x1B"
17
- SDLK_BACKSPACE = "\b"
18
- SDLK_TAB = "\t"
19
- SDLK_SPACE = ' '
20
- SDLK_EXCLAIM = '!'
21
- SDLK_QUOTEDBL = '"'
22
- SDLK_HASH = '#'
23
- SDLK_PERCENT = '%'
24
- SDLK_DOLLAR = '$'
25
- SDLK_AMPERSAND = '&'
26
- SDLK_QUOTE = '\''
27
- SDLK_LEFTPAREN = '('
28
- SDLK_RIGHTPAREN = ')'
29
- SDLK_ASTERISK = '*'
30
- SDLK_PLUS = '+'
31
- SDLK_COMMA = ','
32
- SDLK_MINUS = '-'
33
- SDLK_PERIOD = '.'
34
- SDLK_SLASH = '/'
35
- SDLK_0 = '0'
36
- SDLK_1 = '1'
37
- SDLK_2 = '2'
38
- SDLK_3 = '3'
39
- SDLK_4 = '4'
40
- SDLK_5 = '5'
41
- SDLK_6 = '6'
42
- SDLK_7 = '7'
43
- SDLK_8 = '8'
44
- SDLK_9 = '9'
45
- SDLK_COLON = ':'
46
- SDLK_SEMICOLON = ';'
47
- SDLK_LESS = '<'
48
- SDLK_EQUALS = '='
49
- SDLK_GREATER = '>'
50
- SDLK_QUESTION = '?'
51
- SDLK_AT = '@'
15
+ SDLK_RETURN = "\r".unpack1("C")
16
+ SDLK_ESCAPE = "\x1B".unpack1("C")
17
+ SDLK_BACKSPACE = "\b".unpack1("C")
18
+ SDLK_TAB = "\t".unpack1("C")
19
+ SDLK_SPACE = ' '.unpack1("C")
20
+ SDLK_EXCLAIM = '!'.unpack1("C")
21
+ SDLK_QUOTEDBL = '"'.unpack1("C")
22
+ SDLK_HASH = '#'.unpack1("C")
23
+ SDLK_PERCENT = '%'.unpack1("C")
24
+ SDLK_DOLLAR = '$'.unpack1("C")
25
+ SDLK_AMPERSAND = '&'.unpack1("C")
26
+ SDLK_QUOTE = '\''.unpack1("C")
27
+ SDLK_LEFTPAREN = '('.unpack1("C")
28
+ SDLK_RIGHTPAREN = ')'.unpack1("C")
29
+ SDLK_ASTERISK = '*'.unpack1("C")
30
+ SDLK_PLUS = '+'.unpack1("C")
31
+ SDLK_COMMA = ','.unpack1("C")
32
+ SDLK_MINUS = '-'.unpack1("C")
33
+ SDLK_PERIOD = '.'.unpack1("C")
34
+ SDLK_SLASH = '/'.unpack1("C")
35
+ SDLK_0 = '0'.unpack1("C")
36
+ SDLK_1 = '1'.unpack1("C")
37
+ SDLK_2 = '2'.unpack1("C")
38
+ SDLK_3 = '3'.unpack1("C")
39
+ SDLK_4 = '4'.unpack1("C")
40
+ SDLK_5 = '5'.unpack1("C")
41
+ SDLK_6 = '6'.unpack1("C")
42
+ SDLK_7 = '7'.unpack1("C")
43
+ SDLK_8 = '8'.unpack1("C")
44
+ SDLK_9 = '9'.unpack1("C")
45
+ SDLK_COLON = ':'.unpack1("C")
46
+ SDLK_SEMICOLON = ';'.unpack1("C")
47
+ SDLK_LESS = '<'.unpack1("C")
48
+ SDLK_EQUALS = '='.unpack1("C")
49
+ SDLK_GREATER = '>'.unpack1("C")
50
+ SDLK_QUESTION = '?'.unpack1("C")
51
+ SDLK_AT = '@'.unpack1("C")
52
52
 
53
- SDLK_LEFTBRACKET = '['
54
- SDLK_BACKSLASH = '\\'
55
- SDLK_RIGHTBRACKET = ']'
56
- SDLK_CARET = '^'
57
- SDLK_UNDERSCORE = '_'
58
- SDLK_BACKQUOTE = '`'
59
- SDLK_a = 'a'
60
- SDLK_b = 'b'
61
- SDLK_c = 'c'
62
- SDLK_d = 'd'
63
- SDLK_e = 'e'
64
- SDLK_f = 'f'
65
- SDLK_g = 'g'
66
- SDLK_h = 'h'
67
- SDLK_i = 'i'
68
- SDLK_j = 'j'
69
- SDLK_k = 'k'
70
- SDLK_l = 'l'
71
- SDLK_m = 'm'
72
- SDLK_n = 'n'
73
- SDLK_o = 'o'
74
- SDLK_p = 'p'
75
- SDLK_q = 'q'
76
- SDLK_r = 'r'
77
- SDLK_s = 's'
78
- SDLK_t = 't'
79
- SDLK_u = 'u'
80
- SDLK_v = 'v'
81
- SDLK_w = 'w'
82
- SDLK_x = 'x'
83
- SDLK_y = 'y'
84
- SDLK_z = 'z'
53
+ SDLK_LEFTBRACKET = '['.unpack1("C")
54
+ SDLK_BACKSLASH = '\\'.unpack1("C")
55
+ SDLK_RIGHTBRACKET = ']'.unpack1("C")
56
+ SDLK_CARET = '^'.unpack1("C")
57
+ SDLK_UNDERSCORE = '_'.unpack1("C")
58
+ SDLK_BACKQUOTE = '`'.unpack1("C")
59
+ SDLK_a = 'a'.unpack1("C")
60
+ SDLK_b = 'b'.unpack1("C")
61
+ SDLK_c = 'c'.unpack1("C")
62
+ SDLK_d = 'd'.unpack1("C")
63
+ SDLK_e = 'e'.unpack1("C")
64
+ SDLK_f = 'f'.unpack1("C")
65
+ SDLK_g = 'g'.unpack1("C")
66
+ SDLK_h = 'h'.unpack1("C")
67
+ SDLK_i = 'i'.unpack1("C")
68
+ SDLK_j = 'j'.unpack1("C")
69
+ SDLK_k = 'k'.unpack1("C")
70
+ SDLK_l = 'l'.unpack1("C")
71
+ SDLK_m = 'm'.unpack1("C")
72
+ SDLK_n = 'n'.unpack1("C")
73
+ SDLK_o = 'o'.unpack1("C")
74
+ SDLK_p = 'p'.unpack1("C")
75
+ SDLK_q = 'q'.unpack1("C")
76
+ SDLK_r = 'r'.unpack1("C")
77
+ SDLK_s = 's'.unpack1("C")
78
+ SDLK_t = 't'.unpack1("C")
79
+ SDLK_u = 'u'.unpack1("C")
80
+ SDLK_v = 'v'.unpack1("C")
81
+ SDLK_w = 'w'.unpack1("C")
82
+ SDLK_x = 'x'.unpack1("C")
83
+ SDLK_y = 'y'.unpack1("C")
84
+ SDLK_z = 'z'.unpack1("C")
85
85
  SDLK_CAPSLOCK = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_CAPSLOCK)
86
86
  SDLK_F1 = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_F1)
87
87
  SDLK_F2 = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_F2)
@@ -101,7 +101,7 @@ module SDLRuby::SDL
101
101
  SDLK_INSERT = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_INSERT)
102
102
  SDLK_HOME = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_HOME)
103
103
  SDLK_PAGEUP = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_PAGEUP)
104
- SDLK_DELETE = '\x7F'
104
+ SDLK_DELETE = "\x7F".unpack1("C")
105
105
  SDLK_END = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_END)
106
106
  SDLK_PAGEDOWN = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_PAGEDOWN)
107
107
  SDLK_RIGHT = SDL_SCANCODE_TO_KEYCODE(SDL_SCANCODE_RIGHT)