Add x-flipping for anchoring

main
En Yi 2024-08-06 20:36:23 +08:00
parent 657110a66d
commit 1215746e05
4 changed files with 34 additions and 13 deletions

View File

@ -575,15 +575,8 @@ void draw_sprite_pro(Sprite_t* spr, int frame_num, Vector2 pos, float rotation,
);
}
Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
{
Vector2 p1 = get_anchor_offset(bbox, anchor);
Vector2 p2 = get_anchor_offset(new_bbox, anchor);
return Vector2Subtract(p1, p2);
}
Vector2 get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor)
static Vector2 internal_get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor)
{
Vector2 offset = {0};
switch (anchor)
@ -624,4 +617,30 @@ Vector2 get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor)
return offset;
}
Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
{
Vector2 p1 = internal_get_anchor_offset(bbox, anchor);
Vector2 p2 = internal_get_anchor_offset(new_bbox, anchor);
return Vector2Subtract(p1, p2);
}
Vector2 get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor, bool flip_x)
{
if (flip_x)
{
switch(anchor)
{
case AP_TOP_LEFT: anchor = AP_TOP_RIGHT; break;
case AP_TOP_RIGHT: anchor = AP_TOP_LEFT; break;
case AP_MID_LEFT: anchor = AP_MID_RIGHT; break;
case AP_MID_RIGHT: anchor = AP_MID_LEFT; break;
case AP_BOT_LEFT: anchor = AP_BOT_RIGHT; break;
case AP_BOT_RIGHT: anchor = AP_BOT_LEFT; break;
default:
break;
}
}
return internal_get_anchor_offset(bbox, anchor);
}

View File

@ -83,7 +83,7 @@ LevelPack_t* get_level_pack(Assets_t* assets, const char* name);
void draw_sprite(Sprite_t* spr, int frame_num, Vector2 pos, float rotation, bool flip_x);
void draw_sprite_pro(Sprite_t* spr, int frame_num, Vector2 pos, float rotation, uint8_t flip, Vector2 scale, Color colour);
Vector2 get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor);
Vector2 get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor, bool flip_x);
Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor);
typedef struct SFX

View File

@ -438,12 +438,15 @@ static void render_editor_game_scene(Scene_t* scene)
{
pos = Vector2Add(
pos,
get_anchor_offset(p_bbox->size, spr.dest_anchor)
get_anchor_offset(p_bbox->size, spr.dest_anchor, p_cspr->flip_x)
);
pos = Vector2Subtract(pos, spr.src_anchor);
}
pos = Vector2Add(pos, spr.offset);
Vector2 offset = spr.offset;
if (p_cspr->flip_x) offset.x *= -1;
pos = Vector2Add(pos, offset);
draw_sprite(spr.sprite, p_cspr->current_frame, pos, 0.0f, p_cspr->flip_x);
}
}

View File

@ -176,8 +176,7 @@ static bool init_player_file(FILE* in_file, Assets_t* assets)
player_sprite_map[i].sprite = spr;
//player_sprite_map[i].offset = offset;
player_sprite_map[i].offset = (Vector2){0};
player_sprite_map[i].src_anchor = spr->frame_size;
player_sprite_map[i].src_anchor.x /= 2.0f;
player_sprite_map[i].src_anchor = get_anchor_offset(spr->frame_size, AP_BOT_CENTER, false);
player_sprite_map[i].dest_anchor = AP_BOT_CENTER;
i++;
}