Fix various issues with SACT2

Ensure that sact_get_sprite always returns a sprite object if sprite
number is not negative. SACT2 functions in general should work before
pixel data is attached to a sprite.

This also fixes the default for the 'show' property (true) and changes
the logic for adding/removing sprites from the scene to be less
error-prone (checked whenever a sprite is dirtied).
This commit is contained in:
Nunuhara Cabbage
2021-05-06 20:11:33 -07:00
parent 601bb1d556
commit 058eee03e5
3 changed files with 63 additions and 33 deletions
+30 -7
View File
@@ -28,9 +28,14 @@ union vm_value;
struct sact_sprite {
TAILQ_ENTRY(sact_sprite) entry;
// The sprite's texture (CG or solid color). Initialized lazily.
struct texture texture;
// If no CG is attached to the sprite, the solid color to fill with.
SDL_Color color;
// The position and dimensions of the sprite.
Rectangle rect;
// Arbitrary text can be attached to the sprite. This is rendered on a
// separate texture and overlayed.
struct {
struct string *str;
struct texture texture;
@@ -39,18 +44,39 @@ struct sact_sprite {
int char_space;
int line_space;
} text;
// The Z-layer of the sprite within the scene.
int z;
bool show;
// This flag indicates that pixel data has been attached to a sprite; it
// does NOT guarantee that the texture is initialized.
bool has_pixel;
// This flag indicates whether or not the sprite is included in the
// the current scene. A sprite should be in the scene if there is pixel
// or text data attached to it and it is not hidden.
bool in_scene;
// When a sprite is hidden, it is removed from the scene. Attaching new
// pixel data does not alter the state of this flag.
bool hidden;
// The sprite handle.
int no;
// The CG number attached to the sprite.
int cg_no;
};
extern bool sact_dirty;
void sprite_register(struct sact_sprite *sp);
void sprite_unregister(struct sact_sprite *sp);
static inline void sprite_dirty(struct sact_sprite *sp)
{
if (sp && sp->show)
sact_dirty = true;
if (!sp)
return;
sact_dirty = true;
if (sp->hidden) {
sprite_unregister(sp);
} else if (sp->has_pixel || sp->text.texture.handle) {
sprite_register(sp);
}
}
static inline void scene_dirty(void)
@@ -58,9 +84,6 @@ static inline void scene_dirty(void)
sact_dirty = true;
}
void sprite_register(struct sact_sprite *sp);
void sprite_unregister(struct sact_sprite *sp);
void sprite_free(struct sact_sprite *sp);
void sprite_render_scene(void);
void sprite_flip(void);
@@ -85,7 +108,7 @@ static inline int sprite_get_pos_y(struct sact_sprite *sp) { return sp->rect.y;
static inline int sprite_get_width(struct sact_sprite *sp) { return sp->rect.w; }
static inline int sprite_get_height(struct sact_sprite *sp) { return sp->rect.h; }
static inline int sprite_get_z(struct sact_sprite *sp) { return sp->z; }
static inline int sprite_get_show(struct sact_sprite *sp) { return sp->show; }
static inline int sprite_get_show(struct sact_sprite *sp) { return !sp->hidden; }
void sprite_set_text_home(struct sact_sprite *sp, int x, int y);
void sprite_set_text_line_space(struct sact_sprite *sp, int px);
void sprite_set_text_char_space(struct sact_sprite *sp, int px);