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