This commit is contained in:
Alfie King 2025-06-16 03:12:28 +01:00
parent fa76489449
commit 675ce0039a
6 changed files with 118 additions and 26 deletions

View File

@ -1,6 +1,26 @@
#include <SDL.h>
#include <map>
#include <string>
#include <vector>
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<SDL_Keycode> keys);
bool controllerPressed(std::vector<Uint8> buttons, int controller = 0);
};

View File

@ -32,19 +32,20 @@ class Animation
private:
std::vector<std::string> 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<std::string, SDL_Rect> 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<std::string, bool> flip;
std::map<std::string, bool> autoRect;
std::string currentAnimation;
bool playing = false;
Sprite* sprite;
public:
AnimationManager(Sprite* sprite);
void addAnimation(std::string name, std::map<std::string, SDL_Rect> 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);
};

View File

@ -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);
}

View File

@ -30,25 +30,30 @@ int main(int argc, char* argv[])
// frame map
std::map<std::string, SDL_Rect> idle = {
{ "idle", { 76, 456, 68, 128 } },
{ "idle", { 76, 456, 68, 128 } },
};
std::map<std::string, SDL_Rect> 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<std::string, SDL_Rect> 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<std::string, SDL_Rect> 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<std::string, SDL_Rect> 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();

View File

@ -53,4 +53,28 @@ void Input::update()
SDL_GetMouseState(&mouse.x, &mouse.y);
mouse.wheel = event.wheel.y;
}
}
bool Input::keysPressed(std::vector<SDL_Keycode> keys)
{
for (auto key : keys)
{
if (activeKeys[key])
{
return true;
}
}
return false;
}
bool Input::controllerPressed(std::vector<Uint8> buttons, int controller)
{
for (auto button : buttons)
{
if (controllers[controller].buttons[button])
{
return true;
}
}
return false;
}

View File

@ -70,9 +70,18 @@ Animation::Animation(std::map<std::string, SDL_Rect> 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::map<std::string, SDL_
animations[name]->loop = 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;
}
}