class
Eagle::Texture
- Eagle::Texture
- Reference
- Object
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.crConstructors
-
.from_color(width : Int32, height : Int32, color : Color = Color::WHITE) : Texture
A texture filled with a single color.
-
.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.
-
.new(image : Image, filter : GPU::Filter = @@default_filter, wrap : GPU::Wrap = GPU::Wrap::Clamp, mipmaps : Bool = false, path : Nil | String = nil)
Uploads an
Imageto the GPU. -
.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.
-
.white : Texture
A shared 1x1 white texture.
-
.wrap_handle(id : UInt32, w : Int32, h : Int32, filter : GPU::Filter = GPU::Filter::Linear) : Texture
Wraps an existing GPU texture id.
Class Method Summary
-
.default_filter : GPU::Filter
Filter used by new textures and canvases when none is given.
-
.default_filter=(f : GPU::Filter)
Sets the filter for new textures.
Instance Method Summary
-
#bind(unit : Int32 = 0) : Nil
Binds the texture to a sampler unit, for custom rendering code.
-
#center : Vec2
The center point, handy as a pivot:
g.draw(tex, pos, origin: tex.center). -
#dispose : Nil
Frees the GPU texture.
-
#filter : GPU::Filter
How the texture is sampled when scaled:
Nearestfor crisp pixels,Linearfor smooth. -
#filter=(f : GPU::Filter)
Changes the sampling filter.
-
#format : GPU::PixelFormat
Pixel format on the GPU.
-
#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.
-
#height : Int32
Height in pixels.
-
#id : UInt32
The GPU texture handle, for custom rendering code.
-
#mipmaps? : Bool
True when mipmaps were generated.
-
#path : String | Nil
The file the texture came from, if any.
-
#rect : Rect
The whole texture as a
Rect. -
#region(x : Number, y : Number, w : Number, h : Number) : TextureRegion
A sub-rectangle of this texture that can be drawn like a texture.
-
#region(r : Rect) : TextureRegion
A sub-rectangle given as a
Rect. -
#size : Vec2
(width, height). -
#update(data : Bytes, x : Int32, y : Int32, w : Int32, h : Int32) : Nil
Replaces a rectangle of pixels with raw RGBA bytes.
-
#update(image : Image, x : Int32 = 0, y : Int32 = 0) : Nil
Replaces pixels starting at x, y with image.
-
#width : Int32
Width in pixels.
-
#wrap : GPU::Wrap
What happens outside 0..1 texture coordinates:
Clamp,RepeatorMirror. -
#wrap=(w : GPU::Wrap)
Changes the wrap mode.
Constructor Detail
A texture filled with a single color.
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.
Uploads an Image to the GPU.
Creates a blank texture of the given size, optionally filled with raw pixel data.
A shared 1x1 white texture. Shapes use it so they can batch with sprites.
Wraps an existing GPU texture id. Canvas uses this.
Class Method Detail
Filter used by new textures and canvases when none is given.
Sets the filter for new textures. Use GPU::Filter::Nearest for pixel-art games.
Instance Method Detail
Frees the GPU texture. Textures loaded through Assets are freed by Assets.clear instead.
How the texture is sampled when scaled: Nearest for crisp pixels, Linear for smooth.
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.
True when mipmaps were generated. They reduce shimmering on textures drawn much smaller than their size.
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)
Replaces a rectangle of pixels with raw RGBA bytes.
Replaces pixels starting at x, y with image. Use it for textures that change, like a minimap or a paint canvas.