Class: Rubydraw::Image

Inherits:
Surface 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

Methods inherited from Surface

#area, #blit, #fill, #flip, #get_pixel, #height, load_img, load_text, #pixels, #rotozoom, #rotozoom!, #set_pixel, #size, #to_sdl, #width

Constructor Details

#initialize(arg) ⇒ Image

Returns a new instance of Image.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rubydraw/image.rb', line 8

def initialize(arg)
  if arg.kind_of?(SDL::Surface)
    # This means we want to wrap an sdl surface.
    @sdl_surface = arg
  elsif arg.kind_of?(String)
    # This means we want to load an image from a file.
    initialize_from_path(arg)
  else
    # Raise an error if it is neither. I should figure out a less strict, more dynamic way to do this.
    raise SDLError, "Argument to Rubydraw::Image.new was neither an image path nor an SDL::Surface to wrap"
  end
end

Instance Method Details

#initialize_from_path(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubydraw/image.rb', line 21

def initialize_from_path(path)
  # Check if this image has already been initialized. If it has, raise
  # an error.
  unless @sdl_surface.nil?
    raise SDLError, "Images may only be loaded once"
  end
  # In case program is being run from a different directory,
  # provide the _full_ path. Nothing relative here.
  full_path = File.expand_path path
  @sdl_surface = SDL::Image.Load(full_path)
  if @sdl_surface.pointer.null?
    # SDL couln't load the image; usually happens because it doesn't
    # exist.
    raise SDLError, "Failed to load image: #{SDL.GetError}"
  end
end