ruby-sdl2 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.dir-locals.el +2 -0
- data/.gitignore +17 -0
- data/COPYING.txt +165 -0
- data/README.md +30 -0
- data/Rakefile +95 -0
- data/clipboard.c +37 -0
- data/doc/po/ja.po +1160 -0
- data/event.c +1027 -0
- data/extconf.rb +53 -0
- data/filesystem.c +36 -0
- data/gamecontroller.c +194 -0
- data/gl.c.m4 +201 -0
- data/hint.c +56 -0
- data/joystick.c +316 -0
- data/key.c.m4 +794 -0
- data/lib/sdl2.rb +3 -0
- data/lib/sdl2/version.rb +6 -0
- data/main.c +244 -0
- data/messagebox.c +231 -0
- data/mixer.c +1079 -0
- data/mouse.c +286 -0
- data/rubysdl2_internal.h +125 -0
- data/sample/chunk_destroy.rb +16 -0
- data/sample/icon.bmp +0 -0
- data/sample/memory_test/m1.rb +28 -0
- data/sample/memory_test/m2.rb +18 -0
- data/sample/memory_test/m3.rb +12 -0
- data/sample/message_box.rb +33 -0
- data/sample/music_player.rb +133 -0
- data/sample/playwave.rb +19 -0
- data/sample/primitives.rb +32 -0
- data/sample/test_clipboard.rb +11 -0
- data/sample/test_controller.rb +57 -0
- data/sample/test_joystick.rb +48 -0
- data/sample/test_keyboard.rb +44 -0
- data/sample/test_mouse.rb +44 -0
- data/sample/test_surface.rb +8 -0
- data/sample/test_ttf.rb +77 -0
- data/sample/test_video.rb +53 -0
- data/sample/testgl.rb +175 -0
- data/sample/testsprite.rb +296 -0
- data/sample/testspriteminimal.rb +75 -0
- data/sample/timer.rb +11 -0
- data/sample/version.rb +12 -0
- data/sample/video_info.rb +65 -0
- data/timer.c +63 -0
- data/ttf.c +196 -0
- data/video.c.m4 +2935 -0
- metadata +102 -0
data/sample/test_ttf.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'sdl2'
|
2
|
+
|
3
|
+
SDL2.init(SDL2::INIT_EVERYTHING)
|
4
|
+
SDL2::TTF.init
|
5
|
+
|
6
|
+
|
7
|
+
window = SDL2::Window.create("testsprite",
|
8
|
+
SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED,
|
9
|
+
640, 480, 0)
|
10
|
+
renderer = window.create_renderer(-1, 0)
|
11
|
+
|
12
|
+
font = SDL2::TTF.open("font.ttf", 40)
|
13
|
+
|
14
|
+
def draw_three_types(renderer, font, x, ybase)
|
15
|
+
renderer.copy(renderer.create_texture_from(font.render_solid("Foo", [255, 255, 255])),
|
16
|
+
nil, SDL2::Rect.new(x, ybase, 100, 30))
|
17
|
+
|
18
|
+
renderer.copy(renderer.create_texture_from(font.render_shaded("Foo", [255, 255, 255], [0,0,0])),
|
19
|
+
nil, SDL2::Rect.new(x, ybase+40, 100, 30))
|
20
|
+
|
21
|
+
renderer.copy(renderer.create_texture_from(font.render_blended("Foo", [255, 255, 255])),
|
22
|
+
nil, SDL2::Rect.new(x, ybase+80, 100, 30))
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
p font.style
|
27
|
+
p font.outline
|
28
|
+
p font.hinting
|
29
|
+
p font.kerning
|
30
|
+
p font.height
|
31
|
+
p font.ascent
|
32
|
+
p font.descent
|
33
|
+
p font.line_skip
|
34
|
+
p font.num_faces
|
35
|
+
p font.face_is_fixed_width?
|
36
|
+
p font.face_family_name
|
37
|
+
p font.face_style_name
|
38
|
+
p font.size_text("Foo")
|
39
|
+
|
40
|
+
renderer.draw_color = [255,0,0]
|
41
|
+
renderer.fill_rect(SDL2::Rect.new(0,0,640,480))
|
42
|
+
|
43
|
+
draw_three_types(renderer, font, 20, 50)
|
44
|
+
|
45
|
+
font.outline = 1
|
46
|
+
draw_three_types(renderer, font, 150, 50)
|
47
|
+
|
48
|
+
font.outline = 0
|
49
|
+
font.style = SDL2::TTF::STYLE_BOLD
|
50
|
+
draw_three_types(renderer, font, 280, 50)
|
51
|
+
|
52
|
+
font.style = 0
|
53
|
+
font.hinting = SDL2::TTF::HINTING_MONO
|
54
|
+
draw_three_types(renderer, font, 410, 50)
|
55
|
+
|
56
|
+
font.style = 0
|
57
|
+
font.hinting = SDL2::TTF::HINTING_NORMAL
|
58
|
+
font.kerning = false
|
59
|
+
draw_three_types(renderer, font, 540, 50)
|
60
|
+
|
61
|
+
|
62
|
+
loop do
|
63
|
+
while ev = SDL2::Event.poll
|
64
|
+
case ev
|
65
|
+
when SDL2::Event::KeyDown
|
66
|
+
if ev.scancode == SDL2::Key::Scan::ESCAPE
|
67
|
+
exit
|
68
|
+
end
|
69
|
+
when SDL2::Event::Quit
|
70
|
+
exit
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
renderer.present
|
75
|
+
#GC.start
|
76
|
+
sleep 0.1
|
77
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "sdl2"
|
2
|
+
|
3
|
+
SDL2.init(SDL2::INIT_EVERYTHING)
|
4
|
+
|
5
|
+
window = SDL2::Window.create("testsprite",
|
6
|
+
SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED,
|
7
|
+
640, 480, 0)
|
8
|
+
|
9
|
+
renderer = window.create_renderer(-1,
|
10
|
+
SDL2::Renderer::ACCELERATED|SDL2::Renderer::TARGETTEXTURE)
|
11
|
+
texture = renderer.load_texture("icon.bmp")
|
12
|
+
|
13
|
+
rect = SDL2::Rect.new(48, 128, 32, 32)
|
14
|
+
renderer.copy(texture, nil, rect)
|
15
|
+
renderer.copy_ex(texture, nil, SDL2::Rect[128, 256, 48, 48], 45, nil,
|
16
|
+
SDL2::Renderer::FLIP_NONE)
|
17
|
+
texture.blend_mode = SDL2::BLENDMODE_ADD
|
18
|
+
renderer.copy(texture, nil, SDL2::Rect[128, 294, 48, 48])
|
19
|
+
|
20
|
+
texture.blend_mode = SDL2::BLENDMODE_NONE
|
21
|
+
texture.color_mod = [128, 128, 255]
|
22
|
+
renderer.copy(texture, nil, SDL2::Rect[300, 420, 48, 48])
|
23
|
+
|
24
|
+
texture.blend_mode = SDL2::BLENDMODE_NONE
|
25
|
+
texture.color_mod = [255, 255, 255]
|
26
|
+
|
27
|
+
if renderer.support_render_target?
|
28
|
+
empty_texture = renderer.create_texture(renderer.info.texture_formats.first,
|
29
|
+
SDL2::Texture::ACCESS_TARGET, 128, 128)
|
30
|
+
renderer.render_target = empty_texture
|
31
|
+
renderer.draw_color = [255, 0, 255]
|
32
|
+
renderer.draw_line(0, 0, 128, 128)
|
33
|
+
renderer.reset_render_target
|
34
|
+
renderer.copy(empty_texture, SDL2::Rect[0, 0, 128, 128], SDL2::Rect[420, 20, 128, 128])
|
35
|
+
end
|
36
|
+
|
37
|
+
loop do
|
38
|
+
while ev = SDL2::Event.poll
|
39
|
+
p ev
|
40
|
+
case ev
|
41
|
+
when SDL2::Event::KeyDown
|
42
|
+
if ev.scancode == SDL2::Key::Scan::ESCAPE
|
43
|
+
exit
|
44
|
+
else
|
45
|
+
p ev.scancode
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
renderer.present
|
51
|
+
sleep 0.1
|
52
|
+
end
|
53
|
+
|
data/sample/testgl.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'sdl2'
|
2
|
+
require 'gl'
|
3
|
+
|
4
|
+
include Gl
|
5
|
+
|
6
|
+
WINDOW_W = 640
|
7
|
+
WINDOW_H = 480
|
8
|
+
|
9
|
+
shadedCube = true
|
10
|
+
|
11
|
+
SDL2.init(SDL2::INIT_EVERYTHING)
|
12
|
+
SDL2::GL.set_attribute(SDL2::GL::RED_SIZE, 8)
|
13
|
+
SDL2::GL.set_attribute(SDL2::GL::GREEN_SIZE, 8)
|
14
|
+
SDL2::GL.set_attribute(SDL2::GL::BLUE_SIZE, 8)
|
15
|
+
SDL2::GL.set_attribute(SDL2::GL::ALPHA_SIZE, 8)
|
16
|
+
SDL2::GL.set_attribute(SDL2::GL::DOUBLEBUFFER, 1)
|
17
|
+
|
18
|
+
window = SDL2::Window.create("testgl", 0, 0, WINDOW_W, WINDOW_H, SDL2::Window::OPENGL)
|
19
|
+
context = SDL2::GL::Context.create(window)
|
20
|
+
|
21
|
+
printf("OpenGL version %d.%d\n",
|
22
|
+
SDL2::GL.get_attribute(SDL2::GL::CONTEXT_MAJOR_VERSION),
|
23
|
+
SDL2::GL.get_attribute(SDL2::GL::CONTEXT_MINOR_VERSION))
|
24
|
+
|
25
|
+
glViewport( 0, 0, 640, 400 )
|
26
|
+
glMatrixMode( GL_PROJECTION )
|
27
|
+
glLoadIdentity( )
|
28
|
+
|
29
|
+
glMatrixMode( GL_MODELVIEW )
|
30
|
+
glLoadIdentity( )
|
31
|
+
|
32
|
+
glEnable(GL_DEPTH_TEST)
|
33
|
+
|
34
|
+
glDepthFunc(GL_LESS)
|
35
|
+
|
36
|
+
glShadeModel(GL_SMOOTH)
|
37
|
+
|
38
|
+
color =
|
39
|
+
[[ 1.0, 1.0, 0.0],
|
40
|
+
[ 1.0, 0.0, 0.0],
|
41
|
+
[ 0.0, 0.0, 0.0],
|
42
|
+
[ 0.0, 1.0, 0.0],
|
43
|
+
[ 0.0, 1.0, 1.0],
|
44
|
+
[ 1.0, 1.0, 1.0],
|
45
|
+
[ 1.0, 0.0, 1.0],
|
46
|
+
[ 0.0, 0.0, 1.0]]
|
47
|
+
|
48
|
+
cube =
|
49
|
+
[[ 0.5, 0.5, -0.5],
|
50
|
+
[ 0.5, -0.5, -0.5],
|
51
|
+
[-0.5, -0.5, -0.5],
|
52
|
+
[-0.5, 0.5, -0.5],
|
53
|
+
[-0.5, 0.5, 0.5],
|
54
|
+
[ 0.5, 0.5, 0.5],
|
55
|
+
[ 0.5, -0.5, 0.5],
|
56
|
+
[-0.5, -0.5, 0.5]]
|
57
|
+
|
58
|
+
|
59
|
+
loop do
|
60
|
+
|
61
|
+
while event = SDL2::Event.poll
|
62
|
+
case event
|
63
|
+
when SDL2::Event::Quit, SDL2::Event::KeyDown
|
64
|
+
exit
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
glClearColor(0.0, 0.0, 0.0, 1.0);
|
69
|
+
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
70
|
+
|
71
|
+
|
72
|
+
glBegin(GL_QUADS)
|
73
|
+
|
74
|
+
if shadedCube then
|
75
|
+
glColor(color[0])
|
76
|
+
glVertex(cube[0])
|
77
|
+
glColor(color[1])
|
78
|
+
glVertex(cube[1])
|
79
|
+
glColor(color[2])
|
80
|
+
glVertex(cube[2])
|
81
|
+
glColor(color[3])
|
82
|
+
glVertex(cube[3])
|
83
|
+
|
84
|
+
glColor(color[3])
|
85
|
+
glVertex(cube[3])
|
86
|
+
glColor(color[4])
|
87
|
+
glVertex(cube[4])
|
88
|
+
glColor(color[7])
|
89
|
+
glVertex(cube[7])
|
90
|
+
glColor(color[2])
|
91
|
+
glVertex(cube[2])
|
92
|
+
|
93
|
+
glColor(color[0])
|
94
|
+
glVertex(cube[0])
|
95
|
+
glColor(color[5])
|
96
|
+
glVertex(cube[5])
|
97
|
+
glColor(color[6])
|
98
|
+
glVertex(cube[6])
|
99
|
+
glColor(color[1])
|
100
|
+
glVertex(cube[1])
|
101
|
+
|
102
|
+
glColor(color[5])
|
103
|
+
glVertex(cube[5])
|
104
|
+
glColor(color[4])
|
105
|
+
glVertex(cube[4])
|
106
|
+
glColor(color[7])
|
107
|
+
glVertex(cube[7])
|
108
|
+
glColor(color[6])
|
109
|
+
glVertex(cube[6])
|
110
|
+
|
111
|
+
glColor(color[5])
|
112
|
+
glVertex(cube[5])
|
113
|
+
glColor(color[0])
|
114
|
+
glVertex(cube[0])
|
115
|
+
glColor(color[3])
|
116
|
+
glVertex(cube[3])
|
117
|
+
glColor(color[4])
|
118
|
+
glVertex(cube[4])
|
119
|
+
|
120
|
+
glColor(color[6])
|
121
|
+
glVertex(cube[6])
|
122
|
+
glColor(color[1])
|
123
|
+
glVertex(cube[1])
|
124
|
+
glColor(color[2])
|
125
|
+
glVertex(cube[2])
|
126
|
+
glColor(color[7])
|
127
|
+
glVertex(cube[7])
|
128
|
+
|
129
|
+
else
|
130
|
+
glColor(1.0, 0.0, 0.0)
|
131
|
+
glVertex(cube[0])
|
132
|
+
glVertex(cube[1])
|
133
|
+
glVertex(cube[2])
|
134
|
+
glVertex(cube[3])
|
135
|
+
|
136
|
+
glColor(0.0, 1.0, 0.0)
|
137
|
+
glVertex(cube[3])
|
138
|
+
glVertex(cube[4])
|
139
|
+
glVertex(cube[7])
|
140
|
+
glVertex(cube[2])
|
141
|
+
|
142
|
+
glColor(0.0, 0.0, 1.0)
|
143
|
+
glVertex(cube[0])
|
144
|
+
glVertex(cube[5])
|
145
|
+
glVertex(cube[6])
|
146
|
+
glVertex(cube[1])
|
147
|
+
|
148
|
+
glColor(0.0, 1.0, 1.0)
|
149
|
+
glVertex(cube[5])
|
150
|
+
glVertex(cube[4])
|
151
|
+
glVertex(cube[7])
|
152
|
+
glVertex(cube[6])
|
153
|
+
|
154
|
+
glColor(1.0, 1.0, 0.0)
|
155
|
+
glVertex(cube[5])
|
156
|
+
glVertex(cube[0])
|
157
|
+
glVertex(cube[3])
|
158
|
+
glVertex(cube[4])
|
159
|
+
|
160
|
+
glColor(1.0, 0.0, 1.0)
|
161
|
+
glVertex(cube[6])
|
162
|
+
glVertex(cube[1])
|
163
|
+
glVertex(cube[2])
|
164
|
+
glVertex(cube[7])
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
glEnd()
|
169
|
+
|
170
|
+
glMatrixMode(GL_MODELVIEW)
|
171
|
+
glRotate(5.0, 1.0, 1.0, 1.0)
|
172
|
+
|
173
|
+
window.gl_swap
|
174
|
+
|
175
|
+
end
|
@@ -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::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)
|