Skip to content

Commit

Permalink
lots of 'minor' fixes lol, plus a huge overhaul of arithmetic and vec…
Browse files Browse the repository at this point in the history
…tors (#10)

* lots of 'minor' fixes lol, plus a huge overhaul of arithmetic and vectors

* fix const-qualified member preventing copy construction

* fixed implicit cast warnings

* fixed more implicit conversion warnings
  • Loading branch information
oddbookworm authored Jan 14, 2024
1 parent 14afa46 commit 7b6d28d
Show file tree
Hide file tree
Showing 20 changed files with 865 additions and 85 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ add_executable(${TESTING} EXCLUDE_FROM_ALL ${SOURCES} ${TESTS})
target_compile_definitions( ${LIBRARY} PUBLIC LOG_LEVEL=${LOG_LEVEL} )
target_compile_definitions( ${TESTING} PUBLIC LOG_LEVEL=${LOG_LEVEL} )

set_target_properties( ${LIBRARY} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
set_target_properties( ${LIBRARY} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )
set_target_properties( ${LIBRARY} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )

set_target_properties( ${TESTING} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
set_target_properties( ${TESTING} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )
set_target_properties( ${TESTING} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
${CMAKE_SOURCE_DIR}/bin )
Expand Down Expand Up @@ -204,7 +204,7 @@ if (WIN32)
endif()

target_link_libraries(${LIBRARY} PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer tmxlite)
target_link_libraries(${TESTING} PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer tmxlite gtest)
target_link_libraries(${TESTING} PRIVATE SDL2 SDL2_image SDL2_ttf SDL2_mixer tmxlite gtest gtest_main)

target_include_directories(${LIBRARY} PRIVATE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(${TESTING} PRIVATE ${CMAKE_SOURCE_DIR}/include)
Expand Down
3 changes: 3 additions & 0 deletions include/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace kn
{

typedef float float32_t;
typedef double float64_t;

typedef SDL_Scancode KEYS;
typedef SDL_Event Event;
typedef SDL_Color Color;
Expand Down
2 changes: 1 addition & 1 deletion include/Draw.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "Rect.hpp"
#include "Constants.hpp"
#include "Rect.hpp"

namespace kn
{
Expand Down
2 changes: 1 addition & 1 deletion include/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Entity
*/
std::shared_ptr<Texture> getTexture() const;

Rect crop = {0, 0, 0, 0};
Rect crop;
Rect rect;
math::Vec2 position;
math::Vec2 direction;
Expand Down
2 changes: 1 addition & 1 deletion include/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <SDL_ttf.h>

#include "Texture.hpp"
#include "Constants.hpp"
#include "Texture.hpp"

namespace kn
{
Expand Down
8 changes: 3 additions & 5 deletions include/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <SDL.h>

#include "Math.hpp"
#include "Constants.hpp"
#include "Math.hpp"

namespace kn
{
Expand Down Expand Up @@ -43,10 +43,8 @@ const Uint8* getKeysPressed();
*
* @return The vector of the keys pressed.
*/
math::Vec2 getVector(const std::vector<KEYS>& left = {},
const std::vector<KEYS>& right = {},
const std::vector<KEYS>& up = {},
const std::vector<KEYS>& down = {});
math::Vec2 getVector(const std::vector<KEYS>& left = {}, const std::vector<KEYS>& right = {},
const std::vector<KEYS>& up = {}, const std::vector<KEYS>& down = {});

} // namespace input
} // namespace kn
123 changes: 105 additions & 18 deletions include/Math.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
#pragma once

#include "ErrorLogger.hpp"
#include "MathOverflow.hpp"

namespace kn
{

using Overflow::closeToZero;
using Overflow::isProductValid;
using Overflow::isSumValid;

namespace math
{

/**
* @brief A 2D vector.
*/
struct Vec2
class Vec2
{
float x = 0.0f;
float y = 0.0f;
public:
double x;
double y;

Vec2();

template <typename _first, typename _second>
Vec2(_first x, _second y)
: x(static_cast<double>(y)), y(static_cast<double>(y)), tolerance(0.0001)
{
}

Vec2() = default;
Vec2(float x, float y);
Vec2(int x, int y);
Vec2(float x, int y);
Vec2(int x, float y);
template <typename _first, typename _second>
Vec2(_first x, _second y, double tolerance)
: x(static_cast<double>(y)), y(static_cast<double>(y)), tolerance(tolerance)
{
}

/**
* @brief Get a vector with all components set to 0.
Expand All @@ -28,31 +45,69 @@ struct Vec2

/**
* @brief Get the length of the vector.
* @return The length of the vector.
* @return The length of the vector if no overflow happens, otherwise -1.0
*/
float getLength() const;
double getLength() const;

/**
* @brief Normalize the vector.
* @brief Normalize the vector in-place. Fails if an overflow occurs or the vector is the zero
* vector
*/
void normalize();
bool normalize();

/**
* @brief Get the distance to another vector.
*
* @param other The other vector.
*
* @return The distance to another vector.
* @return The distance to another vector if no overflow happens, otherwise -1.0
*/
float distanceTo(const Vec2& other) const;
double distanceTo(const Vec2& other) const;

Vec2 operator*(float scalar) const;
Vec2 operator/(float scalar) const;
template <typename T> Vec2 operator/(T scalar) const { return Vec2(x / scalar, y / scalar); }

/**
* @brief Adds two vectors
*
* @param other the other vector
* @return a vector sum of the other two, or the zero vector on overflow
*/
Vec2 operator+(const Vec2& other) const;

/**
* @brief Subtracts two vectors
*
* @param other the subtracted vector
* @return a vector difference of the other two, or the zero vector on overflow
*/
Vec2 operator-(const Vec2& other) const;
Vec2 operator+=(const Vec2& other);

/**
* @brief in-place addition of another vector to this
*
* @param other the other vector
* @return reference to *this
*/
Vec2& operator+=(const Vec2& other);

/**
* @brief Checks if two vectors are the same
*
* @param other the vector to compare to
* @return true if the vectors are close to the same, false otherwise
*/
bool operator==(const Vec2& other) const;

/**
* @brief Checks if two vectors are different
*
* @param other the vector to compare to
* @return true if the vectors are not close to the same, false otherwise
*/
bool operator!=(const Vec2& other) const;

protected:
double tolerance; //!< the accuracy with which comparisons are made
};

/**
Expand All @@ -75,7 +130,39 @@ Vec2 clampVec(Vec2 vec, const Vec2& min, const Vec2& max);
*
* @return The interpolated vector.
*/
Vec2 lerpVec(const Vec2& a, const Vec2& b, float t);
Vec2 lerpVec(const Vec2& a, const Vec2& b, double t);

/**
* @brief Multiplies a vector by a constant
*
* @tparam T type of the constant
* @param lhs the constant
* @param rhs the vector
* @return A vector scaled by the constant
*/
template <typename T> Vec2 operator*(const T& lhs, const Vec2& rhs)
{
if (!isProductValid(lhs, rhs.x) || !isProductValid(lhs, rhs.y))
{
WARN("Multiplication would result in overflow");
return Vec2::ZERO();
}

const double x = lhs * rhs.x;
const double y = lhs * rhs.y;

return Vec2(x, y);
}

/**
* @brief Multiplies a vector by a constant
*
* @tparam T type of the constant
* @param lhs the vector
* @param rhs the constant
* @return A vector scaled by the constant
*/
template <typename T> Vec2 operator*(const Vec2& lhs, const T& rhs) { return rhs * lhs; }

} // namespace math
} // namespace kn
75 changes: 75 additions & 0 deletions include/MathOverflow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef MATH_OVERFLOW_HPP
#define MATH_OVERFLOW_HPP

#include <cmath>
#include <limits>
#include <stdint.h>

#include "Constants.hpp"

namespace kn
{

namespace Overflow
{

//@{
/**
* @brief Checks if an overflow would occur in summation
*
* @param first first value
* @param second second value
* @return true if sum is valid, false otherwise
*/
bool isSumValid(const float64_t& first, const float64_t& second);

bool isSumValid(const float32_t& first, const float32_t& second);

bool isSumValid(const uint32_t& first, const uint32_t& second);

bool isSumValid(const uint64_t& first, const uint64_t& second);

bool isSumValid(const int32_t& first, const int32_t& second);

bool isSumValid(const int64_t& first, const int64_t& second);
//@}

//@{
/**
* @brief Checks if an overflow would occur in multiplcation
*
* @param first first value
* @param second second value
* @return true if product is valid, false otherwise
*/
bool isProductValid(const float64_t& first, const float64_t& second);

bool isProductValid(const float32_t& first, const float32_t& second);

bool isProductValid(const uint32_t& first, const uint32_t& second);

bool isProductValid(const uint64_t& first, const uint64_t& second);

bool isProductValid(const int32_t& first, const int32_t& second);

bool isProductValid(const int64_t& first, const int64_t& second);
//@}

//@{
/**
* @brief Checks if the value is close to zero
*
* @param value value to check
* @param tolerance the accuracy to use, anything closer to 0 than this will be considered zero
* @return true if close to zero, false otherwise
*/
bool closeToZero(const float64_t& value, const float64_t tolerance = 0.0001);

bool closeToZero(const float32_t& value, const float32_t tolerance = 0.0001f);
//@}

} // namespace Overflow

} // namespace kn

#endif // MATH_OVERFLOW_HPP
2 changes: 1 addition & 1 deletion include/Mixer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Sound final
* @param loops The number of times to loop the sound.
* @param maxMs The number of milliseconds to play the sound.
* @param fadeMs The number of milliseconds to fade in.
*
*
* @warning Fade in is currently not working.
*/
void play(int loops = 0, int maxMs = -1, int fadeMs = 0);
Expand Down
5 changes: 3 additions & 2 deletions include/TileMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ struct Tile

/**
* @brief A class that represents a Tiled map.
*
* @warning This class is currently only compatible with orthogonal maps and tile layers (no objects).
*
* @warning This class is currently only compatible with orthogonal maps and tile layers (no
* objects).
*/
class TileMap final
{
Expand Down
11 changes: 7 additions & 4 deletions include/Time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace time
class Clock final
{
public:
Clock() = default;
Clock();
~Clock() = default;

/**
Expand All @@ -28,9 +28,12 @@ class Clock final
double tick(int frameRate = 60);

private:
double frameTime, targetFrameTime, deltaTime;
double frequency = SDL_GetPerformanceFrequency();
double now, last = SDL_GetPerformanceCounter();
double m_frameTime;
double m_targetFrameTime;
double m_deltaTime;
double m_frequency;
double m_now;
double m_last;
};

} // namespace time
Expand Down
Loading

0 comments on commit 7b6d28d

Please sign in to comment.