rubydraw 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/ball_catch_game.rb +1 -1
- data/examples/image_ex.rb +1 -1
- data/examples/window_ex.rb +3 -3
- data/lib/rubydraw.rb +1 -0
- data/lib/rubydraw/color.rb +5 -5
- data/lib/rubydraw/events.rb +49 -9
- data/lib/rubydraw/flags.rb +20 -0
- data/lib/rubydraw/image.rb +1 -1
- data/lib/rubydraw/point.rb +1 -1
- data/lib/rubydraw/window.rb +17 -26
- metadata +3 -2
data/examples/ball_catch_game.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rubydraw'
|
|
3
3
|
|
4
4
|
class GameWindow < Rubydraw::Window
|
5
5
|
def initialize
|
6
|
-
super(500, 500,
|
6
|
+
super(Point[500, 500], [], Rubydraw::Color::White)
|
7
7
|
@score = 0
|
8
8
|
@score_text = Rubydraw::Text.new("", Rubydraw::Color::Black)
|
9
9
|
@game_objects = []
|
data/examples/image_ex.rb
CHANGED
@@ -4,7 +4,7 @@ require 'rubydraw'
|
|
4
4
|
class MyWindow < Rubydraw::Window
|
5
5
|
# Create a new window with an bug image inside it
|
6
6
|
def initialize
|
7
|
-
super(300, 300)
|
7
|
+
super(Point[300, 300])
|
8
8
|
@image = Rubydraw::Image.new("media/bug.png")
|
9
9
|
whenever Rubydraw::Events::QuitRequest do
|
10
10
|
close
|
data/examples/window_ex.rb
CHANGED
@@ -2,8 +2,8 @@ require 'rubygems'
|
|
2
2
|
require 'rubydraw'
|
3
3
|
|
4
4
|
class MyWindow < Rubydraw::Window
|
5
|
-
def initialize(
|
6
|
-
super(
|
5
|
+
def initialize(dimensions)
|
6
|
+
super(dimensions)
|
7
7
|
puts "New window created with width: #{@width} and height #{@height}"
|
8
8
|
whenever Rubydraw::Events::QuitRequest do
|
9
9
|
close
|
@@ -16,4 +16,4 @@ w = gets.to_i
|
|
16
16
|
print "Window width: "
|
17
17
|
h = gets.to_i
|
18
18
|
|
19
|
-
window = MyWindow.new(w, h).show
|
19
|
+
window = MyWindow.new(Point[w, h]).show
|
data/lib/rubydraw.rb
CHANGED
data/lib/rubydraw/color.rb
CHANGED
@@ -57,9 +57,9 @@ module Rubydraw
|
|
57
57
|
end
|
58
58
|
|
59
59
|
# Convert this color to a numerical value, which only makes sense when
|
60
|
-
# read as a hex number
|
60
|
+
# read as a hex number.
|
61
61
|
#
|
62
|
-
# Also see the comments in: Rubydraw::Color#
|
62
|
+
# Also see the comments in: Rubydraw::Color#calc_num_vals.
|
63
63
|
def to_i(format)
|
64
64
|
if format == :surface or format == :display_fullscreen
|
65
65
|
return @surface_num_val
|
@@ -83,10 +83,10 @@ module Rubydraw
|
|
83
83
|
#
|
84
84
|
# Example:
|
85
85
|
# color = Rubydraw::Color.new(red = 200, green = 60, blue = 5, alpha = 255)
|
86
|
-
# => #<Rubydraw::Color:
|
86
|
+
# => #<Rubydraw::Color: (@red: 200, @green: 60, @blue: 5, @alpha: 255)>
|
87
87
|
# color.to_ary
|
88
88
|
# => [200, 60, 5, 255]
|
89
|
-
def
|
89
|
+
def to_ary
|
90
90
|
[@red, @blue, @green, @alpha]
|
91
91
|
end
|
92
92
|
|
@@ -96,7 +96,7 @@ module Rubydraw
|
|
96
96
|
|
97
97
|
# Create an SDL::Color equivilent to this Rubydraw::Color.
|
98
98
|
def to_sdl
|
99
|
-
SDL::Color.new(
|
99
|
+
SDL::Color.new(to_ary)
|
100
100
|
end
|
101
101
|
|
102
102
|
# Return a new color resulting from mixing this color and +other+ additively.
|
data/lib/rubydraw/events.rb
CHANGED
@@ -21,7 +21,7 @@ module Rubydraw
|
|
21
21
|
# each event class if it matches the SDL event. No case statements here.
|
22
22
|
def self.match(sdl_event)
|
23
23
|
event_classes = Event.all_subclasses.compact
|
24
|
-
rubydraw_event = UnknownEvent.new
|
24
|
+
rubydraw_event = UnknownEvent.new(sdl_event)
|
25
25
|
# Remove all the classes that don't want to be included in the search
|
26
26
|
event_classes.delete_if {|event_class| not event_class.wants_to_match?}
|
27
27
|
event_classes.each {|event_class| rubydraw_event = event_class.from_sdl_event(sdl_event) if event_class.matches?(sdl_event)}
|
@@ -64,6 +64,18 @@ module Rubydraw
|
|
64
64
|
# A special event class that is called when no Rubydraw event is matched to an
|
65
65
|
# SDL event.
|
66
66
|
class UnknownEvent < Event
|
67
|
+
# The SDL event should be passed so that the programmer using this library can still
|
68
|
+
# attempt to implement behavior, but it will have to match and SDL event, not a
|
69
|
+
# Rubydraw::Events::Event.
|
70
|
+
def from_sdl_event(sdl_event)
|
71
|
+
self.new(sdl_event)
|
72
|
+
end
|
73
|
+
|
74
|
+
attr_reader(:sdl_event)
|
75
|
+
|
76
|
+
def initialize(sdl_event)
|
77
|
+
@sdl_event = sdl_event
|
78
|
+
end
|
67
79
|
end
|
68
80
|
|
69
81
|
# Provides methods used in both Rubydraw::Events::KeyPressed and
|
@@ -211,7 +223,8 @@ module Rubydraw
|
|
211
223
|
end
|
212
224
|
end
|
213
225
|
|
214
|
-
# Created when the window gains focus.
|
226
|
+
# Created when the window gains focus, e.g. clicking on the window after previously using
|
227
|
+
# another application.
|
215
228
|
class FocusGain < FocusEvent
|
216
229
|
def self.wants_to_match?
|
217
230
|
true
|
@@ -220,11 +233,10 @@ module Rubydraw
|
|
220
233
|
# Redefine Event#matches? because both this class and Rubydraw::Events::FocusLoss use
|
221
234
|
# SDL::ActiveEvent.
|
222
235
|
def self.matches?(sdl_event)
|
223
|
-
result = false
|
224
236
|
if super(sdl_event)
|
225
|
-
|
237
|
+
return sdl_event.gain == 1
|
226
238
|
end
|
227
|
-
|
239
|
+
return false
|
228
240
|
end
|
229
241
|
end
|
230
242
|
|
@@ -237,20 +249,48 @@ module Rubydraw
|
|
237
249
|
# Redefine Event#matches? because both this class and Rubydraw::Events::FocuGain use
|
238
250
|
# SDL::ActiveEvent
|
239
251
|
def self.matches?(sdl_event)
|
240
|
-
result = false
|
241
252
|
if super(sdl_event)
|
242
|
-
|
253
|
+
return sdl_event.gain == 0
|
243
254
|
end
|
244
|
-
|
255
|
+
return false
|
245
256
|
end
|
246
257
|
end
|
247
258
|
|
248
259
|
# Created when the user attempts to close the window.
|
249
260
|
class QuitRequest < Event
|
250
|
-
|
251
261
|
def self.matching_sdl_type
|
252
262
|
SDL::QUIT
|
253
263
|
end
|
254
264
|
end
|
265
|
+
|
266
|
+
# Created when the user resizes the window. This can only happen if Rubydraw::Flags::Resizable
|
267
|
+
# is passed when the window is created.
|
268
|
+
class WindowResize < Event
|
269
|
+
def self.matching_sdl_type
|
270
|
+
SDL::VIDEORESIZE
|
271
|
+
end
|
272
|
+
|
273
|
+
def self.from_sdl_event(sdl_event)
|
274
|
+
self.new(Point[sdl_event.w, sdl_event.h])
|
275
|
+
end
|
276
|
+
|
277
|
+
attr_accessor(:dimensions)
|
278
|
+
|
279
|
+
def initialize(dimensions)
|
280
|
+
@dimensions = dimensions
|
281
|
+
end
|
282
|
+
|
283
|
+
def width
|
284
|
+
@dimensions.x
|
285
|
+
end
|
286
|
+
|
287
|
+
def height
|
288
|
+
@dimensions.y
|
289
|
+
end
|
290
|
+
|
291
|
+
alias :x :width
|
292
|
+
|
293
|
+
alias :y :height
|
294
|
+
end
|
255
295
|
end
|
256
296
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rubydraw
|
2
|
+
# Contains constants for new window flags, such as Fullscreen and Resizable.
|
3
|
+
module Flags
|
4
|
+
# Collapse +flags+ using a bitwise or (thanks to Rubygame for most of this
|
5
|
+
# chunk of code)
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# Rubydraw::Flags.collapse(0b10, 0b1000, 0b100000)
|
9
|
+
# => 42 #(0b101010)
|
10
|
+
def self.collapse(*flags)
|
11
|
+
first_mem = flags[0]
|
12
|
+
flags = first_mem if first_mem.is_a?(Array)
|
13
|
+
flags.inject(0) {|total, num|
|
14
|
+
num | total}
|
15
|
+
end
|
16
|
+
|
17
|
+
Fullscreen = SDL::FULLSCREEN
|
18
|
+
Resizable = SDL::RESIZABLE
|
19
|
+
end
|
20
|
+
end
|
data/lib/rubydraw/image.rb
CHANGED
@@ -32,7 +32,7 @@ module Rubydraw
|
|
32
32
|
if @sdl_surface.pointer.null?
|
33
33
|
# SDL couln't load the image; usually happens because it doesn't
|
34
34
|
# exist.
|
35
|
-
raise
|
35
|
+
raise SDLError, "Failed to load image: #{SDL.GetError}"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
data/lib/rubydraw/point.rb
CHANGED
data/lib/rubydraw/window.rb
CHANGED
@@ -5,34 +5,29 @@ module Rubydraw
|
|
5
5
|
# (which starts when Rubydraw::Window#open is called) is *not* forked! It will break
|
6
6
|
# when Rubydraw::Window#close is called.
|
7
7
|
class Window
|
8
|
-
attr_reader(:fullscreen)
|
9
8
|
attr_accessor(:bkg_color)
|
10
9
|
|
11
10
|
# Create a new window.
|
12
|
-
def initialize(
|
11
|
+
def initialize(dimensions, flags=[], bkg_color=Color::Black)
|
12
|
+
width, height = dimensions.to_ary
|
13
|
+
@fullscreen = flags.include?(Rubydraw::Flags::Fullscreen)
|
13
14
|
if width < 0
|
14
15
|
raise SDLError, "New window width cannot be less than zero"
|
15
16
|
end
|
16
17
|
if height < 0
|
17
18
|
raise SDLError, "New window height cannot be less than zero"
|
18
19
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
unless flags.include?(Flags::Fullscreen)
|
21
|
+
if height == 0
|
22
|
+
# Compensate for the height of the window bar itself, the menu bar at the top
|
23
|
+
# of the screen, and a space of around 4px at the bottom of the screen. Not
|
24
|
+
# sure what the numbers would be for anything other than Mac OSX; more info
|
25
|
+
# would be appreciated.
|
26
|
+
height = Rubydraw::screen_height - 48
|
27
|
+
end
|
25
28
|
end
|
26
|
-
@width, @height = width, height
|
27
|
-
@fullscreen = fullscreen
|
28
|
-
@bkg_color = bkg_color
|
29
|
+
@width, @height, @flags, @bkg_color = width, height, Flags.collapse(flags), bkg_color
|
29
30
|
@open = false
|
30
|
-
@flags =
|
31
|
-
if fullscreen
|
32
|
-
SDL::FULLSCREEN
|
33
|
-
else
|
34
|
-
0
|
35
|
-
end
|
36
31
|
|
37
32
|
@event_queue = EventQueue.new
|
38
33
|
|
@@ -133,10 +128,6 @@ module Rubydraw
|
|
133
128
|
# Rubydraw::Window#register_action on how to use it.
|
134
129
|
def handle_events
|
135
130
|
events = @event_queue.get_events
|
136
|
-
|
137
|
-
#events.each {|event|
|
138
|
-
# block = @registered_actions[event.class]
|
139
|
-
# block.call(event) unless block.nil?}
|
140
131
|
events.each { |event|
|
141
132
|
blocks = @registered_actions[event.class]
|
142
133
|
unless blocks.nil?
|
@@ -144,12 +135,13 @@ module Rubydraw
|
|
144
135
|
end }
|
145
136
|
end
|
146
137
|
|
147
|
-
#
|
148
|
-
# window, not this.
|
138
|
+
# Causes the main loop to exit as soon as it get the chance to.
|
149
139
|
def break_main_loop
|
150
140
|
@open = false
|
151
141
|
end
|
152
142
|
|
143
|
+
private :break_main_loop
|
144
|
+
|
153
145
|
# Returns if this window is open.
|
154
146
|
def open?
|
155
147
|
@open
|
@@ -196,7 +188,7 @@ module Rubydraw
|
|
196
188
|
#
|
197
189
|
# area: The area which the new rectangle will cover. Should be an instance of Rubydraw::Rectangle.
|
198
190
|
# color: Specifies what color to use when drawing the rectangle. If +fill+ is enabled, it will fill the new rect with this color. If not, it will only affect the border.
|
199
|
-
# fill: A boolean determining whether to fill it in or not.
|
191
|
+
# fill: A boolean determining whether to fill it in or not. (anti-aliasing is not needed because all the lines are perfectly straigt)
|
200
192
|
def draw_rectangle(area, color, fill=true)
|
201
193
|
tl = area.top_left
|
202
194
|
br = area.bottom_right
|
@@ -218,8 +210,7 @@ module Rubydraw
|
|
218
210
|
# center: The center of the ellipse, should be a Rubydraw::Point object.
|
219
211
|
# dimensions: Determines the width and the height of the ellipse to be drawn; should be a Rubydraw::Point.
|
220
212
|
# color: The color to use when drawing; should be an instance of Rubydraw::Color.
|
221
|
-
#
|
222
|
-
# anti_aliasing: When set to true, smoothing happens.
|
213
|
+
# mode: Can be one of three modes; +:fill+, +:outline+, or +:aa+. When set to +:fill+, it will draw a *solid* ellipse. When it is +:outline+, it renders without filling. When set to +:aa+ (anti-aliasing), it will draw without filling and smooth the border.
|
223
214
|
def draw_ellipse(center, dimensions, color, mode=:fill)
|
224
215
|
x, y = center.to_a
|
225
216
|
width, height = (dimensions / 2).to_a
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rubydraw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- J. Wostenberg
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-12-
|
13
|
+
date: 2011-12-02 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- README
|
37
37
|
- lib/rubydraw.rb
|
38
38
|
- lib/rubydraw/window.rb
|
39
|
+
- lib/rubydraw/flags.rb
|
39
40
|
- lib/rubydraw/surface.rb
|
40
41
|
- lib/rubydraw/image.rb
|
41
42
|
- lib/rubydraw/sound.rb
|