Class: Rubydraw::Image

Inherits:
Object show all
Defined in:
lib/rubydraw/image.rb

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

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.expand_path 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

#heightObject

Returns the image height



39
40
41
# File 'lib/rubydraw/image.rb', line 39

def height
  @sdl_image.h
end

#sdl_imageObject

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

#widthObject

Returns the image width



34
35
36
# File 'lib/rubydraw/image.rb', line 34

def width
  @sdl_image.w
end