OldSchool Library
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. Remember that the maximum size of an image is 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.

@param img
        Pointer to the image to be used as a sprite sheet.
@param width
        The width of each sprite frame in pixels.
@param height
        The 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:

\code
sprite 1 | sprite 2
---------|---------
sprite 3 | sprite 4

Coordinates:
0, 0     | 32, 0
---------|---------
0, 32    | 32, 32
\endcode

\b Important: The maximum size of an image is 512x512! Ensure that your sprite sheet dimensions do not exceed this limit.

\see 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.

@param img
        Pointer to the image to set the frame for.
@param frame
        The index of the frame to be displayed. Frames are indexed starting from 0.

\b Example:
\code
// Draws a specific frame of an image
void DrawImageFrame(OSL_IMAGE *img, int frame) {
        oslSetImageFrame(img, frame);
        oslDrawImage(img);
}

 // 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);
           oslDrawImage(img);
   }

   // 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);
           [...]
   }
   \endcode