Add sprite rotation

scene_man
En Yi 2023-11-02 21:18:47 +08:00
parent b01edded42
commit ad421d724a
10 changed files with 40 additions and 28 deletions

View File

@ -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

View File

@ -183,6 +183,7 @@ typedef struct Sprite {
Texture2D* texture;
Vector2 frame_size;
Vector2 origin;
Vector2 anchor;
int frame_count;
int current_frame;
int elapsed;

View File

@ -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
);
}

View File

@ -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
{

View File

@ -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);
}
}
}

View File

@ -3,6 +3,7 @@
#include "raylib.h"
#include "engine_conf.h"
#include "sc_queue.h"
#include "assets.h"
#include <stdint.h>
#include <stdbool.h>
@ -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;

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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);
}
}
}