Skip to content

Commit

Permalink
mouse functions and new animation loader
Browse files Browse the repository at this point in the history
mouse functions:
* getRel
* lock
* unlock
* isLocked
* hide
* show
* isHidden

animation loader functions
* addAnim -> loadSpriteSheet
* loadFolder
  • Loading branch information
durkisneer1 committed Dec 30, 2024
1 parent fca16ef commit b86dce7
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 13 deletions.
4 changes: 3 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ int main()
const double dt = clock.tick() / 1000.0;

while (kn::window::pollEvent(event))
if (event.type == kn::KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
{
if (event.type == kn::KEYDOWN && event.key.keysym.sym == kn::K_ESCAPE)
kn::window::close();
}

kn::window::clear(bgColor);

Expand Down
5 changes: 2 additions & 3 deletions example/src/player.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "Player.hpp"
#include <algorithm>

#define AxisX 0
#define AxisY 1

Player::Player(const kn::TileMap& tileMap)
: rect(48, 120, 13, 16), collisionLayer(tileMap.getLayer("Wall"))
{
animController.addAnim("idle", "../example/assets/player_idle.png", {13, 16}, 5);
animController.addAnim("walk", "../example/assets/player_walk.png", {13, 16}, 5);
animController.loadSpriteSheet("idle", "../example/assets/player_idle.png", {13, 16}, 5);
animController.loadSpriteSheet("walk", "../example/assets/player_walk.png", {13, 16}, 5);

interactables = tileMap.getTileCollection({"Mirror", "Bed", "Desk"});
}
Expand Down
20 changes: 14 additions & 6 deletions include/AnimationController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ struct Animation
class AnimationController final
{
public:
/**
* @brief Construct a new Animation Controller object.
*/
AnimationController() = default;
~AnimationController() = default;

/**
* @brief Set up the animation controller.
* @brief Load a sprite sheet image file as an animation.
*
* @param name The name of the animation.
* @param filePath The path to the sprite sheet image file.
Expand All @@ -49,9 +46,20 @@ class AnimationController final
*
* @return ``true`` if the setup was successful, ``false`` otherwise.
*/
[[maybe_unused]] bool addAnim(const std::string& name, const std::string& filePath,
[[maybe_unused]] bool loadSpriteSheet(const std::string& name, const std::string& filePath,
const math::Vec2& frameSize, int fps);

/**
* @brief Load all images in a directory as an animation.
*
* @param name The name of the animation.
* @param dirPath The path to the directory containing the images.
* @param fps The frame rate of the animation.
*
* @return ``true`` if the setup was successful, ``false`` otherwise.
**/
[[maybe_unused]] bool loadFolder(const std::string& name, const std::string& dirPath, int fps);

/**
* @brief Remove an animation from the controller.
*
Expand Down Expand Up @@ -112,7 +120,7 @@ class AnimationController final
/**
* @brief Resume the animation.
*
* @note Will not do anything if fps is set to 0.
* @note Will not do anything if fps is set to ``0``.
*/
void resume();

Expand Down
42 changes: 42 additions & 0 deletions include/Mouse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,54 @@ namespace mouse
*/
math::Vec2 getPos();

/**
* @brief Get the mouse offset since the last frame.
*
* @return A vector representing the mouse offset since the last frame.
*/
math::Vec2 getRel();

/**
* @brief Get all the mouse buttons that are pressed.
*
* @return A bitfield of all the mouse buttons that are pressed.
*/
uint32_t getPressed();

/**
* @brief Lock the cursor to the center of the window and hide it.
* Commonly used when you want to solely focus on relative mouse movement.
*/
void lock();

/**
* @brief Unlock the cursor and show it.
*/
void unlock();

/**
* @brief Check if the cursor is locked.
*
* @return ``true`` if locked, ``false`` otherwise.
*/
bool isLocked();

/**
* @brief Hide the cursor.
*/
void hide();

/**
* @brief Show the cursor.
*/
void show();

/**
* @brief Check if the cursor is hidden.
*
* @return ``true`` if hidden, ``false`` otherwise.
*/
bool isHidden();

} // namespace mouse
} // namespace kn
39 changes: 36 additions & 3 deletions src/animation_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
#include "ErrorLogger.hpp"
#include "Math.hpp"

#include <filesystem>
namespace fs = std::filesystem;

namespace kn
{

bool AnimationController::addAnim(const std::string& name, const std::string& filePath,
bool AnimationController::loadSpriteSheet(const std::string& name, const std::string& filePath,
const math::Vec2& frameSize, const int fps)
{
const std::shared_ptr<Texture> texPtr(new Texture());
Expand All @@ -27,8 +30,8 @@ bool AnimationController::addAnim(const std::string& name, const std::string& fi

Animation newAnim;
newAnim.fps = fps;
for (int x = 0; x < size.x; x += frameWidth)
for (int y = 0; y < size.y; y += frameHeight)
for (int y = 0; y < size.y; y += frameHeight)
for (int x = 0; x < size.x; x += frameWidth)
{
const Frame frame{texPtr, {x, y, frameWidth, frameHeight}};
newAnim.frames.emplace_back(frame);
Expand All @@ -40,6 +43,36 @@ bool AnimationController::addAnim(const std::string& name, const std::string& fi
return true;
}

bool AnimationController::loadFolder(const std::string& name, const std::string& dirPath, const int fps)
{
if (m_animMap.find(name) != m_animMap.end())
m_animMap.erase(name);

Animation newAnim;
newAnim.fps = fps;

for (const auto& entry : fs::directory_iterator(dirPath))
{
const std::string filePath = entry.path().string();

const std::shared_ptr<Texture> texPtr(new Texture());
if (!texPtr->loadFromFile(filePath))
continue;

const Frame frame{texPtr, texPtr->getRect()};
newAnim.frames.emplace_back(frame);
}

if (newAnim.frames.empty())
return false;

m_animMap[name] = std::move(newAnim);
m_currAnim = name;

return true;
}


void AnimationController::removeAnim(const std::string& name)
{
if (m_animMap.find(name) != m_animMap.end())
Expand Down
39 changes: 39 additions & 0 deletions src/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ math::Vec2 getPos()
return pos / window::getScale() + camera;
}

math::Vec2 getRel()
{
int dx, dy;
SDL_GetRelativeMouseState(&dx, &dy);
const math::Vec2 rel{dx, dy};
return rel / window::getScale();
}

uint32_t getPressed() { return SDL_GetMouseState(nullptr, nullptr); }

void lock()
{
SDL_SetRelativeMouseMode(SDL_TRUE);
}

void unlock()
{
SDL_SetRelativeMouseMode(SDL_FALSE);
}

bool isLocked()
{
return SDL_GetRelativeMouseMode();
}

void hide()
{
SDL_ShowCursor(SDL_DISABLE);
}

void show()
{
SDL_ShowCursor(SDL_ENABLE);
}

bool isHidden()
{
return !SDL_ShowCursor(SDL_QUERY);
}


} // namespace kn::mouse

0 comments on commit b86dce7

Please sign in to comment.