sdl2-win93 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.dir-locals.el +2 -0
  3. data/.github/workflows/gempush.yml +29 -0
  4. data/.gitignore +14 -0
  5. data/COPYING.txt +165 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +24 -0
  8. data/Makefile +4 -0
  9. data/README.md +36 -0
  10. data/Rakefile +51 -0
  11. data/doc/po/ja.po +10357 -0
  12. data/ext/sdl2_ext/clipboard.c +61 -0
  13. data/ext/sdl2_ext/color.c +103 -0
  14. data/ext/sdl2_ext/color.h +4 -0
  15. data/ext/sdl2_ext/event.c +1298 -0
  16. data/ext/sdl2_ext/extconf.rb +22 -0
  17. data/ext/sdl2_ext/filesystem.c +63 -0
  18. data/ext/sdl2_ext/gamecontroller.c +408 -0
  19. data/ext/sdl2_ext/gamecontroller.c.m4 +408 -0
  20. data/ext/sdl2_ext/gl.c +351 -0
  21. data/ext/sdl2_ext/gl.c.m4 +351 -0
  22. data/ext/sdl2_ext/hint.c +99 -0
  23. data/ext/sdl2_ext/joystick.c +339 -0
  24. data/ext/sdl2_ext/joystick.c.m4 +339 -0
  25. data/ext/sdl2_ext/key.c +1302 -0
  26. data/ext/sdl2_ext/key.c.m4 +833 -0
  27. data/ext/sdl2_ext/main.c +258 -0
  28. data/ext/sdl2_ext/messagebox.c +233 -0
  29. data/ext/sdl2_ext/mixer.c +1205 -0
  30. data/ext/sdl2_ext/mixer.c.m4 +1205 -0
  31. data/ext/sdl2_ext/mouse.c +286 -0
  32. data/ext/sdl2_ext/rubysdl2_internal.h +127 -0
  33. data/ext/sdl2_ext/timer.c +63 -0
  34. data/ext/sdl2_ext/ttf.c +376 -0
  35. data/ext/sdl2_ext/ttf.c.m4 +376 -0
  36. data/ext/sdl2_ext/video.c +4093 -0
  37. data/ext/sdl2_ext/video.c.m4 +3867 -0
  38. data/lib/sdl2.rb +3 -0
  39. data/lib/sdl2/event.rb +55 -0
  40. data/lib/sdl2/version.rb +8 -0
  41. data/sample/chunk_destroy.rb +16 -0
  42. data/sample/gfxprimitives.rb +54 -0
  43. data/sample/icon.bmp +0 -0
  44. data/sample/memory_test/m1.rb +28 -0
  45. data/sample/memory_test/m2.rb +18 -0
  46. data/sample/memory_test/m3.rb +12 -0
  47. data/sample/message_box.rb +33 -0
  48. data/sample/music_player.rb +137 -0
  49. data/sample/playwave.rb +19 -0
  50. data/sample/primitives.rb +32 -0
  51. data/sample/test_clipboard.rb +16 -0
  52. data/sample/test_controller.rb +62 -0
  53. data/sample/test_joystick.rb +53 -0
  54. data/sample/test_keyboard.rb +52 -0
  55. data/sample/test_mouse.rb +50 -0
  56. data/sample/test_surface.rb +13 -0
  57. data/sample/test_ttf.rb +82 -0
  58. data/sample/test_video.rb +59 -0
  59. data/sample/testgl.rb +175 -0
  60. data/sample/testsprite.rb +296 -0
  61. data/sample/testspriteminimal.rb +75 -0
  62. data/sample/timer.rb +11 -0
  63. data/sample/version.rb +12 -0
  64. data/sample/video_info.rb +64 -0
  65. data/sdl2-win93.gemspec +31 -0
  66. metadata +158 -0
@@ -0,0 +1,296 @@
1
+ require 'sdl2'
2
+ require 'optparse'
3
+
4
+ MAX_SPEED = 1
5
+
6
+ class Integer
7
+ def bit?(mask)
8
+ (self & mask) != 0
9
+ end
10
+ end
11
+
12
+ class Cycle < Struct.new(:cycle_color, :cycle_alpha, :color, :alpha,
13
+ :color_direction, :alpha_direction)
14
+ def update
15
+ self.color += self.color_direction
16
+ if self.color < 0
17
+ self.color = 0
18
+ self.color_direction *= -1
19
+ end
20
+ if color > 255
21
+ self.color = 255
22
+ self.color_direction *= -1
23
+ end
24
+ self.alpha += self.alpha_direction
25
+ if self.alpha < 0
26
+ self.alpha = 0
27
+ self.alpha_direction *= -1
28
+ end
29
+ if self.alpha > 255
30
+ self.alpha = 255
31
+ self.alpha_direction *= -1
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ class WindowData
38
+ def initialize(sdl_window, renderer, cycle, blend_mode, use_color_key)
39
+ @sdl_window = sdl_window
40
+ @renderer = renderer
41
+ @cycle = cycle
42
+ @blend_mode = blend_mode
43
+ @use_color_key = use_color_key
44
+ end
45
+
46
+ def setup(spritepath, num_sprites)
47
+ load_sprite(spritepath)
48
+ @sprite.blend_mode = @blend_mode
49
+ @sprites = Array.new(num_sprites){
50
+ Sprite.new(@sdl_window, @renderer, @sprite)
51
+ }
52
+ end
53
+
54
+ def load_sprite(fname)
55
+ bitmap = SDL2::Surface.load(fname)
56
+ bitmap.color_key = bitmap.pixel(0, 0) if @use_color_key
57
+ @sprite = @renderer.create_texture_from(bitmap)
58
+ bitmap.destroy
59
+ end
60
+
61
+ def update
62
+ @sprites.each(&:update)
63
+ end
64
+
65
+ def draw
66
+ viewport = @renderer.viewport
67
+ # Draw a gray background
68
+ @renderer.draw_color = [0xA0, 0xA0, 0xA0]
69
+ @renderer.clear
70
+
71
+ # Points
72
+ @renderer.draw_color = [0xff, 0, 0]
73
+ @renderer.draw_point(0, 0)
74
+ @renderer.draw_point(viewport.w - 1, 0)
75
+ @renderer.draw_point(0 ,viewport.h - 1)
76
+ @renderer.draw_point(viewport.w - 1, viewport.h - 1)
77
+
78
+ # Fill rect
79
+ @renderer.draw_color = [0xff, 0xff, 0xff]
80
+ @renderer.fill_rect(SDL2::Rect[viewport.w/2 - 50, viewport.h/2 - 50, 100, 100])
81
+
82
+ # Draw rect
83
+ @renderer.draw_color = [0, 0, 0]
84
+ @renderer.draw_rect(SDL2::Rect[viewport.w/2 - 50, viewport.h/2 - 50, 100, 100])
85
+
86
+ # lines
87
+ @renderer.draw_color = [0, 0xff, 0]
88
+ @renderer.draw_line(1, 1, viewport.w - 2, viewport.h - 2)
89
+ @renderer.draw_line(1, viewport.h - 2, viewport.w - 2, 1)
90
+
91
+ @sprite.color_mod = [@cycle.color, 255, @cycle.color] if @cycle.cycle_color
92
+ @sprite.alpha_mod = @cycle.alpha if @cycle.cycle_alpha
93
+
94
+ @sprites.each(&:draw)
95
+ @renderer.present
96
+ end
97
+
98
+ class Sprite
99
+ def initialize(window, renderer, sprite, max_speed = MAX_SPEED)
100
+ @renderer = renderer
101
+ @sprite = sprite
102
+ window_w, window_h = window.size
103
+ @position = SDL2::Rect[rand(window_w - sprite.w),
104
+ rand(window_h - sprite.h),
105
+ sprite.w, sprite.h]
106
+ @velocity = random_velocity(max_speed)
107
+ end
108
+
109
+ def random_velocity(max_speed)
110
+ loop do
111
+ dx = rand(2*max_speed + 1) - max_speed
112
+ dy = rand(2*max_speed + 1) - max_speed
113
+ return SDL2::Point[dx, dy] if dx != 0 || dy != 0
114
+ end
115
+ end
116
+
117
+ def update
118
+ viewport = @renderer.viewport
119
+ @position.x += @velocity.x
120
+ @position.y += @velocity.y
121
+ unless (0 .. viewport.w - @sprite.w).cover?(@position.x)
122
+ @velocity.x *= -1
123
+ @position.x += @velocity.x
124
+ end
125
+
126
+ unless (0 .. viewport.h - @sprite.h).cover?(@position.y)
127
+ @velocity.y *= -1
128
+ @position.y += @velocity.y
129
+ end
130
+ end
131
+
132
+ def draw
133
+ @renderer.copy(@sprite, nil, @position)
134
+ end
135
+ end
136
+ end
137
+
138
+ class App
139
+ def initialize
140
+ @title = "testsprite"
141
+ @window_flags = 0
142
+ @icon_path = nil
143
+ @num_window = 1
144
+ @window_x = @window_y = SDL2::Window::POS_UNDEFINED
145
+ @window_w = 640
146
+ @window_h = 480
147
+ @spritepath = "icon.bmp"
148
+ @renderer_flags = 0
149
+ @num_sprites = 100
150
+ @blend_mode = SDL2::BlendMode::BLEND
151
+ @cycle = Cycle.new(false, false, rand(255), rand(255), [1,-1].sample, [1,-1].sample)
152
+ @use_color_key = true
153
+ end
154
+
155
+ def options
156
+ opts = OptionParser.new("Usage: testsprite [options] [SPRITE]")
157
+ opts.version = SDL2::VERSION
158
+ opts.release = nil
159
+
160
+ opts.on("-m", "--mode MODE", "fullscreen|fullscreen-desktop|window"){|mode|
161
+ case mode
162
+ when "fullscreen"
163
+ raise "Only one window is available for fullscreen mode" if @num_window != 1
164
+ @window_flags |= SDL2::Window::FULLSCREEN
165
+ when "fullscreen-desktop"
166
+ raise "Only one window is available for fullscreen-desktop mode" if @num_window != 1
167
+ @window_flags |= SDL2::Window::FULLSCREEN_DESKTOP
168
+ when "window"
169
+ @window_flags &= ~(SDL2::Window::FULLSCREEN|SDL2::Window::FULLSCREEN_DESKTOP)
170
+ end
171
+ }
172
+
173
+ opts.on("--windows N", "Number of windows", Integer){|n|
174
+ if n != 1 and @window_flags.bit?(SDL2::Window::FULLSCREEN|SDL2::Window::FULLSCREEN_DESKTOP)
175
+ raise "Only one window is available for fullscreen mode"
176
+ end
177
+ @num_window = n
178
+ }
179
+
180
+ opts.on("--geometry WxH", "size of the window/screen"){|s|
181
+ raise "geomtry format error #{s}" unless /\A(\d+)x(\d+)\Z/ =~ s
182
+ @window_w = Integer($1); @window_h = Integer($2)
183
+ }
184
+
185
+ opts.on("--center", "Window will be centered"){
186
+ @window_x = @window_y = SDL2::Window::POS_CENTERED
187
+ }
188
+
189
+ opts.on("-t", "--title TITLE", "Window title"){|title| @title = title }
190
+
191
+ opts.on("--icon ICON", "Window icon"){|path| @icon_path = path }
192
+
193
+ opts.on("--position X,Y", "Position of the window", Array){|x,y|
194
+ @window_x = Integer(x); @window_y = Integer(y)
195
+ }
196
+
197
+ opts.on("--noframe", "Window is borderless") { @window_flags |= SDL2::Window::BORDERLESS }
198
+
199
+ opts.on("--resize", "Window is resizable") { @window_flags |= SDL2::Window::RESIZABLE }
200
+
201
+ opts.on("--allow-highdip"){ @window_flags |= SDL2::Window::ALLOW_HIGHDPI }
202
+
203
+ opts.on("--blend MODE", "none|blend|add|mod"){|blend_mode|
204
+ @blend_mode = case blend_mode
205
+ when "none"
206
+ SDL2::BlendMode::NONE
207
+ when "blend"
208
+ SDL2::BlendMode::BLEND
209
+ when "add"
210
+ SDL2::BlendMode::ADD
211
+ when "mod"
212
+ SDL2::BlendMode::MOD
213
+ end
214
+ }
215
+
216
+ opts.on("--cycle-color"){ @cycle.cycle_color = true }
217
+
218
+ opts.on("--cycle-alpha"){ @cycle.cycle_alpha = true }
219
+
220
+ opts.on("--vsync", "Present is syncronized with the refresh rate") {
221
+ @renderer_flags |= SDL2::Renderer::Flags::PRESENTVSYNC
222
+ }
223
+
224
+ opts.on("--use-color-key (yes|no)", TrueClass){|bool| @use_color_key = bool }
225
+ opts
226
+ end
227
+
228
+ def run(argv)
229
+ options.parse!(argv)
230
+ @spritepath = argv.shift || @spritepath
231
+
232
+ SDL2.init(SDL2::INIT_VIDEO)
233
+ icon = load_icon
234
+ @windows = @num_window.times.map do |n|
235
+ window = SDL2::Window.create("#{@title} #{n}",
236
+ @window_x, @window_y, @window_w, @window_h,
237
+ @window_flags)
238
+ renderer = window.create_renderer(-1, @renderer_flags)
239
+ window.icon = icon if icon
240
+
241
+ WindowData.new(window, renderer, @cycle, @blend_mode, @use_color_key)
242
+ end
243
+
244
+ @windows.each{|win| win.setup(@spritepath, @num_sprites) }
245
+
246
+ loop do
247
+ while event = SDL2::Event.poll
248
+ handle_event(event)
249
+ end
250
+ @cycle.update
251
+ @windows.each {|window| window.update; window.draw }
252
+ sleep 0.01
253
+ end
254
+ end
255
+
256
+ def load_icon
257
+ return nil if @icon_path.nil?
258
+ SDL2::Surface.load(@icon_path)
259
+ end
260
+
261
+ def handle_event(event)
262
+ case event
263
+ when SDL2::Event::Quit
264
+ exit
265
+ when SDL2::Event::KeyDown
266
+ case event.sym
267
+ when SDL2::Key::ESCAPE
268
+ exit
269
+ when SDL2::Key::RETURN
270
+ if event.mod.bit?(SDL2::Key::Mod::ALT)
271
+ if event.window.fullscreen_mode == 0
272
+ event.window.fullscreen_mode = SDL2::Window::FULLSCREEN_DESKTOP
273
+ else
274
+ event.window.fullscreen_mode = 0
275
+ end
276
+ end
277
+ when SDL2::Key::M
278
+ if event.mod.bit?(SDL2::Key::Mod::CTRL|SDL2::Key::Mod::CAPS)
279
+ window = event.window
280
+ if window.flags & SDL2::Window::MAXIMIZED != 0
281
+ window.restore
282
+ else
283
+ window.maximize
284
+ end
285
+ end
286
+ when SDL2::Key::Z
287
+ if event.mod.bit?(SDL2::Key::Mod::CTRL|SDL2::Key::Mod::CAPS)
288
+ window = event.window
289
+ window.minimize if window
290
+ end
291
+ end
292
+ end
293
+ end
294
+ end
295
+
296
+ App.new.run(ARGV)
@@ -0,0 +1,75 @@
1
+ require 'sdl2'
2
+
3
+ SpriteMotion = Struct.new(:position, :velocity)
4
+ WINDOW_W = 640
5
+ WINDOW_H = 480
6
+ NUM_SPRITES = 100
7
+ MAX_SPEED = 1
8
+
9
+ def load_sprite(filename, renderer)
10
+ bitmap = SDL2::Surface.load_bmp(filename)
11
+ bitmap.color_key = bitmap.pixel(0, 0)
12
+ sprite = renderer.create_texture_from(bitmap)
13
+ bitmap.destroy
14
+
15
+ sprite
16
+ end
17
+
18
+ def move_sprite(motions, renderer, sprite)
19
+ # draw a gray background
20
+ renderer.draw_color = [0xA0, 0xA0, 0xA0]
21
+ renderer.clear
22
+
23
+ sprite_w = sprite.w; sprite_h = sprite.h
24
+
25
+ motions.each do |m|
26
+ m.position.x += m.velocity.x
27
+ if m.position.x < 0 || m.position.x >= WINDOW_W - sprite_w
28
+ m.velocity.x *= -1
29
+ m.position.x += m.velocity.x
30
+ end
31
+
32
+ m.position.y += m.velocity.y
33
+ if m.position.y < 0 || m.position.y >= WINDOW_H - sprite_h
34
+ m.velocity.y *= -1
35
+ m.position.y += m.velocity.y
36
+ end
37
+
38
+ renderer.copy(sprite, nil, m.position)
39
+ end
40
+
41
+ renderer.present
42
+ end
43
+
44
+ def random_position(sprite_w, sprite_h)
45
+ SDL2::Rect[rand(WINDOW_W - sprite_w), rand(WINDOW_H - sprite_h), sprite_w, sprite_h]
46
+ end
47
+
48
+ def random_velocity
49
+ loop do
50
+ x = rand(MAX_SPEED*2+1) - MAX_SPEED
51
+ y = rand(MAX_SPEED*2+1) - MAX_SPEED
52
+ return SDL2::Point.new(x, y) if x != 0 || y != 0
53
+ end
54
+ end
55
+
56
+ window = SDL2::Window.create("testspritesimple", 0, 0, WINDOW_W, WINDOW_H, 0)
57
+ renderer = window.create_renderer(-1, 0)
58
+ sprite = load_sprite("icon.bmp", renderer)
59
+ sprite_w = sprite.w; sprite_h = sprite.h
60
+
61
+ motions = Array.new(NUM_SPRITES) do
62
+ SpriteMotion.new(random_position(sprite_w, sprite_h), random_velocity)
63
+ end
64
+
65
+ loop do
66
+ while event = SDL2::Event.poll
67
+ case event
68
+ when SDL2::Event::Quit, SDL2::Event::KeyDown
69
+ exit
70
+ end
71
+ end
72
+
73
+ move_sprite(motions, renderer, sprite)
74
+ sleep 0.01
75
+ end
@@ -0,0 +1,11 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_TIMER)
4
+
5
+ p SDL2.get_ticks
6
+ p SDL2.get_performance_counter
7
+ p SDL2.get_performance_frequency
8
+ SDL2.delay 50
9
+ p SDL2.get_ticks
10
+ p SDL2.get_performance_counter
11
+ p SDL2.get_performance_frequency
@@ -0,0 +1,12 @@
1
+ require 'sdl2'
2
+
3
+ p SDL2::LIBSDL_VERSION
4
+ p SDL2::LIBSDL_VERSION_NUMBER
5
+ p SDL2::LIBSDL_REVISION
6
+ p SDL2::LIBSDL_REVISION_NUMBER
7
+ p SDL2::LIBSDL_IMAGE_VERSION
8
+ p SDL2::LIBSDL_IMAGE_VERSION_NUMBER
9
+ p SDL2::LIBSDL_MIXER_VERSION
10
+ p SDL2::LIBSDL_MIXER_VERSION_NUMBER
11
+ p SDL2::LIBSDL_TTF_VERSION
12
+ p SDL2::LIBSDL_TTF_VERSION_NUMBER
@@ -0,0 +1,64 @@
1
+ require 'sdl2'
2
+ require "pp"
3
+
4
+ SDL2.init(SDL2::INIT_VIDEO)
5
+
6
+ pp SDL2::PixelFormat::FORMATS.map{|f| f.name}
7
+ p SDL2::PixelFormat::RGBA8888
8
+
9
+ p SDL2::Display.displays
10
+ SDL2::Display.displays.each{|display| p display.modes }
11
+ display = SDL2::Display.displays.first
12
+ print "curent mode: "; p display.current_mode
13
+ print "desktop mode: "; p display.desktop_mode
14
+ search_mode = SDL2::Display::Mode.new(SDL2::PixelFormat::UNKNOWN, 640, 480, 60)
15
+ puts "The mode closest to #{search_mode.inspect} is #{display.closest_mode(search_mode).inspect}"
16
+ print "bounds: "; p display.bounds
17
+ puts "current video driver: #{SDL2.current_video_driver}"
18
+
19
+ window = SDL2::Window.create("video info", 10, 10, 640, 480, 0)
20
+ renderer = window.create_renderer(-1, 0)
21
+ texture = renderer.load_texture("icon.bmp")
22
+
23
+ puts "window id: #{window.window_id}"
24
+ p SDL2::Window.all_windows
25
+ p window.display_mode
26
+ p window.display
27
+ print "window brightness: "; p window.brightness
28
+ print "window input grabbing: "; p window.input_is_grabbed?
29
+ print "window maximum size: "; p window.maximum_size
30
+ print "window minimum size: "; p window.minimum_size
31
+ print "window position: "; p window.position
32
+ print "window size: "; p window.size
33
+ print "window title: "; p window.title
34
+ print "window bordered: "; p window.bordered
35
+
36
+ p window.renderer
37
+ p renderer
38
+
39
+ p SDL2.video_drivers
40
+ p SDL2::Renderer.drivers_info
41
+ p renderer.info
42
+ p renderer.clip_rect
43
+ p renderer.logical_size
44
+ p renderer.scale
45
+ p renderer.viewport
46
+ p renderer.support_render_target?
47
+ p renderer.output_size
48
+
49
+ p renderer.info.texture_formats
50
+ renderer.info.texture_formats.each do |format|
51
+ p [format.format, format.name, format.type, format.order, format.layout, format.bpp, format.bytes_per_pixel,
52
+ format.indexed?, format.alpha?, format.fourcc?]
53
+
54
+ end
55
+
56
+ p texture
57
+ texture.destroy
58
+ p texture
59
+
60
+ p SDL2::ScreenSaver.enabled?
61
+ SDL2::ScreenSaver.disable
62
+ p SDL2::ScreenSaver.enabled?
63
+
64
+ renderer.present