Functions | |
void | oslSetImageFrameSize (OSL_IMAGE *img, u16 width, u16 height) |
void | oslSetImageFrame (OSL_IMAGE *img, int frame) |
void oslSetImageFrameSize | ( | OSL_IMAGE * | img, | |
u16 | width, | |||
u16 | height | |||
) | [inline] |
Defines the size of a frame in an image. This prepares the image for an use as a sprite sheet. In a sprite sheet, you have to arrange your sprites (small images) in a grid, whose dimensions are fixed. That is, if you decide for example that the sprite size is 29x37, EVERY sprite must have the same size, and be padded if it doesn't. Sprites are placed from left to right, and then on the next line from top to bottom (just like the text you are reading). For example, a 64x64 image containing 32x32 sprites (total: 4 sprites) would have the following disposition:
sprite 1 | sprite 2 ---------|--------- sprite 3 | sprite 4 That is, the following coordinates: 0, 0 | 32, 0 ---------|--------- 0, 32 | 32, 32
Important: The maximum size of an image is 512x512! See considerations with oslCreateImage.
void oslSetImageFrame | ( | OSL_IMAGE * | img, | |
int | frame | |||
) |
To make a smart use of this feature, remember that you can always code macros to simplify and wrap your code.
//Remember to never use a name beginning by oslSomething for your functions //because it may exist some day in OSLib, and it will break your 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 (won't work with Visual Studio, except if you remove the parenthesis, //but they are needed because of a bug with GCC) #define DrawImageFrame(img, frame) ({ oslSetImageFrame(img, frame); oslDrawImage(img); }) //If you really want to do an inline thing (the same way as a macro), use this instead: 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 (like a real sprite) void DrawImageFrameXY(OSL_IMAGE *img, int x, int y, int frame) { oslSetImageFrame(img, frame); oslDrawImageXY(img, x, y); } //Load an image specifying the frame size directly void LoadSpriteFilePNG(char *filename, int location, int pixelFormat, int frameWidth, int frameHeight) { OSL_IMAGE *img = oslLoadImageFile(filename, location, pixelFormat); if (img) oslSetImageFrameSize(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) (top-left corner of the screen). DrawImageFrameXY(myImage, 0, 0, 2); [...] }