class Eagle::Texture

Overview

An image on the GPU that you can draw. Sprites, tile sets, fonts and canvases all use textures.

Load one from a file with Texture.load, which caches it, or build one from an Image you generated in code. Draw it with Graphics#draw or give it to a Sprite2D.

player = Texture.load("res://player.png")
tiles = Texture.load("res://tiles.png", filter: GPU::Filter::Nearest)

sheet = Texture.new(Image.checkerboard(128, 32, 16))
frames = sheet.frames(32, 32) # four 32x32 regions, left to right
dot = Texture.new(Image.circle(8, Color::WHITE))

For pixel art, set Texture.default_filter = GPU::Filter::Nearest before loading so pixels stay crisp when scaled.

Defined in:

eagle/graphics/texture.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.from_color(width : Int32, height : Int32, color : Color = Color::WHITE) : Texture #

A texture filled with a single color.


def self.load(path : String, filter : GPU::Filter = @@default_filter, wrap : GPU::Wrap = GPU::Wrap::Clamp, mipmaps : Bool = false) : Texture #

Loads a texture from a file through the asset cache, so loading the same path twice returns the same texture. res:// paths resolve against the assets folder.


def self.new(image : Image, filter : GPU::Filter = @@default_filter, wrap : GPU::Wrap = GPU::Wrap::Clamp, mipmaps : Bool = false, path : Nil | String = nil) #

Uploads an Image to the GPU.


def self.new(width : Int32, height : Int32, format : GPU::PixelFormat = GPU::PixelFormat::RGBA8, data : Bytes | Nil = nil, filter : GPU::Filter = @@default_filter, wrap : GPU::Wrap = GPU::Wrap::Clamp, mipmaps : Bool = false) #

Creates a blank texture of the given size, optionally filled with raw pixel data.


def self.white : Texture #

A shared 1x1 white texture. Shapes use it so they can batch with sprites.


def self.wrap_handle(id : UInt32, w : Int32, h : Int32, filter : GPU::Filter = GPU::Filter::Linear) : Texture #

Wraps an existing GPU texture id. Canvas uses this.


Class Method Detail

def self.default_filter : GPU::Filter #

Filter used by new textures and canvases when none is given.


def self.default_filter=(f : GPU::Filter) #

Sets the filter for new textures. Use GPU::Filter::Nearest for pixel-art games.


Instance Method Detail

def bind(unit : Int32 = 0) : Nil #

Binds the texture to a sampler unit, for custom rendering code.


def center : Vec2 #

The center point, handy as a pivot: g.draw(tex, pos, origin: tex.center).


def dispose : Nil #

Frees the GPU texture. Textures loaded through Assets are freed by Assets.clear instead.


def filter : GPU::Filter #

How the texture is sampled when scaled: Nearest for crisp pixels, Linear for smooth.


def filter=(f : GPU::Filter) #

Changes the sampling filter.


def format : GPU::PixelFormat #

Pixel format on the GPU.


def frames(frame_w : Int32, frame_h : Int32, count : Int32 | Nil = nil) : Array(TextureRegion) #

Cuts the texture into a grid of equal frames, left to right and top to bottom. Pass count to stop early when the last row isn't full. Feed the result to AnimatedSprite2D.


def height : Int32 #

Height in pixels.


def id : UInt32 #

The GPU texture handle, for custom rendering code.


def mipmaps? : Bool #

True when mipmaps were generated. They reduce shimmering on textures drawn much smaller than their size.


def path : String | Nil #

The file the texture came from, if any.


def rect : Rect #

The whole texture as a Rect.


def region(x : Number, y : Number, w : Number, h : Number) : TextureRegion #

A sub-rectangle of this texture that can be drawn like a texture. Use it for sprite sheets and atlases.

sheet = Texture.new(Image.checkerboard(64, 64, 16))
icon = sheet.region(16, 0, 16, 16)
g.draw(icon, 10, 10, sx: 4)

def region(r : Rect) : TextureRegion #

A sub-rectangle given as a Rect.


def size : Vec2 #

(width, height).


def update(data : Bytes, x : Int32, y : Int32, w : Int32, h : Int32) : Nil #

Replaces a rectangle of pixels with raw RGBA bytes.


def update(image : Image, x : Int32 = 0, y : Int32 = 0) : Nil #

Replaces pixels starting at x, y with image. Use it for textures that change, like a minimap or a paint canvas.


def width : Int32 #

Width in pixels.


def wrap : GPU::Wrap #

What happens outside 0..1 texture coordinates: Clamp, Repeat or Mirror.


def wrap=(w : GPU::Wrap) #

Changes the wrap mode.