gba-sprite-engine/engine/include/libgba-sprite-engine/sprites/sprite.h

117 lines
3.1 KiB
C
Raw Normal View History

2018-08-01 16:03:16 +02:00
//
// Created by Wouter Groeneveld on 26/07/18.
//
#ifndef GBA_SPRITE_ENGINE_SPRITE_H
#define GBA_SPRITE_ENGINE_SPRITE_H
#include <libgba-sprite-engine/gba/tonc_types.h>
2018-08-01 16:03:16 +02:00
#include <memory>
#ifdef CODE_COMPILED_AS_PART_OF_TEST
#include <libgba-sprite-engine/gba/tonc_math_stub.h>
#else
#include <libgba-sprite-engine/gba/tonc_math.h>
#endif
#include <libgba-sprite-engine/gbavector.h>
2018-08-01 16:03:16 +02:00
#define COLOR_MODE_16 0
#define COLOR_MODE_256 1
#define GFX_MODE 0
#define MOSAIC_MODE 0
#define AFFINE_FLAG_NONE_SET_YET 0
#define HORIZONTAL_FLIP_FLAG 0
#define VERTICAL_FLIP_FLAG 0
#define FLIP_VERTICAL_CLEAR 0xdfff
#define FLIP_HORIZONTAL_CLEAR 0xefff
#define OAM_TILE_OFFSET_CLEAR 0xfc00
#define OAM_TILE_OFFSET_NEW 0x03ff
2018-08-01 16:03:16 +02:00
enum SpriteSize {
SIZE_8_8,
SIZE_16_16,
SIZE_32_32,
SIZE_64_64,
SIZE_16_8,
SIZE_32_8,
SIZE_32_16,
SIZE_64_32,
SIZE_8_16,
SIZE_8_32,
SIZE_16_32,
SIZE_32_64
};
class SpriteManager;
class Sprite {
private:
void updateVelocity();
void updateAnimation();
void syncVelocity();
2018-08-01 16:03:16 +02:00
protected:
const void *data;
2018-08-14 09:08:31 +02:00
int x, y, dx, dy;
2018-12-07 14:44:56 +01:00
u8 animation_offset;
2018-08-14 09:08:31 +02:00
u32 priority, w, h, size_bits, shape_bits;
2018-08-08 14:43:34 +02:00
bool stayWithinBounds;
2018-08-09 12:35:05 +02:00
u32 imageSize, tileIndex;
SpriteSize spriteSize;
u8 animationDelay, numberOfFrames, beginFrame, currentFrame, previousFrame, animationCounter;
2018-08-05 17:11:36 +02:00
bool animating;
OBJ_ATTR oam;
2020-06-24 10:19:40 +02:00
bool isAffine;
2018-08-01 16:03:16 +02:00
2018-12-07 14:44:56 +01:00
void syncAnimation();
2018-08-01 16:03:16 +02:00
virtual void syncOam();
virtual void buildOam(int tileIndex);
void setAttributesBasedOnSize(SpriteSize size);
public:
2018-08-09 12:35:05 +02:00
explicit Sprite(const Sprite& other);
explicit Sprite(const void *imageData, int imageSize, int x, int y, SpriteSize size);
virtual ~Sprite() {}
2018-08-01 16:03:16 +02:00
void makeAnimated(int beginFrame, int numberOfFrames, int animationDelay);
2019-08-01 23:01:03 +02:00
void setBeginFrame(int frame) { this->beginFrame = frame; }
void animateToFrame(int frame) { this->currentFrame = frame; }
2019-08-01 23:01:03 +02:00
void animate() { this->animating = true; }
2018-08-05 17:11:36 +02:00
void stopAnimating() { this->animating = false; }
2018-08-08 14:43:34 +02:00
void setStayWithinBounds(bool b) { stayWithinBounds = b; }
2018-08-01 16:03:16 +02:00
void setVelocity(int dx, int dy) {
this->dx = dx;
this->dy = dy;
}
void update();
2018-08-01 16:03:16 +02:00
void moveTo(int x, int y);
void moveTo(VECTOR location);
2018-08-08 14:43:34 +02:00
bool collidesWith(Sprite &s2);
2018-08-01 16:03:16 +02:00
void flipVertically(bool flip);
void flipHorizontally(bool flip);
2018-08-09 12:35:05 +02:00
u32 getTileIndex() { return tileIndex; }
VECTOR getPos() { return {x, y}; }
GBAVector getPosAsVector() { return GBAVector(getPos()); }
VECTOR getCenter() { return { x + w / 2, y + h / 2 }; }
VECTOR getVelocity() { return { dx, dy}; }
int getX() { return x; }
int getY() { return y; }
2018-08-09 12:35:05 +02:00
u32 getDx() { return dx; }
u32 getDy() { return dy; }
u32 getWidth() { return w; }
2019-07-31 17:54:23 +02:00
u32 getHeight() { return h; }
u32 getAnimationDelay() { return animationDelay; }
u32 getNumberOfFrames() { return numberOfFrames; }
2018-08-09 12:35:05 +02:00
u32 getCurrentFrame() { return currentFrame; }
bool isAnimating() { return animating; };
bool isOffScreen();
2018-08-01 16:03:16 +02:00
friend class SpriteManager;
};
#endif //GBA_SPRITE_ENGINE_SPRITE_H