diff --git a/assets_test.c b/assets_test.c index 9b67f89..0ee0e68 100644 --- a/assets_test.c +++ b/assets_test.c @@ -43,8 +43,8 @@ int main(void) DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED); // Draw the static Sprite and animated Sprite - draw_sprite(spr, (Vector2){64,128}, false); - draw_sprite(spr2, (Vector2){64,180}, true); + draw_sprite(spr, (Vector2){64,128}, 0.0f, false); + draw_sprite(spr2, (Vector2){64,180}, 0.0f, true); EndDrawing(); // Update the animated Sprite diff --git a/engine/EC.h b/engine/EC.h index 844c433..fb322f5 100644 --- a/engine/EC.h +++ b/engine/EC.h @@ -183,6 +183,7 @@ typedef struct Sprite { Texture2D* texture; Vector2 frame_size; Vector2 origin; + Vector2 anchor; int frame_count; int current_frame; int elapsed; diff --git a/engine/assets.c b/engine/assets.c index ccecd73..b9ba3e2 100644 --- a/engine/assets.c +++ b/engine/assets.c @@ -429,7 +429,7 @@ LevelPack_t* get_level_pack(Assets_t* assets, const char* name) return NULL; } -void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x) +void draw_sprite(Sprite_t* spr, Vector2 pos, float rotation, bool flip_x) { Rectangle rec = { spr->origin.x + spr->frame_size.x * spr->current_frame, @@ -437,5 +437,18 @@ void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x) spr->frame_size.x * (flip_x ? -1:1), spr->frame_size.y }; - DrawTextureRec(*spr->texture, rec, pos, WHITE); + //DrawTextureRec(*spr->texture, rec, pos, WHITE); + Rectangle dest = { + .x = pos.x - spr->anchor.x, + .y = pos.y - spr->anchor.y, + .width = spr->frame_size.x, + .height = spr->frame_size.y + }; + DrawTexturePro( + *spr->texture, + rec, + dest, + spr->anchor, + rotation, WHITE + ); } diff --git a/engine/assets.h b/engine/assets.h index 2642218..d51b172 100644 --- a/engine/assets.h +++ b/engine/assets.h @@ -64,7 +64,7 @@ Sound* get_sound(Assets_t* assets, const char* name); Font* get_font(Assets_t* assets, const char* name); LevelPack_t* get_level_pack(Assets_t* assets, const char* name); -void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x); +void draw_sprite(Sprite_t* spr, Vector2 pos, float rotation, bool flip_x); typedef struct SFX { diff --git a/engine/particle_sys.c b/engine/particle_sys.c index f0dbf7c..2816be5 100644 --- a/engine/particle_sys.c +++ b/engine/particle_sys.c @@ -119,7 +119,7 @@ void draw_particle_system(ParticleSystem_t* system) { if (part->alive) { - if (emitter->tex == NULL || emitter->tex->width == 0) + if (emitter->spr->texture == NULL || emitter->spr->texture->width == 0) { Rectangle rect = { .x = part->position.x, @@ -135,21 +135,7 @@ void draw_particle_system(ParticleSystem_t* system) } else { - Rectangle source = { - 0.0f, 0.0f, - (float)emitter->tex->width, (float)emitter->tex->height - }; - - Rectangle dest = { - part->position.x, part->position.y, - (float)emitter->tex->width, (float)emitter->tex->height - }; - Vector2 origin = { (float)emitter->tex->width / 2, (float)emitter->tex->height / 2 }; - DrawTexturePro( - *emitter->tex, - source, dest, origin, - part->rotation, WHITE - ); + draw_sprite(emitter->spr, part->position, part->rotation, false); } } } diff --git a/engine/particle_sys.h b/engine/particle_sys.h index 3c24ea8..09094d2 100644 --- a/engine/particle_sys.h +++ b/engine/particle_sys.h @@ -3,6 +3,7 @@ #include "raylib.h" #include "engine_conf.h" #include "sc_queue.h" +#include "assets.h" #include #include @@ -41,7 +42,7 @@ typedef struct ParticleEmitter Vector2 position; Particle_t particles[MAX_PARTICLES]; uint32_t n_particles; - Texture2D* tex; + Sprite_t* spr; uint32_t timer; bool one_shot; bool finished; diff --git a/particle_test.c b/particle_test.c index e4b70b3..2473650 100644 --- a/particle_test.c +++ b/particle_test.c @@ -52,6 +52,17 @@ int main(void) init_particle_system(&part_sys); Texture2D tex = LoadTexture("res/bomb.png"); + Sprite_t spr = { + .texture = &tex, + .frame_size = (Vector2){tex.width, tex.height}, + .origin = (Vector2){0, 0}, + .anchor = (Vector2){tex.width / 2, tex.height / 2}, + .frame_count = 0, + .current_frame = 0, + .elapsed = 0, + .speed = 0, + .name = "test_spr" + }; EmitterConfig_t conf ={ .launch_range = {0, 360}, @@ -64,7 +75,7 @@ int main(void) .config = conf, .n_particles = MAX_PARTICLES, .one_shot = true, - .tex = &tex, + .spr = &spr, }; bool key_press = false; diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 46e2851..40192a0 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -225,7 +225,7 @@ static void render_editor_game_scene(Scene_t* scene) uint8_t tile_sprite_idx = tilemap.tiles[i].tile_type + tilemap.tiles[i].rotation; if (data->tile_sprites[tile_sprite_idx] != NULL) { - draw_sprite(data->tile_sprites[tile_sprite_idx], (Vector2){x,y}, false); + draw_sprite(data->tile_sprites[tile_sprite_idx], (Vector2){x,y}, 0.0f, false); } else if (tilemap.tiles[i].tile_type == SOLID_TILE) { @@ -429,7 +429,7 @@ static void render_editor_game_scene(Scene_t* scene) if (spr.sprite != NULL) { Vector2 pos = Vector2Add(p_ct->position, spr.offset); - draw_sprite(spr.sprite, pos, p_cspr->flip_x); + draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x); } } } diff --git a/scenes/game_scene.c b/scenes/game_scene.c index 95a3ea9..6041411 100644 --- a/scenes/game_scene.c +++ b/scenes/game_scene.c @@ -131,7 +131,7 @@ static void render_regular_game_scene(Scene_t* scene) if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL) { - draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, false); + draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, 0.0f, false); } else if (tilemap.tiles[i].tile_type == SOLID_TILE) { @@ -223,7 +223,7 @@ static void render_regular_game_scene(Scene_t* scene) if (spr.sprite != NULL) { Vector2 pos = Vector2Add(p_ct->position, spr.offset); - draw_sprite(spr.sprite, pos, p_cspr->flip_x); + draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x); } continue; } diff --git a/water_test.c b/water_test.c index cdccfe1..e8f1a29 100644 --- a/water_test.c +++ b/water_test.c @@ -109,7 +109,7 @@ static void level_scene_render_func(Scene_t* scene) if (spr.sprite != NULL) { Vector2 pos = Vector2Add(p_ct->position, spr.offset); - draw_sprite(spr.sprite, pos, p_cspr->flip_x); + draw_sprite(spr.sprite, pos, 0.0f, p_cspr->flip_x); } } }