rubydraw 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ require 'ext/object'
8
8
  # Require all the rubydraw files
9
9
  files = %w[
10
10
  window
11
+ surface
11
12
  image
12
13
  sound
13
14
  text
@@ -34,31 +34,51 @@ module Rubydraw
34
34
  raise IndexError, "One or more color values are out of bounds (must be between 0 and 255)"
35
35
  end
36
36
  @red, @green, @blue, @alpha = red, green, blue, alpha
37
- calc_num_val
37
+ calc_num_vals
38
38
  end
39
39
 
40
- # Calculate and store the numerical value in @num_val. You shouldn't
41
- # need this (see Rubydraw::Color.new).
42
- def calc_num_val
40
+ # Calculate and store the numerical values. You shouldn't need
41
+ # this (see Rubydraw::Color.new).
42
+ def calc_num_vals
43
43
  # Get the hex string for each value.
44
44
  hex_alpha = (@alpha.to_s(16)).color_string
45
45
  hex_red = (@red.to_s(16)).color_string
46
46
  hex_green = (@green.to_s(16)).color_string
47
47
  hex_blue = (@blue.to_s(16)).color_string
48
- # Construct a hex string using the previously determined hex colors.
49
- # *Note:* it appears that SDL's colors are in the format +BBGGRRAA+.
50
- color_str = hex_blue + hex_green + hex_red + hex_alpha
51
- @num_val = color_str.to_i(16)
48
+ # Construct a hex string (only compatible with surfaces other than
49
+ # the window) using the previously determined hex colors. Not sure
50
+ # how alpha comes in here yet... I'll figure it out.
51
+ surf_color_string = hex_red + hex_green + hex_blue
52
+ @surface_num_val = surf_color_string.to_i(16)
53
+ # *Note:* SDL's color format for the display is different than other
54
+ # ordinary surfaces. It is: +BBGGRRAA+.
55
+ display_color_string = hex_blue + hex_green + hex_red + hex_alpha
56
+ @display_num_val = display_color_string.to_i(16)
52
57
  end
53
58
 
54
59
  # Convert this color to a numerical value, which only makes sense when
55
60
  # read as a hex number, e.g. red would be: +0000ff00+.
56
61
  #
57
62
  # Also see the comments in: Rubydraw::Color#calc_num_val.
58
- def to_i
59
- @num_val
63
+ def to_i(format)
64
+ if format == :surface or format == :display_fullscreen
65
+ return @surface_num_val
66
+ end
67
+ if format == :display
68
+ return @display_num_val
69
+ end
70
+ raise ArgumentError, "Unrecognized color format \"@{format}\""
71
+ end
72
+
73
+ # Returns the complimentary color.
74
+ def invert
75
+ Color.new(255 - @red, 255 - @green, 255 - @blue, @alpha)
60
76
  end
61
77
 
78
+ alias complimentary invert
79
+ alias opposite invert
80
+ alias reverse invert
81
+
62
82
  # Returns an Array containing each +rgba+ value.
63
83
  #
64
84
  # Example:
@@ -70,6 +90,10 @@ module Rubydraw
70
90
  [@red, @blue, @green, @alpha]
71
91
  end
72
92
 
93
+ def to_s
94
+ "#<#{self.class}: (@red: #{@red}, @green: #{@green}, @blue: #{@blue}, @alpha: #{@alpha})>"
95
+ end
96
+
73
97
  # Create an SDL::Color equivilent to this Rubydraw::Color.
74
98
  def to_sdl
75
99
  SDL::Color.new(to_a)
@@ -20,7 +20,7 @@ module Rubydraw
20
20
  end
21
21
 
22
22
  def load_from_path(path) #:nodoc:
23
- # Check if this image has already been initialize. If it has, raise
23
+ # Check if this image has already been initialized. If it has, raise
24
24
  # an error.
25
25
  unless @sdl_image.nil?
26
26
  raise SDLError, "Images may only be loaded once"
@@ -45,6 +45,8 @@ module Rubydraw
45
45
  @sdl_image = surface
46
46
  end
47
47
 
48
+ private :load_from_path, :load_from_surface
49
+
48
50
  # Blit (copy) into +surface at +position+ (see Rubydraw::Point).
49
51
  # No graphical effects are applied.
50
52
  def blit(surface, position)
@@ -0,0 +1,49 @@
1
+ module Rubydraw
2
+ # The basic class whose instances can blit themselves to other surfaces or the window.
3
+ # You can only manipulate the surface's pixels, but there are subclasses to do other
4
+ # things (like Rubydraw::Image or Rubydraw::Font)
5
+ class Surface
6
+ # Create a new, blank surface with the given dimensions.
7
+ def initialize(dimensions)
8
+ @dimensions = dimensions
9
+ pixel_format = SDL.GetVideoInfo.vfmt
10
+ rmsk, gmsk, bmsk, amsk = 0xff0000, 0x00ff00, 0x0000ff, 0x000000
11
+ depth = pixel_format.BitsPerPixel
12
+ @sdl_surface = SDL.CreateRGBSurface(0, dimensions.x, dimensions.y, depth, rmsk, gmsk, bmsk, amsk)
13
+ if @sdl_surface.pointer.null?
14
+ raise SDLError, "Failed to create Rubydraw surface: #{SDL.GetError}"
15
+ end
16
+ self
17
+ end
18
+
19
+ # Blit (copy) into +surface at +position+ (see Rubydraw::Point).
20
+ # No graphical effects are applied.
21
+ def blit(surface, position)
22
+ if surface.nil?
23
+ raise SDLError, "Surface to blit to cannot be nil"
24
+ end
25
+ if position.nil?
26
+ raise SDLError, "Position to blit at cannot be nil"
27
+ end
28
+ source_rect = Rectangle[Point[0, 0], Point[width, height]]
29
+ blit_rect = Rectangle[position, Point[surface.width, surface.height]]
30
+ SDL::BlitSurface(@sdl_surface, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
31
+ self
32
+ end
33
+
34
+ # Returns the width of this surface.
35
+ def width
36
+ @sdl_surface.w
37
+ end
38
+
39
+ # Returns the height of this surface.
40
+ def height
41
+ @sdl_surface.h
42
+ end
43
+
44
+ # Fills this surface with the given color.
45
+ def fill(color)
46
+ SDL.FillRect(@sdl_surface, nil, color.to_i(:surface))
47
+ end
48
+ end
49
+ end
@@ -9,16 +9,29 @@ module Rubydraw
9
9
 
10
10
  # Create a new window.
11
11
  def initialize(width, height, fullscreen=false, bkg_color=Color::Black)
12
+ if width < 0
13
+ raise SDLError, "New window width cannot be less than zero"
14
+ end
15
+ if height < 0
16
+ raise SDLError, "New window height cannot be less than zero"
17
+ end
18
+ if height == 0
19
+ # Compensate for the height of the window bar itself, the menu bar at the top
20
+ # of the screen, and a space of around 4px at the bottom of the screen. Not
21
+ # sure what the numbers would be for anything other than Mac OSX; more info
22
+ # would be appreciated.
23
+ height = Rubydraw::screen_height - 48
24
+ end
12
25
  @width, @height = width, height
13
26
  @fullscreen = fullscreen
14
27
  @bkg_color = bkg_color
15
28
  @open = false
16
29
  @flags =
17
- if fullscreen
18
- SDL::FULLSCREEN
19
- else
20
- 0
21
- end
30
+ if fullscreen
31
+ SDL::FULLSCREEN
32
+ else
33
+ 0
34
+ end
22
35
 
23
36
  @event_queue = EventQueue.new
24
37
 
@@ -78,7 +91,17 @@ module Rubydraw
78
91
  # Fill the entire window with a Rubydraw::Color. Do this by feeding SDL the numeric
79
92
  # verion of the color; see Rubydraw::Color#to_i
80
93
  def fill_with(color)
81
- SDL::FillRect(@screen, nil, color.to_i)
94
+ SDL::FillRect(@screen, nil, convert(color))
95
+ end
96
+
97
+ # Convert +color+ to the display format.
98
+ def convert(color)
99
+ puts @fullscreen
100
+ if @fullscreen
101
+ return color.to_i(:display_fullscreen)
102
+ else
103
+ return color.to_i(:display)
104
+ end
82
105
  end
83
106
 
84
107
  # Call this method to tell SDL to quit drawing. The loop (started in
@@ -114,11 +137,11 @@ module Rubydraw
114
137
  #events.each {|event|
115
138
  # block = @registered_actions[event.class]
116
139
  # block.call(event) unless block.nil?}
117
- events.each {|event|
140
+ events.each { |event|
118
141
  blocks = @registered_actions[event.class]
119
142
  unless blocks.nil?
120
- blocks.each {|b| b.call(event) unless b.nil?}
121
- end}
143
+ blocks.each { |b| b.call(event) unless b.nil? }
144
+ end }
122
145
  end
123
146
 
124
147
  # This causes the main loop to exit. Use Rubydraw::Window#close to close the
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
5
+ version: 0.2.7
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-11-29 00:00:00 -07:00
13
+ date: 2011-12-01 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/surface.rb
39
40
  - lib/rubydraw/image.rb
40
41
  - lib/rubydraw/sound.rb
41
42
  - lib/rubydraw/text.rb