#ifndef GAME_SPRITE_H #define GAME_SPRITE_H #include "Bitmap.h" #define SPR_SUBPX_TO_PX(v) ((v) >= 0 ? ((v) >> 8) : -((-(v)) >> 8)) #define SPR_PX_TO_SUBPX(v) ((v) >= 0 ? ((v) << 8) : -((-(v)) << 8)) class Sprite { public: virtual ~Sprite() = default; virtual void Tick() = 0; [[nodiscard]] virtual int GetWidth() const { return _w; } [[nodiscard]] virtual int GetHeight() const { return _h; } [[nodiscard]] int GetX() const { return SPR_SUBPX_TO_PX(_sx); } [[nodiscard]] int GetY() const { return SPR_SUBPX_TO_PX(_sy); } void SetX(int x) { _sx = x >= 0 ? (x << 8) : -((-x) << 8); } void SetY(int y) { _sy = y >= 0 ? (y << 8) : -((-y) << 8); } [[nodiscard]] Rect GetRect() const { auto x = SPR_SUBPX_TO_PX(_sx), y = SPR_SUBPX_TO_PX(_sy); return { x, y, x + _w, y + _h }; } void SetPalette(uint8_t palette) { _palette = palette << 4; } [[nodiscard]] uint8_t GetPalette() const { return _palette >> 4; } [[nodiscard]] Bitmap *GetBitmap() const { return _bitmap; } [[nodiscard]] unsigned GetYOffset() const { return _frame * _h; } [[nodiscard]] bool isMirroredX() const { return _mirrorX; } [[nodiscard]] bool isMirroredY() const { return _mirrorY; } protected: Sprite() = default; Bitmap *_bitmap{nullptr}; uint8_t _palette{0}; int _sx{0}, _sy{0}; int _w{0}, _h{0}; unsigned _frame{0}; bool _mirrorX{false}; bool _mirrorY{false}; }; #endif //GAME_SPRITE_H