add support for an intrinsic offset property in sprites

This commit is contained in:
Andrei Alexeyev 2019-05-18 16:48:21 +03:00
parent c0e8ec8507
commit 9ecd6d08ae
No known key found for this signature in database
GPG key ID: 363707CD4C7FE8A4
4 changed files with 24 additions and 3 deletions

View file

@ -206,6 +206,10 @@ static void _r_sprite_batch_add(Sprite *spr, const SpriteParams *params, SDL_RWo
glm_scale(attribs.transform, (vec3) { scale_x * spr->w, scale_y * spr->h, 1 });
if(spr->offset.x || spr->offset.y) {
glm_translate(attribs.transform, (vec3) { spr->offset.x / spr->w, spr->offset.y / spr->h });
}
if(params->color == NULL) {
// XXX: should we use r_color_current here?
attribs.rgba[0] = attribs.rgba[1] = attribs.rgba[2] = attribs.rgba[3] = 1;

View file

@ -459,6 +459,7 @@ static Glyph* load_glyph(Font *font, FT_UInt gindex, SpriteSheetAnchor *spritesh
}
Glyph *glyph = font->glyphs + font->glyphs_used - 1;
memset(glyph, 0, sizeof(*glyph));
FT_Error err = FT_Load_Glyph(font->face, gindex, FT_LOAD_NO_BITMAP | FT_LOAD_TARGET_LIGHT);

View file

@ -68,6 +68,8 @@ void* load_sprite_begin(const char *path, uint flags) {
{ "region_h", .out_float = &spr->tex_area.h },
{ "w", .out_float = &spr->w },
{ "h", .out_float = &spr->h },
{ "offset_x", .out_float = &spr->offset.x },
{ "offset_y", .out_float = &spr->offset.y },
{ NULL }
})) {
free(spr);
@ -168,7 +170,7 @@ void draw_sprite_batched_p(float x, float y, Sprite *spr) {
void begin_draw_sprite(float x, float y, float scale_x, float scale_y, Sprite *spr) {
begin_draw_texture(
(FloatRect){ x, y, spr->w * scale_x, spr->h * scale_y },
(FloatRect){ x + spr->offset.x * scale_x, y + spr->offset.y * scale_y, spr->w * scale_x, spr->h * scale_y },
(FloatRect){ spr->tex_area.x, spr->tex_area.y, spr->tex_area.w, spr->tex_area.h },
spr->tex
);

View file

@ -17,10 +17,24 @@
typedef struct Sprite {
Texture *tex;
FloatRect tex_area;
float w;
float h;
union {
FloatRect sprite_area;
struct {
union {
FloatOffset offset;
struct { float x, y; };
};
union {
FloatExtent extent;
struct { float w, h; };
};
};
};
} Sprite;
static_assert(offsetof(Sprite, sprite_area.offset) == offsetof(Sprite, offset), "Sprite struct layout inconsistent with FloatRect");
static_assert(offsetof(Sprite, sprite_area.extent) == offsetof(Sprite, extent), "Sprite struct layout inconsistent with FloatRect");
char* sprite_path(const char *name);
void* load_sprite_begin(const char *path, uint flags);
void* load_sprite_end(void *opaque, const char *path, uint flags);