Class: Rubydraw::Image
Overview
Image instances can be initialized with Image#new, passing the window to draw in, and the path to the image file to draw with. Then to actually draw the image (doesn’t happen immediatley; read the documentation for Image#draw), call it’s #draw method and pass it the x
and y
coordinates.
Instance Method Summary collapse
-
#draw(window, position) ⇒ Object
Blit (copy) into the window at
position
(see point.rb). -
#height ⇒ Object
Returns the image height.
-
#initialize(path) ⇒ Image
constructor
Create a new image in the given window and load the image from
path.
. -
#sdl_image ⇒ Object
Returns the SDL image that was created in Image#initialize.
-
#width ⇒ Object
Returns the image width.
Constructor Details
#initialize(path) ⇒ Image
Create a new image in the given window and load the image from path.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rubydraw/image.rb', line 10 def initialize(path) # In case program is being run from a different directory, # provide the _full_ path. Nothing relative here. full_path = File. path @sdl_image = SDL::Image.Load(full_path) if @sdl_image.pointer.null? # SDL couln't load the image; usually happens because it doesn't # exist. raise Rubydraw::SDLError "Failed to load image from: '#{full_path}'" end end |
Instance Method Details
#draw(window, position) ⇒ Object
Blit (copy) into the window at position
(see point.rb). No graphical effects are applied.
Notice that you don’t blit surfaces to other surfaces when using Rubygame, but instead you draw things.
27 28 29 30 31 |
# File 'lib/rubydraw/image.rb', line 27 def draw(window, position) source_rect = SDL::Rect.new([0, 0, width, height]) blit_rect = SDL::Rect.new([position.x, position.y, window.width, window.height]) SDL::BlitSurface(@sdl_image, source_rect, window.sdl_surface, blit_rect) end |
#height ⇒ Object
Returns the image height
39 40 41 |
# File 'lib/rubydraw/image.rb', line 39 def height @sdl_image.h end |
#sdl_image ⇒ Object
Returns the SDL image that was created in Image#initialize. Rubydraw::Window uses this to draw this Image instance.
45 46 47 |
# File 'lib/rubydraw/image.rb', line 45 def sdl_image @sdl_image end |
#width ⇒ Object
Returns the image width
34 35 36 |
# File 'lib/rubydraw/image.rb', line 34 def width @sdl_image.w end |