OldSchool Library 1.6.0
Loading...
Searching...
No Matches
Simili sprite system

Functions

static void oslSetImageFrameSize (OSL_IMAGE *img, u16 width, u16 height)
 
void oslSetImageFrame (OSL_IMAGE *img, int frame)
 

Detailed Description

A basic system allowing you to cut your images just as if they were sprites. Frames are drawn with oslDrawImage, which wraps past texel 511: keep sprite sheets within 512x512.

Function Documentation

◆ oslSetImageFrameSize()

static void oslSetImageFrameSize ( OSL_IMAGE *  img,
u16  width,
u16  height 
)
inlinestatic

Defines the size of a frame in an image, preparing it for use as a sprite sheet.

A sprite sheet is an image where multiple sprites (small images) are arranged in a grid. Each sprite in the sheet has the same dimensions, and the grid is typically filled from left to right and top to bottom.

Parameters
imgPointer to the image to be used as a sprite sheet.
widthThe width of each sprite frame in pixels.
heightThe height of each sprite frame in pixels.

For example, if you have a 64x64 image containing 32x32 sprites, the image will be divided into four sprites as follows:

sprite 1 | sprite 2
---------|---------
sprite 3 | sprite 4
Coordinates:
0, 0 | 32, 0
---------|---------
0, 32 | 32, 32

Important: Frames are drawn with oslDrawImage, which samples at most 512 texels per axis (column 512 shows column 0 again), so keep the sprite sheet within 512x512.

See also
oslCreateImage for considerations when creating images.

◆ oslSetImageFrame()

void oslSetImageFrame ( OSL_IMAGE *  img,
int  frame 
)
extern

Sets the current frame of an image for sprite-based rendering.

This function prepares the image to display a specific frame from a sprite sheet. A sprite sheet is an image where multiple sprites (small images) are arranged in a grid. This function is used to select the sprite frame that you want to draw.

Parameters
imgPointer to the image to set the frame for.
frameThe index of the frame to be displayed. Frames are indexed starting from 0.

Example:

// Draws a specific frame of an image
void DrawImageFrame(OSL_IMAGE *img, int frame) {
oslSetImageFrame(img, frame);
}
// Alternative method using a macro
\#define DrawImageFrame(img, frame) ({ oslSetImageFrame(img, frame); oslDrawImage(img); })
// Inline function for specific frame drawing
extern inline void DrawImageFrame(OSL_IMAGE *img, int frame) {
oslSetImageFrame(img, frame);
}
// Draws a specific frame of an image at a specified location
void DrawImageFrameXY(OSL_IMAGE *img, int x, int y, int frame) {
oslSetImageFrame(img, frame);
oslDrawImageXY(img, x, y);
}
// Load an image with specified frame size
OSL_IMAGE *LoadSpriteFilePNG(char *filename, int location, int pixelFormat, int frameWidth, int frameHeight) {
OSL_IMAGE *img = oslLoadImageFile(filename, location, pixelFormat);
if (img)
oslSetImageFrameSize(img, frameWidth, frameHeight);
return img;
}
void main() {
// Initialization
[...]
// 30x40 pixels per sprite
OSL_IMAGE *myImage;
myImage = LoadSpriteFilePNG("test.png", OSL_IN_RAM, OSL_PF_5551, 30, 40);
[...]
// Draw the frame n°2 (that is, the third sprite) at location (0, 0)
DrawImageFrameXY(myImage, 0, 0, 2);
[...]
}
#define oslDrawImageXY(img, px, py)
Draws an image at the specified position.
Definition drawing.h:1334
void oslDrawImage(OSL_IMAGE *img)
Draws an image at its current position.
static void oslSetImageFrameSize(OSL_IMAGE *img, u16 width, u16 height)
Definition drawing.h:2401
void oslSetImageFrame(OSL_IMAGE *img, int frame)
OSL_IMAGE * oslLoadImageFile(char *filename, int location, int pixelFormat)
Loads an image from a file, allowing selection of its location and pixel format.
@ OSL_IN_RAM
In RAM.
Definition drawing.h:976
@ OSL_PF_5551
15 bits, 5 bits per component, 1 alpha bit
Definition drawing.h:1009
Structure representing an image loaded in memory.
Definition drawing.h:929