Add horizontal flip to sprites

scene_man
En Yi 2023-05-23 21:33:05 +08:00
parent d2de5cfed9
commit b1a6430eb7
6 changed files with 12 additions and 9 deletions

View File

@ -43,8 +43,8 @@ int main(void)
DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED); DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED);
// Draw the static Sprite and animated Sprite // Draw the static Sprite and animated Sprite
draw_sprite(spr, (Vector2){64,128}); draw_sprite(spr, (Vector2){64,128}, false);
draw_sprite(spr2, (Vector2){64,180}); draw_sprite(spr2, (Vector2){64,180}, true);
EndDrawing(); EndDrawing();
// Update the animated Sprite // Update the animated Sprite

View File

@ -53,7 +53,7 @@ static void level_scene_render_func(Scene_t* scene)
if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL) if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL)
{ {
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}); draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, false);
} }
else if (tilemap.tiles[i].tile_type == SOLID_TILE) else if (tilemap.tiles[i].tile_type == SOLID_TILE)
{ {
@ -126,7 +126,7 @@ static void level_scene_render_func(Scene_t* scene)
if (spr.sprite != NULL) if (spr.sprite != NULL)
{ {
Vector2 pos = Vector2Add(p_ct->position, spr.offset); Vector2 pos = Vector2Add(p_ct->position, spr.offset);
draw_sprite(spr.sprite, pos); draw_sprite(spr.sprite, pos, p_cspr->flip_x);
} }
} }
} }

View File

@ -124,14 +124,14 @@ typedef struct _SpriteRenderInfo
{ {
Sprite_t* sprite; Sprite_t* sprite;
Vector2 offset; Vector2 offset;
bool flip_x;
bool flip_y;
} SpriteRenderInfo_t; } SpriteRenderInfo_t;
typedef struct _CSprite_t { typedef struct _CSprite_t {
SpriteRenderInfo_t* sprites; SpriteRenderInfo_t* sprites;
sprite_transition_func_t transition_func; sprite_transition_func_t transition_func;
unsigned int current_idx; unsigned int current_idx;
bool flip_x;
bool flip_y;
} CSprite_t; } CSprite_t;
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)

View File

@ -151,12 +151,12 @@ Font* get_font(Assets_t* assets, const char* name)
return NULL; return NULL;
} }
void draw_sprite(Sprite_t* spr, Vector2 pos) void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x)
{ {
Rectangle rec = { Rectangle rec = {
spr->origin.x + spr->frame_size.x * spr->current_frame, spr->origin.x + spr->frame_size.x * spr->current_frame,
spr->origin.y, spr->origin.y,
spr->frame_size.x, spr->frame_size.x * (flip_x ? -1:1),
spr->frame_size.y spr->frame_size.y
}; };
DrawTextureRec(*spr->texture, rec, pos, WHITE); DrawTextureRec(*spr->texture, rec, pos, WHITE);

View File

@ -28,5 +28,5 @@ Sprite_t* get_sprite(Assets_t* assets, const char* name);
Sound* get_sound(Assets_t* assets, const char* name); Sound* get_sound(Assets_t* assets, const char* name);
Font* get_font(Assets_t* assets, const char* name); Font* get_font(Assets_t* assets, const char* name);
void draw_sprite(Sprite_t* spr, Vector2 pos); void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x);
#endif // __ASSETS_H #endif // __ASSETS_H

View File

@ -16,6 +16,9 @@ static SpriteRenderInfo_t player_sprite_map[N_PLAYER_SPRITES] = {0};
static unsigned int player_sprite_transition_func(Entity_t* ent) static unsigned int player_sprite_transition_func(Entity_t* ent)
{ {
CTransform_t* p_ctrans = get_component(ent, CTRANSFORM_COMP_T); CTransform_t* p_ctrans = get_component(ent, CTRANSFORM_COMP_T);
CSprite_t* p_spr = get_component(ent, CSPRITE_T);
if (p_ctrans->velocity.x > 0) p_spr->flip_x = true;
else if (p_ctrans->velocity.x < 0) p_spr->flip_x = false;
if (Vector2LengthSqr(p_ctrans->velocity) > 10000.0f) if (Vector2LengthSqr(p_ctrans->velocity) > 10000.0f)
{ {
return SPR_PLAYER_RUN; return SPR_PLAYER_RUN;