I was Testing Dynamically Allocating Sprites During the Main Loop to see if it was possible. I built this class which I"m currently improving that handles all my sprite allocation and deallocation during runtime.
#ifndef SPRITECONTROLLER_H_
#define SPRITECONTROLLER_H_
#include "DarkGDK.h"
//
// Need to Make Sprite Controller Smart enough to
// Recognize the Same Filename and reuse the already
// Loaded Image
//
// TACOS!
class Sprite_Controller {
public:
// Keep Track of Currently Allocated Sprites and Images
// In Memory
unsigned int total_sprites;
unsigned int total_images;
Sprite_Controller() {
total_sprites = 0;
total_images = 0;
}
// Returns Pointer to this Object
Sprite_Controller* get_ptr() {
return this;
}
// Encapsulates dbSprite command to use the current Image
// of SpriteID// This Function is for simple movement of the
// Sprite around the Screen
void MoveSprite(int SpriteID, int iX, int iY) {
dbSprite(SpriteID, iX, iY, dbSpriteImage(SpriteID));
}
// Functions Loads a Image and Makes a Static Sprite
// Returns the Sprite's Number
// Sets the Coordinates of New Sprite to iX, iY
// No need to Return the Image number as it can be
// Accessed with the sprite number Handle
unsigned int Sprite(char* filename, int iX, int iY) {
// Load Image
dbLoadImage(filename, ++total_images);
// Create Sprite
dbSprite(++total_sprites, iX, iY, total_images);
return total_sprites;
}
// Function Creates a Static Sprite with a Previously Loaded Image
unsigned int Sprite(int Image_number, int iX, int iY) {
dbSprite(++total_sprites, iX, iY, Image_number);
return total_sprites;
}
// Function Loads a Image, Creates Animated Sprite from Image and Sets the
// Coordinates of the Sprite
// Returns the Sprites Number Handle
unsigned int AnimSprite(char* filename, int hor_frames, int vert_frames, int iX, int iY) {
// Combo Func, loads image and creates the Sprite
dbCreateAnimatedSprite(++total_sprites, filename, hor_frames, vert_frames, ++total_images);
// Sets Sprite Location
// total_sprites, total_images already incremented ^up^ in last func call
dbSprite(total_sprites, iX, iY, total_images);
return total_sprites;
}
unsigned int AnimSpriteMultiple(char* filename, int hor_frames, int vert_frames, int iX, int iY, int Image_Handle) {
// uses the Previously loaded Image as the basis for Sprite
dbCreateAnimatedSprite(++total_sprites, filename, hor_frames, vert_frames, Image_Handle);
// Set Sprite Location
// total_sprites already incremented ^up^ in last func call
dbSprite(total_sprites, iX, iY, Image_Handle);
return total_sprites;
}
// Delete all Sprites Currently Allocated
// Delete all Images Currently Allocated
void ClearMemory() {
if(total_sprites != 0)
{ // Clear Sprites From Memory
for(int i = 1; i <=total_sprites; i++)
{ dbHideSprite(i);
dbDeleteSprite(i);
}
// Set Counter to 0
total_sprites = 0;
}
if(total_images != 0)
{ // Clear Images From Memory
for(int i = 1; i <=total_images; i++)
dbDeleteImage(i);
// Set Counter to 0
total_images = 0;
}
}
~Sprite_Controller() {
ClearMemory();
}
};
The Code from the first post was testing to make sure that the following bit of code performed correctly during the Main loop
if(dbDownKey())
// When this is called
// k = 321 also happens
{ dbCreateAnimatedSprite(++k, "Sprite.bmp", 4, 4, 1);
dbSprite(1, 10, 10, 1);
dbPlaySprite(1,1,16,60);
}
I removed the k incrementation and the variable was still displaying 321 when the code ran.
Taco Justice