Class: Ruby2D::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/texture.rb

Overview

This internal class is used to hold raw pixel data which in turn is used to render textures via openGL rendering code.

Constant Summary collapse

WHITE_OPAQUE_AR =
[1.0, 1.0, 1.0, 1.0].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pixel_data, width, height) ⇒ Texture

Returns a new instance of Texture.



13
14
15
16
17
18
# File 'lib/ruby2d/texture.rb', line 13

def initialize(pixel_data, width, height)
  @pixel_data = pixel_data
  @width = width
  @height = height
  @texture_id = 0
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/ruby2d/texture.rb', line 9

def height
  @height
end

#texture_idObject (readonly)

Returns the value of attribute texture_id.



9
10
11
# File 'lib/ruby2d/texture.rb', line 9

def texture_id
  @texture_id
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/ruby2d/texture.rb', line 9

def width
  @width
end

Instance Method Details

#deleteObject



34
35
36
# File 'lib/ruby2d/texture.rb', line 34

def delete
  ext_delete(@texture_id)
end

#draw(coordinates, texture_coordinates, color = nil) ⇒ Object

Draw the texture

Parameters:

  • coordinates (Array(x1, y1, x2, y2, x3, y3, x4, y4))

    Destination coordinates

  • texture_coordinates (Array(tx1, ty1, tx2, ty2, tx3, ty3, tx1, ty3))

    Source (texture) coordinates

  • color (Ruby2D::Color) (defaults to: nil)

    Tint/blend the texture when it’s drawn



24
25
26
27
28
29
30
31
32
# File 'lib/ruby2d/texture.rb', line 24

def draw(coordinates, texture_coordinates, color = nil)
  if @texture_id.zero?
    @texture_id = ext_create(@pixel_data, @width, @height)
    @pixel_data = nil
  end

  color = color.nil? ? WHITE_OPAQUE_AR : [color.r, color.g, color.b, color.a]
  ext_draw(coordinates, texture_coordinates, color, @texture_id)
end