Skip to content

Commit

Permalink
more key functions
Browse files Browse the repository at this point in the history
animation rewind
color constants
controller button constants
controller button checking if pressed
bind keys to names and get a direction
math get only angle of vector
  • Loading branch information
durkisneer1 committed Dec 14, 2024
1 parent 1ccc733 commit 35a5523
Show file tree
Hide file tree
Showing 26 changed files with 377 additions and 116 deletions.
4 changes: 4 additions & 0 deletions docs/reference/animation_controller.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
AnimationController
===================

.. warning::

This class is still in development and is subject to change at any time.

Description
-----------

Expand Down
11 changes: 11 additions & 0 deletions docs/reference/controller.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
controller
==========

.. warning::

This namespace is still in development and is subject to change at any time.

Description
-----------

Expand All @@ -24,6 +28,11 @@ Usage
// Do something
}
// Check if the A button is pressed
if (kn::controller::isPressed(kn::CONTROLLER_A)) {
// Do something
}
Functions
---------

Expand All @@ -35,6 +44,8 @@ Functions

.. doxygenfunction:: kn::controller::getRightTrigger

.. doxygenfunction:: kn::controller::isPressed

.. doxygenfunction:: kn::controller::setDeadZone

.. doxygenfunction:: kn::controller::getDeadZone
25 changes: 25 additions & 0 deletions docs/reference/draw.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
draw
====

Description
-----------

The **draw** namespace provides functions to draw basic shapes and lines on the screen.

Usage
-----

.. code-block:: cpp
// Draw a rectangle at position (50, 50) with size (16, 16).
kn::draw::rect({50, 50, 16, 16}, kn::color::RED);
// Draw a line from (50, 50) to (100, 100).
kn::draw::line({50, 50}, {100, 100}, kn::color::GREEN);
// Draw a point at position (50, 50).
kn::draw::point({50, 50}, kn::color::BLUE);
// Draw a circle at position (50, 50) with radius 16.
kn::draw::circle({50, 50}, 16, kn::color::YELLOW);
Functions
---------

.. doxygenfunction:: kn::draw::rect

.. doxygenfunction:: kn::draw::line
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage
kn::Font font("assets/font.ttf", 16);
// Render a texture containing the text "Hello, World!" in white with no anti-aliasing.
const kn::Texture text = font.render("Hello, World!", false, {255, 255, 255});
const kn::Texture text = font.render("Hello, World!", false, kn::color::WHITE);
// Draw the text texture at position (50, 50).
kn::window::blit(text, {50, 50});
Expand Down
33 changes: 31 additions & 2 deletions docs/reference/key.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,48 @@ Scancodes are the physical location of the key on the keyboard.
Keycodes are the character that the key represents.
For example, 'W' has a scancode of ``kn::S_w`` and a keycode of ``kn::K_w``.

Keycodes are more for text input, while scancodes are more for game input.

.. note:: A function similar to the removed ``kn::input::getVector`` will eventually be added to this namespace.

Usage
-----

.. code-block:: cpp
// Get all currently pressed keys.
const auto* keys = kn::key::getPressed();
// Check if the 'W' key is pressed.
if (kn::key::isPressed(kn::S_w)) {
if (keys[kn::S_w]) {
// Handle 'W' key press.
}
// Bind keys to the name "up".
kn::key::bind("up", {kn::S_w, kn::S_UP});
// Check if any of the "up" binds are pressed.
if (kn::key::isPressed("up")) {
// Handle "up" bind.
}
// Make three more binds for left, right, and down.
kn::key::bind("left", {kn::S_a, kn::S_LEFT});
kn::key::bind("right", {kn::S_d, kn::S_RIGHT});
kn::key::bind("down", {kn::S_s, kn::S_DOWN});
// Get the direction of the four binds.
auto direction = kn::key::getDirection("left", "right", "up", "down");
Functions
---------

.. doxygenfunction:: kn::key::isPressed
.. doxygenfunction:: kn::key::getPressed

.. doxygenfunction:: kn::key::isPressed

.. doxygenfunction:: kn::key::bind

.. doxygenfunction:: kn::key::unbind

.. doxygenfunction:: kn::key::getDirection
23 changes: 17 additions & 6 deletions docs/reference/math.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
math
====

Description
-----------

This module provides a set of mathematical functions and classes for working with 2D vectors and angles.
In case you're not familiar with the concept of a vector, it's a mathematical object that has both a magnitude and a direction.
You may read more on the subject `here <https://www.mathsisfun.com/algebra/vectors.html>`_.

Members
-------

.. doxygenstruct:: kn::math::PolarCoordinate
:members:
:undoc-members:

.. doxygenclass:: kn::math::Vec2
:members:

.. doxygenfunction:: kn::math::clampVec

.. doxygenfunction:: kn::math::lerpVec
Functions
---------

.. doxygenfunction:: kn::math::scaleToLength

.. doxygenfunction:: kn::math::fromPolar

.. doxygenfunction:: kn::math::normalize

.. doxygenfunction:: kn::math::clampVec

.. doxygenfunction:: kn::math::lerpVec

.. doxygenfunction:: kn::math::lerp

.. doxygenfunction:: kn::math::remap
Expand All @@ -30,6 +43,4 @@ math

.. doxygenfunction:: kn::math::cross

.. doxygenfunction:: kn::math::angleBetween

.. doxygenfunction:: kn::math::angleOfDifference
.. doxygenfunction:: kn::math::angleBetween
6 changes: 3 additions & 3 deletions docs/reference/mouse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Usage

.. code-block:: cpp
// Check if the left mouse button is pressed.
if (kn::mouse::isPressed(kn::BUTTON_LEFT)) {
// Check if the user left clicked.
if (kn::mouse::isPressed(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, {255, 0, 0});
kn::draw::circle(mousePos, 10, kn::color::RED);
Functions
---------
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/rect.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Rect
====

.. warning::

This class may undergo changes in the near future.

Description
-----------

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/texture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Usage
imageTexture.angle = 30.0;
// Create a 16x16 red texture.
kn::Texture colorTexture({16, 16}, {255, 0, 0});
kn::Texture colorTexture({16, 16}, kn::color::RED);
// Draw the textures.
kn::window::blit(imageTexture, {50, 50});
Expand Down
12 changes: 12 additions & 0 deletions docs/reference/window.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
window
======

Description
-----------

The window namespace provides functions to create and manage a window.
It provides methods to draw onto the screen, get input events, and manage the window's properties.

Variables
---------

.. doxygenvariable:: kn::camera

Functions
---------

.. doxygennamespace:: kn::window
7 changes: 6 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ int main()
kn::time::Clock clock;
kn::camera = {-32, -26};

constexpr kn::Color bgColor = {21, 18, 37, 255};

kn::key::bind("left", {kn::S_LEFT, kn::S_a});
kn::key::bind("right", {kn::S_RIGHT, kn::S_d});

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

Player player(tileMap);
Expand All @@ -23,7 +28,7 @@ int main()
kn::window::close();
}

kn::window::clear({21, 18, 37});
kn::window::clear(bgColor);

tileMap.drawMap();
player.update(dt);
Expand Down
19 changes: 11 additions & 8 deletions example/src/player.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "include/Player.hpp"
#include "Player.hpp"
#include <algorithm>

#define AxisX 0
#define AxisY 1
Expand All @@ -14,25 +15,27 @@ Player::Player(const kn::TileMap& tileMap)

void Player::update(const double dt)
{
const auto* keys = kn::key::getPressed();
if (onGround)
{
if (kn::key::isPressed(kn::S_SPACE))
if (keys[kn::S_SPACE] || kn::controller::isPressed(kn::CONTROLLER_A))
{
velocity.y = -200;
onGround = false;
}
}
else
velocity.y += 980.0 * dt;
velocity.y += 980.7 * dt;

const int xDir = kn::key::isPressed(kn::S_d) - kn::key::isPressed(kn::S_a);
if (xDir != 0)
double xDir = kn::key::getDirection("left", "right").x + kn::controller::getLeftJoystick().x;
xDir = std::clamp(xDir, -1.0, 1.0);
if (xDir != 0.0)
{
animController.setAnim("walk");

if (xDir > 0)
if (xDir > 0.0)
facingRight = true;
else if (xDir < 0)
else if (xDir < 0.0)
facingRight = false;
}
else
Expand All @@ -47,7 +50,7 @@ void Player::update(const double dt)

for (const auto& tile : interactables)
if (rect.collideRect(tile.collider))
kn::draw::rect(tile.rect, {255, 255, 0}, 1);
kn::draw::rect(tile.rect, kn::color::YELLOW, 1);

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

Expand Down
7 changes: 6 additions & 1 deletion include/AnimationController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ class AnimationController final
*
* @return The name of the current animation.
*/
[[nodiscard]] const std::string& getCurrentAnim() const { return m_currAnim; }
[[nodiscard]] const std::string& getCurrentAnim() const;

/**
* @brief Rewind the currently playing animation to the beginning.
*/
void rewind();

/**
* @brief Pause the animation.
Expand Down
Loading

0 comments on commit 35a5523

Please sign in to comment.