From 675ce0039a76886ea8bb3c78203c711bc9c16bff Mon Sep 17 00:00:00 2001 From: Alfie King Date: Mon, 16 Jun 2025 03:12:28 +0100 Subject: [PATCH] stuff --- include/input.h | 22 ++++++++++++++++++++ include/sprite.h | 9 +++++--- src/engine.cpp | 9 +++++++- src/example-game.cpp | 49 ++++++++++++++++++++++++++------------------ src/input.cpp | 24 ++++++++++++++++++++++ src/sprite.cpp | 31 ++++++++++++++++++++++++++-- 6 files changed, 118 insertions(+), 26 deletions(-) diff --git a/include/input.h b/include/input.h index 7858f0c..cc37d74 100644 --- a/include/input.h +++ b/include/input.h @@ -1,6 +1,26 @@ #include #include #include +#include + +enum CBUTTONS +{ + DPAD_UP = SDL_CONTROLLER_BUTTON_DPAD_UP, + DPAD_DOWN = SDL_CONTROLLER_BUTTON_DPAD_DOWN, + DPAD_LEFT = SDL_CONTROLLER_BUTTON_DPAD_LEFT, + DPAD_RIGHT = SDL_CONTROLLER_BUTTON_DPAD_RIGHT, + A = SDL_CONTROLLER_BUTTON_A, + B = SDL_CONTROLLER_BUTTON_B, + X = SDL_CONTROLLER_BUTTON_X, + Y = SDL_CONTROLLER_BUTTON_Y, + BACK = SDL_CONTROLLER_BUTTON_BACK, + GUIDE = SDL_CONTROLLER_BUTTON_GUIDE, + START = SDL_CONTROLLER_BUTTON_START, + LEFTSTICK = SDL_CONTROLLER_BUTTON_LEFTSTICK, + RIGHTSTICK = SDL_CONTROLLER_BUTTON_RIGHTSTICK, + LEFTSHOULDER = SDL_CONTROLLER_BUTTON_LEFTSHOULDER, + RIGHTSHOULDER = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER +}; struct Controller { @@ -36,4 +56,6 @@ class Input Input(); void update(); + bool keysPressed(std::vector keys); + bool controllerPressed(std::vector buttons, int controller = 0); }; \ No newline at end of file diff --git a/include/sprite.h b/include/sprite.h index 11d9051..4b3474e 100644 --- a/include/sprite.h +++ b/include/sprite.h @@ -32,19 +32,20 @@ class Animation private: std::vector frames; Sprite* sprite; - int currentFrame = 0; int frameDuration = 0; int frameCounter = 0; std::string currentFrameName; - int frameIndex = 0; - int frameDirection = 1; public: + bool end = false; + int frameDirection = 1; + int frameIndex = 0; bool loop = true; bool pingpong = false; Animation(std::map frames, Sprite* sprite, int frameDuration); void update(); void draw(SDL_Renderer* renderer, bool autoRect = true, bool flip = false); + void reset(); }; class AnimationManager @@ -54,12 +55,14 @@ class AnimationManager std::map flip; std::map autoRect; std::string currentAnimation; + bool playing = false; Sprite* sprite; public: AnimationManager(Sprite* sprite); void addAnimation(std::string name, std::map animation, int frameDuration = 10, bool flip = false, bool autoRect = true, bool loop = true, bool pingpong = false); void setAnimation(std::string name); + void playAnimation(std::string name); void update(); void draw(SDL_Renderer* renderer); }; \ No newline at end of file diff --git a/src/engine.cpp b/src/engine.cpp index 4fbb0df..4fcb556 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -10,7 +10,14 @@ Engine::Engine(std::string title, int width, int height) SDL_Init(SDL_INIT_EVERYTHING); // Create window and renderer - window = SDL_CreateWindow(ctitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN); + window = SDL_CreateWindow( + ctitle, + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_SHOWN + ); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); } diff --git a/src/example-game.cpp b/src/example-game.cpp index 27fd56e..19fc64c 100644 --- a/src/example-game.cpp +++ b/src/example-game.cpp @@ -30,25 +30,30 @@ int main(int argc, char* argv[]) // frame map std::map idle = { - { "idle", { 76, 456, 68, 128 } }, + { "idle", { 76, 456, 68, 128 } }, }; std::map front = { - { "front", { 4, 456, 68, 125 } }, - { "front2", { 76, 456, 68, 128 } }, - { "front3", { 148, 456, 68, 125 } } + { "front", { 4, 456, 68, 125 } }, + { "front2", { 76, 456, 68, 128 } }, + { "front3", { 148, 456, 68, 125 } } }; std::map back = { - { "back", { 662, 456, 68, 125 } }, - { "back2", { 734, 456, 68, 128 } }, - { "back3", { 806, 456, 68, 125 } } + { "back", { 662, 456, 68, 125 } }, + { "back2", { 734, 456, 68, 128 } }, + { "back3", { 806, 456, 68, 125 } } }; std::map walk = { - { "walk", { 220, 456, 68, 124 } }, - { "walk2", { 292, 456, 73, 127 } }, - { "walk3", { 369, 456, 68, 124 } } + { "walk", { 220, 456, 68, 124 } }, + { "walk2", { 292, 456, 73, 127 } }, + { "walk3", { 369, 456, 68, 124 } } + }; + + std::map test = { + { "test", { 4, 2095, 68, 59 } }, + { "test2", { 76, 2095, 68, 59 } } }; // Create sprite and animation @@ -59,6 +64,7 @@ int main(int argc, char* argv[]) animations->addAnimation("walk_right", walk, 10, true, true, true, false); animations->addAnimation("walk_back", back, 10, false, true, true, false); animations->addAnimation("walk_front", front, 10, false, true, true, true); + animations->addAnimation("test", test, 20, false, true, false, false); animations->setAnimation("idle"); // Play music @@ -73,41 +79,44 @@ int main(int argc, char* argv[]) // Update input engine->update(); - // Exit if window is closed + // Exit if window s closed if (engine->input.exit || engine->input.activeKeys[SDLK_ESCAPE]) { quit(); } - if (engine->input.activeKeys[SDLK_d] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] || engine->input.controllers[0].leftStickX > 8000) + if (engine->input.activeKeys[SDLK_d] || engine->input.controllers[0].buttons[CBUTTONS::DPAD_RIGHT] || engine->input.controllers[0].leftStickX > 8000) { animations->setAnimation("walk_right"); sprite->move({ 1, 0 }); } - if (engine->input.activeKeys[SDLK_a] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] || engine->input.controllers[0].leftStickX < -8000) + if (engine->input.activeKeys[SDLK_a] || engine->input.controllers[0].buttons[CBUTTONS::DPAD_LEFT] || engine->input.controllers[0].leftStickX < -8000) { animations->setAnimation("walk_left"); sprite->move({ -1, 0 }); } - if (engine->input.activeKeys[SDLK_w] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] || engine->input.controllers[0].leftStickY < -8000) + if (engine->input.activeKeys[SDLK_w] || engine->input.controllers[0].buttons[CBUTTONS::DPAD_UP] || engine->input.controllers[0].leftStickY < -8000) { animations->setAnimation("walk_back"); sprite->move({ 0, -1 }); } - if (engine->input.activeKeys[SDLK_s] || engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] || engine->input.controllers[0].leftStickY > 8000) + if (engine->input.activeKeys[SDLK_s] || engine->input.controllers[0].buttons[CBUTTONS::DPAD_DOWN] || engine->input.controllers[0].leftStickY > 8000) { animations->setAnimation("walk_front"); sprite->move({ 0, 1 }); } - if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s] - && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_RIGHT] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_LEFT] - && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_UP] && !engine->input.controllers[0].buttons[SDL_CONTROLLER_BUTTON_DPAD_DOWN] - && engine->input.controllers[0].leftStickX == 0 && engine->input.controllers[0].leftStickY == 0) + if (engine->input.activeKeys[SDLK_SPACE] || engine->input.controllers[0].buttons[CBUTTONS::A]) + { + animations->playAnimation("test"); + } + if (!engine->input.keysPressed({ SDLK_w, SDLK_a, SDLK_s, SDLK_d }) && engine->input.controllers[0].leftStickX < 8000 && engine->input.controllers[0].leftStickX > -8000 && + engine->input.controllers[0].leftStickY < 8000 && engine->input.controllers[0].leftStickY > -8000 && + !engine->input.controllerPressed({ CBUTTONS::DPAD_UP, CBUTTONS::DPAD_DOWN, CBUTTONS::DPAD_LEFT, CBUTTONS::DPAD_RIGHT })) { animations->setAnimation("idle"); } - + // Update animation animations->update(); diff --git a/src/input.cpp b/src/input.cpp index 3bab838..662a97c 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -53,4 +53,28 @@ void Input::update() SDL_GetMouseState(&mouse.x, &mouse.y); mouse.wheel = event.wheel.y; } +} + +bool Input::keysPressed(std::vector keys) +{ + for (auto key : keys) + { + if (activeKeys[key]) + { + return true; + } + } + return false; +} + +bool Input::controllerPressed(std::vector buttons, int controller) +{ + for (auto button : buttons) + { + if (controllers[controller].buttons[button]) + { + return true; + } + } + return false; } \ No newline at end of file diff --git a/src/sprite.cpp b/src/sprite.cpp index d68b61f..2030fb1 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -70,9 +70,18 @@ Animation::Animation(std::map frames, Sprite* sprite, int currentFrameName = this->frames[0]; } +void Animation::reset() +{ + frameIndex = 0; + frameDirection = 1; + currentFrameName = frames[0]; + end = false; +} + void Animation::update() { frameCounter++; + end = false; if (frameCounter >= frameDuration) { frameCounter = 0; @@ -84,6 +93,7 @@ void Animation::update() if (!loop) { frameDirection = 0; + end = true; } else { frameDirection *= -1; } @@ -97,6 +107,7 @@ void Animation::update() if (!loop) { frameIndex = frames.size() - 1; + end = true; } else { frameIndex = 0; } @@ -126,18 +137,34 @@ void AnimationManager::addAnimation(std::string name, std::maploop = loop; animations[name]->pingpong = pingpong; } - void autorect(std::string frame); + void AnimationManager::setAnimation(std::string name) { - currentAnimation = name; + if (!playing) { + currentAnimation = name; + } } void AnimationManager::update() { animations[currentAnimation]->update(); + if (playing && animations[currentAnimation]->end) + { + playing = false; + } } void AnimationManager::draw(SDL_Renderer* renderer) { animations[currentAnimation]->draw(renderer, autoRect[currentAnimation], flip[currentAnimation]); +} + +void AnimationManager::playAnimation(std::string name) +{ + if (!playing) { + animations[name]->loop = false; + animations[name]->reset(); + setAnimation(name); + playing = true; + } } \ No newline at end of file