SDLRuby 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 808c30b69ff9a14dd61fae1fa5c6812a1be91e6313affb2a46ff136cbffcfeac
4
- data.tar.gz: 42794a0310f8329168a8237897d62dddf14cb14922d1db1d631570308708c8da
3
+ metadata.gz: fb651b451bbc9fc356e5f3a1ecb08d5855a690fa7b0556f7a9da5f0f11f2d6d4
4
+ data.tar.gz: 9f53754821bf14951f4cd014f1b776d07a5b3807a81ceee937ce96e6f71e148e
5
5
  SHA512:
6
- metadata.gz: c840b525923d6e083094d5783ad70057f758dbd4be86f1660d16f7873eeb9e12bf5bdfdfb739c0664287eda2bde514926bc8e0fffd25f1822001482b024b3820
7
- data.tar.gz: 1bec511a45a3786d35c407d51c66b3e75fb0b339835b2d00e492cce65d73c98873a9433043b8faa43f3ac6117988746c4178c14b7468d68a8873fd99a0d45785
6
+ metadata.gz: f5cfbf90c23eb97fcec12e34af27a2966e7d5c3fc010186a28ff1ec2c4caaf5a5cefc0ed68863907f9dad417688b1ec9dc61388e4d4723fb199cfcf5b7582553
7
+ data.tar.gz: b91967cab249f58717362fa4d1241e650d0a38b54ff340167a631a76691a5a17811961fc675a9edfbd0ea8ff2259e4d9c0800c08999cf2273535091f529ce2b4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.1] - 2023-09-28
4
+ - Refactored RWOps class.
5
+
3
6
  ## [0.3.0] - 2023-09-19
4
7
  - Implemented Mouse Module.
5
8
  - Implementation of the Timer Module.
@@ -0,0 +1,116 @@
1
+ module SDLRuby
2
+ class RW
3
+ module Operational
4
+ def close
5
+ unless closed?
6
+ err = SDL.SDL_RWclose(to_ptr)
7
+
8
+ raise SDLError if err < 0
9
+ end
10
+ end
11
+
12
+ def closed? = to_ptr.freed?
13
+
14
+ def eof?
15
+ raise SDLError, "closed and freed rw" if closed?
16
+
17
+ s = SDL.SDL_RWsize(to_ptr)
18
+ raise SDLError if s < 0
19
+
20
+ t = SDL.SDL_RWtell(to_ptr)
21
+ raise SDLError if t < 0
22
+
23
+ s == t
24
+ end
25
+
26
+ def read(length = nil, buffer = nil)
27
+ raise SDLError, "closed and freed rw" if closed?
28
+
29
+ if length.nil?
30
+ len = size - tell
31
+ else
32
+ len = Integer.try_convert(length)
33
+ if len.nil?
34
+ raise TypeError,
35
+ "no implicit conversion of #{length.class} into Integer"
36
+ end
37
+ end
38
+
39
+ if len < 0
40
+ raise ArgumentError, "negative length #{len} given"
41
+ end
42
+
43
+ if buffer.nil?
44
+ buffer = ""
45
+ else
46
+ buffer = String.try_convert(buffer)
47
+ if buffer.nil?
48
+ raise TypeError,
49
+ "no implicit conversion of #{buffer.class} into String"
50
+ end
51
+ end
52
+
53
+ if eof? || len == 0
54
+ buffer.clear
55
+ return len == 0 ? buffer : nil
56
+ end
57
+
58
+ bytesize = buffer.bytesize
59
+ if len == bytesize
60
+ buffer
61
+ elsif len > bytesize
62
+ buffer.gsub!(/\z/nm, "\x00" * (len - bytesize))
63
+ elsif len < bytesize
64
+ buffer.gsub!(/[\x00-\xFF]{#{bytesize - len}}\z/nm, "")
65
+ end
66
+
67
+ n = SDL.SDL_RWread(to_ptr, buffer, 1, len)
68
+ if n < len
69
+ buffer.gsub!(/[\x00-\xFF]{#{n}}$/nm, "")
70
+ end
71
+ raise SDLError if n == 0
72
+
73
+ buffer
74
+ end
75
+
76
+ def seek(offset, whence = IO::SEEK_SET)
77
+ raise SDLError, "closed and freed rw" if closed?
78
+
79
+ n = SDL.SDL_RWseek(to_ptr, offset, whence)
80
+ raise SDLError if n < 0
81
+ 0
82
+ end
83
+
84
+ alias pos= seek
85
+
86
+ def size
87
+ raise SDLError, "closed and freed rw" if closed?
88
+
89
+ n = SDL.SDL_RWsize(to_ptr)
90
+ raise SDLError if n < 0
91
+ n
92
+ end
93
+
94
+ def tell
95
+ raise SDLError, "closed and freed rw" if closed?
96
+
97
+ n = SDL.SDL_RWtell(to_ptr)
98
+ raise SDLError if n < 0
99
+ n
100
+ end
101
+
102
+ alias pos tell
103
+
104
+ def write(obj)
105
+ raise SDLError, "closed and freed rw" if closed?
106
+
107
+ ptr = obj.to_s
108
+ len = ptr.bytesize
109
+
110
+ n = SDL.SDL_RWwrite(to_ptr, ptr, 1, len)
111
+ raise SDLError if n < len
112
+ n
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,28 @@
1
+ module SDLRuby
2
+ class RW
3
+ class ReadClosure < Fiddle::Closure
4
+ include Fiddle
5
+
6
+ def initialize(obj)
7
+ # size_t (*read) (SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
8
+ #
9
+ super(TYPE_SIZE_T, [TYPE_VOIDP, TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T])
10
+ @obj = obj
11
+ end
12
+
13
+ def call(_, ptr, size, maxnum)
14
+ len = size * maxnum
15
+ return 0 if len.zero?
16
+
17
+ s = @obj.read(len)
18
+ return 0 if s.nil? || s.empty? # EOF
19
+
20
+ ptr[0, s.size] = s
21
+ s.size / size
22
+ rescue => e
23
+ SDL.last_error_message = e.message
24
+ 0
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ module SDLRuby
2
+ class RW
3
+ class SeekClosure < Fiddle::Closure
4
+ include Fiddle
5
+
6
+ def initialize(obj)
7
+ # Sint64 (*seek) (SDL_RWops *context, Sint64 offset, int whence)
8
+ #
9
+ super(TYPE_INT64_T, [TYPE_VOIDP, TYPE_INT64_T, TYPE_INT])
10
+ @obj = obj
11
+ end
12
+
13
+ def call(_, offset, whence)
14
+ @obj.seek(offset, whence)
15
+ @obj.tell
16
+ rescue => e
17
+ SDL.last_error_message = e.message
18
+ -1
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module SDLRuby
2
+ class RW
3
+ class SizeClosure < Fiddle::Closure
4
+ include Fiddle
5
+
6
+ def initialize(obj)
7
+ # Sint64 (*size) (SDL_RWops *context)
8
+ #
9
+ super(TYPE_INT64_T, [TYPE_VOIDP])
10
+ @obj = obj
11
+ end
12
+
13
+ def call(_)
14
+ @obj.size
15
+ rescue => e
16
+ SDL.last_error_message = e.message
17
+ -1
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module SDLRuby
2
+ class RW
3
+ class WriteClosure < Fiddle::Closure
4
+ include Fiddle
5
+
6
+ def initialize(obj)
7
+ # size_t (*write) (SDL_RWops *context, const void *ptr, size_t size, size_t num)
8
+ #
9
+ super(TYPE_SIZE_T, [TYPE_VOIDP, TYPE_VOIDP, TYPE_SIZE_T, TYPE_SIZE_T])
10
+ @obj = obj
11
+ end
12
+
13
+ def call(_, ptr, size, num)
14
+ @obj.write(ptr.to_str(size * num)) / size
15
+ rescue => e
16
+ SDL.last_error_message = e.message
17
+ 0
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module SDLRuby
2
+ class RWOps
3
+ include Fiddle, SDL
4
+
5
+ # これはSDL3では不要になります。
6
+ # SDL3ではメンバーの関数ポインターにNULLを設定できます。
7
+ # それは”適切な”デフォルトの動作を意味します。
8
+ #
9
+ #
10
+ #
11
+ # SDL2ではcloseにはC関数ポインターを設定する必要がある。
12
+ # Rubyのクロージャ―を設定するとGCが動作時に該当のクロージャ―が既に回収されているため
13
+ # コアダンプする。
14
+ # また、NULLに設定するとSDL_RWclose関数の呼び出し時にコアダンプする。
15
+ # ここではSDLをハックしてmem_close関数を割り当てた。
16
+ # この関数は内部でSDL_FreeRW関数を呼び出すだけである。
17
+ #
18
+ # int (*close) (SDL_RWops *context)
19
+ #
20
+ MEM_CLOSE = module_eval do
21
+ ptr = Pointer.malloc(1, RUBY_FREE)
22
+ rw = SDL.SDL_RWFromMem(ptr, ptr.size)
23
+ raise SDLError if rw.null?
24
+ SDL_RWops.new(rw, SDL_FREE_RW)["close"].to_int
25
+ end
26
+ end
27
+ end
@@ -1,128 +1,34 @@
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
1
+ require_relative '../rw/read_closure'
2
+ require_relative '../rw/seek_closure'
3
+ require_relative '../rw/size_closure'
4
+ require_relative '../rw/write_closure'
5
+ require_relative 'mem_close'
6
+
7
+ module SDLRuby
8
+ class RWOps
9
+ class RWObject
10
+ include Fiddle, SDL
11
+
12
+ # 引数objに与えたオブジェクトはSDLからcloseを呼び出されてもクローズしない。
13
+ # このインスタンスを単独で扱う場合は使い終わったらclose関数を呼び出す必要がある。
14
+ # そうしなければポインターは開放されずメモリーリークする。
15
+ #
16
+ def initialize(obj)
17
+ ptr = SDL.SDL_AllocRW
18
+ raise SDLError if ptr.null?
19
+
20
+ @st = SDL_RWops.new(ptr)
21
+
22
+ # SDLではSDL_RWopsポインターの開放はclose関数ポインターを呼び出して行う仕様となっている。
23
+ #
24
+ @st.close = MEM_CLOSE
25
+ @st.read = @read = RW::ReadClosure.new(obj)
26
+ @st.seek = @seek = RW::SeekClosure.new(obj)
27
+ @st.size = @size = RW::SizeClosure.new(obj)
28
+ @st.write = @write = RW::WriteClosure.new(obj)
29
+ end
30
+
31
+ def to_ptr = @st.to_ptr
32
+ end
33
+ end
34
+ end
@@ -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
data/lib/SDLRuby/sdl.rb CHANGED
@@ -1,218 +1,218 @@
1
- require "fiddle/import"
2
-
3
- module SDLRuby
4
- module SDL
5
- extend Fiddle::Importer
6
-
7
- dlload "SDL2"
8
-
9
- SDL_DESTROY_RENDERER = import_symbol("SDL_DestroyRenderer").to_int
10
- SDL_DESTROY_TEXTURE = import_symbol("SDL_DestroyTexture").to_int
11
-
12
- # SDL_freeはSDL_SetMemoryFunctionsで設定された関数を使用する。
13
- # SDL側のメモリーを開放する場合はFiddle::PointerへSDL_FREEを設定してよい。
14
- #
15
- SDL_FREE = SDL.import_symbol("SDL_free").to_int
16
- SDL_FREE_CURSOR = import_symbol("SDL_FreeCursor").to_int
17
- SDL_FREE_FORMAT = import_symbol("SDL_FreeFormat").to_int
18
- SDL_FREE_PALETTE = import_symbol("SDL_FreePalette").to_int
19
- SDL_FREE_RW = import_symbol("SDL_FreeRW").to_int
20
- SDL_FREE_SURFACE = import_symbol("SDL_FreeSurface").to_int
21
-
22
- def self.tmp_value(*a, &)
23
- raise ArgumentError if a.empty?
24
-
25
- a.map { |ty|
26
- st = create_value(ty)
27
- st.to_ptr.free = Fiddle::RUBY_FREE
28
- st
29
- } => vals
30
-
31
- v = vals.one? ? vals.first : vals
32
- block_given? ? yield(*v) : v
33
- end
34
-
35
- require_relative 'sdl/sdl'
36
- require_relative 'sdl_error'
37
-
38
- require_relative 'audio'
39
- require_relative 'cursor'
40
- require_relative 'display'
41
- require_relative 'event'
42
- require_relative 'hint'
43
- require_relative 'keyboard'
44
- require_relative 'mouse'
45
- require_relative 'palette'
46
- require_relative 'pixel_formatter'
47
- require_relative 'rect'
48
- require_relative 'renderer'
49
- require_relative 'rw_ops'
50
- require_relative 'surface'
51
- require_relative 'text_input'
52
- require_relative 'texture'
53
- require_relative 'timer'
54
- require_relative 'window'
55
-
56
- class << self
57
- # sdl
58
- #
59
- def init(flags = nil)
60
- err = SDL.SDL_Init(flags || SDL_INIT_EVERYTHING)
61
- raise SDLError if err < 0
62
- end
63
-
64
- # Description:
65
- #
66
- # This method checks the initialization status of SDL subsystems based on
67
- # the provided flags. You can specify the SDL subsystems you want to check
68
- # by combining the SDL_INIT_* flags using bitwise OR.
69
- #
70
- # Parameters:
71
- #
72
- # flags (optional): An integer or a combination of SDL_INIT_* flags
73
- # representing the SDL subsystems to check. Default is nil.
74
- #
75
- # Return Value:
76
- #
77
- # If all the specified SDL subsystems in flags are initialized,
78
- # the method returns true.
79
- # If any of the specified SDL subsystems are not initialized,
80
- # it returns false.
81
- # If flags is given as nil or 0,
82
- # the method returns true if any of the SDL subsystems are initialized.
83
- #
84
- def init?(flags = nil)
85
- if flags && flags != 0
86
- SDL.SDL_WasInit(flags) == flags
87
- else
88
- SDL.SDL_WasInit(0) != 0
89
- end
90
- end
91
-
92
- def quit = SDL.SDL_Quit
93
-
94
- # clipboard
95
- #
96
- def clipboard_text
97
- ptr = SDL.SDL_GetClipboardText
98
- raise SDLError if ptr.null?
99
-
100
- ptr.free = SDL_FREE
101
- ptr.to_s.force_encoding("UTF-8")
102
- end
103
-
104
- def clipboard_text? = SDL.SDL_HasClipboardText == 1
105
-
106
- def clipboard_text=(s)
107
- err = SDL.SDL_SetClipboardText(s.to_s.encode("UTF-8"))
108
- raise SDLError if err < 0
109
- end
110
-
111
- # cpu info
112
- #
113
- def cpu_cache_line_size = SDL.SDL_GetCPUCacheLineSize
114
-
115
- def cpu_count = SDL.SDL_GetCPUCount
116
-
117
- def system_ram = SDL.SDL_GetSystemRAM
118
-
119
- # error
120
- #
121
- def last_error_message = SDL.SDL_GetError.to_s
122
-
123
- def last_error_message=(s)
124
- SDL.SDL_SetError(s.to_s.gsub(/%/, "%%"))
125
- end
126
-
127
- # filesystem
128
- #
129
- def base_path
130
- ptr = SDL.SDL_GetBasePath
131
- raise SDLError if ptr.null?
132
-
133
- ptr.free = SDL_FREE
134
- ptr.to_s
135
- end
136
-
137
- # locale
138
- #
139
- def locales
140
- ptr = SDL.SDL_GetPreferredLocales
141
- return [] if ptr.null?
142
-
143
- ptr.free = SDL_FREE
144
- size = SDL_Locale.size
145
- (0..).inject([]) do |memo, i|
146
- st = SDL_Locale.new(ptr + i * size)
147
- break memo if st.language.null?
148
- memo << [st.language.to_s,
149
- (c = st.country).null? ? nil : c.to_s]
150
- end
151
- end
152
-
153
- # message box
154
- #
155
- def alert(message, title = nil, flags: nil, window: nil)
156
- err = SDL.SDL_ShowSimpleMessageBox(flags,
157
- title.to_s.encode("UTF-8"),
158
- message.to_s.encode("UTF-8"),
159
- window)
160
- raise SDLError if err < 0
161
- end
162
-
163
- def error_alert(message, title = "Error")
164
- alert(message, title, flags: SDL_MESSAGEBOX_ERROR)
165
- end
166
-
167
- def warn_alert(message, title = "Warning")
168
- alert(message, title, flags: SDL_MESSAGEBOX_WARNING)
169
- end
170
-
171
- def info_alert(message, title = "Information")
172
- alert(message, title, flags: SDL_MESSAGEBOX_INFORMATION)
173
- end
174
-
175
- # misc
176
- #
177
- def open_url(url)
178
- raise SDLError if SDL.SDL_OpenURL(url) < 0
179
- end
180
-
181
- # platform
182
- #
183
- def platform = SDL.SDL_GetPlatform.to_s
184
-
185
- # timer
186
- #
187
- def ticks = Timer.ticks
188
-
189
- # version
190
- #
191
- def revision = SDL.SDL_GetRevision.to_s
192
-
193
- def version
194
- st = SDL_version.malloc(Fiddle::RUBY_FREE)
195
- SDL.SDL_GetVersion(st)
196
- st.to_a.join(".")
197
- end
198
-
199
- # video
200
- #
201
- def video_driver
202
- (ptr = SDL.SDL_GetCurrentVideoDriver).null? ? nil : ptr.to_s
203
- end
204
-
205
- def screen_saver=(b)
206
- b ? SDL.SDL_EnableScreenSaver : SDL.SDL_DisableScreenSaver
207
- end
208
-
209
- def screen_saver? = SDL.SDL_IsScreenSaverEnabled != 0
210
-
211
- def video_drivers
212
- SDL.SDL_GetNumVideoDrivers.times.map do |i|
213
- (ptr = SDL.SDL_GetVideoDriver(i)).null? ? nil : ptr.to_s
214
- end
215
- end
216
- end
217
- end
218
- end
1
+ require "fiddle/import"
2
+
3
+ module SDLRuby
4
+ module SDL
5
+ extend Fiddle::Importer
6
+
7
+ dlload "SDL2"
8
+
9
+ SDL_DESTROY_RENDERER = import_symbol("SDL_DestroyRenderer").to_int
10
+ SDL_DESTROY_TEXTURE = import_symbol("SDL_DestroyTexture").to_int
11
+
12
+ # SDL_freeはSDL_SetMemoryFunctionsで設定された関数を使用する。
13
+ # SDL側のメモリーを開放する場合はFiddle::PointerへSDL_FREEを設定してよい。
14
+ #
15
+ SDL_FREE = import_symbol("SDL_free").to_int
16
+ SDL_FREE_CURSOR = import_symbol("SDL_FreeCursor").to_int
17
+ SDL_FREE_FORMAT = import_symbol("SDL_FreeFormat").to_int
18
+ SDL_FREE_PALETTE = import_symbol("SDL_FreePalette").to_int
19
+ SDL_FREE_RW = import_symbol("SDL_FreeRW").to_int
20
+ SDL_FREE_SURFACE = import_symbol("SDL_FreeSurface").to_int
21
+
22
+ def self.tmp_value(*a, &)
23
+ raise ArgumentError if a.empty?
24
+
25
+ a.map { |ty|
26
+ st = create_value(ty)
27
+ st.to_ptr.free = Fiddle::RUBY_FREE
28
+ st
29
+ } => vals
30
+
31
+ v = vals.one? ? vals.first : vals
32
+ block_given? ? yield(*v) : v
33
+ end
34
+
35
+ require_relative 'sdl/sdl'
36
+ require_relative 'sdl_error'
37
+
38
+ require_relative 'audio'
39
+ require_relative 'cursor'
40
+ require_relative 'display'
41
+ require_relative 'event'
42
+ require_relative 'hint'
43
+ require_relative 'keyboard'
44
+ require_relative 'mouse'
45
+ require_relative 'palette'
46
+ require_relative 'pixel_formatter'
47
+ require_relative 'rect'
48
+ require_relative 'renderer'
49
+ require_relative 'rw_ops'
50
+ require_relative 'surface'
51
+ require_relative 'text_input'
52
+ require_relative 'texture'
53
+ require_relative 'timer'
54
+ require_relative 'window'
55
+
56
+ class << self
57
+ # sdl
58
+ #
59
+ def init(flags = nil)
60
+ err = SDL.SDL_Init(flags || SDL_INIT_EVERYTHING)
61
+ raise SDLError if err < 0
62
+ end
63
+
64
+ # Description:
65
+ #
66
+ # This method checks the initialization status of SDL subsystems based on
67
+ # the provided flags. You can specify the SDL subsystems you want to check
68
+ # by combining the SDL_INIT_* flags using bitwise OR.
69
+ #
70
+ # Parameters:
71
+ #
72
+ # flags (optional): An integer or a combination of SDL_INIT_* flags
73
+ # representing the SDL subsystems to check. Default is nil.
74
+ #
75
+ # Return Value:
76
+ #
77
+ # If all the specified SDL subsystems in flags are initialized,
78
+ # the method returns true.
79
+ # If any of the specified SDL subsystems are not initialized,
80
+ # it returns false.
81
+ # If flags is given as nil or 0,
82
+ # the method returns true if any of the SDL subsystems are initialized.
83
+ #
84
+ def init?(flags = nil)
85
+ if flags && flags != 0
86
+ SDL.SDL_WasInit(flags) == flags
87
+ else
88
+ SDL.SDL_WasInit(0) != 0
89
+ end
90
+ end
91
+
92
+ def quit = SDL.SDL_Quit
93
+
94
+ # clipboard
95
+ #
96
+ def clipboard_text
97
+ ptr = SDL.SDL_GetClipboardText
98
+ raise SDLError if ptr.null?
99
+
100
+ ptr.free = SDL_FREE
101
+ ptr.to_s.force_encoding("UTF-8")
102
+ end
103
+
104
+ def clipboard_text? = SDL.SDL_HasClipboardText == 1
105
+
106
+ def clipboard_text=(s)
107
+ err = SDL.SDL_SetClipboardText(s.to_s.encode("UTF-8"))
108
+ raise SDLError if err < 0
109
+ end
110
+
111
+ # cpu info
112
+ #
113
+ def cpu_cache_line_size = SDL.SDL_GetCPUCacheLineSize
114
+
115
+ def cpu_count = SDL.SDL_GetCPUCount
116
+
117
+ def system_ram = SDL.SDL_GetSystemRAM
118
+
119
+ # error
120
+ #
121
+ def last_error_message = SDL.SDL_GetError.to_s
122
+
123
+ def last_error_message=(s)
124
+ SDL.SDL_SetError(s.to_s.gsub(/%/, "%%"))
125
+ end
126
+
127
+ # filesystem
128
+ #
129
+ def base_path
130
+ ptr = SDL.SDL_GetBasePath
131
+ raise SDLError if ptr.null?
132
+
133
+ ptr.free = SDL_FREE
134
+ ptr.to_s
135
+ end
136
+
137
+ # locale
138
+ #
139
+ def locales
140
+ ptr = SDL.SDL_GetPreferredLocales
141
+ return [] if ptr.null?
142
+
143
+ ptr.free = SDL_FREE
144
+ size = SDL_Locale.size
145
+ (0..).inject([]) do |memo, i|
146
+ st = SDL_Locale.new(ptr + i * size)
147
+ break memo if st.language.null?
148
+ memo << [st.language.to_s,
149
+ (c = st.country).null? ? nil : c.to_s]
150
+ end
151
+ end
152
+
153
+ # message box
154
+ #
155
+ def alert(message, title = nil, flags: nil, window: nil)
156
+ err = SDL.SDL_ShowSimpleMessageBox(flags,
157
+ title.to_s.encode("UTF-8"),
158
+ message.to_s.encode("UTF-8"),
159
+ window)
160
+ raise SDLError if err < 0
161
+ end
162
+
163
+ def error_alert(message, title = "Error")
164
+ alert(message, title, flags: SDL_MESSAGEBOX_ERROR)
165
+ end
166
+
167
+ def warn_alert(message, title = "Warning")
168
+ alert(message, title, flags: SDL_MESSAGEBOX_WARNING)
169
+ end
170
+
171
+ def info_alert(message, title = "Information")
172
+ alert(message, title, flags: SDL_MESSAGEBOX_INFORMATION)
173
+ end
174
+
175
+ # misc
176
+ #
177
+ def open_url(url)
178
+ raise SDLError if SDL.SDL_OpenURL(url) < 0
179
+ end
180
+
181
+ # platform
182
+ #
183
+ def platform = SDL.SDL_GetPlatform.to_s
184
+
185
+ # timer
186
+ #
187
+ def ticks = Timer.ticks
188
+
189
+ # version
190
+ #
191
+ def revision = SDL.SDL_GetRevision.to_s
192
+
193
+ def version
194
+ st = SDL_version.malloc(Fiddle::RUBY_FREE)
195
+ SDL.SDL_GetVersion(st)
196
+ st.to_a.join(".")
197
+ end
198
+
199
+ # video
200
+ #
201
+ def video_driver
202
+ (ptr = SDL.SDL_GetCurrentVideoDriver).null? ? nil : ptr.to_s
203
+ end
204
+
205
+ def screen_saver=(b)
206
+ b ? SDL.SDL_EnableScreenSaver : SDL.SDL_DisableScreenSaver
207
+ end
208
+
209
+ def screen_saver? = SDL.SDL_IsScreenSaverEnabled != 0
210
+
211
+ def video_drivers
212
+ SDL.SDL_GetNumVideoDrivers.times.map do |i|
213
+ (ptr = SDL.SDL_GetVideoDriver(i)).null? ? nil : ptr.to_s
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SDLRuby
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: SDLRuby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - shinokaro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-18 00:00:00.000000000 Z
11
+ date: 2023-09-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: SDLRuby is a GEM that allows calling the SDL library from Ruby, enabling
14
14
  window creation, image display, audio playback, and more. It easily manages C pointers
@@ -51,7 +51,13 @@ files:
51
51
  - lib/SDLRuby/renderer/accessor.rb
52
52
  - lib/SDLRuby/renderer/drawer.rb
53
53
  - lib/SDLRuby/renderer/renderer_info.rb
54
+ - lib/SDLRuby/rw/operational.rb
55
+ - lib/SDLRuby/rw/read_closure.rb
56
+ - lib/SDLRuby/rw/seek_closure.rb
57
+ - lib/SDLRuby/rw/size_closure.rb
58
+ - lib/SDLRuby/rw/write_closure.rb
54
59
  - lib/SDLRuby/rw_ops.rb
60
+ - lib/SDLRuby/rw_ops/mem_close.rb
55
61
  - lib/SDLRuby/rw_ops/rw_object.rb
56
62
  - lib/SDLRuby/sdl.rb
57
63
  - lib/SDLRuby/sdl/include/SDL.h.rb