From 27f2dba7fb2e4e053dd56a3bad97090299c1d34e Mon Sep 17 00:00:00 2001 From: doyle Date: Sat, 16 Sep 2023 12:46:28 +1000 Subject: [PATCH] renderer: Draw sprite centered on world pos --- feely_pona.cpp | 34 ++++++++++++++++++---------------- feely_pona.h | 15 +++++++++++++++ feely_pona_unity.h | 1 + 3 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 feely_pona.h diff --git a/feely_pona.cpp b/feely_pona.cpp index bf40c45..d822e44 100644 --- a/feely_pona.cpp +++ b/feely_pona.cpp @@ -47,11 +47,12 @@ void TELY_DLL_Init(void *user_data) // NOTE: TELY Game ============================================================================= TELY_Assets *assets = &platform->assets; - TELY_Game *game = Dqn_Arena_New(&platform->arena, TELY_Game, Dqn_ZeroMem_Yes); + Feely_Pona *pona = Dqn_Arena_New(&platform->arena, Feely_Pona, Dqn_ZeroMem_Yes); + TELY_Game *game = &pona->game; game->chunk_pool.arena = &platform->arena; platform->user_data = game; { - TELY_AssetSpriteSheet *sheet = &game->hero_sprite_sheet; + TELY_AssetSpriteSheet *sheet = &pona->hero_sprite_sheet; Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_String8 sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/adventurer-v1.5-sheet.png", DQN_STRING_FMT(assets->textures_dir)); sheet->tex_handle = platform->func_load_texture(assets, DQN_STRING8("Hero"), sheet_path); @@ -82,8 +83,8 @@ void TELY_DLL_Init(void *user_data) {DQN_STRING8("Leap slice C"), /*index*/ 103, /*count*/ 6}, }; - game->hero_sprite_anims = Dqn_Slice_Alloc(&platform->arena, DQN_ARRAY_UCOUNT(hero_anims), Dqn_ZeroMem_No); - DQN_MEMCPY(game->hero_sprite_anims.data, &hero_anims, sizeof(hero_anims[0]) * DQN_ARRAY_UCOUNT(hero_anims)); + pona->hero_sprite_anims = Dqn_Slice_Alloc(&platform->arena, DQN_ARRAY_UCOUNT(hero_anims), Dqn_ZeroMem_No); + DQN_MEMCPY(pona->hero_sprite_anims.data, &hero_anims, sizeof(hero_anims[0]) * DQN_ARRAY_UCOUNT(hero_anims)); } game->entities = Dqn_VArray_Init(&platform->arena, 1024 * 8); @@ -169,9 +170,9 @@ void TELY_DLL_Init(void *user_data) TELY_GameEntity *first_entity = TELY_Game_MakeEntityPointerF(game, "Hero"); first_entity->local_pos = Dqn_V2_InitNx2(100.f, 100.f); first_entity->size_scale = Dqn_V2_InitNx1(4); - first_entity->sprite_sheet = &game->hero_sprite_sheet; - first_entity->sprite_anims = game->hero_sprite_anims; - first_entity->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size); + first_entity->sprite_sheet = &pona->hero_sprite_sheet; + first_entity->sprite_anims = pona->hero_sprite_anims; + first_entity->local_hit_box_size = Dqn_V2_InitV2I(pona->hero_sprite_sheet.sprite_size); first_entity->flags |= TELY_EntityFlag_Clickable; first_entity->flags |= TELY_EntityFlag_MoveByKeyboard; first_entity->flags |= TELY_EntityFlag_MoveByMouse; @@ -179,10 +180,10 @@ void TELY_DLL_Init(void *user_data) uint16_t font_size = 18; game->camera.scale = Dqn_V2_InitNx1(1); - game->inter_regular_font = platform->func_load_font(assets, DQN_STRING8("Inter (Regular)"), DQN_STRING8("Data/Inter-Regular.otf"), font_size); - game->inter_italic_font = platform->func_load_font(assets, DQN_STRING8("Inter (Italic)"), DQN_STRING8("Data/Inter-Italic.otf"), font_size); - game->jetbrains_mono_font = platform->func_load_font(assets, DQN_STRING8("JetBrains Mono NL (Regular)"), DQN_STRING8("Data/JetBrainsMonoNL-Regular.ttf"), font_size); - game->test_audio = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa")); + pona->inter_regular_font = platform->func_load_font(assets, DQN_STRING8("Inter (Regular)"), DQN_STRING8("Data/Inter-Regular.otf"), font_size); + pona->inter_italic_font = platform->func_load_font(assets, DQN_STRING8("Inter (Italic)"), DQN_STRING8("Data/Inter-Italic.otf"), font_size); + pona->jetbrains_mono_font = platform->func_load_font(assets, DQN_STRING8("JetBrains Mono NL (Regular)"), DQN_STRING8("Data/JetBrainsMonoNL-Regular.ttf"), font_size); + pona->test_audio = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa")); // NOTE: TELY audio ============================================================================ @@ -202,14 +203,15 @@ void TELY_DLL_FrameUpdate(void *user_data) TELY_PlatformInput *input = &platform->input; TELY_Assets *assets = &platform->assets; TELY_Renderer *renderer = &platform->renderer; - TELY_Game *game = DQN_CAST(TELY_Game *) platform->user_data; + Feely_Pona *pona = DQN_CAST(Feely_Pona *) platform->user_data; + TELY_Game *game = &pona->game; TELY_UI *ui = &game->ui; TELY_UI_FrameSetup(ui, assets, &platform->frame_arena); - TELY_UI_PushFont(ui, game->jetbrains_mono_font); + TELY_UI_PushFont(ui, pona->jetbrains_mono_font); TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb); - TELY_Render_PushFont(renderer, game->jetbrains_mono_font); + TELY_Render_PushFont(renderer, pona->jetbrains_mono_font); { Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_String8Builder builder = {}; @@ -269,7 +271,7 @@ void TELY_DLL_FrameUpdate(void *user_data) TELY_Audio *audio = &platform->audio; if (audio->playback_size == 0) { - TELY_Audio_Play(audio, game->test_audio, 1.f /*volume*/); + TELY_Audio_Play(audio, pona->test_audio, 1.f /*volume*/); } // ============================================================================================= @@ -401,8 +403,8 @@ void TELY_DLL_FrameUpdate(void *user_data) src_rect.size.h = DQN_CAST(Dqn_f32)sprite_sheet->sprite_size.h; Dqn_Rect dest_rect = {}; - dest_rect.pos = world_pos; dest_rect.size = src_rect.size * entity->size_scale; + dest_rect.pos = world_pos - (dest_rect.size * .5f); if (entity->facing_left) dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally diff --git a/feely_pona.h b/feely_pona.h new file mode 100644 index 0000000..aab0a86 --- /dev/null +++ b/feely_pona.h @@ -0,0 +1,15 @@ +#if defined(__clang__) +#pragma once +#include "feely_pona_unity.h" +#endif + +struct Feely_Pona +{ + TELY_Game game; + TELY_AssetFontHandle inter_regular_font; + TELY_AssetFontHandle inter_italic_font; + TELY_AssetFontHandle jetbrains_mono_font; + Dqn_Slice hero_sprite_anims; + TELY_AssetSpriteSheet hero_sprite_sheet; + TELY_AssetAudioHandle test_audio; +}; diff --git a/feely_pona_unity.h b/feely_pona_unity.h index cc97a0a..30d2fbb 100644 --- a/feely_pona_unity.h +++ b/feely_pona_unity.h @@ -51,6 +51,7 @@ DQN_MSVC_WARNING_DISABLE(4505) // warning C4505: unreferenced function with inte #include "tely_ui.h" #include "tely_rfui.h" #include "tely_game.h" +#include "feely_pona.h" #include "tely_tools.cpp" #include "tely_game.cpp"