Compare commits

...

4 Commits

Author SHA1 Message Date
En Yi f20daa9cce Update main game sprite rendering 2024-08-06 21:43:40 +08:00
En Yi 4a54bfe84f Incorporate anchor point in player sprite info
Changelog:
- src anchorpoint is also an enum. The offset needs to be computed
  dynamically due to flip_x
- Re-add back the offset field.
- Add symbol parsing for anchor point.
2024-08-06 21:36:17 +08:00
En Yi 1215746e05 Add x-flipping for anchoring 2024-08-06 20:36:23 +08:00
En Yi 657110a66d Implement sprite rendering anchor 2024-08-05 21:50:01 +08:00
7 changed files with 166 additions and 89 deletions

View File

@ -192,10 +192,25 @@ typedef struct Sprite {
} Sprite_t;
typedef unsigned int (*sprite_transition_func_t)(Entity_t *ent); // Transition requires knowledge of the entity
typedef enum AnchorPoint {
AP_TOP_LEFT,
AP_TOP_CENTER,
AP_TOP_RIGHT,
AP_MID_LEFT,
AP_MID_CENTER,
AP_MID_RIGHT,
AP_BOT_LEFT,
AP_BOT_CENTER,
AP_BOT_RIGHT,
} AnchorPoint_t;
typedef struct _SpriteRenderInfo
{
Sprite_t* sprite;
Vector2 offset;
AnchorPoint_t src_anchor;
AnchorPoint_t dest_anchor;
} SpriteRenderInfo_t;
typedef struct _CSprite_t {

View File

@ -1,6 +1,7 @@
#include "assets.h"
#include "assert.h"
#include "engine_conf.h"
#include "raymath.h"
#define RRES_RAYLIB_IMPLEMENTATION
#include "rres.h"
@ -551,15 +552,20 @@ void draw_sprite_pro(Sprite_t* spr, int frame_num, Vector2 pos, float rotation,
spr->frame_size.x * ((flip & 1) ? -1 : 1),
spr->frame_size.y * ((flip & 2) ? -1 : 1),
};
Rectangle dest = {
.x = pos.x,
.y = pos.y,
.width = spr->frame_size.x * scale.x,
.height = spr->frame_size.y * scale.y
};
// The anchor here is only for rotation and scaling.
// Translational anchor is expected to be accounted for
// So need to offset render position with anchor position
Vector2 anchor = spr->anchor;
anchor.x *= scale.x;
anchor.y *= scale.y;
Rectangle dest = {
.x = pos.x + anchor.x,
.y = pos.y + anchor.y,
.width = spr->frame_size.x * scale.x,
.height = spr->frame_size.y * scale.y
};
DrawTexturePro(
*spr->texture,
rec,
@ -568,3 +574,73 @@ void draw_sprite_pro(Sprite_t* spr, int frame_num, Vector2 pos, float rotation,
rotation, colour
);
}
static Vector2 internal_get_anchor_offset(Vector2 bbox, AnchorPoint_t anchor)
{
Vector2 offset = {0};
switch (anchor)
{
case AP_TOP_LEFT:
break;
case AP_TOP_CENTER:
offset.x = bbox.x / 2;
break;
case AP_TOP_RIGHT:
offset.x = bbox.x;
break;
case AP_MID_LEFT:
offset.x = 0;
offset.y = bbox.y / 2;
break;
case AP_MID_CENTER:
offset.x = bbox.x / 2;
offset.y = bbox.y / 2;
break;
case AP_MID_RIGHT:
offset.x = bbox.x;
offset.y = bbox.y / 2;
break;
case AP_BOT_LEFT:
offset.x = 0;
offset.y = bbox.y;
break;
case AP_BOT_CENTER:
offset.x = bbox.x / 2;
offset.y = bbox.y;
break;
case AP_BOT_RIGHT:
offset.x = bbox.x;
offset.y = bbox.y;
break;
}
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,6 +83,8 @@ 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, bool flip_x);
Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor);
typedef struct SFX
{

View File

@ -220,6 +220,7 @@ static void render_editor_game_scene(Scene_t* scene)
int y = tile_y * TILE_SIZE;
//if (!tilemap.tiles[i].moveable)
//{
// // Draw water tile
@ -432,7 +433,23 @@ static void render_editor_game_scene(Scene_t* scene)
const SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ent->position, spr.offset);
Vector2 pos = p_ent->position;
if (p_bbox != NULL)
{
pos = Vector2Add(
pos,
get_anchor_offset(p_bbox->size, spr.dest_anchor, p_cspr->flip_x)
);
pos = Vector2Subtract(
pos,
get_anchor_offset(spr.sprite->frame_size, spr.src_anchor, p_cspr->flip_x)
);
}
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

@ -214,7 +214,23 @@ static void render_regular_game_scene(Scene_t* scene)
const SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ent->position, spr.offset);
Vector2 pos = p_ent->position;
if (p_bbox != NULL)
{
pos = Vector2Add(
pos,
get_anchor_offset(p_bbox->size, spr.dest_anchor, p_cspr->flip_x)
);
pos = Vector2Subtract(
pos,
get_anchor_offset(spr.sprite->frame_size, spr.src_anchor, p_cspr->flip_x)
);
}
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);
}
continue;

View File

@ -12,17 +12,6 @@ bool check_in_water(const ParticleEmitter_t* emitter, float delta_time);
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1};
typedef enum AnchorPoint {
AP_TOP_LEFT,
AP_TOP_CENTER,
AP_TOP_RIGHT,
AP_MID_LEFT,
AP_MID_CENTER,
AP_MID_RIGHT,
AP_BOT_LEFT,
AP_BOT_CENTER,
AP_BOT_RIGHT,
} AnchorPoint_t;
static inline unsigned int get_tile_idx(int x, int y, TileGrid_t gridmap)
{
@ -160,73 +149,6 @@ collision_end:
return overlap_mode > 0;
}
static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
{
Vector2 p1;
Vector2 p2;
Vector2 offset = {0};
switch (anchor)
{
case AP_TOP_LEFT:
// When resizing bbox, it is implicitly assumed that to be already in topleft
// due to the coordindate system (+ve towards right and downwards)
// So do nothing
return offset;
case AP_TOP_CENTER:
p1.x = bbox.x / 2;
p1.y = 0;
p2.x = new_bbox.x / 2;
p2.y = 0;
break;
case AP_TOP_RIGHT:
p1.x = bbox.x;
p1.y = 0;
p2.x = new_bbox.x;
p2.y = 0;
break;
case AP_MID_LEFT:
p1.x = 0;
p1.y = bbox.y / 2;
p2.x = 0;
p2.y = new_bbox.y / 2;
break;
case AP_MID_CENTER:
p1.x = bbox.x / 2;
p1.y = bbox.y / 2;
p2.x = new_bbox.x / 2;
p2.y = new_bbox.y / 2;
break;
case AP_MID_RIGHT:
p1.x = bbox.x;
p1.y = bbox.y / 2;
p2.x = new_bbox.x;
p2.y = new_bbox.y / 2;
break;
case AP_BOT_LEFT:
p1.x = 0;
p1.y = bbox.y;
p2.x = 0;
p2.y = new_bbox.y;
break;
case AP_BOT_CENTER:
p1.x = bbox.x / 2;
p1.y = bbox.y;
p2.x = new_bbox.x / 2;
p2.y = new_bbox.y;
break;
case AP_BOT_RIGHT:
p1.x = bbox.x;
p1.y = bbox.y;
p2.x = new_bbox.x;
p2.y = new_bbox.y;
break;
}
offset.x = p1.x - p2.x;
offset.y = p1.y - p2.y;
return offset;
}
void destroy_entity(Scene_t* scene, TileGrid_t* tilemap, Entity_t* p_ent)
{
Vector2 half_size = {0,0};

View File

@ -136,6 +136,30 @@ Entity_t* create_dead_player(EntityManager_t* ent_manager)
return p_ent;
}
static AnchorPoint_t parse_anchor_symbol(const char symbol[2])
{
if (symbol[0] == 't')
{
if (symbol[1] == 'l') return AP_TOP_LEFT;
if (symbol[1] == 'c') return AP_TOP_CENTER;
if (symbol[1] == 'r') return AP_TOP_RIGHT;
}
else if (symbol[0] == 'm')
{
if (symbol[1] == 'l') return AP_MID_LEFT;
if (symbol[1] == 'c') return AP_MID_CENTER;
if (symbol[1] == 'r') return AP_MID_RIGHT;
}
else if (symbol[0] == 'b')
{
if (symbol[1] == 'l') return AP_BOT_LEFT;
if (symbol[1] == 'c') return AP_BOT_CENTER;
if (symbol[1] == 'r') return AP_BOT_RIGHT;
}
return AP_TOP_LEFT;
}
static bool init_player_file(FILE* in_file, Assets_t* assets)
{
static bool already_init = false;
@ -162,18 +186,23 @@ static bool init_player_file(FILE* in_file, Assets_t* assets)
while(*info_str == ' ' || *info_str == '\t') info_str++;
Vector2 offset;
char src_ap_symbol[3];
char dest_ap_symbol[3];
int data_count = sscanf(
info_str, "%f,%f",
&offset.x, &offset.y
info_str, "%f,%f,%2s,%2s",
&offset.x, &offset.y, src_ap_symbol, dest_ap_symbol
);
if (data_count !=2)
if (data_count != 4)
{
printf("Unable to parse info for player at line %lu\n", line_num);
return false;
}
Sprite_t* spr = get_sprite(assets, name);
spr->anchor = Vector2Scale(spr->frame_size, 0.5f);
player_sprite_map[i].sprite = spr;
player_sprite_map[i].offset = offset;
player_sprite_map[i].src_anchor = parse_anchor_symbol(src_ap_symbol);
player_sprite_map[i].dest_anchor = parse_anchor_symbol(dest_ap_symbol);
i++;
}
already_init = true;