ruby-sdl2 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.
@@ -0,0 +1,18 @@
1
+ require "sdl2"
2
+
3
+ SDL2.init(SDL2::INIT_VIDEO|SDL2::INIT_EVENTS)
4
+
5
+ $window = SDL2::Window.create("testsprite", 0, 0, 128, 128, 0)
6
+
7
+ def f
8
+ renderer = $window.create_renderer(-1, 0)
9
+ surface = SDL2::Surface.load_bmp("icon.bmp")
10
+ a = Array.new(10) { renderer.create_texture_from(surface) }
11
+ p a.map{|tex| tex.debug_info }
12
+ p renderer.debug_info
13
+ a
14
+ end
15
+
16
+ a = f
17
+ GC.start
18
+ p a.map{|tex| tex.debug_info }
@@ -0,0 +1,12 @@
1
+ require "sdl2"
2
+
3
+ SDL2.init(SDL2::INIT_VIDEO|SDL2::INIT_EVENTS)
4
+
5
+ window = SDL2::Window.create("testsprite",
6
+ SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED,
7
+ 640, 480, 0)
8
+ renderer = window.create_renderer(-1, 0)
9
+ surface = SDL2::Surface.load_bmp("icon.bmp")
10
+ 10.times { renderer.create_texture_from(surface) }
11
+
12
+ window.destroy
@@ -0,0 +1,33 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'sdl2'
3
+
4
+ SDL2.show_simple_message_box(SDL2::MESSAGEBOX_WARNING, "warning! warning!",
5
+ "A huge battleship is approaching fast", nil)
6
+
7
+ button = SDL2.show_message_box(flags: SDL2::MESSAGEBOX_WARNING,
8
+ window: nil,
9
+ title: "警告ウインドウ",
10
+ message: "ここに警告文が出ます",
11
+ buttons: [ { # flags is ignored
12
+ id: 0,
13
+ text: "いいえ",
14
+ },
15
+ {flags: SDL2::MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
16
+ id: 1,
17
+ text: "はい",
18
+ },
19
+ {flags: SDL2::MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
20
+ id: 2,
21
+ text: "キャンセル",
22
+ },
23
+ ],
24
+ color_scheme: {
25
+ bg: [255, 0, 0],
26
+ text: [0, 255, 0],
27
+ button_border: [255, 0, 0],
28
+ button_bg: [0, 0, 255],
29
+ button_selected: [255, 0, 0]
30
+ }
31
+ )
32
+ p button
33
+
@@ -0,0 +1,133 @@
1
+ require 'sdl2'
2
+
3
+ class TextureForText
4
+ def initialize(renderer, font)
5
+ @renderer = renderer
6
+ @text_surfaces = {}
7
+ @font = font
8
+ @texture = nil
9
+ end
10
+
11
+ def add_text(text)
12
+ @text_surfaces[text] = @font.render_blended(text, [0xff, 0xff, 0xff])
13
+ end
14
+
15
+ def creat_texture!
16
+ @text_rects = Hash.new
17
+ h = @text_surfaces.inject(0){|y, (text, surface)|
18
+ @text_rects[text] = SDL2::Rect[0, y, surface.w, surface.h]
19
+ y + surface.h
20
+ }
21
+ w = @text_rects.values.map(&:w).max
22
+
23
+ tmp_surface = SDL2::Surface.new(w, h, 32)
24
+ @text_surfaces.each do |text, surface|
25
+ SDL2::Surface.blit(surface, nil, tmp_surface, @text_rects[text])
26
+ end
27
+ @texture = @renderer.create_texture_from(tmp_surface)
28
+ tmp_surface.destroy
29
+
30
+ @text_surfaces = nil
31
+ end
32
+
33
+ def box_for(text)
34
+ @text_rects[text]
35
+ end
36
+
37
+ def render(text, dst)
38
+ srcrect = @text_rects[text]
39
+ dst = SDL2::Rect[dst.x, dst.y, srcrect.w, srcrect.h] if dst.is_a?(SDL2::Point)
40
+ @renderer.copy(@texture, srcrect, dst)
41
+ end
42
+
43
+ attr_reader :texture
44
+ end
45
+
46
+ class MusicPlayer
47
+ def run(argv)
48
+ file = argv.shift
49
+
50
+ SDL2.init(SDL2::INIT_EVERYTHING)
51
+ SDL2::Mixer.init(SDL2::Mixer::INIT_FLAC|SDL2::Mixer::INIT_MOD|
52
+ SDL2::Mixer::INIT_MODPLUG|SDL2::Mixer::INIT_MP3|
53
+ SDL2::Mixer::INIT_OGG)
54
+ SDL2::Mixer.open(44100, SDL2::Mixer::DEFAULT_FORMAT, 2, 512)
55
+ SDL2::TTF.init
56
+
57
+ @window = SDL2::Window.create("music player sample by Ruby/SDL2", 0, 0, 640, 480, 0)
58
+ @renderer = @window.create_renderer(-1, 0)
59
+ @music = SDL2::Mixer::Music.load(file)
60
+ font = SDL2::TTF.open("font.ttf", 24)
61
+
62
+ @texts = TextureForText.new(@renderer, font)
63
+ @texts.add_text(file)
64
+ @texts.add_text("playing")
65
+ @texts.add_text("playing (fade in)")
66
+ @texts.add_text("stopping")
67
+ @texts.add_text("fade out")
68
+ @texts.add_text("pause")
69
+ @texts.creat_texture!
70
+
71
+ loop do
72
+ while event = SDL2::Event.poll
73
+ handle_event(event)
74
+ end
75
+
76
+ @renderer.draw_color = [0, 0, 0]
77
+ @renderer.clear
78
+
79
+ @texts.render(file, SDL2::Point[20, 20])
80
+ @texts.render(playing_state, SDL2::Point[20, 120])
81
+
82
+ @renderer.present
83
+ sleep 0.01
84
+ end
85
+ end
86
+
87
+ def handle_event(event)
88
+ case event
89
+ when SDL2::Event::Quit
90
+ exit
91
+ when SDL2::Event::KeyDown
92
+ keydown(event)
93
+ end
94
+ end
95
+
96
+ def keydown(event)
97
+ match_keydown("ESCAPE", event){ exit }
98
+ match_keydown("p", event) { SDL2::Mixer::MusicChannel.play(@music, 1) }
99
+ match_keydown("h", event) { SDL2::Mixer::MusicChannel.halt }
100
+ match_keydown("r", event) { SDL2::Mixer::MusicChannel.rewind }
101
+ match_keydown("o", event) { SDL2::Mixer::MusicChannel.fade_out(1000) }
102
+ match_keydown("i", event) { SDL2::Mixer::MusicChannel.fade_in(@music, 1, 1000) }
103
+ match_keydown("SPACE", event) {
104
+ if SDL2::Mixer::MusicChannel.pause?
105
+ SDL2::Mixer::MusicChannel.resume
106
+ else
107
+ SDL2::Mixer::MusicChannel.pause
108
+ end
109
+ }
110
+ end
111
+
112
+ def match_keydown(key, event)
113
+ sym = SDL2::Key.keycode_from_name(key)
114
+ yield if event.sym == sym
115
+ end
116
+
117
+ def playing_state
118
+ return "stopping" if !SDL2::Mixer::MusicChannel.play?
119
+ return "pause" if SDL2::Mixer::MusicChannel.pause?
120
+ case SDL2::Mixer::MusicChannel.fading
121
+ when SDL2::Mixer::NO_FADING
122
+ return "playing"
123
+ when SDL2::Mixer::FADING_IN
124
+ return "playing (fade in)"
125
+ when SDL2::Mixer::FADING_OUT
126
+ return "fade out"
127
+ end
128
+ end
129
+ end
130
+
131
+
132
+ MusicPlayer.new.run(ARGV)
133
+
@@ -0,0 +1,19 @@
1
+ # ruby playwave.rb sample.wav
2
+ require 'sdl2'
3
+
4
+ SDL2::init(SDL2::INIT_AUDIO)
5
+
6
+ SDL2::Mixer.init(SDL2::Mixer::INIT_FLAC|SDL2::Mixer::INIT_MOD|
7
+ SDL2::Mixer::INIT_MP3|SDL2::Mixer::INIT_OGG)
8
+ SDL2::Mixer.open(22050, SDL2::Mixer::DEFAULT_FORMAT, 2, 512)
9
+ p SDL2::Mixer::Chunk.decoders
10
+ p SDL2::Mixer::Music.decoders
11
+ wave = SDL2::Mixer::Chunk.load(ARGV[0])
12
+
13
+ SDL2::Mixer::Channels.fade_in(0, wave, 0, 600)
14
+
15
+ while SDL2::Mixer::Channels.play?(0)
16
+ sleep 1
17
+ end
18
+
19
+
@@ -0,0 +1,32 @@
1
+ require "sdl2"
2
+
3
+ SDL2.init(SDL2::INIT_VIDEO|SDL2::INIT_EVENTS)
4
+
5
+ window = SDL2::Window.create("testsprite",
6
+ SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED,
7
+ 640, 480, 0)
8
+ renderer = window.create_renderer(-1, 0)
9
+
10
+ renderer.draw_color = [255, 0, 0, 255]
11
+ renderer.draw_line(0,0,640,480)
12
+ renderer.draw_color = [255, 255, 0]
13
+ renderer.draw_point(200, 200)
14
+ renderer.draw_color = [0, 255, 255]
15
+ renderer.draw_rect(SDL2::Rect.new(500, 20, 40, 60))
16
+ renderer.fill_rect(SDL2::Rect.new(20, 400, 60, 40))
17
+ renderer.draw_blend_mode = SDL2::BLENDMODE_ADD
18
+ renderer.draw_color = [255, 0, 0]
19
+ renderer.draw_rect(SDL2::Rect.new(40, 420, 60, 40))
20
+
21
+ renderer.present
22
+
23
+ loop do
24
+ while ev = SDL2::Event.poll
25
+ if SDL2::Event::KeyDown === ev && ev.scancode == SDL2::Key::Scan::ESCAPE
26
+ exit
27
+ end
28
+ end
29
+
30
+ renderer.present
31
+ sleep 0.1
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_EVERYTHING)
4
+ window = SDL2::Window.create("clipboard", 0, 0, 640, 480, 0)
5
+
6
+ # you can call clipboard functions after creating window (on X11)
7
+ p SDL2::Clipboard.has_text?
8
+ p SDL2::Clipboard.text
9
+ # text= is not reliable on X11 environment
10
+
11
+
@@ -0,0 +1,57 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_EVERYTHING)
4
+
5
+
6
+ p SDL2::GameController.axis_name_of(SDL2::GameController::AXIS_LEFTY)
7
+ p SDL2::GameController.axis_name_of(129) rescue p $!
8
+ p SDL2::GameController.axis_from_name("rightx")
9
+ p SDL2::GameController.button_name_of(SDL2::GameController::BUTTON_Y)
10
+ p SDL2::GameController.button_from_name("x")
11
+ p SDL2::GameController.button_from_name("rightx") rescue p $!
12
+
13
+ if SDL2::Joystick.num_connected_joysticks == 0
14
+ puts "You need to connect at least one joystick"
15
+ exit
16
+ end
17
+
18
+ joystick = SDL2::Joystick.open(0)
19
+ guid = joystick.GUID
20
+ joystick.close
21
+ SDL2::GameController.add_mapping([guid, "No1",
22
+ "leftx:a0,lefty:a1",
23
+ "a:b3,b:b2,x:b1,y:b0"
24
+ ].join(","))
25
+
26
+
27
+ p SDL2::Joystick.game_controller?(0)
28
+ p SDL2::GameController.device_names
29
+ controller = SDL2::GameController.open(0)
30
+ p controller.name
31
+ p controller.attached?
32
+ p controller.mapping
33
+
34
+ $window = SDL2::Window.create("test_controller", 0,0,640,480,0)
35
+
36
+ loop do
37
+ while event = SDL2::Event.poll
38
+ case event
39
+ when SDL2::Event::Quit
40
+ exit
41
+ when SDL2::Event::KeyDown
42
+ exit if event.sym == SDL2::Key::ESCAPE
43
+ when SDL2::Event::ControllerAxisMotion
44
+ p event
45
+ p controller.axis(SDL2::GameController::AXIS_LEFTX)
46
+ p controller.axis(SDL2::GameController::AXIS_LEFTY)
47
+ when SDL2::Event::ControllerButton
48
+ p event
49
+ p controller.button_pressed?(SDL2::GameController::BUTTON_A)
50
+ p controller.button_pressed?(SDL2::GameController::BUTTON_B)
51
+ p controller.button_pressed?(SDL2::GameController::BUTTON_X)
52
+ p controller.button_pressed?(SDL2::GameController::BUTTON_Y)
53
+ when SDL2::Event::ControllerDevice
54
+ p event
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_EVERYTHING)
4
+ SDL2::TTF.init
5
+
6
+ p SDL2::Joystick.num_connected_joysticks
7
+ p SDL2::Joystick.devices
8
+
9
+ if SDL2::Joystick.num_connected_joysticks > 0
10
+ $joystick = SDL2::Joystick.open(0)
11
+ p $joystick.name
12
+ p $joystick.num_axes
13
+ p $joystick.num_hats
14
+ p $joystick.num_buttons
15
+ p $joystick.num_balls
16
+ p $joystick.GUID
17
+ p $joystick.attached?
18
+ p $joystick.index
19
+ end
20
+
21
+ window = SDL2::Window.create("testsprite",0, 0, 640, 480, 0)
22
+
23
+ renderer = window.create_renderer(-1, 0)
24
+
25
+
26
+ loop do
27
+ while ev = SDL2::Event.poll
28
+ case ev
29
+ when SDL2::Event::JoyButton, SDL2::Event::JoyAxisMotion
30
+ p ev
31
+ when SDL2::Event::JoyDeviceAdded
32
+ p ev
33
+ $joystick = SDL2::Joystick.open(ev.which)
34
+ when SDL2::Event::JoyDeviceRemoved
35
+ p ev
36
+ p $joystick.name
37
+ when SDL2::Event::KeyDown
38
+ if ev.scancode == SDL2::Key::Scan::ESCAPE
39
+ exit
40
+ end
41
+ when SDL2::Event::Quit
42
+ exit
43
+ end
44
+ end
45
+
46
+ renderer.present
47
+ sleep 0.1
48
+ end
@@ -0,0 +1,44 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_EVERYTHING)
4
+
5
+ window = SDL2::Window.create("testsprite", 0, 0, 640, 480, 0)
6
+ renderer = window.create_renderer(-1, 0)
7
+
8
+ puts "scancode of \"X\": #{SDL2::Key::Scan.from_name("X")}"
9
+ puts "scancode of SDL2::Key::X: #{SDL2::Key::Scan.from_keycode(SDL2::Key::X)}"
10
+ puts "keycode of \"X\": #{SDL2::Key.keycode_from_name("X")}"
11
+ puts "keycode of SDL2::Key::Scan::X: #{SDL2::Key.keycode_from_scancode(SDL2::Key::Scan::X)}"
12
+ SDL2::TextInput.rect = SDL2::Rect[20, 20, 200, 20]
13
+
14
+
15
+ def toggle_text_input
16
+ if SDL2::TextInput.active?
17
+ SDL2::TextInput.stop
18
+ else
19
+ SDL2::TextInput.start
20
+ end
21
+ p SDL2::TextInput.active?
22
+ end
23
+
24
+ loop do
25
+ while ev = SDL2::Event.poll
26
+ case ev
27
+ when SDL2::Event::Quit
28
+ exit
29
+ when SDL2::Event::TextInput, SDL2::Event::TextEditing
30
+ p ev
31
+ when SDL2::Event::KeyDown
32
+ puts "scancode: #{ev.scancode}(#{SDL2::Key::Scan.name_of(ev.scancode)})"
33
+ puts "keycode: #{ev.sym}(#{SDL2::Key.name_of(ev.sym)})"
34
+ puts "mod: #{ev.mod}"
35
+ puts "mod(SDL2::Key::Mod.state): #{SDL2::Key::Mod.state}"
36
+ if ev.sym == SDL2::Key::RETURN
37
+ toggle_text_input
38
+ end
39
+ end
40
+ end
41
+
42
+ renderer.present
43
+ sleep 0.01
44
+ end
@@ -0,0 +1,44 @@
1
+ require 'sdl2'
2
+
3
+ SDL2.init(SDL2::INIT_EVERYTHING)
4
+ window = SDL2::Window.create("testsprite",0, 0, 640, 480, 0)
5
+ renderer = window.create_renderer(0, SDL2::Renderer::ACCELERATED)
6
+ SDL2::TextInput.stop
7
+
8
+ loop do
9
+ while ev = SDL2::Event.poll
10
+ case ev
11
+ when SDL2::Event::Quit
12
+ exit
13
+ when SDL2::Event::KeyDown
14
+ case ev.sym
15
+ when SDL2::Key::ESCAPE
16
+ exit
17
+ when SDL2::Key::S
18
+ state = SDL2::Mouse.state
19
+ p state
20
+ p [state.x, state.y, state.button_bits,
21
+ state.pressed?(1), state.pressed?(2), state.pressed?(3)]
22
+ when SDL2::Key::R
23
+ p SDL2::Mouse.relative_state
24
+ when SDL2::Key::SPACE
25
+ SDL2::Mouse.relative_mode = ! SDL2::Mouse.relative_mode?
26
+ when SDL2::Key::T
27
+ if SDL2::Mouse::Cursor.shown?
28
+ SDL2::Mouse::Cursor.hide
29
+ else
30
+ SDL2::Mouse::Cursor.show
31
+ end
32
+ when SDL2::Key::F
33
+ p SDL2::Mouse.focused_window
34
+ when SDL2::Key::W
35
+ SDL2::Mouse::Cursor.warp(window, 100, 20)
36
+ end
37
+ when SDL2::Event::MouseButtonDown
38
+ p ev
39
+ end
40
+ end
41
+
42
+ renderer.present
43
+ sleep 0.01
44
+ end