naval_swarm/src/types.hpp
2025-02-18 15:23:34 +01:00

108 lines
2.3 KiB
C++

#include "const.hpp"
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <iostream>
#ifndef TYPES_NS
#define TYPES_NS
struct Angle360 {
private:
int value{0};
void normalize() {
value %= 360;
if (value < 0)
value += 360; // Handle negative values
}
public:
// Constructor
Angle360(int val = 0) : value(val) { normalize(); }
// Assignment operator
Angle360 &operator=(int val) {
value = val;
normalize();
return *this;
}
// Compound assignment
Angle360 &operator+=(int rhs) {
value += rhs;
normalize();
return *this;
}
Angle360 &operator-=(int rhs) {
value -= rhs;
normalize();
return *this;
}
// Type conversion
operator int() const { return value; }
// Stream output
friend std::ostream &operator<<(std::ostream &os, const Angle360 &a) {
return os << a.value;
}
};
struct Entity {
private:
SDL_FRect m_position;
SDL_FPoint m_offset{0, 0};
SDL_FRect m_central_cache; // New cached position
public:
// Direct access reference with auto-sync
SDL_FRect& position() {
// Update cache when position changes
m_central_cache = {
m_position.x + m_offset.x,
m_position.y + m_offset.y,
m_position.w,
m_position.h
};
return m_position;
}
// Return reference to cached central position
SDL_FRect& Central_position() {
// Auto-update cache before return
m_central_cache.x = m_position.x + m_offset.x;
m_central_cache.y = m_position.y + m_offset.y;
return m_central_cache;
}
// Set offset values with cache invalidation
void set_central_offset(float x, float y) {
m_offset = {x, y};
Central_position(); // Update cache
}
// --- Existing members ---
SDL_FRect Tposition;
bool gotoT;
SDL_Texture* texture;
SDL_Rect srcRect;
float speed;
Angle360 angle = 0;
unsigned int width;
unsigned int height;
};
struct Camera {
SDL_FRect view = {0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT};
bool followPlayer = false;
float speed = 500.0f;
float smoothness = 0.1f;
};
struct sdl_session {
SDL_Window *window;
SDL_Renderer *renderer;
};
#endif