SDLRuby 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/SDLRuby/sdl.rb CHANGED
@@ -1,191 +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 'window'
54
-
55
- class << self
56
- # sdl
57
- #
58
- def init(flags = nil)
59
- err = SDL.SDL_Init(flags || SDL_INIT_EVERYTHING)
60
- raise SDLError if err < 0
61
- end
62
-
63
- def init?(flags) = SDL.WasInit(flags).nonzero?
64
-
65
- def quit = SDL.SDL_Quit
66
-
67
- # clipboard
68
- #
69
- def clipboard_text
70
- ptr = SDL.SDL_GetClipboardText
71
- raise SDLError if ptr.null?
72
-
73
- ptr.free = SDL_FREE
74
- ptr.to_s.force_encoding("UTF-8")
75
- end
76
-
77
- def clipboard_text? = SDL.SDL_HasClipboardText == 1
78
-
79
- def clipboard_text=(s)
80
- err = SDL.SDL_SetClipboardText(s.to_s.encode("UTF-8"))
81
- raise SDLError if err < 0
82
- end
83
-
84
- # cpu info
85
- #
86
- def cpu_cache_line_size = SDL.SDL_GetCPUCacheLineSize
87
-
88
- def cpu_count = SDL.SDL_GetCPUCount
89
-
90
- def system_ram = SDL.SDL_GetSystemRAM
91
-
92
- # error
93
- #
94
- def last_error_message = SDL.SDL_GetError.to_s
95
-
96
- def last_error_message=(s)
97
- SDL.SDL_SetError(s.to_s.gsub(/%/, "%%"))
98
- end
99
-
100
- # filesystem
101
- #
102
- def base_path
103
- ptr = SDL.SDL_GetBasePath
104
- raise SDLError if ptr.null?
105
-
106
- ptr.free = SDL_FREE
107
- ptr.to_s
108
- end
109
-
110
- # locale
111
- #
112
- def locales
113
- ptr = SDL.SDL_GetPreferredLocales
114
- return [] if ptr.null?
115
-
116
- ptr.free = SDL_FREE
117
- size = SDL_Locale.size
118
- (0..).inject([]) do |memo, i|
119
- st = SDL_Locale.new(ptr + i * size)
120
- break memo if st.language.null?
121
- memo << [st.language.to_s,
122
- (c = st.country).null? ? nil : c.to_s]
123
- end
124
- end
125
-
126
- # message box
127
- #
128
- def alert(message, title = nil, flags: nil, window: nil)
129
- err = SDL.SDL_ShowSimpleMessageBox(flags,
130
- title.to_s.encode("UTF-8"),
131
- message.to_s.encode("UTF-8"),
132
- window)
133
- raise SDLError if err < 0
134
- end
135
-
136
- def error_alert(message, title = "Error")
137
- alert(message, title, flags: SDL_MESSAGEBOX_ERROR)
138
- end
139
-
140
- def warn_alert(message, title = "Warning")
141
- alert(message, title, flags: SDL_MESSAGEBOX_WARNING)
142
- end
143
-
144
- def info_alert(message, title = "Information")
145
- alert(message, title, flags: SDL_MESSAGEBOX_INFORMATION)
146
- end
147
-
148
- # misc
149
- #
150
- def open_url(url)
151
- raise SDLError if SDL.SDL_OpenURL(url) < 0
152
- end
153
-
154
- # platform
155
- #
156
- def platform = SDL.SDL_GetPlatform.to_s
157
-
158
- # timer
159
- #
160
- def ticks = SDL.SDL_GetTicks
161
-
162
- # version
163
- #
164
- def revision = SDL.SDL_GetRevision.to_s
165
-
166
- def version
167
- st = SDL_version.malloc(Fiddle::RUBY_FREE)
168
- SDL.SDL_GetVersion(st)
169
- st.to_a.join(".")
170
- end
171
-
172
- # video
173
- #
174
- def video_driver
175
- (ptr = SDL.SDL_GetCurrentVideoDriver).null? ? nil : ptr.to_s
176
- end
177
-
178
- def screen_saver=(b)
179
- b ? SDL.SDL_EnableScreenSaver : SDL.SDL_DisableScreenSaver
180
- end
181
-
182
- def screen_saver? = SDL.SDL_IsScreenSaverEnabled != 0
183
-
184
- def video_drivers
185
- SDL.SDL_GetNumVideoDrivers.times.map do |i|
186
- (ptr = SDL.SDL_GetVideoDriver(i)).null? ? nil : ptr.to_s
187
- end
188
- end
189
- end
190
- end
191
- 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
@@ -0,0 +1,25 @@
1
+ module SDLRuby
2
+ module Timer
3
+ class << self
4
+ def ticks
5
+ # Use the same Ticks as the one utilized for SDL event timestamps.
6
+ #
7
+ SDL.SDL_GetTicks
8
+ end
9
+
10
+ def inspect
11
+ n = SDL.SDL_GetTicks64
12
+ epoch_time = n / 1000
13
+
14
+ sprintf(
15
+ "%d %02d:%02d:%02d.%03d0000",
16
+ epoch_time / 86400, # days
17
+ epoch_time / 3600 % 24, # hours
18
+ epoch_time / 60 % 60, # minutes
19
+ epoch_time % 60, # seconds
20
+ n % 1000 # fractional seconds
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SDLRuby
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -1,37 +1,37 @@
1
- module SDLRuby
2
- class Window
3
- module Surfacer
4
- include SDL
5
-
6
- def destroy_surface
7
- err = SDL.SDL_DestroyWindowSurface(self)
8
- raise SDLError if err < 0
9
- end
10
-
11
- def surface
12
- ptr = SDL.SDL_GetWindowSurface(self)
13
- raise SDLError if ptr.null?
14
-
15
- # ウィンドウからのサーフェスにはSDL_DONTFREEフラグが設定されている。
16
- # このためにSDL_FreeSurfaceではリファレンスカウンターを減じる事ができない。
17
- # これはRubyのGC管理では問題になる(メモリーリークする)。
18
- # そのためSDL_DONTFREEフラグを解除する!
19
- #
20
- st = SDL_Surface.new(ptr)
21
- st.flags &= ~SDL_DONTFREE
22
-
23
- Surface.new(ptr, add_ref: true)
24
- end
25
-
26
- # ウィンドウのサーフェスがvalidかどうか確認していない。
27
- # リサイズなどで壊れていてもtrueを戻す。
28
- #
29
- def surface? = SDL.SDL_HasWindowSurface(self) == 1
30
-
31
- def update_surface
32
- err = SDL.SDL_UpdateWindowSurface(self)
33
- raise SDLError if err < 0
34
- end
35
- end
36
- end
37
- end
1
+ module SDLRuby
2
+ class Window
3
+ module Surfacer
4
+ include SDL
5
+
6
+ def destroy_surface
7
+ err = SDL.SDL_DestroyWindowSurface(self)
8
+ raise SDLError if err < 0
9
+ end
10
+
11
+ def surface
12
+ ptr = SDL.SDL_GetWindowSurface(self)
13
+ raise SDLError if ptr.null?
14
+
15
+ # ウィンドウからのサーフェスにはSDL_DONTFREEフラグが設定されている。
16
+ # このためにSDL_FreeSurfaceではリファレンスカウンターを減じる事ができない。
17
+ # これはRubyのGC管理では問題になる(メモリーリークする)。
18
+ # そのためSDL_DONTFREEフラグを解除する!
19
+ #
20
+ st = SDL_Surface.new(ptr)
21
+ st.flags &= ~SDL_DONTFREE
22
+
23
+ Surface.new(ptr, add_ref: true)
24
+ end
25
+
26
+ # ウィンドウのサーフェスがvalidかどうか確認していない。
27
+ # リサイズなどで壊れていてもtrueを戻す。
28
+ #
29
+ def surface? = SDL.SDL_HasWindowSurface(self) == 1
30
+
31
+ def update_surface
32
+ err = SDL.SDL_UpdateWindowSurface(self)
33
+ raise SDLError if err < 0
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ module SDLRuby
2
+ module Keyboard
3
+ include SDL
4
+
5
+ def self.==: (untyped other) -> bool
6
+
7
+ def self.any?: () -> bool
8
+
9
+ def self.clear: () -> void
10
+
11
+ def self.mod: () -> Integer
12
+
13
+ def self.scancode: (String | Symbol s) -> Integer
14
+
15
+ def self.scancode_name: (Integer num) -> String
16
+
17
+ def self.size: () -> Integer
18
+
19
+ alias self.length self.size
20
+
21
+ def self.to_a: () -> Array[Integer]
22
+
23
+ def self.to_str: () -> String
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module SDLRuby
2
+ module Mouse
3
+ include SDL
4
+
5
+ def self.button: () -> Array[bool]
6
+
7
+ def self.capture=: (boolish) -> void
8
+
9
+ def self.global_button: () -> Array[bool]
10
+
11
+ def self.global_pos: () -> [Integer, Integer]
12
+
13
+ def self.global_pos=: ([Integer, Integer] xy) -> void
14
+
15
+ def self.global_state: () -> [Array[bool], Integer, Integer]
16
+
17
+ def self.pos: () -> [Integer, Integer]
18
+
19
+ def self.pos=: ([Integer, Integer] xy) -> void
20
+
21
+ def self.relative=: (boolish) -> untyped
22
+
23
+ def self.relative?: () -> bool
24
+
25
+ def self.relative_pos: () -> [Integer, Integer]
26
+
27
+ def self.relative_state: () -> [Array[bool], Integer, Integer]
28
+
29
+ def self.state: () -> [Array[bool], Integer, Integer]
30
+
31
+ private
32
+
33
+ def self.button_to_ary: (Integer num) -> ::Array[bool]
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module SDLRuby
2
+ module Timer
3
+ def self.ticks: () -> Integer
4
+
5
+ def self.inspect: () -> String
6
+ end
7
+ 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.2.1
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-12 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
@@ -102,6 +108,7 @@ files:
102
108
  - lib/SDLRuby/surface/surface_renderer.rb
103
109
  - lib/SDLRuby/text_input.rb
104
110
  - lib/SDLRuby/texture.rb
111
+ - lib/SDLRuby/timer.rb
105
112
  - lib/SDLRuby/ttf.rb
106
113
  - lib/SDLRuby/ttf/include/SDL_ttf.h.rb
107
114
  - lib/SDLRuby/ttf/ttf.rb
@@ -122,7 +129,10 @@ files:
122
129
  - sig/lib/SDLRuby/event.rbs
123
130
  - sig/lib/SDLRuby/event/accessor.rbs
124
131
  - sig/lib/SDLRuby/event/type.rbs
132
+ - sig/lib/SDLRuby/keyboard.rbs
133
+ - sig/lib/SDLRuby/mouse.rbs
125
134
  - sig/lib/SDLRuby/sdl.rbs
135
+ - sig/lib/SDLRuby/timer.rbs
126
136
  homepage: https://github.com/shinokaro/SDLRuby
127
137
  licenses:
128
138
  - MIT