Class: DXRubySDL::Image

Inherits:
Object
  • Object
show all
Includes:
Color
Defined in:
lib/dxruby_sdl/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Color

to_sdl_alpha, to_sdl_color

Constructor Details

#initialize(width, height, color = [0, 0, 0, 0]) ⇒ Image

Returns a new instance of Image.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dxruby_sdl/image.rb', line 32

def initialize(width, height, color = [0, 0, 0, 0])
  @color = color

  if width == 0 && height == 0
    return
  end

  @_surface =
    SDL::Surface.new(SDL::SWSURFACE, width, height, Window.send(:screen))
  @_surface.fill_rect(0, 0, width, height, @color)

  # TODO: a値が0の時にしか対応していない。
  # 1から254の値に対応すること
  if color[3] == 0
    set_color_key(@color[0..2])
  end
end

Instance Attribute Details

#_surfaceObject (readonly)

Returns the value of attribute _surface.



7
8
9
# File 'lib/dxruby_sdl/image.rb', line 7

def _surface
  @_surface
end

Class Method Details

.load(filename, x = nil, y = nil, width = nil, height = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/dxruby_sdl/image.rb', line 9

def self.load(filename, x = nil, y = nil, width = nil, height = nil)
  image = new(0, 0)
  surface = SDL::Surface.load(filename)
  image.instance_variable_set('@_surface', surface)
  return image
end

.load_tiles(filename, xcount, ycount) ⇒ Object Also known as: loadTiles, load_to_array



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dxruby_sdl/image.rb', line 16

def self.load_tiles(filename, xcount, ycount)
  surface = SDL::Surface.load(filename)
  width = surface.w / xcount
  height = surface.h / ycount
  images = []
  ycount.times do |y|
    xcount.times do |x|
      image = new(0, 0)
      s = surface.copy_rect(x * width, y * height, width, height)
      image.instance_variable_set('@_surface', s)
      images << image
    end
  end
  return images
end

Instance Method Details

#box(x1, y1, x2, y2, color) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dxruby_sdl/image.rb', line 98

def box(x1, y1, x2, y2, color)
  x = x1 < x2 ? x1 : x2
  w = (x2 - x1).abs
  y = y1 < y2 ? y1 : y2
  h = (y2 - y1).abs
  lock do
    @_surface.draw_rect(x, y, w, h, to_sdl_color(color), false,
                        to_sdl_alpha(color))
  end
  return self
end

#box_fill(x1, y1, x2, y2, color) ⇒ Object Also known as: boxFill



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dxruby_sdl/image.rb', line 110

def box_fill(x1, y1, x2, y2, color)
  x = x1 < x2 ? x1 : x2
  w = (x2 - x1).abs
  y = y1 < y2 ? y1 : y2
  h = (y2 - y1).abs
  lock do
    @_surface.draw_rect(x, y, w, h, to_sdl_color(color), true,
                        to_sdl_alpha(color))
  end
  return self
end

#circle(x, y, r, color) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/dxruby_sdl/image.rb', line 82

def circle(x, y, r, color)
  lock do
    @_surface.draw_circle(x, y, r, to_sdl_color(color), false, true,
                          to_sdl_alpha(color))
  end
  return self
end

#circle_fill(x, y, r, color) ⇒ Object Also known as: circleFill



90
91
92
93
94
95
96
# File 'lib/dxruby_sdl/image.rb', line 90

def circle_fill(x, y, r, color)
  lock do
    @_surface.draw_circle(x, y, r, to_sdl_color(color), true, false,
                          to_sdl_alpha(color))
  end
  return self
end

#compare(x, y, color) ⇒ Object



62
63
64
65
# File 'lib/dxruby_sdl/image.rb', line 62

def compare(x, y, color)
  pixel = lock { @_surface.get_pixel(x, y) }
  return @_surface.format.get_rgb(pixel) == color
end

#draw(x, y, image, x1 = 0, y1 = 0, width = image.width, height = image.height) ⇒ Object



122
123
124
# File 'lib/dxruby_sdl/image.rb', line 122

def draw(x, y, image, x1 = 0, y1 = 0, width = image.width, height = image.height)
  SDL.blitSurface(image._surface, x1, y1, width, height, self._surface, x, y)
end

#draw_font(x, y, string, font, color = [255, 255, 255]) ⇒ Object Also known as: drawFont



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dxruby_sdl/image.rb', line 126

def draw_font(x, y, string, font, color = [255, 255, 255])
  if string.empty?
    return
  end
  r, g, b = *color
  h = font._ttf.height + 1
  string.lines.each.with_index do |line, i|
    line.chomp!
    if line.empty?
      next
    end
    font._ttf.draw_blended_utf8(@_surface, line, x, y + i * h, r, g, b)
  end
  return self
end

#heightObject



54
55
56
# File 'lib/dxruby_sdl/image.rb', line 54

def height
  return @_surface.h
end

#line(x1, y1, x2, y2, color) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/dxruby_sdl/image.rb', line 74

def line(x1, y1, x2, y2, color)
  lock do
    @_surface.draw_line(x1, y1, x2, y2,
                        to_sdl_color(color), true, to_sdl_alpha(color))
  end
  return self
end

#set_color_key(color) ⇒ Object Also known as: setColorKey



58
59
60
# File 'lib/dxruby_sdl/image.rb', line 58

def set_color_key(color)
  @_surface.set_color_key(SDL::SRCCOLORKEY | SDL::RLEACCEL, color)
end

#slice(x, y, width, height) ⇒ Object



67
68
69
70
71
72
# File 'lib/dxruby_sdl/image.rb', line 67

def slice(x, y, width, height)
  s = @_surface.copy_rect(x, y, width, height)
  image = Image.new(0, 0)
  image.instance_variable_set('@_surface', s)
  return image
end

#widthObject



50
51
52
# File 'lib/dxruby_sdl/image.rb', line 50

def width
  return @_surface.w
end