Class: Rubydraw::Surface

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

Overview

The basic class whose instances can blit themselves to other surfaces or the window. You can only manipulate the surface’s pixels, but there are subclasses to do other things (like Rubydraw::Image or Rubydraw::Text)

Direct Known Subclasses

Image, Text

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Surface

Returns a new instance of Surface.



16
17
18
19
20
21
22
23
# File 'lib/rubydraw/surface.rb', line 16

def initialize(*args)
  if args.size == 1
    # Must mean to wrap an SDL surface.
    @sdl_surface = args[0]
  else
    load_from_color(*args)
  end
end

Class Method Details

.load_img(path) ⇒ Object

Load an image from path.



7
8
9
# File 'lib/rubydraw/surface.rb', line 7

def self.load_img(path)
  Image.new(path)
end

.load_text(contents, color) ⇒ Object

Create a new Rubydraw::Font with text as its contents.



12
13
14
# File 'lib/rubydraw/surface.rb', line 12

def self.load_text(contents, color)
  Text.new(contents, color)
end

Instance Method Details

#areaObject

Returns the area of this surface; e.g. if width were 5 and height were 4, this method would return 20.



227
228
229
# File 'lib/rubydraw/surface.rb', line 227

def area
  width * height
end

#blit(surface, position) ⇒ Object Also known as: draw

Blit (copy) into +surface at position (see Rubydraw::Point). No graphical effects are applied.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubydraw/surface.rb', line 43

def blit(surface, position)
  if surface.nil?
    raise SDLError, "Surface to blit to cannot be nil"
  end
  if position.nil?
    raise SDLError, "Position to blit at cannot be nil"
  end
  source_rect = Rectangle[Point[0, 0], Point[width, height]]
  blit_rect = Rectangle[position, Point[surface.width, surface.height]]
  SDL::BlitSurface(@sdl_surface, source_rect.to_sdl, surface.to_sdl, blit_rect.to_sdl)
  self
end

#fill(color) ⇒ Object

Fills this surface with the given color.



73
74
75
# File 'lib/rubydraw/surface.rb', line 73

def fill(color)
  SDL.FillRect(@sdl_surface, nil, color.to_i(:surface))
end

#flip(axis) ⇒ Object

Flip the surface on an axis.

Raises:

  • (ArgumentError)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rubydraw/surface.rb', line 205

def flip(axis)
  axis = axis.to_sym
  if axis == :horizontal
    pixels.each {
      |color, x, y|
      set_pixel(Point[x, height - y], color)
    }
    return self
  end
  if axis == :vertical
    pixels.each {
      |color, x, y|
      set_pixel(Point[width - x, y], color)
    }
    return self
  end
  # Only get here if no axis mode was matched.
  raise ArgumentError, "Unknown flip mode: \"#{axis}\""
end

#get_pixel(point) ⇒ Object

Returns the color of the pixel at point.



174
175
176
177
178
179
# File 'lib/rubydraw/surface.rb', line 174

def get_pixel(point)
  SDL.LockSurface(@sdl_surface)
  result = basic_get_pix(point)
  SDL.UnlockSurface(@sdl_surface)
  return result
end

#heightObject

Returns the height of this surface.



64
65
66
# File 'lib/rubydraw/surface.rb', line 64

def height
  @sdl_surface.h
end

#pixelsObject

Returns a two-dimensional array (from rubygems.org/gems/2DArray) containing each pixel color at its proper position.

There’s probably a better way to implement this… :/



193
194
195
196
197
198
199
200
201
202
# File 'lib/rubydraw/surface.rb', line 193

def pixels
  ary = Array2D.new(width, height)
  x = 0
  y = 0
  SDL.LockSurface(@sdl_surface)
  ary.each { |elem, elem_x, elem_y|
    ary[elem_x, elem_y] = basic_get_pix(Point[elem_x, elem_y]) }
  SDL.UnlockSurface(@sdl_surface)
  ary
end

#rotozoom(angle, zoom, smooth = false) ⇒ Object

Returns a rotated and/or expanded image, without modifying the reciever.



94
95
96
97
# File 'lib/rubydraw/surface.rb', line 94

def rotozoom(angle, zoom, smooth=false)
  new_image = self.class.new(@sdl_surface)
  new_image.rotozoom!(angle, zoom, smooth)
end

#rotozoom!(angle, zoom, smooth = false) ⇒ Object

Rotates and/or expands the image. Note that this modifies the image itself.

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubydraw/surface.rb', line 79

def rotozoom!(angle, zoom, smooth=false)
  smooth =
      if smooth
        1
      else
        0
      end

  @sdl_surface = SDL::Gfx.rotozoomSurface(@sdl_surface, angle, zoom, smooth)
  raise SDLError, "Filed to perform rotozoom: #{SDL.GetError}" if @sdl_surface.pointer.null?
  self
end

#set_pixel(point, new) ⇒ Object

Sets the color at point.



182
183
184
185
186
187
# File 'lib/rubydraw/surface.rb', line 182

def set_pixel(point, new)
  SDL.LockSurface(@sdl_surface)
  result = basic_set_pix(point, new)
  SDL.UnlockSurface(@sdl_surface)
  return result
end

#sizeObject



68
69
70
# File 'lib/rubydraw/surface.rb', line 68

def size
  Point[width, height]
end

#to_sdlObject

Returns the sdl surface.



100
101
102
# File 'lib/rubydraw/surface.rb', line 100

def to_sdl
  @sdl_surface
end

#widthObject

Returns the width of this surface.



59
60
61
# File 'lib/rubydraw/surface.rb', line 59

def width
  @sdl_surface.w
end