implemented simple spritesheet

efd/v1
DavoSK 2023-02-01 17:10:16 +01:00
parent f35c9b8a1c
commit bdd43143f8
6 changed files with 76 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

@ -43,7 +43,7 @@ void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob); ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer); ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee); ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob); //ECS_OBSERVER(ecs, MobDetectPlayers1, EcsOnAdd, components.Mob);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

View File

@ -91,6 +91,44 @@ void platform_input() {
} }
} }
void debug_draw_spritesheet() {
if (nk_begin(game_ui, "Spritesheet debug", nk_rect(660, 100, 240, 800),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_TITLE))
{
nk_layout_row_dynamic(game_ui, 0, 1);
static int spritesheet_frame_id = 0;
if(nk_button_label(game_ui, "Prev")){
spritesheet_frame_id-=10;
}
if(nk_button_label(game_ui, "Next")){
spritesheet_frame_id+=10;
}
static bool loaded = false;
static struct nk_image nuclear_image;
if (!loaded) {
nuclear_image = TextureToNuklear(main_sprite_sheet.texture);
}
nk_layout_row_static(game_ui, 32, 32, 6);
for(size_t i = 0; i < 10; i++) {
int frame = spritesheet_frame_id + i;
float ox = (frame % main_sprite_sheet.framesWide) * main_sprite_sheet.frameSize.x;
float oy = (int)(frame / main_sprite_sheet.framesWide) * main_sprite_sheet.frameSize.y;
nuclear_image.region[0] = (nk_short)ox;
nuclear_image.region[1] = (nk_short)oy;
nuclear_image.region[2] = 32;
nuclear_image.region[3] = 32;
nk_image(game_ui, nuclear_image);
nk_labelf(game_ui, NK_TEXT_ALIGN_LEFT, "%d", frame);
}
nk_end(game_ui);
}
}
void platform_render() { void platform_render() {
platform_resize_window(); platform_resize_window();
@ -120,6 +158,7 @@ void platform_render() {
renderer_debug_draw(); renderer_debug_draw();
debug_draw(); debug_draw();
debug_draw_spritesheet();
notification_draw(); notification_draw();
game_draw_ui(); game_draw_ui();
} }

View File

@ -1,6 +1,10 @@
#include "spritesheet.c"
static Camera2D render_camera; static Camera2D render_camera;
static float zoom_overlay_tran = 0.0f; static float zoom_overlay_tran = 0.0f;
static SpriteSheet main_sprite_sheet = { 0 };
#define CAM_OVERLAY_ZOOM_LEVEL 0.15f #define CAM_OVERLAY_ZOOM_LEVEL 0.15f
#define ALPHA(x) ColorAlpha(x, data->tran_time) #define ALPHA(x) ColorAlpha(x, data->tran_time)
@ -89,8 +93,9 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr
float y = data->y; float y = data->y;
float health = (data->hp / data->max_hp); float health = (data->hp / data->max_hp);
DrawNametag("Player", key, data, x, y-16); DrawNametag("Player", key, data, x, y-16);
DrawTextureRec(GetSpriteTexture2D(assets_find(ASSET_PLAYER)), ASSET_SRC_RECT(), (Vector2){data->x-(WORLD_BLOCK_SIZE/2), data->y-(WORLD_BLOCK_SIZE/2)}, ColorAlpha(WHITE, data->tran_time)); //DrawTextureRec(GetSpriteTexture2D(assets_find(ASSET_PLAYER)), ASSET_SRC_RECT(), (Vector2){data->x-(WORLD_BLOCK_SIZE/2), data->y-(WORLD_BLOCK_SIZE/2)}, ColorAlpha(WHITE, data->tran_time));
//DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time)); //DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
sprite_draw(&main_sprite_sheet, 129, x, y, 0.0f, 2.0f, WHITE);
//if (data->has_items && !data->inside_vehicle) { //if (data->has_items && !data->inside_vehicle) {
// float ix = data->x; // float ix = data->x;
@ -132,7 +137,6 @@ void renderer_draw(void) {
BeginMode2D(render_camera); BeginMode2D(render_camera);
game_world_view_render_world(); game_world_view_render_world();
EndMode2D(); EndMode2D();
} }
@ -159,6 +163,12 @@ void renderer_init(void) {
blocks_setup(); blocks_setup();
assets_setup(); assets_setup();
// NOTE(DavoSK): Init others spritesheets here
main_sprite_sheet.texture = LoadTexture("art/gen/spritesheet.png");
main_sprite_sheet.frameSize = (Vector2){ 32, 32 };
main_sprite_sheet.framesWide = 64;
main_sprite_sheet.origin = (Vector2){ 16, 16 };
} }
void renderer_shutdown(void) { void renderer_shutdown(void) {

View File

@ -0,0 +1,18 @@
typedef struct {
Texture2D texture;
Vector2 frameSize;
int framesWide;
Vector2 origin;
} SpriteSheet;
// enum {
// SPRITE_PLAYER =
// };
void sprite_draw(SpriteSheet* sprite, int frame, float x, float y, float ang, float scale, Color c) {
float ox = (frame % sprite->framesWide) * sprite->frameSize.x;
float oy = (int)(frame / sprite->framesWide) * sprite->frameSize.y;
DrawTexturePro(sprite->texture, (Rectangle){ox, oy, sprite->frameSize.x,sprite->frameSize.y},
(Rectangle){x, y, sprite->frameSize.x * scale, sprite->frameSize.y * scale},
(Vector2){sprite->origin.x * scale, sprite->origin.y * scale}, ang, c);
}

View File

@ -542,6 +542,11 @@ DrawNuklear(struct nk_context * ctx)
Rectangle dest = {(float)i->x * scale, (float)i->y * scale, (float)i->w * scale, (float)i->h * scale}; Rectangle dest = {(float)i->x * scale, (float)i->y * scale, (float)i->w * scale, (float)i->h * scale};
Vector2 origin = {0, 0}; Vector2 origin = {0, 0};
Color tint = ColorFromNuklear(i->col); Color tint = ColorFromNuklear(i->col);
if(i->img.region[0] || i->img.region[1] || i->img.region[2] || i->img.region[3]) {
source = (Rectangle){(float)i->img.region[0], (float)i->img.region[1], (float)i->img.region[2], (float)i->img.region[3]};
}
DrawTexturePro(texture, source, dest, origin, 0, tint); DrawTexturePro(texture, source, dest, origin, 0, tint);
} break; } break;