Sprites are 2D images that represent game objects like characters, items, and environment elements. Talon provides Raylib’s powerful texture system for working with sprites.
Loading Textures
Textures must be loaded before they can be drawn. Talon provides two main ways to work with textures:
Static Method (Simple)
Use Raylib.loadTexture() for simple texture loading:
import "raylib" for Raylib
var texture = Raylib.loadTexture("path/to/image.png")
Texture2D Class (Advanced)
Use the Texture2D class for more control:
import "raylib" for Texture2D
var texture = Texture2D.loadTexture("res/sprite.png")
Both methods work identically. The Texture2D class provides a more object-oriented approach and is used in the official examples.
Drawing Textures
Once loaded, textures can be drawn using various methods depending on your needs.
Basic Drawing
Draw a texture at a specific position:
Raylib.drawTexture(texture, x, y, Color.White)
Drawing with Vectors
Use Vector2 for position:
import "raylib" for Vector2
var position = Vector2.new(100, 100)
Raylib.drawTextureV(texture, position, Color.White)
Advanced Drawing with drawTexturePro
For precise control over source and destination rectangles, scaling, rotation, and tinting:
import "raylib" for Raylib, Rectangle, Vector2, Color
var texture = Texture2D.loadTexture("res/paddle.png")
// Define source rectangle (which part of texture to draw)
var sourceRect = Rectangle.new(0.0, 0.0, 64.0, 16.0)
// Define destination rectangle (where and how big to draw)
var destRect = Rectangle.new(100.0, 100.0, 128.0, 32.0)
// Origin point for rotation
var origin = Vector2.new(0.0, 0.0)
// Draw the texture
Raylib.drawTexturePro(
texture,
sourceRect,
destRect,
origin,
0.0, // rotation in degrees
Color.new(255, 255, 255, 255) // tint color
)
drawTexturePro is powerful for sprite sheets, scaling sprites to different sizes, and rotating sprites.
Real-World Example: Breakout Game
Here’s how the Breakout example loads and draws multiple textures:
Load textures during initialization
class Game {
construct new() {
_brick_texture = Texture2D.loadTexture("res/brick.png")
_ball_texture = Texture2D.loadTexture("res/tennis.png")
_paddle_texture = Texture2D.loadTexture("res/paddle.png")
// ... other initialization
}
}
Create drawable game objects
class Paddle {
construct new() {
_rec = Rectangle.new(
SCREEN_WIDTH / 2.0 - PADDLE_W / 2.0,
SCREEN_HEIGHT - PADDLE_H - 20.0,
PADDLE_W,
PADDLE_H
)
}
rec { _rec }
draw(texture) {
var sourceRect = Rectangle.new(0.0, 0.0, 64.0, 16.0)
Raylib.drawTexturePro(
texture,
sourceRect,
_rec,
Vector2.new(0.0, 0.0),
0.0,
Color.new(255, 255, 255, 255)
)
}
}
Draw in the game loop
class Game {
draw() {
Raylib.beginDrawing()
Raylib.clearBackground(Color.new(0, 0, 0, 255))
_paddle.draw(_paddle_texture)
_ball.draw(_ball_texture)
for (brick in _bricks) {
brick.draw(_brick_texture)
}
Raylib.endDrawing()
}
}
Raylib (and thus Talon) supports common image formats:
PNG (recommended for sprites with transparency)
JPG/JPEG
BMP
TGA
GIF
Use PNG format for game sprites as it supports transparency (alpha channel).
Texture Properties
Textures loaded with Texture2D provide useful properties:
var texture = Texture2D.loadTexture("sprite.png")
// Access texture dimensions
var width = texture.width
var height = texture.height
Unloading Textures
Always unload textures when you’re done to free memory:
// Using Texture2D class
texture.unloadTexture()
// Or using Raylib static method
Raylib.unloadTexture(texture)
Unload textures when changing levels, closing the game, or when textures are no longer needed.
Complete Sprite Example
Simple Sprite
Animated Sprite
import "raylib" for Raylib, Texture2D, Color
var screenWidth = 800
var screenHeight = 450
Raylib.initWindow(screenWidth, screenHeight, "Sprite Example")
var playerTexture = Texture2D.loadTexture("player.png")
var x = 100
var y = 100
Raylib.setTargetFPS(60)
while (!Raylib.windowShouldClose()) {
Raylib.beginDrawing()
Raylib.clearBackground(Color.RayWhite)
// Draw the sprite
Raylib.drawTexture(playerTexture, x, y, Color.White)
Raylib.endDrawing()
}
playerTexture.unloadTexture()
Raylib.closeWindow()
Next Steps