Skip to content

Commit

Permalink
0.0.3dev (#28)
Browse files Browse the repository at this point in the history
* fix draw rect and update animation controller

* implement icon setter

* draw line, point, and circle

Add default window icon and fix mouse position when camera vector is not zero.

* remove big icon
  • Loading branch information
durkisneer1 authored Nov 16, 2024
1 parent dac1523 commit 85a3327
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ subprojects/**/
*.gz
*.tar
.vs/
.cache/

# Wrap files that meson is scared of for some reason
subprojects/libpng.wrap
subprojects/zlib.wrap
subprojects/zlib.wrap
Binary file added docs/_static/kraken_engine_window_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 1 addition & 10 deletions example/include/Player.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#pragma once

#include <KrakenEngine.hpp>
#include <unordered_map>

enum AnimState
{
IDLE,
WALK
};

class Player final
{
Expand All @@ -22,12 +15,10 @@ class Player final

kn::AnimationController animController;
bool facingRight = true;
AnimState animState = IDLE;
std::unordered_map<AnimState, kn::Texture> animMap;

kn::Rect rect;
kn::math::Vec2 velocity;
const kn::Layer* collisionLayer;
bool onGround = false;
float moveSpeed = 45.0f;
};
};
7 changes: 3 additions & 4 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <KrakenEngine.hpp>
#include <vector>

#include "include/Player.hpp"

int main()
{
kn::window::init({320, 180}, "Kraken", 4);
kn::window::init({320, 180}, "Night Terror", 4);
kn::time::Clock clock;
kn::camera = {-32, -26};

Expand All @@ -22,11 +21,11 @@ int main()
for (const auto& event : kn::window::getEvents())
{
if (event.type == kn::QUIT ||
event.type == kn::KEYDOWN && event.key.keysym.sym == kn::K_ESCAPE)
(event.type == kn::KEYDOWN && event.key.keysym.sym == kn::K_ESCAPE))
done = true;
}

kn::window::clear();
kn::window::clear({21, 18, 37});
tileMap.drawLayer("Background");
tileMap.drawLayer("Wall");

Expand Down
20 changes: 7 additions & 13 deletions example/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@
Player::Player(const kn::Layer* collisionLayer)
: animController(5), rect(48, 120, 13, 16), collisionLayer(collisionLayer)
{
animMap[IDLE] = kn::Texture();
animMap[WALK] = kn::Texture();

if (!animMap.at(IDLE).loadFromFile("../example/assets/player_idle.png") ||
!animMap.at(WALK).loadFromFile("../example/assets/player_walk.png"))
ERROR("Failed to load texture");

if (!animController.setup(animMap.at(IDLE).getSize(), 13, 16))
ERROR("Failed to set up animation");
animController.addAnim("../example/assets/player_idle.png", "idle", 13, 16);
animController.addAnim("../example/assets/player_walk.png", "walk", 13, 16);
}

void Player::update(const double dt)
Expand All @@ -34,15 +27,15 @@ void Player::update(const double dt)
const auto dirVec = kn::input::getVector({kn::S_a}, {kn::S_d});
if (dirVec.x != 0.0)
{
animState = WALK;
animController.setAnim("walk");

if (dirVec.x > 0.0)
facingRight = true;
else if (dirVec.x < 0.0)
facingRight = false;
}
else
animState = IDLE;
animController.setAnim("idle");

velocity.x = dirVec.x * moveSpeed;

Expand All @@ -51,10 +44,11 @@ void Player::update(const double dt)
rect.y += static_cast<float>(velocity.y * dt);
handleCollision(AxisY);

const kn::Frame frame = animController.nextFrame(dt);
if (facingRight)
kn::window::blit(animMap.at(animState), rect, animController.update(dt));
kn::window::blit(*frame.tex, rect, frame.rect);
else
kn::window::blitEx(animMap.at(animState), rect, animController.update(dt), 0, true);
kn::window::blitEx(*frame.tex, rect, frame.rect, 0, true);
}

void Player::handleCollision(const int axis)
Expand Down
39 changes: 32 additions & 7 deletions include/AnimationController.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
#pragma once

#include "Rect.hpp"
#include "Texture.hpp"
#include <memory>
#include <unordered_map>
#include <vector>

namespace kn
{

/**
* @brief Container for a texture pointer and rect
*/
struct Frame
{
std::shared_ptr<Texture> tex;
Rect rect;
};

/**
* @brief Handler for sprite sheet animations.
*/
Expand All @@ -22,22 +35,33 @@ class AnimationController final
/**
* @brief Set up the animation controller.
*
* @param size The size of the sprite sheet.
* @param filePath The path to the sprite sheet image file.
* @param name The name of the animation.
* @param frameWidth The width of each frame.
* @param frameHeight The height of each frame.
*
* @return true if the setup was successful, false otherwise.
*/
[[maybe_unused]] bool setup(const math::Vec2& size, int frameWidth, int frameHeight);
[[maybe_unused]] bool addAnim(const std::string& filePath, const std::string& name,
int frameWidth, int frameHeight);

/**
* @brief Change the active animation.
*
* @param name The name of an added animation.
*
* @return true if the animation was successfully changed, false otherwise.
*/
[[maybe_unused]] bool setAnim(const std::string& name);

/**
* @brief Update the animation.
* @brief Get the next frame of the animation.
*
* @param deltaTime The time since the last frame.
* @param deltaTime The time since the last time this function was called.
*
* @return The srcRect area of the current frame. Use when blitting.
* @return The next frame's texture and area to render from.
*/
[[nodiscard]] const Rect& update(double deltaTime);
[[nodiscard]] const Frame& nextFrame(double deltaTime);

/**
* @brief Set the frame rate of the animation.
Expand All @@ -64,10 +88,11 @@ class AnimationController final
int m_fps;
int m_index = 0;
bool m_paused = false;
std::string m_currAnim;

double m_frameTime_ms;
double m_timeLeftInFrame_ms = 0.0;

std::vector<Rect> m_frameRects;
std::unordered_map<std::string, std::vector<Frame>> m_animMap;
};
} // namespace kn
38 changes: 37 additions & 1 deletion include/Draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace kn
{
struct Rect;
namespace math
{
struct Vec2;
}

namespace draw
{
Expand All @@ -17,6 +21,38 @@ namespace draw
*
* @note If thickness remains 0, the rectangle will be filled.
*/
void rect(Rect& rect, const Color& color, float thickness = 0.0f);
void rect(const Rect& rect, const Color& color, int thickness = 0);

/**
* @brief Draw a line.
*
* @param start The starting point of the line.
* @param end The ending point of the line.
* @param color The color of the line.
*
* @note A 'thickness' parameter is in development.
*/
void line(const math::Vec2& start, const math::Vec2& end, const Color& color);

/**
* @brief Draw a point.
*
* @param point The point to draw.
* @param color The color of the point.
*/
void point(const math::Vec2& point, const Color& color);

/**
* @brief Draw a circle.
*
* @param center The center of the circle.
* @param radius The radius of the circle.
* @param color The color of the circle.
* @param thickness The thickness of the circle.
*
* @note If thickness remains 0, the circle will be filled.
*/
void circle(const math::Vec2& center, double radius, const Color& color, int thickness = 0);

} // namespace draw
} // namespace kn
4 changes: 2 additions & 2 deletions include/Rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ struct Rect : SDL_FRect
*
* @return The position of the rectangle.
*/
math::Vec2 getPos() const;
[[nodiscard]] math::Vec2 getPos() const;

/**
* @brief Get the size of the rectangle.
*
* @return The size of the rectangle.
*/
math::Vec2 getSize() const;
[[nodiscard]] math::Vec2 getSize() const;

/**
* @brief Set the size of the rectangle.
Expand Down
8 changes: 8 additions & 0 deletions include/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,13 @@ void setFullscreen(bool fullscreen);
* @brief Quit SDL and destroy the window.
*/
void quit();

/**
* @brief Set the window icon.
*
* @param path The path to the icon image file.
*/
void setIcon(const std::string& path);

} // namespace window
} // namespace kn
46 changes: 36 additions & 10 deletions src/animation_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ AnimationController::AnimationController(const int fps) : m_fps(fps)
m_frameTime_ms = (fps == 0) ? 0.0 : MILLISECONDS_PER_SECOND / fps;
}

bool AnimationController::setup(const math::Vec2& size, int frameWidth, int frameHeight)
bool AnimationController::addAnim(const std::string& filePath, const std::string& name,
const int frameWidth, const int frameHeight)
{
if (!m_frameRects.empty())
std::shared_ptr<Texture> texPtr(new Texture());
if (!texPtr->loadFromFile(filePath))
return false;

if (m_animMap.find(name) != m_animMap.end()) // if animation name already exists
{
m_frameRects.clear();
m_animMap.at(name).clear();
m_timeLeftInFrame_ms = (m_fps == 0) ? 0.0 : m_frameTime_ms;
m_index = 0;
}

m_animMap[name] = {};
const math::Vec2 size = texPtr->getSize();

if (static_cast<int>(size.x) % frameWidth || static_cast<int>(size.y) % frameHeight)
{
ERROR("Sprite sheet dimensions are not divisible by frame dimensions");
Expand All @@ -29,20 +37,37 @@ bool AnimationController::setup(const math::Vec2& size, int frameWidth, int fram

for (int x = 0; x < size.x; x += frameWidth)
for (int y = 0; y < size.y; y += frameHeight)
m_frameRects.emplace_back(x, y, frameWidth, frameHeight);
{
const Frame frame{texPtr, {x, y, frameWidth, frameHeight}};
m_animMap.at(name).emplace_back(frame);
}

m_currAnim = name;

return true;
}

bool AnimationController::setAnim(const std::string& name)
{
if (m_animMap.find(name) == m_animMap.end())
{
ERROR(name + " does not exist in animation controller.")
return false;
}

m_currAnim = name;
return true;
}

const Rect& AnimationController::update(const double deltaTime)
const Frame& AnimationController::nextFrame(const double deltaTime)
{
if (m_paused)
return m_frameRects.at(m_index);
return m_animMap.at(m_currAnim).at(m_index);

if (!isProductValid(static_cast<double>(MILLISECONDS_PER_SECOND), deltaTime))
{
ERROR("Cannot multiply values");
return m_frameRects.at(m_index);
return m_animMap.at(m_currAnim).at(m_index);
}

if (const double msTakenLastFrame = MILLISECONDS_PER_SECOND * deltaTime;
Expand All @@ -51,19 +76,20 @@ const Rect& AnimationController::update(const double deltaTime)
const int numberOfFramesPassed =
static_cast<int>((msTakenLastFrame - m_timeLeftInFrame_ms) / m_frameTime_ms) + 1;
m_timeLeftInFrame_ms = m_frameTime_ms;
m_index = (m_index + numberOfFramesPassed) % static_cast<int>(m_frameRects.size());
m_index =
(m_index + numberOfFramesPassed) % static_cast<int>(m_animMap.at(m_currAnim).size());
}
else
{
if (!isSumValid(m_timeLeftInFrame_ms, -msTakenLastFrame))
{
ERROR("Cannot subtract values");
return m_frameRects.at(m_index);
return m_animMap.at(m_currAnim).at(m_index);
}
m_timeLeftInFrame_ms -= msTakenLastFrame;
}

return m_frameRects.at(m_index);
return m_animMap.at(m_currAnim).at(m_index);
}

void AnimationController::pause() { m_paused = true; }
Expand Down
Loading

0 comments on commit 85a3327

Please sign in to comment.