rubydraw 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rubydraw.rb +9 -2
- data/lib/rubydraw/color.rb +42 -16
- data/lib/rubydraw/events.rb +10 -0
- data/lib/rubydraw/image.rb +5 -3
- data/lib/rubydraw/rectangle.rb +11 -1
- data/lib/rubydraw/text.rb +23 -0
- data/lib/rubydraw/window.rb +12 -17
- metadata +5 -4
data/lib/rubydraw.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
# The only dependency
|
2
2
|
require 'ruby-sdl-ffi'
|
3
3
|
|
4
|
+
# The extention must be loaded first.
|
5
|
+
require 'ext/string'
|
6
|
+
|
4
7
|
# Require all the rubydraw files
|
5
8
|
files = %w[
|
6
9
|
window
|
7
10
|
image
|
8
11
|
sound
|
12
|
+
text
|
9
13
|
events
|
10
14
|
event_queue
|
11
15
|
keys
|
@@ -14,7 +18,6 @@ files = %w[
|
|
14
18
|
sdl_error
|
15
19
|
rectangle]
|
16
20
|
files.each {|f| require("rubydraw/" + f)}
|
17
|
-
require 'ext/string'
|
18
21
|
|
19
22
|
# Rubydraw is a high level game/graphics library, like Gosu or Rubygame, and is written completely
|
20
23
|
# in Ruby. Its only dependency is ruby-sdl-ffi, which it uses to access SDL functions. Also, thanks,
|
@@ -30,7 +33,11 @@ module Rubydraw
|
|
30
33
|
# Initialize SDL.
|
31
34
|
def self.initialize_sdl
|
32
35
|
if SDL::Init(SDL::INIT_EVERYTHING) != 0
|
33
|
-
raise
|
36
|
+
raise SDLError "Failed to initialize SDL: #{SDL.GetError}"
|
37
|
+
end
|
38
|
+
# Initialize fonts.
|
39
|
+
if (SDL::TTF.WasInit == 0 and not SDL::TTF.Init == 0)
|
40
|
+
raise SDLError "Failed to initialize SDL TTF: #{SDL.GetError}"
|
34
41
|
end
|
35
42
|
end
|
36
43
|
end
|
data/lib/rubydraw/color.rb
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
module Rubydraw
|
2
2
|
# Instances of Color are created with four arguments: Red, green,
|
3
|
-
# blue, and alpha
|
4
|
-
# values ranging from 0 to 255.
|
5
|
-
#
|
6
|
-
# but only Rubydraw itself
|
3
|
+
# blue, and alpha (see Rubydraw::Color#initialize) The last is 0
|
4
|
+
# by default, and all should have values ranging from 0 to 255.
|
5
|
+
# Use this to specify colors instead of hex numbers. Each color
|
6
|
+
# instance can return its numerical value, but only Rubydraw itself
|
7
|
+
# should need it.
|
7
8
|
class Color
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# Shorthand new method.
|
10
|
+
def self.[](red, green, blue, alpha = 0)
|
11
|
+
self.new(red, green, blue, alpha)
|
12
|
+
end
|
13
|
+
|
13
14
|
attr_reader(:red, :green, :blue, :alpha)
|
14
15
|
|
15
16
|
# Create a new color with the given red, green, blue, and alpha
|
16
17
|
# values. Alpha is 0 by default.
|
17
18
|
#
|
18
|
-
# TODO: Add other color specs, like HSV or maybe
|
19
|
-
def initialize(red, green, blue, alpha =
|
19
|
+
# TODO: Add other color specs, like HSV or maybe CYMK
|
20
|
+
def initialize(red, green, blue, alpha = 255)
|
20
21
|
@red, @green, @blue, @alpha = red, green, blue, alpha
|
22
|
+
calc_num_val
|
21
23
|
end
|
22
24
|
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
def to_i
|
25
|
+
# Calculate and store the numerical value in @num_val. You shouldn't
|
26
|
+
# need this (see Rubydraw::Color.new).
|
27
|
+
def calc_num_val
|
27
28
|
# Get the hex string for each value.
|
28
29
|
hex_alpha = (@alpha.to_s(16)).color_string
|
29
30
|
hex_red = (@red.to_s(16)).color_string
|
@@ -33,9 +34,34 @@ module Rubydraw
|
|
33
34
|
# *Note:* it appears that SDL's (or maybe ruby-sdl-ffi's) color
|
34
35
|
# is backwards. The order appears to be: +BBGGRRAA+
|
35
36
|
color_str = hex_blue + hex_green + hex_red + hex_alpha
|
36
|
-
color_str.to_i(16)
|
37
|
+
@num_val = color_str.to_i(16)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Convert this color to a numerical value, which only makes sense when
|
41
|
+
# read as a hex number, e.g. red would be: +0000ff00+.
|
42
|
+
#
|
43
|
+
# Also see the comments in: Rubydraw::Color#calc_num_val.
|
44
|
+
def to_i
|
45
|
+
@num_val
|
37
46
|
end
|
38
47
|
|
48
|
+
# Returns an Array containing each +rgba+ value.
|
49
|
+
#
|
50
|
+
# Example:
|
51
|
+
# color = Rubydraw::Color.new(red = 200, green = 60, blue = 5, alpha = 255)
|
52
|
+
# => #<Rubydraw::Color:0x10039cf50 @green=60, @red=200, @alpha=255, @num_val=87869695, @blue=5>
|
53
|
+
# color.to_ary
|
54
|
+
# => [200, 60, 5, 255]
|
55
|
+
def to_a
|
56
|
+
[@red, @blue, @green, @alpha]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Create an SDL::Color equivilent to this Rubydraw::Color.
|
60
|
+
def to_sdl
|
61
|
+
SDL::Color.new(to_a)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
39
65
|
White = new(255, 255, 255)
|
40
66
|
Black = new(0, 0, 0)
|
41
67
|
# The primary colors
|
data/lib/rubydraw/events.rb
CHANGED
@@ -191,6 +191,16 @@ module Rubydraw
|
|
191
191
|
def initialize(position, relative_position)
|
192
192
|
@position, @relative_position = position, relative_position
|
193
193
|
end
|
194
|
+
|
195
|
+
# Returns the new x positon of the mouse.
|
196
|
+
def x
|
197
|
+
@position.x
|
198
|
+
end
|
199
|
+
|
200
|
+
# Returns the new y mouse positon.
|
201
|
+
def y
|
202
|
+
@position.y
|
203
|
+
end
|
194
204
|
end
|
195
205
|
|
196
206
|
# Created either when the window gains or loses focus. This is the parent class for
|
data/lib/rubydraw/image.rb
CHANGED
@@ -25,9 +25,11 @@ module Rubydraw
|
|
25
25
|
# Notice that you don't blit surfaces to other surfaces when using
|
26
26
|
# Rubygame, but instead you draw things.
|
27
27
|
def draw(window, position)
|
28
|
-
source_rect =
|
29
|
-
blit_rect =
|
30
|
-
|
28
|
+
#source_rect = Rectangle[Point[0, 0], width, height]
|
29
|
+
#blit_rect = Rectangle[position, window.width, window.height]
|
30
|
+
source_rect = Rectangle[width, height, Point[0, 0]]
|
31
|
+
blit_rect = Rectangle[window.width, window.height, position]
|
32
|
+
SDL::BlitSurface(@sdl_image, source_rect.to_sdl, window.sdl_surface, blit_rect.to_sdl)
|
31
33
|
self
|
32
34
|
end
|
33
35
|
|
data/lib/rubydraw/rectangle.rb
CHANGED
@@ -5,6 +5,11 @@ module Rubydraw
|
|
5
5
|
class Rectangle
|
6
6
|
attr_reader(:position, :width, :height)
|
7
7
|
|
8
|
+
# Shorthand new method
|
9
|
+
def self.[](width, height, position)
|
10
|
+
self.new(width, height, position)
|
11
|
+
end
|
12
|
+
|
8
13
|
# Create a new rectangle with the given dimensions and position.
|
9
14
|
def initialize(width, height, position)
|
10
15
|
@width, @height, @position = width, height, position
|
@@ -28,9 +33,14 @@ module Rubydraw
|
|
28
33
|
Point[x + @width, y + @height]
|
29
34
|
end
|
30
35
|
|
31
|
-
# Returns if the given point is inside this rectangle. See Rubydraw::Point
|
36
|
+
# Returns if the given point is inside this rectangle. See Rubydraw::Point#inside?
|
32
37
|
def contains?(point)
|
33
38
|
point.inside?(top_left, bottom_right)
|
34
39
|
end
|
40
|
+
|
41
|
+
# Returns an SDL::Rectangle equal to this one.
|
42
|
+
def to_sdl
|
43
|
+
SDL::Rect.new([x, y, width, height])
|
44
|
+
end
|
35
45
|
end
|
36
46
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Rubydraw
|
2
|
+
# Text objects are instantiazed with the font (can be a TTF file) and
|
3
|
+
# with the text to display. They can be drawn at a position on the
|
4
|
+
# screen.
|
5
|
+
class Text
|
6
|
+
attr_accessor(:font, :contents, :size, :color)
|
7
|
+
# Create a new drawable Text object with the given font and contents.
|
8
|
+
def initialize(font, contents, color = Rubydraw::Color::White, size = 25)
|
9
|
+
@font, @contents, @size, @color = font, contents, size, color
|
10
|
+
@drawable = SDL::TTF.OpenFont(font, size)
|
11
|
+
raise(SDLError, "Failed to initialize font: #{SDL.GetError}") if @drawable.pointer.null?
|
12
|
+
end
|
13
|
+
|
14
|
+
# Draw the font in the given window at a position.
|
15
|
+
def draw(window, position)
|
16
|
+
sdl_color = @color.to_sdl
|
17
|
+
sdl_surface = SDL::TTF.RenderText_Blended(@drawable, @contents, sdl_color)
|
18
|
+
source_rect = Rectangle[sdl_surface.w, sdl_surface.h, Point[0, 0]]
|
19
|
+
blit_rect = Rectangle[window.width, window.height, position]
|
20
|
+
SDL::BlitSurface(sdl_surface, source_rect.to_sdl, window.sdl_surface, blit_rect.to_sdl)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/rubydraw/window.rb
CHANGED
@@ -5,6 +5,8 @@ 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(:width, :height)
|
9
|
+
|
8
10
|
# Create a new window.
|
9
11
|
def initialize(width, height, bkg_color=Color::Black)
|
10
12
|
unless height.is_a?(Numeric) and width.is_a?(Numeric)
|
@@ -100,28 +102,21 @@ module Rubydraw
|
|
100
102
|
def tick
|
101
103
|
end
|
102
104
|
|
103
|
-
#
|
104
|
-
|
105
|
-
@width
|
106
|
-
end
|
107
|
-
|
108
|
-
# Return the height of this window.
|
109
|
-
def height
|
110
|
-
@height
|
111
|
-
end
|
112
|
-
|
113
|
-
# Send +selector+ to +reciever+ when +event+ happens, and include the specific event
|
114
|
-
# instace as a parameter if send_params = true.
|
115
|
-
|
116
|
-
# Execute the given block on the appearance of an instance of +event+.
|
105
|
+
# Execute the given block on the appearance of an instance of +event+ and pass that
|
106
|
+
# instance to the block.
|
117
107
|
#
|
118
108
|
# Example:
|
119
109
|
# class MyWindow < Rubydraw::Window
|
120
110
|
# def initialize
|
121
111
|
# super(300, 300)
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
112
|
+
# whenever(Rubydraw::Events::QuitRequest) do
|
113
|
+
# puts "Goodbye!"
|
114
|
+
# close
|
115
|
+
# end
|
116
|
+
# whenever(Rubydraw::Events::MouseMove) do |event|
|
117
|
+
# new_pos = event.position
|
118
|
+
# puts "Mouse moved to #{new_pos.x}, #{new_pos.y}.}"
|
119
|
+
# end
|
125
120
|
# end
|
126
121
|
# end
|
127
122
|
def whenever(event, &block)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubydraw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 9
|
10
|
+
version: 0.1.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- J. Wostenberg
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-11-
|
18
|
+
date: 2011-11-19 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/rubydraw/window.rb
|
54
54
|
- lib/rubydraw/image.rb
|
55
55
|
- lib/rubydraw/sound.rb
|
56
|
+
- lib/rubydraw/text.rb
|
56
57
|
- lib/rubydraw/sdl_error.rb
|
57
58
|
- lib/rubydraw/keys.rb
|
58
59
|
- lib/rubydraw/event_queue.rb
|