Alright it's been a while since I've programmed in C++ so this could be a simple fix but I'm just not seeing it.
I'm using Allegro 5, I have a main.cpp, a character.h, and a character.cpp
I have some test code in my main.cpp that works as it should:
ALLEGRO_BITMAP *characterSpriteSheet = al_load_bitmap("CharacterSpriteSheet.png");
characterAnimation walkingDown(6, 32, 32, 0, characterSpriteSheet);
std::vector <ALLEGRO_BITMAP*> testvector(1);
testvector[0] = al_create_sub_bitmap(characterSpriteSheet, 0, 0, 32, 32);
and then in the main game loop:
al_draw_bitmap(al_create_sub_bitmap(testvector[0], 0, 0, 32, 32), 0, 0, 0); al_flip_display(); //Blit to screen
al_create_sub_bitmap creates an ALLEGRO_BITMAP* object that shares the same memory but with different size / clipping.
my character.h looks like this:
#ifndef CHARACTER_H
#define CHARACTER_H
#include "allegro5/allegro.h"
#include <vector>
#include <string>
class characterAnimation
{
public:
characterAnimation(short int numOfSteps, short int height, short int width, short int row, ALLEGRO_BITMAP *spriteSheet);
ALLEGRO_BITMAP *getNextFrame(void);
private:
short int numberOfSteps;
short int currentStep;
short int imageHeight;
short int imageWidth;
short int row;
std::vector <ALLEGRO_BITMAP*> images;
};
#endif
my character.cpp looks like this:
#include "character.h"
characterAnimation::characterAnimation(short int numOfSteps, short int height, short int width, short int animationRow, ALLEGRO_BITMAP *spriteSheet)
{
numberOfSteps = (numOfSteps - 1);
currentStep = 0;
imageHeight = height;
imageWidth = width;
row = animationRow;
std::vector <ALLEGRO_BITMAP*> images(numberOfSteps);
for(int x = 0; x <= numberOfSteps; x++)
{
images[x] = al_create_sub_bitmap(spriteSheet, (imageWidth * x), (imageHeight * row), imageWidth, imageHeight);
}
}
the characterAnimation::getNextFrame(void) function is currently setup just to return the first element (0) of the vector to make things easier right now (since it's not working)
The problem seems to be in characterAnimations constructor - I'm thinking that the al_create_sub_bitmap() created pointers goes out of scope and gets destroyed possibly at the end of the constructor? resulting in a bunch of pointers that don't go anywhere sensible. Which would explain why my program crashes when I try to call al_draw_bitmap(images[x]). My question is, is that what's happening?
Currently my program crashes only when I try to draw an element from characterAnimation::images to the screen, For some reason these pointers aren't getting set, or are being destroyed, I have no idea why though, so any help would be awesome.






