better angle precision

This commit is contained in:
PoliEcho 2025-02-19 11:20:48 +01:00
parent 4dd53284ca
commit efecc44a52

View File

@ -3,39 +3,40 @@
#include <SDL3_image/SDL_image.h> #include <SDL3_image/SDL_image.h>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <cmath>
#ifndef TYPES_NS #ifndef TYPES_NS
#define TYPES_NS #define TYPES_NS
struct Angle360 { struct Angle {
private: private:
int value{0}; float value{0};
void normalize() { void normalize() {
value %= 360; value = std::fmod(value, 360);
if (value < 0) if (value < 0)
value += 360; // Handle negative values value += 360; // Handle negative values
} }
public: public:
// Constructor // Constructor
Angle360(int val = 0) : value(val) { normalize(); } Angle(int val = 0) : value(val) { normalize(); }
// Assignment operator // Assignment operator
Angle360 &operator=(int val) { Angle &operator=(int val) {
value = val; value = val;
normalize(); normalize();
return *this; return *this;
} }
// Compound assignment // Compound assignment
Angle360 &operator+=(int rhs) { Angle &operator+=(int rhs) {
value += rhs; value += rhs;
normalize(); normalize();
return *this; return *this;
} }
Angle360 &operator-=(int rhs) { Angle &operator-=(int rhs) {
value -= rhs; value -= rhs;
normalize(); normalize();
return *this; return *this;
@ -45,7 +46,7 @@ public:
operator int() const { return value; } operator int() const { return value; }
// Stream output // Stream output
friend std::ostream &operator<<(std::ostream &os, const Angle360 &a) { friend std::ostream &operator<<(std::ostream &os, const Angle &a) {
return os << a.value; return os << a.value;
} }
}; };
@ -62,7 +63,7 @@ struct Entity {
SDL_Texture *texture; SDL_Texture *texture;
SDL_Rect srcRect; SDL_Rect srcRect;
float speed; float speed;
Angle360 angle = 0; Angle angle = 0;
basic_cords Central_position(std::optional<float> x = std::nullopt, basic_cords Central_position(std::optional<float> x = std::nullopt,
std::optional<float> y = std::nullopt) { std::optional<float> y = std::nullopt) {