#include <spriteloader.h>
Public Member Functions | |
SpriteLoader (const char *fname, const char *path="") | |
Loads a file specifying a sprite. | |
bool | Load (const char *fname, const char *path="") |
Attempts to parse a .spr file and load the specified data. | |
bool | Good () |
Tells if the load was successful. | |
Sprite * | GetSprite (Sprite *sprite=NULL) |
Creates or converts a sprite to configuration data. | |
int | YFrames () const |
Returns # of y frames. | |
int | XFrames () const |
Returns # of x frames. | |
Uint8 | R () const |
Returns red transparency color. | |
Uint8 | G () const |
Returns blue transparency color. | |
Uint8 | B () const |
Returns green transparency color. | |
bitmask ** | Bmask () const |
Returns bitmasks. | |
SDL_Surface * | GetSurface () const |
Returns the loaded image. | |
ColType | Layer () const |
Returns the parsed collision layer mask. | |
ColType | Index () const |
Returns the parsed collision index mask. | |
int | DrawLayer () const |
Returns drawing layer. | |
~SpriteLoader () | |
Destructor, frees all loaded data. | |
Protected Member Functions | |
bool | ProcessLine (const char *buf) |
Processes a single line from sprite-config file. | |
void | Reset () |
Frees all data and returns variables to blank state. | |
Protected Attributes | |
bitmask ** | bmask |
Collison bitmasks. | |
SDL_Surface * | image |
Image. | |
int | r |
Transparency color. | |
int | g |
Transparency color. | |
int | b |
Transparency color. | |
int | drawlayer |
Drawing layer. | |
Uint32 | colindex |
Collision index mask. Uint32 so sscanf can use it. | |
Uint32 | colmask |
Collision layer mask. Uint32 so sscanf can use it. | |
bool | good |
Status - true=successful load, false=error. | |
bool | loadbitmask |
true means create collision bitmask from image | |
char | imagename [300] |
String containing image file name. | |
int | xframes |
Integers specifying number of rows and columns in image. | |
int | yframes |
Integers specifying number of rows and columns in image. | |
Sfxp | framerate |
Fixed point integer specifying frame rate. |
I began to realize that, with images, frames, bitmasks, layers, collision masks, positions, etc. etc. etc. all in one sprite, that was a heck of a lot of data to squeeze through a constructor, and quite tricky to get all in one place at once..
so I broke the sprite's initialization procedure down into a whole bunch of Init_Something() functions, and created this class to handle the tricky task of getting all the data ready for the sprite itself.
It works by loading a sprite configuration file, which contains information on drawing layers, animation frames, collision masks and bitmasks, image names, and so forth. The file is parsed, the data loaded, and once all that is done you can create as many identical sprites as you want from it. This also allows you to create dozens of sprites of whatever kind you want, sharing the exact same data, cutting down on data duplication quite a bit.
The config files are 100% pure ASCII, so notepad users rejoice. Here is the syntax of a .spr config file:
# This is a comment line IMAGE "faces.png FRAMES 2 2 FRAMERATE 10 DRAWLAYER 1 # Collision layer 0, can collide with layer 0 COLLAYER 0 0 BITMASK TRANSCOLOR 255 0 255
I think we can all guess what IMAGE does. Frames tells it how many X and Y frames there are in the image(assumes 1*1 if not present). This is what it the frame order in a 2*2 image would be. +---+---+ The next one, FRAMERATE, also does the obvious, except | 0 | 1 | it's a fixed-point number, not a straight integer; in +---+---+ it's current incarnation with 10 fractional bits, that | 2 | 3 | number means it advances 10/1024 frames per millisecond, +---+---+ or approximately 10 frames per second.
DRAWLAYER is simply the sprite's position in the drawing order. Lower layers get drawn last.
COLLAYER is a bit tricky, but makes sense if you think about it
- it inhabits collision layer 0, and can collide with layer 0. Ergo it can collide with identical sprites. You can specify as many layers to collide with as you want(like COLLAYER 0 0 1 2 3 4 ) but a sprite can only INHABIT one layer.
TRANSCOLOR is used to specify the image's color key, i.e. the precise RGB color that should be considered transparent. It's a good idea to make your images with some insane color as the colorkey, like pure magenta(RGB255,0,255); it helps hunt down stubborn supposed-to-be-transparent pixels better than dark colors, or even black.
BITMASK is a paramaterless option that tells SpriteLoader to convert the image frames into collision bitmasks, used for pixel-level collision detection. By colorkey, transparent pixels are assumed to be empty space and opaque ones solid.
Definition at line 114 of file spriteloader.h.
Constructor & Destructor Documentation
|
Attempts to load and parse an entire .spr file upon creation. Definition at line 194 of file spriteloader.cpp. References b, bmask, colindex, colmask, drawlayer, framerate, g, good, image, imagename, Load(), loadbitmask, r, xframes, and yframes. |
Here is the call graph for this function:
|
Destructor, frees all data. Definition at line 221 of file spriteloader.cpp. References Reset(). |
Here is the call graph for this function:
|
Uses the information from a parsed .spr file to convert or create a new sprite to it's specifications. Definition at line 239 of file spriteloader.cpp. References bmask, colindex, colmask, drawlayer, framerate, Good(), image, Sprite::Init_Bitmask(), Sprite::Init_Collision(), Sprite::Init_Frames(), Sprite::Init_Layers(), Sprite::Init_Surface(), loadbitmask, Sprite::SetFlags(), Sprite::SetFrameRate(), SPRITE_DELETEABLE, xframes, and yframes. Referenced by main(). |
Here is the call graph for this function:
|
Current status of spriteloader. Returns true if the last load succeeded, false if not. Definition at line 230 of file spriteloader.cpp. References good. Referenced by GetSprite(), and main(). |
|
Attempts to load and parse an entire .spr file and specified contents. Definition at line 283 of file spriteloader.cpp. References b, bmask, g, good, image, imagename, isblankstr(), loadbitmask, LoadConvertBMP(), MakeBitMask(), ProcessLine(), r, Reset(), SprMakePath(), xframes, and yframes. Referenced by SpriteLoader(). |
Here is the call graph for this function:
|
Processes one line from a .spr configuration file, and puts valid parameters into sprite description information to be loaded later. Definition at line 91 of file spriteloader.cpp. References b, CheckLineSyntax(), COL_LAYER, colindex, colmask, drawlayer, framerate, g, imagename, isblankstr(), LineToTypes(), loadbitmask, r, TokenType::subtype, TokenType::token, TokenType::type, TYPE_ENDCLOSE, TYPE_ENDOPEN, Types, xframes, and yframes. Referenced by Load(). |
Here is the call graph for this function:
|
Resets all sprite specification data. Definition at line 366 of file spriteloader.cpp. References b, bmask, colindex, colmask, drawlayer, framerate, g, good, image, imagename, loadbitmask, r, xframes, and yframes. Referenced by Load(), and ~SpriteLoader(). |