rubydraw 0.2.9.3 → 0.3.0

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.
@@ -1,5 +1,6 @@
1
- # The only dependency
1
+ # Require dependencies
2
2
  require 'ruby-sdl-ffi'
3
+ require '2DArray'
3
4
 
4
5
  # The extentions must be loaded first, except for +aliases.rb+
5
6
  require 'ext/string'
@@ -18,9 +18,15 @@ module Rubydraw
18
18
  }
19
19
  end
20
20
 
21
+ # Create a new color from an array with the format [r, g, b, a].
22
+ # Ignores any extraneous values.
23
+ def self.from_ary(ary)
24
+ return self.new(ary[0], ary[1], ary[2], ary[3])
25
+ end
26
+
21
27
  # Shorthand new method.
22
- def self.[](red, green, blue, alpha = 0)
23
- self.new(red, green, blue, alpha)
28
+ def self.[](*args)
29
+ self.new(*args)
24
30
  end
25
31
 
26
32
  attr_reader(:red, :green, :blue, :alpha)
@@ -29,7 +35,7 @@ module Rubydraw
29
35
  # values. Alpha is 0 by default.
30
36
  #
31
37
  # TODO: Add other color specs, like HSV or maybe CYMK
32
- def initialize(red, green, blue, alpha = 255)
38
+ def initialize(red, green, blue, alpha=255)
33
39
  unless self.class.in_bounds?(red, green, blue, alpha)
34
40
  raise IndexError, "One or more color values are out of bounds (must be between 0 and 255)"
35
41
  end
@@ -79,6 +79,8 @@ module Rubydraw
79
79
  end
80
80
  end
81
81
 
82
+ alias :eql? :==
83
+
82
84
  def to_ary
83
85
  [@x, @y]
84
86
  end
@@ -84,5 +84,109 @@ module Rubydraw
84
84
  def to_sdl
85
85
  @sdl_surface
86
86
  end
87
+
88
+ # This is a private method because it doesn't lock the SDL surface (too
89
+ # much locking and unlocking for Rubydraw::Surface#pixels). Use
90
+ # Rubydraw::Surface#get_pixel.
91
+ #
92
+ # Credit goes to Rubygame and the SDL docs for showing how to access this
93
+ # information.
94
+ def basic_get_pix(point)
95
+ bpp = @sdl_surface.format.BytesPerPixel
96
+ p = @sdl_surface.pixels + (point.y * @sdl_surface.pitch + point.x * bpp)
97
+
98
+ pcolor =
99
+ case bpp
100
+ when 1
101
+ p.get_uint8(0)
102
+ when 2
103
+ p.get_uint16(0)
104
+ when 3
105
+ if (FFI::Platform::BYTE_ORDER == FFI::Platform::BIG_ENDIAN)
106
+ (ptr.get_uint8(0) << 16)|(ptr.get_uint8(1) << 8)|ptr.get_uint8(2)
107
+ else
108
+ ptr.get_uint8(0)|(ptr.get_uint8(1) << 8)|(ptr.get_uint8(2) << 16)
109
+ end
110
+ when 4
111
+ p.get_uint32(0)
112
+ end
113
+
114
+ r, g, b, a = SDL.GetRGBA(pcolor, @sdl_surface.format)
115
+ Rubydraw::Color.new(r, g, b, a)
116
+ end
117
+
118
+ # This is a private method because it doesn't lock the SDL surface (too
119
+ # much locking and unlocking for Rubydraw::Surface#pixels_do). Use
120
+ # Rubydraw::Surface#set_pixel.
121
+ #
122
+ # Credit goes to Rubygame and the SDL docs for showing how to access this
123
+ # information.
124
+ def basic_set_pix(point, new_color)
125
+ color = new_color.to_i(:surface)
126
+
127
+ bpp = @sdl_surface.format.BytesPerPixel
128
+ p = @sdl_surface.pixels + (point.y * @sdl_surface.pitch + point.x * bpp)
129
+
130
+ case bpp
131
+ when 1
132
+ p.put_uint8(0, color)
133
+ when 2
134
+ p.put_uint16(0, color)
135
+ when 3
136
+ if (FFI::Platform::BYTE_ORDER == FFI::Platform::BIG_ENDIAN)
137
+ p.put_uint8(0, (color >> 16) & 0xff)
138
+ p.put_uint8(1, (color >> 8) & 0xff)
139
+ p.put_uint8(2, color & 0xff)
140
+ else
141
+ p.put_uint8(0, color & 0xff)
142
+ p.put_uint8(1, (color >> 8) & 0xff)
143
+ p.put_uint8(2, (color >> 16) & 0xff)
144
+ end
145
+ when 4
146
+ p.put_uint32(0, color)
147
+ end
148
+
149
+ return new_color
150
+ end
151
+
152
+ private :basic_set_pix
153
+ private :basic_get_pix
154
+
155
+ # Returns the color of the pixel at +point+.
156
+ def get_pixel(point)
157
+ SDL.LockSurface(@sdl_surface)
158
+ result = basic_get_pix(point)
159
+ SDL.UnlockSurface(@sdl_surface)
160
+ return result
161
+ end
162
+
163
+ # Sets the color at +point+.
164
+ def set_pixel(point, new)
165
+ SDL.LockSurface(@sdl_surface)
166
+ result = basic_set_pix(point, new)
167
+ SDL.UnlockSurface(@sdl_surface)
168
+ return result
169
+ end
170
+
171
+ # Returns a two-dimensional array (from https://rubygems.org/gems/2DArray)
172
+ # containing each pixel color at its proper position.
173
+ #
174
+ # There's probably a better way to implement this... :/
175
+ def pixels
176
+ ary = Array2D.new(width, height)
177
+ x = 0
178
+ y = 0
179
+ SDL.LockSurface(@sdl_surface)
180
+ ary.each { |elem, elem_x, elem_y|
181
+ ary[elem_x, elem_y] = basic_get_pix(Point[elem_x, elem_y]) }
182
+ SDL.UnlockSurface(@sdl_surface)
183
+ ary
184
+ end
185
+
186
+ # Returns the area of this surface; e.g. if +width+ were 5 and +height+ were
187
+ # 4, this method would return 20.
188
+ def area
189
+ width * height
190
+ end
87
191
  end
88
192
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rubydraw
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.9.3
5
+ version: 0.3.0
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-03 00:00:00 -07:00
13
+ date: 2011-12-10 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -24,7 +24,18 @@ dependencies:
24
24
  version: "0.4"
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
- description: "\n Rubydraw is a high level drawing/game library,\n like Gosu or Rubygame. Its only dependencies are\n ruby-sdl-ffi--which it uses to access SDL\n functions--and SDL itself (See README on how to\n install the latter)."
27
+ - !ruby/object:Gem::Dependency
28
+ name: 2DArray
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.1.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ description: "\n Rubydraw is a high level drawing/game library,\n like Gosu or Rubygame.\n Dependancies: ruby-sdl-ffi, SDL (not a ruby gem),\n and 2DArray."
28
39
  email:
29
40
  executables: []
30
41