Skip to content

Commit

Permalink
loader functions
Browse files Browse the repository at this point in the history
* more appealing doc styling update
* added camera to "things to know"
* TileMap constructor -> loadTMX
* Sound constructor -> loadFromFile
* Remove Rect getPos
* Font constructor -> openFromFile
  • Loading branch information
durkisneer1 committed Jan 1, 2025
1 parent 2989eb2 commit d51a59e
Show file tree
Hide file tree
Showing 31 changed files with 507 additions and 444 deletions.
598 changes: 311 additions & 287 deletions docs/_static/css/custom.css

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions docs/manual/things_to_know.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ For example, to check if the ``A`` button on a controller is pressed, you would
.. code-block:: cpp
if (kn::controller::isPressed(kn::C_A)) {
std::cout << "A button is pressed!" << std::endl;
std::cout << "A button is pressed!\n";
}
Keycode VS Scancode
Expand Down Expand Up @@ -53,5 +53,17 @@ Here is a comparison of keycodes and scancodes for different scenarios:
- ❌ User layout might vary
- ✅ Always maps to physical key

Camera
~~~~~~

.. note:: This page is a work in progress and will be updated as more information becomes available.
There is a global ``kn::camera`` vector that you can use to offset rendering.
This is useful for creating a camera system that follows the player or to pan around the game world.

In the example usage below, the rectangle's top left corner is visually at ``(150, 150)``:

.. code-block:: cpp
kn::camera = {-100, -100};
kn::draw::rect({50, 50, 16, 16}, kn::color::RED);
.. note:: This page is a work in progress and will be updated as more information becomes available.
4 changes: 2 additions & 2 deletions docs/reference/animation_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ Usage
// Using AnimationController::nextFrame is simple.
// It returns the current frame texture, srcRect, and advances the animation.
const kn::Frame frame = animController.nextFrame(deltaTime);
const kn::Frame* frame = animController.nextFrame(deltaTime);
// Draw the current frame texture at position (50, 50) and size (16, 16).
kn::window::blit(*frame.tex, {50, 50, 16, 16}, frame.rect);
kn::window::blit(*frame->tex, {50, 50, 16, 16}, frame->rect);
Members
-------
Expand Down
10 changes: 5 additions & 5 deletions docs/reference/draw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ Usage
// Draw a circle at position (50, 50) with radius 16.
kn::draw::circle({50, 50}, 16, kn::color::YELLOW);
Constants
---------

.. doxygennamespace:: kn::color

Functions
---------

Expand All @@ -38,3 +33,8 @@ Functions
.. doxygenfunction:: kn::draw::point

.. doxygenfunction:: kn::draw::circle

Constants
---------

.. doxygennamespace:: kn::color
7 changes: 5 additions & 2 deletions docs/reference/font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ Usage

.. code-block:: cpp
// Create a Font object with a font size of 16.
kn::Font font("assets/font.ttf", 16);
// Load a font file with a point size of 16.
kn::Font font;
if (!font.openFromFile("assets/font.ttf", 16)) {
// Handle error
}
// Render a texture containing the text "Hello, World!" in white with no anti-aliasing.
const kn::Texture text = font.render("Hello, World!", false, kn::color::WHITE);
Expand Down
6 changes: 2 additions & 4 deletions docs/reference/mouse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ Usage
.. code-block:: cpp
// Check if the user left clicked.
const auto mousePressed = kn::mouse::getPressed();
if (mousePressed == kn::MOUSE_LEFT) {
if (kn::mouse::getPressed() == kn::MOUSE_LEFT) {
// Handle left mouse button press.
}
// Get the mouse position and draw a circle at that position.
kn::math::Vec2 mousePos = kn::mouse::getPos();
kn::draw::circle(mousePos, 10, kn::color::RED);
kn::draw::circle(kn::mouse::getPos(), 10, kn::color::RED);
Functions
---------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/rect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Usage
// Check if they collide.
if (rectA.collideRect(rectB)) {
// handle collision
// Handle collision
}
// Move the top left of rectB to the bottom right of rectA
Expand Down
9 changes: 6 additions & 3 deletions docs/reference/sound.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ Usage
.. code-block:: cpp
// Create a Sound object from an audio file.
kn::Sound sound("assets/sound.ogg");
kn::Sound sfx;
if (!sfx.loadFromFile("assets/sfx.ogg")) {
// Handle error
}
// Set the volume of the sound to 50%.
sound.setVolume(0.5f);
sfx.setVolume(0.5f);
// Play the sound 3 times after it fades in over 2 seconds.
sound.play(2, -1, 2000);
sfx.play(2, -1, 2000);
Members
-------
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/texture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage
// Instantiate a Texture object and load an image from a file.
kn::Texture imageTexture;
if (!imageTexture.loadFromFile("assets/image.png")) {
std::cerr << "Failed to load image.png" << std::endl;
// Handle error
}
// Flip the image texture horizontally and rotate it 30 degrees.
Expand All @@ -32,5 +32,7 @@ Usage
Members
-------

.. note:: The internal SDL_Texture* is a nullptr until ``loadFromFile`` is called.

.. doxygenclass:: kn::Texture
:members:
10 changes: 7 additions & 3 deletions docs/reference/tile_map.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ It also allows for getting a layer or a collection of tiles from one or more lay
What the TileMap class does **NOT** support at the moment is object type layers or rotated tiles.
Multiple tile sets per tile map are not supported either.

.. note:: It is recommended to have a border around the tiles to prevent texture bleeding.

Usage
-----

.. code-block:: cpp
// Load a tile map from a tmx file.
// The second argument is the border size around every tile in the tile set. (recommended)
kn::TileMap tileMap("assets/map.tmx", 1);
// Load a tile map from a tmx file where its tileset has a 1px border around each tile.
kn::TileMap tileMap;
if (!tileMap.loadTMX("assets/map.tmx", 1)) {
// Handle error.
}
// Get the layer named "ground" from the tile map.
const kn::Layer* groundLayer = tileMap.getLayer("ground");
Expand Down
5 changes: 0 additions & 5 deletions docs/reference/window.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ It provides methods to draw onto the screen, get input events, and manage the wi

You can find an example of window usage in the `Creating a Window <../getting_started/create_window.html>`_ guide.

Variables
---------

.. doxygenvariable:: kn::camera

Functions
---------

Expand Down
3 changes: 2 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ int main()
}
);

const kn::TileMap tileMap("../example/assets/room.tmx");
kn::TileMap tileMap;
tileMap.loadTMX("../example/assets/room.tmx");

Player player(tileMap);

Expand Down
6 changes: 3 additions & 3 deletions example/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ void Player::update(const double dt)
if (rect.collideRect(tile.collider))
kn::draw::rect(tile.rect, kn::color::YELLOW, 1);

const kn::Frame frame = animController.nextFrame(dt);
const kn::Frame* frame = animController.nextFrame(dt);

frame.tex->flip.x = !facingRight;
kn::window::blit(*frame.tex, rect, frame.rect);
frame->tex->flip.x = !facingRight;
kn::window::blit(*frame->tex, rect, frame->rect);
}

void Player::handleCollision(const int axis)
Expand Down
28 changes: 15 additions & 13 deletions include/AnimationController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@

namespace kn
{

/**
* @brief Container for a texture pointer and source rect
*/
struct Frame
{
/**
* @brief A pointer to the full loaded animation texture.
*/
std::shared_ptr<Texture> tex;
/**
* @brief The area of the texture to render.
*/
Rect rect;
};

/**
* @brief Container for a list of frames.
*/
struct Animation
{
/**
* @brief A collection of frames that make up the animation.
*/
std::vector<Frame> frames;
/**
* @brief The frame rate of the animation.
*/
int fps;
};

/**
* @brief Handler for sprite sheet animations.
*/
class AnimationController final
{
public:
Expand All @@ -47,7 +49,7 @@ class AnimationController final
* @return ``true`` if the setup was successful, ``false`` otherwise.
*/
[[maybe_unused]] bool loadSpriteSheet(const std::string& name, const std::string& filePath,
const math::Vec2& frameSize, int fps);
const math::Vec2& frameSize, int fps);

/**
* @brief Load all images in a directory as an animation.
Expand Down Expand Up @@ -81,9 +83,9 @@ class AnimationController final
*
* @param deltaTime The time since the last time this function was called.
*
* @return The next frame's texture and area to render from.
* @return A pointer to the next frame of the animation.
*/
[[nodiscard]] const Frame& nextFrame(double deltaTime);
[[nodiscard]] const Frame* nextFrame(double deltaTime);

/**
* @brief Set the playback speed of animations.
Expand Down
23 changes: 8 additions & 15 deletions include/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,30 @@ namespace kn
{
class Texture;

/**
* @brief A class to render text.
*/
class Font final
{
public:
Font() = default;
~Font();

/**
* @brief Create a font object.
*
* @brief Open a font from a file.
* @param fileDir The directory of the font file.
* @param ptSize The point size of the font.
* @return ``true`` if the font was opened successfully, ``false`` otherwise.
*/
Font(const std::string& fileDir, int ptSize);
~Font()
{
if (font)
TTF_CloseFont(font);
}
[[maybe_unused]] bool openFromFile(const std::string& fileDir, int ptSize);

/**
* @brief Generate a texture with rendered text.
*
* @param text The text to render.
* @param antialias Whether to antialias the text.
* @param color The color of the text.
* @param wrapLength The length to wrap the text.
* If 0, the text will not be wrapped.
* @param wrapLength The length to wrap the text. If ``0``, the text will not be wrapped.
*
* @return The generated texture object.
*
* @note Do not confuse this with rendering text to the screen.
* If a font has not been opened, an empty texture will be returned.
*/
[[nodiscard]] Texture render(const std::string& text, bool antialias, Color color,
int wrapLength = 0) const;
Expand Down
12 changes: 8 additions & 4 deletions include/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ enum class InputType
CONTROLLER_BUTTON
};

/**
* @brief An input action.
* Can either be a keyboard key, a mouse button, a controller button or a controller joystick.
*/
struct InputAction
{
InputType type;
Expand All @@ -36,22 +32,30 @@ struct InputAction
};

/**
* @brief Construct a new keyboard input action.
*
* @param key The keyboard key.
*/
explicit InputAction(Scancode key);

/**
* @brief Construct a new controller axis input action.
*
* @param axis The controller axis.
* @param isPositive Whether the axis is positive or negative.
*/
InputAction(ControllerAxis axis, bool isPositive);

/**
* @brief Construct a new controller button input action.
*
* @param controllerButton The controller button.
*/
explicit InputAction(ControllerButton controllerButton);

/**
* @brief Construct a new mouse input action.
*
* @param mouseButton The mouse button.
*/
explicit InputAction(int mouseButton);
Expand Down
2 changes: 0 additions & 2 deletions include/Key.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include "Constants.hpp"

namespace kn
{
namespace math
Expand Down
13 changes: 6 additions & 7 deletions include/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ using overflow::isSumValid;

namespace math
{

/**
* @brief Polar coordinate representation.
*/
struct PolarCoordinate
{
/**
* @brief The angle in degrees.
*/
double angle;
/**
* @brief The magnitude of the vector.
*/
double radius;
};

/**
* @brief A 2D vector.
*/
class Vec2
{
public:
Expand Down
Loading

0 comments on commit d51a59e

Please sign in to comment.