Permalink
Browse files

Added #from_blob and rspec for #from_blob and #create_image. Changes …

…to make it 1.9.2 compatible (regarding $LOAD_PATH).
  • Loading branch information...
1 parent 242e846 commit f0d2879fd03653a9861471b984e122554fd89796 @Spooner Spooner committed Aug 19, 2010
Showing with 130 additions and 12 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 .rspec
  3. +6 −0 Rakefile
  4. +33 −12 lib/texplay.rb
  5. +7 −0 spec/image_spec.rb
  6. +80 −0 spec/texplay_spec.rb
View
@@ -0,0 +1,2 @@
+lib/**/*.so
+.idea/
View
2 .rspec
@@ -0,0 +1,2 @@
+--colour
+--format documentation
View
@@ -1,3 +1,4 @@
+$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), '..')
require 'rake/clean'
require 'rake/gempackagetask'
@@ -66,3 +67,8 @@ Rake::GemPackageTask.new(specification) do |package|
package.need_zip = false
package.need_tar = false
end
+
+desc "Run rspec 2.0"
+task :rspec do
+ system "rspec spec/**/*_spec.rb"
+end
View
@@ -35,6 +35,8 @@ def create_image(window, width, height, options={})
:color => :alpha,
:caching => false,
}.merge!(options)
+
+ raise ArgumentError, "Height and width must be positive" if height <= 0 or width <= 0
img = Gosu::Image.new(window, EmptyImageStub.new(width, height), :caching => options[:caching])
img.rect 0, 0, img.width - 1, img.height - 1, :color => options[:color], :fill => true
@@ -44,6 +46,23 @@ def create_image(window, width, height, options={})
alias_method :create_blank_image, :create_image
+ # Image can be :tileable, but it will break if it is tileable AND gets modified after creation.
+ def from_blob(window, blob_data, width, height, options={})
+ options = {
+ :caching => @options[:caching],
+ :tileable => false,
+ }.merge!(options)
+
+ raise ArgumentError, "Height and width must be positive (received #{width}x#{height})" if height <= 0 or width <= 0
+
+ expected_size = height * width * 4
+ if blob_data.size != expected_size
+ raise ArgumentError, "Blob data is not of the correct size (expected #{expected_size} but received #{blob_data.size} bytes)"
+ end
+
+ Gosu::Image.new(window, ImageStub.new(blob_data, width, height), options[:tileable], :caching => options[:caching])
+ end
+
def set_options(options = {})
@options.merge!(options)
end
@@ -100,22 +119,24 @@ def clear(options = {})
end
-# credit to philomory for this class
-class EmptyImageStub
- def initialize(w, h)
- @w, @h = w, h
- end
+# Used to create images from blob data.
+class ImageStub
+ attr_reader :rows, :columns
- def to_blob
- "\0" * @w * @h * 4
+ def initialize(blob_data, width, height)
+ @data, @columns, @rows = blob_data, width, height
end
- def rows
- @h
+ def to_blob
+ @data
end
-
- def columns
- @w
+end
+
+# Used to create blank images.
+# credit to philomory for this class
+class EmptyImageStub < ImageStub
+ def initialize(width, height)
+ super("\0" * (width * height * 4), width, height)
end
end
View
@@ -0,0 +1,7 @@
+$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), '..', 'lib','texplay')
+
+require 'texplay'
+
+describe Gosu::Image do
+ it "should do all sorts of things"
+end
View
@@ -0,0 +1,80 @@
+$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), '..', 'lib','texplay')
+
+require 'texplay'
+
+describe TexPlay do
+ before :each do
+ @window = Gosu::Window.new(640, 480, false)
+ end
+
+ describe "#create_image" do
+ it "should create a blank image of the correct size" do
+ width, height = 30, 10
+ image = described_class.create_image(@window, width, height)
+
+ image.width.should == width
+ image.height.should == height
+
+ width.times do |x|
+ height.times do |y|
+ image.get_pixel(x, y).should == [0.0, 0.0, 0.0, 0.0]
+ end
+ end
+ end
+
+ it "should create a coloured image of the correct size" do
+ width, height = 10, 30
+ color = [1.0, 1.0, 0.0, 1.0]
+ image = described_class.create_image(@window, width, height, :color => color)
+
+ image.width.should == width
+ image.height.should == height
+
+ width.times do |x|
+ height.times do |y|
+ image.get_pixel(x, y).should == color
+ end
+ end
+ end
+
+ it "should raise an error if an image dimension is 0 or less" do
+ lambda { described_class.create_image(@window, 0, 0)}.should raise_error ArgumentError
+ end
+
+ # TODO: Should probably be an ArgumentError.
+ it "should raise an error if the image would be too large" do
+ too_big = TexPlay::TP_MAX_QUAD_SIZE + 1
+ [[too_big, 5], [10, too_big], [too_big, too_big]].each do |width, height|
+ lambda { described_class.create_image(@window, width, height)}.should raise_error Exception
+ end
+ end
+ end
+
+ describe "#from_blob" do
+ it "should create an image with the requested pixel data and size" do
+ # 4 x 3, with columns of red, blue, green, transparent.
+ gosu_colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255], [0, 0, 0, 0]]
+ texplay_colors = gosu_colors.map {|a| a.map {|c| c / 255.0 } }
+ width, height = gosu_colors.size, 3
+
+ image = described_class.from_blob(@window, (gosu_colors * height).flatten.pack('C*'), width, height)
+
+ image.width.should == width
+ image.height.should == height
+
+ texplay_colors.each_with_index do |color, x|
+ 3.times do |y|
+ image.get_pixel(x, y).should == color
+ end
+ end
+ end
+
+ it "should raise an error if the image size is not correct for the blob data" do
+ lambda { described_class.from_blob(@window, [1, 1, 1, 1].pack("C*"), 2, 1) }.should raise_error ArgumentError
+ end
+
+ it "should raise an error if an image dimension is 0 or less" do
+ lambda { described_class.from_blob(@window, '', 0, 0) }.should raise_error ArgumentError
+ end
+ end
+end

0 comments on commit f0d2879

Please sign in to comment.