Trying to get smooth animation working but there's a null access pointer issue

This commit is contained in:
Ben
2018-10-12 18:50:26 +01:00
parent d2c53faa7d
commit 92d63e8308
3 changed files with 37 additions and 5 deletions

View File

@@ -20,10 +20,14 @@ int main(int argc, char** argv) {
woman.ResizeSpriteStateByFactor(SpriteState::STATE_ACCELERATING, 4);
woman.UseSpriteSheet(SpriteState::STATE_RUNNING, 0, 321, 77, 105, 3, 10);
woman.ResizeSpriteStateByFactor(SpriteState::STATE_RUNNING, 4);
woman.SetSpriteState(SpriteState::STATE_RUNNING);
woman.UseSpriteSheet(SpriteState::STATE_TURNING, 3, 430, 76, 103, 3, 4);
woman.ResizeSpriteStateByFactor(SpriteState::STATE_TURNING, 4);
woman.UseSpriteSheet(SpriteState::STATE_DECELERATING, 12, 534, 60, 105, 27, 2);
woman.ResizeSpriteStateByFactor(SpriteState::STATE_DECELERATING, 4);
int i = 0;
int j = 0;
SpriteState lastState = SpriteState::STATE_DEFAULT;
int runningDirection = 0; // 0 = standing, 1 = right, 2 = left
while (!game.renderer->IsDisplayClosed()) {
game.PollEvents();
@@ -48,7 +52,7 @@ int main(int argc, char** argv) {
std::cout << camera.GetRect().ToString() << std::endl;
}
if (timer.ticks % 5 == 0) {
if (timer.ticks % 10 == 0) {
i++;
// Slower animation speed for standing than everything else
if (woman.Spritestate == SpriteState::STATE_STANDING) {
@@ -58,8 +62,29 @@ int main(int argc, char** argv) {
} else {
woman.TickAninmation();
}
if (state[SDL_SCANCODE_D]) {
woman.SetSpriteState(SpriteState::STATE_ACCELERATING);
j++;
if (woman.Spritestate == SpriteState::STATE_DECELERATING) {
woman.SetSpriteState(SpriteState::STATE_RUNNING);
}
if (j > 3) {
woman.SetSpriteState(SpriteState::STATE_RUNNING);
}
}
if (!state[SDL_SCANCODE_D]) {
if (j > 0 && woman.Spritestate == SpriteState::STATE_DECELERATING) {
j--;
woman.SetSpriteState(SpriteState::STATE_DECELERATING);
} else {
woman.SetSpriteState(SpriteState::STATE_STANDING);
}
}
// After inital loading sprite, switches to silouette
if (i > 14 && woman.Spritestate == SpriteState::STATE_DEFAULT) {
if (i > 14 && lastState == SpriteState::STATE_DEFAULT) {
woman.SetSpriteState(SpriteState::STATE_STANDING);
i = 0;
}

View File

@@ -5,6 +5,12 @@ Sprite::Sprite(std::string name, Renderer* renderer, SpriteType mode)
, Pos(new Vec2(0, 0)) {
this->Spritetype = mode;
int length = (int)SpriteState::NUM_ITEMS;
for (unsigned int i = 0; i < length; i++) {
m_spriteMaps[SpriteState(i)][0] = new Rect(0, 0, 0, 0);
m_spriteSize[SpriteState(i)] = new Vec2(0, 0);
}
}
bool Sprite::LoadSpriteTextures(std::string path) {

View File

@@ -42,7 +42,8 @@ enum struct SpriteState {
STATE_MISC2,
STATE_MISC3,
STATE_MISC4,
STATE_MISC5
STATE_MISC5,
NUM_ITEMS
};
class Sprite : public Entity {