From 92d63e8308fdf0d2a65c3a98a58f7d01616a94d2 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 12 Oct 2018 18:50:26 +0100 Subject: [PATCH] Trying to get smooth animation working but there's a null access pointer issue --- crumpet-engine/main.cpp | 33 +++++++++++++++++++++++++++++---- crumpet-engine/sprite.cpp | 6 ++++++ crumpet-engine/sprite.h | 3 ++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crumpet-engine/main.cpp b/crumpet-engine/main.cpp index e75671e..8406d19 100644 --- a/crumpet-engine/main.cpp +++ b/crumpet-engine/main.cpp @@ -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; } diff --git a/crumpet-engine/sprite.cpp b/crumpet-engine/sprite.cpp index 3f228a9..28b0c7b 100644 --- a/crumpet-engine/sprite.cpp +++ b/crumpet-engine/sprite.cpp @@ -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) { diff --git a/crumpet-engine/sprite.h b/crumpet-engine/sprite.h index bf221d4..4df3b2a 100644 --- a/crumpet-engine/sprite.h +++ b/crumpet-engine/sprite.h @@ -42,7 +42,8 @@ enum struct SpriteState { STATE_MISC2, STATE_MISC3, STATE_MISC4, - STATE_MISC5 + STATE_MISC5, + NUM_ITEMS }; class Sprite : public Entity {