Class: Texture2D

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/texture2d.rb,
mrb_doc/models/texture2d.rb

Overview

The Texture2D class is most often used for drawing sprites.

Defined Under Namespace

Classes: NotFoundError

Texture filters collapse

NO_FILTER =

No filter, just pixel approximation.

0
BILINEAR =

Linear filtering.

1
TRILINEAR =

Trilinear filtering (linear with mipmaps).

2
ANISOTROPIC_4X =

Anisotropic filtering 4x.

3
ANISOTROPIC_8X =

Anisotropic filtering 8x.

4
ANISOTROPIC_16X =

Anisotropic filtering 16x.

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Texture2D

Loads an image file from the disk. If the file does not exist, it will raise a NotFoundError error.

Examples:

Basic usage

texture = Texture2D.new("/assets/my_cool_sprite.png")

Parameters:

  • path (String)

Raises:



11
12
13
14
# File 'mrb_doc/models/texture2d.rb', line 11

def initialize(path)
  # mrb_Texture2D_initialize
  # src/mruby_integration/models/texture2d.cpp
end

Instance Attribute Details

#filterInteger

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def filter
  @filter
end

#formatInteger (readonly)

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def format
  @format
end

#heightInteger (readonly)

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def height
  @height
end

#idInteger (readonly)

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def id
  @id
end

#mipmapsInteger (readonly)

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def mipmaps
  @mipmaps
end

#widthInteger (readonly)

Returns:

  • (Integer)


4
5
6
# File 'src/ruby/models/texture2d.rb', line 4

def width
  @width
end

Class Method Details

.mock_return(id: 1, width: 10, height: 15, mipmaps: 2, format: 0) ⇒ String

A method used to generate the mock data for Raylib.

Examples:

Basic usage

Taylor::Raylib.mock_call(
  "LoadTexture",
  Texture2D(id: 1, width: 10, height: 15, mipmaps: 2, format: 0)
)

Parameters:

  • id (Integer) (defaults to: 1)
  • width (Integer) (defaults to: 10)
  • height (Integer) (defaults to: 15)
  • mipmaps (Integer) (defaults to: 2)
  • format (Integer) (defaults to: 0)

Returns:

  • (String)


32
33
34
# File 'src/ruby/models/texture2d.rb', line 32

def self.mock_return(id: 1, width: 10, height: 15, mipmaps: 2, format: 0)
  [id, width, height, mipmaps, format].map(&:to_s).join(" ")
end

Instance Method Details

#draw(source: Rectangle[0, 0, width, height], position: Vector2[0, 0], destination: Rectangle[position.x, position.y, source.width, source.height], origin: Vector2[0, 0], rotation: 0, colour: Colour::WHITE) ⇒ nil

Draws the Texture2D segment defined by source at the given destination, rotated around the origin in the specified colour.

Examples:

Basic usage

texture = Texture2D.new("/assets/my_cool_sprite.png")

# Just draw the whole texture
texture.draw(position: Vector2[100, 120])

# Rotate the texture 90 degrees when drawn
texture.draw(position: Vector2[100, 120], rotation: 90)

# Draw with half opacity
texture.draw(position: Vector2[100, 120], colour: Colour[255, 255, 255, 128])

# Stretch as you draw it
texture.draw(
  source: Rectangle[0, 0, 16, 16],
  destination: Rectangle[100, 120, 64, 32],
)

Basic sprite sheet usage

sheet = Texture2D.new("/assets/my_cool_sprite_sheet.png")
player = Rectangle[0, 0, 16, 16]
grass = Rectangle[16, 0, 16, 16]

10.times do |y|
  10.times do |x|
    sheet.draw(source: grass, position: Vector[x * 16, y * 16])
  end
end

sheet.draw(source: player, position: Vector2[32, 32])

Parameters:

  • source (Rectangle) (defaults to: Rectangle[0, 0, width, height])
  • position (Vector2) (defaults to: Vector2[0, 0])
  • destination (Rectangle) (defaults to: Rectangle[position.x, position.y, source.width, source.height])
  • origin (Vector2) (defaults to: Vector2[0, 0])
  • rotation (Integer) (defaults to: 0)

    In degrees

  • colour (Colour) (defaults to: Colour::WHITE)

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if both position and destination are passed in



123
124
125
126
127
128
129
130
131
132
133
134
# File 'mrb_doc/models/texture2d.rb', line 123

def draw(
  source: Rectangle[0, 0, width, height],
  position: Vector2[0, 0],
  destination: Rectangle[position.x, position.y, source.width, source.height],
  origin: Vector2[0, 0],
  rotation: 0,
  colour: Colour::WHITE
)
  # mrb_Texture2D_draw
  # src/mruby_integration/models/texture2d.cpp
  nil
end

#generate_mipmapsnil

Generates mipmaps for the Texture2D.

Examples:

Basic usage

big_texture = Texture2D.new("/assets/my_cool_background.png")

big_texture.generate_mipmaps

big_texture.unload

Returns:

  • (nil)


74
75
76
77
78
# File 'mrb_doc/models/texture2d.rb', line 74

def generate_mipmaps
  # mrb_Texture2D_generate_mipmaps
  # src/mruby_integration/models/texture2d.cpp
  nil
end

#to_hHash

Return the object represented by a Hash.

Returns:

  • (Hash)


8
9
10
11
12
13
14
15
16
# File 'src/ruby/models/texture2d.rb', line 8

def to_h
  {
    id: id,
    width: width,
    height: height,
    mipmaps: mipmaps,
    format: format
  }
end

#unloadnil

Unloads the Texture2D from memory.

Examples:

Basic usage

texture = Texture2D.new("/assets/my_cool_sprite.png")
texture.unload

Returns:

  • (nil)


23
24
25
26
27
# File 'mrb_doc/models/texture2d.rb', line 23

def unload
  # mrb_Texture2D_unload
  # src/mruby_integration/models/texture2d.cpp
  nil
end

#valid?Boolean

Checks if the Texture2D was successfully loaded onto the GPU and is valid.

Examples:

Basic usage

texture = Texture2D.new("/assets/image.png")
raise "Bad image!" unless texture.valid?
texture.unload

Returns:

  • (Boolean)


37
38
39
40
41
# File 'mrb_doc/models/texture2d.rb', line 37

def valid?
  # mrb_Texture_valid
  # src/mruby_integration/models/texture.cpp
  true
end