rubydraw 0.2.7.1 → 0.2.8
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.
- data/lib/rubydraw/color.rb +22 -2
- data/lib/rubydraw/image.rb +8 -56
- data/lib/rubydraw/surface.rb +40 -1
- data/lib/rubydraw/text.rb +1 -29
- data/lib/rubydraw/window.rb +2 -1
- metadata +1 -1
data/lib/rubydraw/color.rb
CHANGED
@@ -99,6 +99,21 @@ module Rubydraw
|
|
99
99
|
SDL::Color.new(to_a)
|
100
100
|
end
|
101
101
|
|
102
|
+
# Return a new color resulting from mixing this color and +other+ additively.
|
103
|
+
def +(other, a=255)
|
104
|
+
r = [@red + other.red, 255].min
|
105
|
+
g = [@green + other.green, 255].min
|
106
|
+
b = [@blue + other.blue, 255].min
|
107
|
+
Color.new(r, g, b, a)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Return a new color that is the average of this color and +other+.
|
111
|
+
def /(other, a=255)
|
112
|
+
r = [(@red + other.red) / 2, 255].min
|
113
|
+
g = [(@green + other.green) / 2, 255].min
|
114
|
+
b = [(@blue + other.blue) / 2, 255].min
|
115
|
+
Color.new(r, g, b, a)
|
116
|
+
end
|
102
117
|
|
103
118
|
White = new(255, 255, 255)
|
104
119
|
Black = new(0, 0, 0)
|
@@ -107,8 +122,13 @@ module Rubydraw
|
|
107
122
|
Green = new(0, 255, 0)
|
108
123
|
Blue = new(0, 0, 255)
|
109
124
|
# Secondary colors
|
110
|
-
Yellow = new(255,
|
111
|
-
Magenta = new(255,
|
125
|
+
Yellow = new(255, 255, 0)
|
126
|
+
Magenta = new(255, 0, 255)
|
112
127
|
Cyan = new(0, 255, 255)
|
128
|
+
# Other colors
|
129
|
+
Purple = new(128, 0, 255)
|
130
|
+
Orange = new(255, 128, 0)
|
131
|
+
Pink = new(255, 0, 128)
|
132
|
+
Grey = new(128, 128, 128)
|
113
133
|
end
|
114
134
|
end
|
data/lib/rubydraw/image.rb
CHANGED
@@ -4,9 +4,9 @@ module Rubydraw
|
|
4
4
|
# Then to actually draw the image (doesn't happen immediatley;
|
5
5
|
# read the documentation for Image#draw), call it's #draw method
|
6
6
|
# and pass it the +x+ and +y+ coordinates.
|
7
|
-
class Image
|
8
|
-
# Create a new image and load the file from a path. Or,
|
9
|
-
#
|
7
|
+
class Image < Surface
|
8
|
+
# Create a new image and load the file from a path. Or, wrap an
|
9
|
+
# SDL::Surface.
|
10
10
|
def initialize(arg)
|
11
11
|
if arg.is_a?(String)
|
12
12
|
# This must mean to load from a path.
|
@@ -22,14 +22,14 @@ module Rubydraw
|
|
22
22
|
def load_from_path(path) #:nodoc:
|
23
23
|
# Check if this image has already been initialized. If it has, raise
|
24
24
|
# an error.
|
25
|
-
unless @
|
25
|
+
unless @sdl_surface.nil?
|
26
26
|
raise SDLError, "Images may only be loaded once"
|
27
27
|
end
|
28
28
|
# In case program is being run from a different directory,
|
29
29
|
# provide the _full_ path. Nothing relative here.
|
30
30
|
full_path = File.expand_path path
|
31
|
-
@
|
32
|
-
if @
|
31
|
+
@sdl_surface = SDL::Image.Load(full_path)
|
32
|
+
if @sdl_surface.pointer.null?
|
33
33
|
# SDL couln't load the image; usually happens because it doesn't
|
34
34
|
# exist.
|
35
35
|
raise Rubydraw::SDLError "Failed to load image: #{SDL.GetError}"
|
@@ -39,60 +39,12 @@ module Rubydraw
|
|
39
39
|
def load_from_surface(surface) #:nodoc:
|
40
40
|
# Check if this image has already been initialized. If it has, raise
|
41
41
|
# an error.
|
42
|
-
unless @
|
42
|
+
unless @sdl_surface.nil?
|
43
43
|
raise SDLError, "Images may only be loaded once"
|
44
44
|
end
|
45
|
-
@
|
45
|
+
@sdl_surface = surface
|
46
46
|
end
|
47
47
|
|
48
48
|
private :load_from_path, :load_from_surface
|
49
|
-
|
50
|
-
# Blit (copy) into +surface at +position+ (see Rubydraw::Point).
|
51
|
-
# No graphical effects are applied.
|
52
|
-
def blit(surface, position)
|
53
|
-
source_rect = Rectangle[Point[0, 0], Point[width, height]]
|
54
|
-
blit_rect = Rectangle[position, Point[surface.width, surface.height]]
|
55
|
-
SDL::BlitSurface(@sdl_image, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
|
56
|
-
self
|
57
|
-
end
|
58
|
-
|
59
|
-
alias draw blit
|
60
|
-
|
61
|
-
# Rotates and/or expands the image. Note that this modifies the image
|
62
|
-
# itself.
|
63
|
-
def rotozoom!(angle, zoom, smooth=false)
|
64
|
-
smooth =
|
65
|
-
if smooth
|
66
|
-
1
|
67
|
-
else
|
68
|
-
0
|
69
|
-
end
|
70
|
-
|
71
|
-
@sdl_image = SDL::Gfx.rotozoomSurface(@sdl_image, angle, zoom, smooth)
|
72
|
-
raise SDLError, "Filed to perform rotozoom: #{SDL.GetError}" if @sdl_image.pointer.null?
|
73
|
-
return self
|
74
|
-
end
|
75
|
-
|
76
|
-
# Returns a rotated and/or expanded image, without modifying the
|
77
|
-
# reciever.
|
78
|
-
def rotozoom(angle, zoom, smooth=false)
|
79
|
-
new_image = self.class.new(@sdl_image)
|
80
|
-
new_image.rotozoom!(angle, zoom, smooth)
|
81
|
-
end
|
82
|
-
|
83
|
-
# Returns the image width
|
84
|
-
def width
|
85
|
-
@sdl_image.w
|
86
|
-
end
|
87
|
-
|
88
|
-
# Returns the image height
|
89
|
-
def height
|
90
|
-
@sdl_image.h
|
91
|
-
end
|
92
|
-
|
93
|
-
# Returns the sdl surface.
|
94
|
-
def to_sdl
|
95
|
-
@sdl_image
|
96
|
-
end
|
97
49
|
end
|
98
50
|
end
|
data/lib/rubydraw/surface.rb
CHANGED
@@ -1,8 +1,18 @@
|
|
1
1
|
module Rubydraw
|
2
2
|
# The basic class whose instances can blit themselves to other surfaces or the window.
|
3
3
|
# You can only manipulate the surface's pixels, but there are subclasses to do other
|
4
|
-
# things (like Rubydraw::Image or Rubydraw::
|
4
|
+
# things (like Rubydraw::Image or Rubydraw::Text)
|
5
5
|
class Surface
|
6
|
+
# Load an image from +path+.
|
7
|
+
def self.load_img(path)
|
8
|
+
Image.new(path)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Create a new Rubydraw::Font with +text+ as its contents.
|
12
|
+
def self.load_text(contents, color)
|
13
|
+
Text.new(contents, color)
|
14
|
+
end
|
15
|
+
|
6
16
|
# Create a new, blank surface with the given dimensions.
|
7
17
|
def initialize(dimensions)
|
8
18
|
@dimensions = dimensions
|
@@ -31,6 +41,8 @@ module Rubydraw
|
|
31
41
|
self
|
32
42
|
end
|
33
43
|
|
44
|
+
alias draw blit
|
45
|
+
|
34
46
|
# Returns the width of this surface.
|
35
47
|
def width
|
36
48
|
@sdl_surface.w
|
@@ -45,5 +57,32 @@ module Rubydraw
|
|
45
57
|
def fill(color)
|
46
58
|
SDL.FillRect(@sdl_surface, nil, color.to_i(:surface))
|
47
59
|
end
|
60
|
+
|
61
|
+
# Rotates and/or expands the image. Note that this modifies the image
|
62
|
+
# itself.
|
63
|
+
def rotozoom!(angle, zoom, smooth=false)
|
64
|
+
smooth =
|
65
|
+
if smooth
|
66
|
+
1
|
67
|
+
else
|
68
|
+
0
|
69
|
+
end
|
70
|
+
|
71
|
+
@sdl_surface = SDL::Gfx.rotozoomSurface(@sdl_surface, angle, zoom, smooth)
|
72
|
+
raise SDLError, "Filed to perform rotozoom: #{SDL.GetError}" if @sdl_surface.pointer.null?
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
# Returns a rotated and/or expanded image, without modifying the
|
77
|
+
# reciever.
|
78
|
+
def rotozoom(angle, zoom, smooth=false)
|
79
|
+
new_image = self.class.new(@sdl_surface)
|
80
|
+
new_image.rotozoom!(angle, zoom, smooth)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns the sdl surface.
|
84
|
+
def to_sdl
|
85
|
+
@sdl_surface
|
86
|
+
end
|
48
87
|
end
|
49
88
|
end
|
data/lib/rubydraw/text.rb
CHANGED
@@ -2,7 +2,7 @@ module Rubydraw
|
|
2
2
|
# Text objects are instantiazed with the font (can be a TTF file) and
|
3
3
|
# with the text to display. They can be drawn at a position on the
|
4
4
|
# screen.
|
5
|
-
class Text
|
5
|
+
class Text < Surface
|
6
6
|
attr_reader(:contents, :font, :color, :size)
|
7
7
|
|
8
8
|
# Create a new drawable Text object with the given font and contents.
|
@@ -24,33 +24,5 @@ module Rubydraw
|
|
24
24
|
sdl_color = @color.to_sdl
|
25
25
|
@sdl_surface = SDL::TTF.RenderText_Blended(sdl_text, @contents, sdl_color)
|
26
26
|
end
|
27
|
-
|
28
|
-
# Returns the SDL surface for this object.
|
29
|
-
def sdl_surface
|
30
|
-
@sdl_surface
|
31
|
-
end
|
32
|
-
|
33
|
-
alias to_sdl sdl_surface
|
34
|
-
|
35
|
-
# Blit (copy) into +surface at +position+ (see Rubydraw::Point).
|
36
|
-
# No graphical effects are applied.
|
37
|
-
def blit(surface, position)
|
38
|
-
source_rect = Rectangle[Point[0, 0], Point[width, height]]
|
39
|
-
blit_rect = Rectangle[position, Point[surface.width, surface.height]]
|
40
|
-
SDL::BlitSurface(sdl_surface, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
|
41
|
-
self
|
42
|
-
end
|
43
|
-
|
44
|
-
alias draw blit
|
45
|
-
|
46
|
-
# Returns the width.
|
47
|
-
def width
|
48
|
-
@sdl_surface.w
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns the height.
|
52
|
-
def height
|
53
|
-
@sdl_surface.h
|
54
|
-
end
|
55
27
|
end
|
56
28
|
end
|
data/lib/rubydraw/window.rb
CHANGED
@@ -5,7 +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(:fullscreen
|
8
|
+
attr_reader(:fullscreen)
|
9
|
+
attr_accessor(:bkg_color)
|
9
10
|
|
10
11
|
# Create a new window.
|
11
12
|
def initialize(width, height, fullscreen=false, bkg_color=Color::Black)
|