feely_pona/feely_pona.cpp

1267 lines
64 KiB
C++
Raw Normal View History

2023-09-16 02:21:24 +00:00
#if defined(__clang__)
#pragma once
#include "feely_pona_unity.h"
#endif
extern "C" __declspec(dllexport)
void TELY_DLL_Reload(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
Dqn_Library_SetPointer(platform->core.dqn_lib);
}
extern "C" __declspec(dllexport)
void TELY_DLL_Init(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
TELY_DLL_Reload(user_data);
2023-09-19 14:21:05 +00:00
{
uint32_t array[] = {1};
Dqn_BinarySearchResult result = {};
// NOTE: Match =============================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 1);
// NOTE: Lower bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 1);
// NOTE: Upper bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 1);
}
{
uint32_t array[] = {1, 2};
// NOTE: Match =============================================================================
Dqn_BinarySearchResult result = {};
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 2);
// NOTE: Lower bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 2);
// NOTE: Upper bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 2);
}
{
uint32_t array[] = {1, 2, 3};
Dqn_BinarySearchResult result = {};
// NOTE: Match =============================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 3);
// NOTE: Lower bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 3);
// NOTE: Upper bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 3);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 3);
}
{
uint32_t array[] = {1, 2, 3, 4};
Dqn_BinarySearchResult result = {};
// NOTE: Match =============================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 3);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 5U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 4);
// NOTE: Lower bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 3);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 5U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 4);
// NOTE: Upper bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 1);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 3);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 4);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 5U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 4);
}
{
uint32_t array[] = {1, 1, 2, 2, 3};
Dqn_BinarySearchResult result = {};
// NOTE: Match =============================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 4);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_Match);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 5);
// NOTE: Lower bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 4);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 4U /*find*/, Dqn_BinarySearchType_LowerBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 5);
// NOTE: Upper bound =======================================================================
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 0U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 0);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 1U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 2);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 2U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(result.found);
DQN_ASSERT(result.index == 4);
result = Dqn_BinarySearch<uint32_t>(array, DQN_ARRAY_UCOUNT(array), 3U /*find*/, Dqn_BinarySearchType_UpperBound);
DQN_ASSERT(!result.found);
DQN_ASSERT(result.index == 5);
}
2023-09-16 02:21:24 +00:00
{
Dqn_Arena_TempMemoryScope(&platform->arena);
TELY_ChunkPool pool = {};
pool.arena = &platform->arena;
void *bytes16 = TELY_ChunkPool_Alloc(&pool, 16);
TELY_ChunkPool_Dealloc(&pool, bytes16);
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_16b] == DQN_CAST(void *)(DQN_CAST(char *)bytes16 - sizeof(TELY_ChunkPoolSlot)));
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_16b]->next == nullptr);
void *bytes17 = TELY_ChunkPool_Alloc(&pool, 17);
TELY_ChunkPool_Dealloc(&pool, bytes17);
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_32b] == DQN_CAST(void *)(DQN_CAST(char *)bytes17 - sizeof(TELY_ChunkPoolSlot)));
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_32b]->next == nullptr);
void *bytes1 = TELY_ChunkPool_Alloc(&pool, 1);
void *bytes2 = TELY_ChunkPool_Alloc(&pool, 1);
TELY_ChunkPool_Dealloc(&pool, bytes1);
TELY_ChunkPool_Dealloc(&pool, bytes2);
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_16b] == DQN_CAST(void *)(DQN_CAST(char *)bytes2 - sizeof(TELY_ChunkPoolSlot)));
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_16b]->next == DQN_CAST(void *)(DQN_CAST(char *)bytes1 - sizeof(TELY_ChunkPoolSlot)));
void *bytes128k = TELY_ChunkPool_Alloc(&pool, DQN_KILOBYTES(128));
TELY_ChunkPool_Dealloc(&pool, bytes128k);
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_128k] == DQN_CAST(void *)(DQN_CAST(char *)bytes128k - sizeof(TELY_ChunkPoolSlot)));
DQN_ASSERT(pool.slots[TELY_ChunkPoolSlotSize_128k]->next == nullptr);
}
// NOTE: TELY Game =============================================================================
TELY_Assets *assets = &platform->assets;
2023-09-17 10:24:07 +00:00
FP_Game *game = Dqn_Arena_New(&platform->arena, FP_Game, Dqn_ZeroMem_Yes);
game->chunk_pool = &platform->chunk_pool;
2023-09-16 02:21:24 +00:00
platform->user_data = game;
{
2023-09-17 10:24:07 +00:00
TELY_AssetSpriteSheet *sheet = &game->hero_sprite_sheet;
2023-09-16 02:21:24 +00:00
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);
sheet->sprite_count = 109;
sheet->sprites_per_row = 7;
sheet->sprite_size = Dqn_V2I_InitNx2(50, 37);
TELY_AssetSpriteAnimation hero_anims[] = {
2023-09-18 13:00:30 +00:00
{DQN_STRING8("Everything"), /*index*/ 0, /*count*/ sheet->sprite_count, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Idle"), /*index*/ 0, /*count*/ 3, /*seconds_per_frame*/ 1 / 4.f},
{DQN_STRING8("Run"), /*index*/ 8, /*count*/ 6, /*seconds_per_frame*/ 1 / 8.f},
{DQN_STRING8("Jump"), /*index*/ 14, /*count*/ 10, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Floor slide"), /*index*/ 24, /*count*/ 5, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Unknown"), /*index*/ 29, /*count*/ 9, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Attack A"), /*index*/ 42, /*count*/ 7, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Attack B"), /*index*/ 49, /*count*/ 4, /*seconds_per_frame*/ 1 / 8.f},
{DQN_STRING8("Attack C"), /*index*/ 53, /*count*/ 6, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Hurt A"), /*index*/ 59, /*count*/ 5, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Hurt B"), /*index*/ 64, /*count*/ 5, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Unsheath sword"), /*index*/ 69, /*count*/ 4, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Sheath sword"), /*index*/ 73, /*count*/ 4, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Air drift"), /*index*/ 77, /*count*/ 2, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Air drop"), /*index*/ 79, /*count*/ 2, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Ladder climb"), /*index*/ 81, /*count*/ 4, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Chi push"), /*index*/ 85, /*count*/ 8, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Leap slice A"), /*index*/ 93, /*count*/ 7, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Leap slice B"), /*index*/ 100, /*count*/ 3, /*seconds_per_frame*/ 1 / 12.f},
{DQN_STRING8("Leap slice C"), /*index*/ 103, /*count*/ 6, /*seconds_per_frame*/ 1 / 12.f},
2023-09-16 02:21:24 +00:00
};
2023-09-17 10:24:07 +00:00
game->hero_sprite_anims = Dqn_Slice_Alloc<TELY_AssetSpriteAnimation>(&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));
2023-09-16 02:21:24 +00:00
}
2023-09-17 10:24:07 +00:00
game->entities = Dqn_VArray_Init<FP_GameEntity>(&platform->arena, 1024 * 8);
2023-09-18 11:46:42 +00:00
game->root_entity = Dqn_VArray_Make(&game->entities, Dqn_ZeroMem_No);
2023-09-16 02:21:24 +00:00
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
// NOTE: Unit test DFS pre-order and post-order walk
{
// NOTE: Setup entity-tree =================================================================
2023-09-17 10:24:07 +00:00
FP_GameEntity *f = FP_Game_MakeEntityPointerF(game, "F");
FP_Game_PushParentEntity(game, f->handle);
FP_GameEntity *b = FP_Game_MakeEntityPointerF(game, "B");
FP_GameEntity *g = FP_Game_MakeEntityPointerF(game, "G");
FP_Game_PushParentEntity(game, b->handle);
FP_GameEntity *a = FP_Game_MakeEntityPointerF(game, "A");
FP_GameEntity *d = FP_Game_MakeEntityPointerF(game, "D");
FP_Game_PushParentEntity(game, d->handle);
FP_GameEntity *c = FP_Game_MakeEntityPointerF(game, "C");
FP_GameEntity *e = FP_Game_MakeEntityPointerF(game, "E");
FP_Game_PopParentEntity(game);
FP_Game_PopParentEntity(game);
FP_Game_PushParentEntity(game, g->handle);
FP_GameEntity *i = FP_Game_MakeEntityPointerF(game, "I");
FP_Game_PushParentEntity(game, i->handle);
FP_GameEntity *h = FP_Game_MakeEntityPointerF(game, "H");
FP_Game_PopParentEntity(game);
FP_Game_PopParentEntity(game);
FP_Game_PopParentEntity(game);
2023-09-16 02:21:24 +00:00
// NOTE: Pre order test ====================================================================
2023-09-17 10:24:07 +00:00
FP_GameEntity *pre_order_walk[9] = {};
2023-09-16 02:21:24 +00:00
Dqn_usize pre_order_walk_count = 0;
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity);) {
2023-09-16 02:21:24 +00:00
DQN_ASSERT(pre_order_walk_count < DQN_ARRAY_UCOUNT(pre_order_walk));
pre_order_walk[pre_order_walk_count++] = it.entity;
}
DQN_ASSERT(pre_order_walk_count == DQN_ARRAY_UCOUNT(pre_order_walk));
DQN_ASSERT(pre_order_walk[0] == f);
DQN_ASSERT(pre_order_walk[1] == b);
DQN_ASSERT(pre_order_walk[2] == a);
DQN_ASSERT(pre_order_walk[3] == d);
DQN_ASSERT(pre_order_walk[4] == c);
DQN_ASSERT(pre_order_walk[5] == e);
DQN_ASSERT(pre_order_walk[6] == g);
DQN_ASSERT(pre_order_walk[7] == i);
DQN_ASSERT(pre_order_walk[8] == h);
// NOTE: Post order test ===================================================================
2023-09-17 10:24:07 +00:00
FP_GameEntity *post_order_walk[9] = {};
2023-09-16 02:21:24 +00:00
Dqn_usize post_order_walk_count = 0;
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity);) {
2023-09-16 02:21:24 +00:00
DQN_ASSERT(post_order_walk_count < DQN_ARRAY_UCOUNT(post_order_walk));
post_order_walk[post_order_walk_count++] = it.entity;
}
DQN_ASSERT(post_order_walk_count == DQN_ARRAY_UCOUNT(post_order_walk));
DQN_ASSERT(post_order_walk[0] == a);
DQN_ASSERT(post_order_walk[1] == c);
DQN_ASSERT(post_order_walk[2] == e);
DQN_ASSERT(post_order_walk[3] == d);
DQN_ASSERT(post_order_walk[4] == b);
DQN_ASSERT(post_order_walk[5] == h);
DQN_ASSERT(post_order_walk[6] == i);
DQN_ASSERT(post_order_walk[7] == g);
DQN_ASSERT(post_order_walk[8] == f);
// NOTE: Cleanup ===========================================================================
2023-09-17 10:24:07 +00:00
FP_Game_DeleteEntity(game, game->root_entity->handle);
2023-09-16 02:21:24 +00:00
DQN_ASSERT(game->root_entity->first_child == nullptr);
DQN_ASSERT(game->root_entity->last_child == nullptr);
DQN_ASSERT(game->root_entity->next == nullptr);
DQN_ASSERT(game->root_entity->prev == nullptr);
DQN_ASSERT(game->root_entity->parent == nullptr);
}
2023-09-16 07:32:25 +00:00
// NOTE: Hero
2023-09-16 02:21:24 +00:00
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *hero = FP_Game_MakeEntityPointerF(game, "Hero");
2023-09-16 07:32:25 +00:00
hero->local_pos = Dqn_V2_InitNx2(100.f, 100.f);
hero->size_scale = Dqn_V2_InitNx1(4);
2023-09-17 10:24:07 +00:00
hero->sprite_sheet = &game->hero_sprite_sheet;
hero->sprite_anims = game->hero_sprite_anims;
hero->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size);
hero->flags |= FP_EntityFlag_Clickable;
hero->flags |= FP_EntityFlag_MoveByKeyboard;
hero->flags |= FP_EntityFlag_MoveByMouse;
2023-09-16 07:32:25 +00:00
game->clicked_entity = hero->handle;
}
// NOTE: Enemy
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *enemy = FP_Game_MakeEntityPointerF(game, "Enemy");
2023-09-16 07:32:25 +00:00
enemy->local_pos = Dqn_V2_InitNx2(300.f, 300.f);
enemy->size_scale = Dqn_V2_InitNx1(4);
2023-09-17 10:24:07 +00:00
enemy->sprite_sheet = &game->hero_sprite_sheet;
enemy->sprite_anims = game->hero_sprite_anims;
enemy->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size);
enemy->flags |= FP_EntityFlag_Clickable;
enemy->flags |= FP_EntityFlag_MoveByKeyboard;
enemy->flags |= FP_EntityFlag_MoveByMouse;
2023-09-16 02:21:24 +00:00
}
2023-09-16 09:06:16 +00:00
// NOTE: Wall
{
2023-09-17 10:24:07 +00:00
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "V. Wall");
2023-09-16 09:06:16 +00:00
entity->local_pos = Dqn_V2_InitNx2(100.f, 300.f);
entity->local_hit_box_size = Dqn_V2_InitNx2(100.f, 300.f);
2023-09-17 10:24:07 +00:00
entity->flags |= FP_EntityFlag_Clickable;
entity->flags |= FP_EntityFlag_MoveByKeyboard;
entity->flags |= FP_EntityFlag_MoveByMouse;
2023-09-16 09:06:16 +00:00
2023-09-17 10:24:07 +00:00
FP_GameShape *wall = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
wall->type = FP_GameShapeType_Rect;
2023-09-16 09:06:16 +00:00
wall->p2 = entity->local_hit_box_size;
wall->colour = TELY_COLOUR_GREEN_DARK_KHAKI_V4;
}
2023-09-16 02:33:40 +00:00
uint16_t font_size = 18;
2023-09-16 02:21:24 +00:00
game->camera.scale = Dqn_V2_InitNx1(1);
2023-09-17 10:24:07 +00:00
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"));
2023-09-16 02:21:24 +00:00
}
2023-09-16 14:37:26 +00:00
struct AStarNode
{
Dqn_usize cost;
Dqn_usize heuristic;
Dqn_V2I came_from;
};
2023-09-16 09:06:16 +00:00
Dqn_f32 const FP_TILE_SIZE = 37.f;
2023-09-16 14:37:26 +00:00
Dqn_Slice<Dqn_V2I> AStarPathFind(Dqn_Arena *arena, TELY_Platform *platform, Dqn_V2I src_tile, Dqn_V2I dest_tile)
{
Dqn_DSMap<AStarNode> astar_info = Dqn_DSMap_Init<AStarNode>(128);
DQN_DEFER { Dqn_DSMap_Deinit(&astar_info); };
Dqn_FArray<Dqn_V2I, 128> frontier = {};
Dqn_FArray_Add(&frontier, src_tile);
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / FP_TILE_SIZE);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / FP_TILE_SIZE);
// NOTE: Initialise the starting cost
uint64_t src_tile_u64 = (DQN_CAST(uint64_t)src_tile.y << 32) | (DQN_CAST(uint64_t)src_tile.x << 0);
Dqn_DSMap_MakeKeyU64(&astar_info, src_tile_u64);
while (frontier.size) {
Dqn_V2I curr_tile = Dqn_FArray_PopFront(&frontier, 1);
if (curr_tile == dest_tile)
break;
Dqn_FArray<Dqn_V2I, 4> neighbours = {};
{
Dqn_V2I left = Dqn_V2I_InitNx2(curr_tile.x - 1, curr_tile.y);
Dqn_V2I right = Dqn_V2I_InitNx2(curr_tile.x + 1, curr_tile.y);
Dqn_V2I top = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y - 1);
Dqn_V2I bottom = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y + 1);
if (left.x >= 0)
Dqn_FArray_Add(&neighbours, left);
if (right.x <= tile_count_x)
Dqn_FArray_Add(&neighbours, right);
if (top.y >= 0)
Dqn_FArray_Add(&neighbours, top);
if (bottom.y <= tile_count_y)
Dqn_FArray_Add(&neighbours, bottom);
}
uint64_t const curr_tile_u64 = (DQN_CAST(uint64_t)curr_tile.y << 32) | (DQN_CAST(uint64_t)curr_tile.x << 0);
Dqn_usize const curr_cost = Dqn_DSMap_FindKeyU64(&astar_info, curr_tile_u64).value->cost;
for (Dqn_V2I next_tile : neighbours) {
Dqn_usize new_cost = curr_cost + 1;
uint64_t next_tile_u64 = (DQN_CAST(uint64_t)next_tile.y << 32) | (DQN_CAST(uint64_t)next_tile.x << 0);
Dqn_DSMapResult<AStarNode> next_cost_result = Dqn_DSMap_MakeKeyU64(&astar_info, next_tile_u64);
if (next_cost_result.found && new_cost >= next_cost_result.value->cost)
continue;
Dqn_usize manhattan_dist = DQN_ABS(dest_tile.x - next_tile.x) + DQN_ABS(dest_tile.y - next_tile.y);
next_cost_result.value->cost = new_cost;
next_cost_result.value->came_from = curr_tile;
next_cost_result.value->heuristic = new_cost + manhattan_dist;
2023-09-19 14:21:05 +00:00
struct AStarBinarySearchContext
{
Dqn_DSMap<AStarNode> *astar_info;
Dqn_f32 heuristic;
};
2023-09-16 14:37:26 +00:00
// TODO(doyle): Find the insert location into the frontier
bool inserted = false;
DQN_FOR_UINDEX(index, frontier.size) {
Dqn_V2I frontier_tile = frontier.data[index];
uint64_t frontier_tile_u64 = DQN_CAST(uint64_t)frontier_tile.y << 32 | DQN_CAST(uint64_t)frontier_tile.x << 0;
Dqn_usize frontier_heuristic = Dqn_DSMap_FindKeyU64(&astar_info, frontier_tile_u64).value->heuristic;
if (next_cost_result.value->heuristic >= frontier_heuristic)
continue;
2023-09-18 11:46:42 +00:00
Dqn_FArray_Insert(&frontier, index, next_tile);
2023-09-16 14:37:26 +00:00
inserted = true;
break;
}
if (inserted)
continue;
Dqn_FArray_Add(&frontier, next_tile);
}
}
Dqn_usize slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; slice_size++) {
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
Dqn_Slice<Dqn_V2I> result = Dqn_Slice_Alloc<Dqn_V2I>(arena, slice_size, Dqn_ZeroMem_No);
slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; ) {
result.data[slice_size++] = it;
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
DQN_ASSERT(result.size == slice_size);
return result;
}
2023-09-17 10:24:07 +00:00
void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, TELY_PlatformInput *input)
2023-09-16 07:32:25 +00:00
{
2023-09-16 02:21:24 +00:00
if (TELY_Platform_InputKeyIsReleased(input->mouse_left))
game->clicked_entity = game->prev_active_entity;
Dqn_V2 dir_vector = {};
// NOTE: Keyboard movement input
2023-09-16 02:21:24 +00:00
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_W))
dir_vector.y = -1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_A))
dir_vector.x = -1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_S))
dir_vector.y = +1.f;
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_D))
dir_vector.x = +1.f;
// NOTE: Gamepad movement input
for (uint32_t gamepad = 0; gamepad < MAX_GAMEPADS; gamepad++) {
if (input->button_codes[gamepad]) {
dir_vector.x += input->left_stick[gamepad].x;
dir_vector.y += input->left_stick[gamepad].y;
}
}
2023-09-21 07:18:32 +00:00
if (dir_vector.x && dir_vector.y) {
dir_vector.x *= 0.7071067811865475244f;
dir_vector.y *= 0.7071067811865475244f;
}
2023-09-16 02:21:24 +00:00
if (game->clicked_entity.id) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Delete))
2023-09-17 10:24:07 +00:00
FP_Game_DeleteEntity(game, game->clicked_entity);
2023-09-16 02:21:24 +00:00
} else {
game->camera.world_pos += dir_vector * 5.f;
}
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
2023-09-16 02:21:24 +00:00
entity->alive_time_s += input->delta_s;
// NOTE: Move entity by keyboard ===========================================================
2023-09-16 07:32:25 +00:00
Dqn_V2 acceleration = {};
2023-09-21 07:11:07 +00:00
if (game->clicked_entity == entity->handle &&
(entity->action.state == FP_GameEntityState_Run || entity->action.state == FP_GameEntityState_Idle)) {
2023-09-17 10:24:07 +00:00
if (entity->flags & FP_EntityFlag_MoveByKeyboard) {
2023-09-16 07:32:25 +00:00
acceleration = dir_vector * 10000000.f;
2023-09-16 02:21:24 +00:00
if (dir_vector.x)
entity->facing_left = dir_vector.x < 0.f;
}
}
2023-09-17 11:55:59 +00:00
// NOTE: Stalk entity ======================================================================
#if 0
2023-09-17 10:24:07 +00:00
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
2023-09-16 14:37:26 +00:00
if (entity->name == DQN_STRING8("Enemy")) {
if (entity->handle != game->clicked_entity && entity->stalk_entity != game->clicked_entity) {
entity->stalk_entity = game->clicked_entity;
}
2023-09-17 10:24:07 +00:00
FP_GameEntity *stalk_entity = FP_Game_GetEntity(game, entity->stalk_entity);
2023-09-16 14:37:26 +00:00
if (stalk_entity) {
2023-09-17 10:24:07 +00:00
Dqn_V2 stalk_world_pos = FP_Game_CalcEntityWorldPos(game, stalk_entity->handle);
2023-09-16 14:37:26 +00:00
Dqn_V2I stalk_tile = Dqn_V2I_InitNx2(stalk_world_pos.x / FP_TILE_SIZE, stalk_world_pos.y / FP_TILE_SIZE);
if (entity->stalk_entity_last_known_tile != stalk_tile) {
entity->stalk_entity_last_known_tile = stalk_tile;
// NOTE: Dealloc all waypoints
2023-09-17 10:24:07 +00:00
for (FP_GameWaypoint *waypoint = entity->waypoints->next; waypoint != entity->waypoints; ) {
FP_GameWaypoint *next = waypoint->next;
TELY_ChunkPool_Dealloc(game->chunk_pool, waypoint);
2023-09-16 14:37:26 +00:00
waypoint = next;
}
entity->waypoints->next = entity->waypoints;
entity->waypoints->prev = entity->waypoints;
Dqn_V2I entity_tile = Dqn_V2I_InitNx2(entity_world_pos.x / FP_TILE_SIZE, entity_world_pos.y / FP_TILE_SIZE);
Dqn_Slice<Dqn_V2I> path_find = AStarPathFind(&platform->arena, platform, entity_tile, stalk_tile);
for (Dqn_usize index = path_find.size - 1; index < path_find.size; index--) {
FP_GameWaypoint *waypoint = TELY_ChunkPool_New(game->chunk_pool, FP_GameWaypoint);
waypoint->pos = path_find.data[index];
waypoint->next = entity->waypoints;
waypoint->prev = entity->waypoints->prev;
waypoint->next->prev = waypoint;
waypoint->prev->next = waypoint;
2023-09-16 14:37:26 +00:00
}
}
}
}
2023-09-17 10:24:07 +00:00
for (FP_GameWaypoint *waypoint = entity->waypoints->next; waypoint != entity->waypoints; waypoint = waypoint->next) {
2023-09-16 14:37:26 +00:00
Dqn_V2 circle_pos = Dqn_V2_InitNx2(waypoint->pos.x * FP_TILE_SIZE + FP_TILE_SIZE * .5f, waypoint->pos.y * FP_TILE_SIZE + FP_TILE_SIZE * .5f);
TELY_Render_CircleColourV4(renderer, circle_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_MAGENTA_V4);
}
if (entity->waypoints->next != entity->waypoints) {
2023-09-17 10:24:07 +00:00
FP_GameWaypoint *waypoint = entity->waypoints->next;
2023-09-16 14:37:26 +00:00
Dqn_V2I target_tile = entity->waypoints->next->pos;
Dqn_V2 target_pos = Dqn_V2_InitNx2(target_tile.x * FP_TILE_SIZE + FP_TILE_SIZE *.5f, target_tile.y * FP_TILE_SIZE + FP_TILE_SIZE * .5f);
Dqn_V2 entity_to_target_pos = target_pos - entity_world_pos;
if (Dqn_V2_LengthSq(entity_to_target_pos) < DQN_SQUARED(entity->local_hit_box_size.x * .5f)) {
waypoint->next->prev = waypoint->prev;
waypoint->prev->next = waypoint->next;
TELY_ChunkPool_Dealloc(game->chunk_pool, waypoint);
2023-09-16 14:37:26 +00:00
} else {
Dqn_V2 entity_to_target_pos_norm = Dqn_V2_Normalise(entity_to_target_pos);
entity->local_pos += entity_to_target_pos_norm * (entity->local_hit_box_size.x * .05f);
}
}
2023-09-17 11:55:59 +00:00
#endif
2023-09-16 14:37:26 +00:00
2023-09-16 07:32:25 +00:00
// NOTE: Core equations of motion ==========================================================
{
// f"(t) = a
// f'(t) = at + v
// f (t) = 0.5f*a(t^2) + vt + p
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(input->delta_s);
Dqn_f32 t_squared = DQN_SQUARED(t);
entity->velocity = (acceleration * t) + entity->velocity * 0.82f;
2023-09-17 11:55:59 +00:00
Dqn_V2 delta_pos = (acceleration * 0.5f * t_squared) + (entity->velocity * t);
2023-09-16 09:06:16 +00:00
2023-09-17 11:55:59 +00:00
Dqn_Rect entity_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
Dqn_V2 entity_pos = Dqn_Rect_Center(entity_world_hit_box);
Dqn_V2 entity_new_pos = entity_pos + delta_pos;
2023-09-17 11:55:59 +00:00
bool has_collision = false;
2023-09-16 09:06:16 +00:00
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator collider_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &collider_it, game->root_entity); ) {
FP_GameEntity *collider = collider_it.entity;
2023-09-16 09:06:16 +00:00
if (collider->handle == entity->handle)
continue;
2023-09-17 11:55:59 +00:00
// NOTE: Sweep collider with half the radius of the source entity
Dqn_Rect collider_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, collider->handle);
Dqn_Rect swept_collider_world_hit_box = collider_world_hit_box;
swept_collider_world_hit_box.pos -= (entity_world_hit_box.size * .5f);
swept_collider_world_hit_box.size += entity_world_hit_box.size;
2023-09-17 11:55:59 +00:00
if (!Dqn_Rect_ContainsPoint(swept_collider_world_hit_box, entity_new_pos))
2023-09-17 11:55:59 +00:00
continue;
Dqn_f32 collider_left_wall_x = swept_collider_world_hit_box.pos.x;
Dqn_f32 collider_right_wall_x = swept_collider_world_hit_box.pos.x + swept_collider_world_hit_box.size.w;
Dqn_f32 collider_top_wall_y = swept_collider_world_hit_box.pos.y;
Dqn_f32 collider_bottom_wall_y = swept_collider_world_hit_box.pos.y + swept_collider_world_hit_box.size.h;
Dqn_V2 o = entity_pos;
Dqn_V2 d = delta_pos;
2023-09-17 11:55:59 +00:00
// NOTE: Solve collision by determining the 't' value at which
// we hit one of the walls of the collider and move the entity
// at exactly that point.
// O + td = x
// td = x - O
// t = (x - O) / d
Dqn_f32 const SENTINEL_T = 999.f;
Dqn_f32 earliest_t = SENTINEL_T;
2023-09-17 11:55:59 +00:00
if (d.x != 0.f) {
Dqn_f32 left_t = (collider_left_wall_x - o.x) / d.x;
Dqn_f32 right_t = (collider_right_wall_x - o.x) / d.x;
if (left_t >= 0.f && left_t <= 1.f)
2023-09-17 11:55:59 +00:00
earliest_t = DQN_MIN(earliest_t, left_t);
if (right_t >= 0.f && right_t <= 1.f)
2023-09-17 11:55:59 +00:00
earliest_t = DQN_MIN(earliest_t, right_t);
2023-09-16 09:06:16 +00:00
}
2023-09-17 11:55:59 +00:00
if (d.y != 0.f) {
Dqn_f32 top_t = (collider_top_wall_y - o.y) / d.y;
Dqn_f32 bottom_t = (collider_bottom_wall_y - o.y) / d.y;
if (top_t >= 0.f && top_t <= 1.f)
2023-09-17 11:55:59 +00:00
earliest_t = DQN_MIN(earliest_t, top_t);
if (bottom_t >= 0.f && bottom_t <= 1.f)
2023-09-17 11:55:59 +00:00
earliest_t = DQN_MIN(earliest_t, bottom_t);
}
if (earliest_t != SENTINEL_T) {
Dqn_V2 pos_just_before_collide = entity_pos + (d * earliest_t);
Dqn_V2 new_delta_p = pos_just_before_collide - entity_pos;
entity->local_pos += new_delta_p;
entity->velocity = {};
has_collision = true;
}
2023-09-17 11:55:59 +00:00
}
if (!has_collision) {
entity->local_pos += delta_pos;
2023-09-16 09:06:16 +00:00
}
2023-09-16 07:32:25 +00:00
}
2023-09-16 02:21:24 +00:00
// NOTE: Move entity by mouse ==============================================================
2023-09-17 10:24:07 +00:00
if (game->active_entity == entity->handle && entity->flags & FP_EntityFlag_MoveByMouse) {
if (entity->flags & FP_EntityFlag_MoveByMouse) {
2023-09-16 07:32:25 +00:00
entity->velocity = {};
2023-09-16 02:21:24 +00:00
entity->local_pos += input->mouse_p_delta;
2023-09-16 07:32:25 +00:00
}
2023-09-16 02:21:24 +00:00
}
2023-09-17 10:24:07 +00:00
if (entity->flags & FP_EntityFlag_DeriveHitBoxFromChildrenBoundingBox) {
2023-09-16 02:21:24 +00:00
Dqn_Rect children_bbox = {};
// TODO(doyle): Is the hit box supposed to include the containing
// entity itself? Not sure
2023-09-17 10:24:07 +00:00
children_bbox.pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
2023-09-16 02:21:24 +00:00
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator child_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &child_it, entity);) {
FP_GameEntity *child = child_it.entity;
2023-09-16 02:21:24 +00:00
DQN_ASSERT(child != entity);
2023-09-17 10:24:07 +00:00
Dqn_Rect bbox = FP_Game_CalcEntityWorldBoundingBox(game, child->handle);
2023-09-16 02:21:24 +00:00
children_bbox = Dqn_Rect_Union(children_bbox, bbox);
}
Dqn_Rect padded_bbox = Dqn_Rect_Expand(children_bbox, 16.f);
entity->local_hit_box_offset = padded_bbox.pos - entity->local_pos + (padded_bbox.size * .5f);
entity->local_hit_box_size = padded_bbox.size;
}
// NOTE: Handle input on entity ============================================================
FP_GameEntityAction *action = &entity->action;
2023-09-16 07:32:25 +00:00
{
bool we_are_clicked_entity = entity->handle == game->clicked_entity;
2023-09-18 13:00:30 +00:00
bool action_has_finished = action->timer_s != FP_GAME_ENTITY_ACTION_INFINITE_TIMER && action->timer_s >= action->end_at_s;
2023-09-21 07:11:07 +00:00
bool entity_has_velocity = entity->velocity.x || entity->velocity.y;
2023-09-18 13:00:30 +00:00
if (action->state == FP_GameEntityState_Nil)
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
if (action->state == FP_GameEntityState_Idle) {
2023-09-18 13:00:30 +00:00
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Idle")).index;
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim);
} else if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackA);
} else if (dir_vector.x || dir_vector.y) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Run);
}
2023-09-16 07:32:25 +00:00
}
2023-09-16 09:06:16 +00:00
}
if (action->state == FP_GameEntityState_AttackA) {
2023-09-18 13:00:30 +00:00
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Attack A")).index;
FP_Game_EntityActionReset(action, anim->count * anim->seconds_per_frame, anim);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
} else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) {
Dqn_f32 t01 = action->timer_s / action->end_at_s;
if (t01 > 0.5f)
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackB);
else
action->flags |= FP_GameEntityActionFlag_Failed;
}
2023-09-16 09:06:16 +00:00
}
2023-09-16 07:32:25 +00:00
}
if (action->state == FP_GameEntityState_AttackB) {
2023-09-18 13:00:30 +00:00
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Attack B")).index;
FP_Game_EntityActionReset(action, anim->count * anim->seconds_per_frame, anim);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
} else if (!FP_Game_EntityActionHasFailed(action) && we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) {
Dqn_f32 t01 = action->timer_s / action->end_at_s;
if (t01 > 0.5f)
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackC);
else
action->flags |= FP_GameEntityActionFlag_Failed;
}
}
}
if (action->state == FP_GameEntityState_AttackC) {
2023-09-18 13:00:30 +00:00
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Attack C")).index;
FP_Game_EntityActionReset(action, anim->count * anim->seconds_per_frame, anim);
} else if (action_has_finished) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
}
}
if (action->state == FP_GameEntityState_Run) {
2023-09-18 13:00:30 +00:00
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Run")).index;
FP_Game_EntityActionReset(action, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, anim);
} else if (we_are_clicked_entity) {
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J)) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_AttackA);
2023-09-21 07:11:07 +00:00
} else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_LeftShift)) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Dash);
}
}
if ((action_has_finished && !entity_has_velocity) || (we_are_clicked_entity && dir_vector.x == 0.f && dir_vector.y == 0.f)) {
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
}
}
if (action->state == FP_GameEntityState_Dash) {
if (action->flags & FP_GameEntityActionFlag_StateTransition) {
TELY_AssetSpriteAnimation *anim = entity->sprite_anims.data + TELY_Asset_GetSpriteAnimation(entity->sprite_anims, DQN_STRING8("Floor slide")).index;
FP_Game_EntityActionReset(action, anim->count * anim->seconds_per_frame, anim);
Dqn_V2 dash_dir = {entity->facing_left ? -1.f : 1.f, 0.f};
Dqn_V2 dash_acceleration = dash_dir * 400'000'000.f;
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(input->delta_s);
entity->velocity = (dash_acceleration * t) + entity->velocity * 2.0f;
} else if (action_has_finished) {
if (entity_has_velocity) {
// TODO(doyle): Not sure if this branch triggers properly.
FP_Game_EntityActionSetState(action, FP_GameEntityState_Run);
} else {
2023-09-18 13:00:30 +00:00
FP_Game_EntityActionSetState(action, FP_GameEntityState_Idle);
}
}
}
// NOTE: Tick entity action ================================================================
2023-09-18 13:00:30 +00:00
action->timer_s += DQN_CAST(Dqn_f32)input->delta_s;
2023-09-16 07:32:25 +00:00
}
// NOTE: Calculate entity attack box =======================================================
2023-09-18 13:00:30 +00:00
if (action->state == FP_GameEntityState_AttackA ||
action->state == FP_GameEntityState_AttackB ||
action->state == FP_GameEntityState_AttackC) {
2023-09-16 07:32:25 +00:00
entity->attack_box_size = entity->local_hit_box_size;
TELY_AssetSpriteSheet const *sprite_sheet = entity->sprite_sheet;
if (sprite_sheet) {
entity->attack_box_size = Dqn_V2_InitV2I(sprite_sheet->sprite_size);
}
// NOTE: Position the attack box
if (entity->facing_left) {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
entity->local_hit_box_offset.y);
} else {
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->local_hit_box_size.w,
entity->local_hit_box_offset.y);
}
} else {
entity->attack_box_size = {};
}
}
2023-09-16 09:06:16 +00:00
// NOTE: Do attacks ============================================================================
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator attacker_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &attacker_it, game->root_entity); ) {
FP_GameEntity *attacker = attacker_it.entity;
2023-09-16 07:32:25 +00:00
// NOTE: Resolve attack boxes
if (!Dqn_V2_Area(attacker->attack_box_size))
continue;
2023-09-16 07:32:25 +00:00
Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle);
Dqn_V2 attacker_world_pos = FP_Game_CalcEntityWorldPos(game, attacker->handle);
2023-09-16 07:32:25 +00:00
for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->root_entity); ) {
FP_GameEntity *defender = defender_it.entity;
if (defender->handle == attacker->handle)
continue;
2023-09-16 07:32:25 +00:00
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
if (!Dqn_Rect_Intersects(attacker_box, defender_box))
continue;
2023-09-16 07:32:25 +00:00
Dqn_V2 defender_world_pos = Dqn_Rect_Center(defender_box);
Dqn_V2 attack_dir_vector = {};
if (attacker_world_pos.x < defender_world_pos.x)
attack_dir_vector.x = 1.f;
else
attack_dir_vector.x = -1.f;
2023-09-16 07:32:25 +00:00
2023-09-21 07:11:07 +00:00
Dqn_V2 acceleration = attack_dir_vector * 500'000.f;
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(input->delta_s);
Dqn_f32 t_squared = DQN_SQUARED(t);
2023-09-16 07:32:25 +00:00
Dqn_V2 delta_p = (acceleration * 0.5f * t_squared) + (defender->velocity * t);
defender->velocity = (acceleration * t) + defender->velocity * 2.0f;
2023-09-16 07:32:25 +00:00
}
}
}
2023-09-17 10:24:07 +00:00
void FP_GameRender(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer)
2023-09-16 07:32:25 +00:00
{
TELY_PlatformInput *input = &platform->input;
2023-09-17 10:24:07 +00:00
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
2023-09-16 07:32:25 +00:00
Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p);
2023-09-16 09:06:16 +00:00
// NOTE: Draw tiles ============================================================================
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / FP_TILE_SIZE);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / FP_TILE_SIZE);
for (Dqn_usize x = 0; x < tile_count_x; x++) {
Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * FP_TILE_SIZE, 0);
Dqn_V2 end = Dqn_V2_InitNx2(start.x, platform->core.window_size.h);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
}
for (Dqn_usize y = 0; y < tile_count_y; y++) {
Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * FP_TILE_SIZE);
Dqn_V2 end = Dqn_V2_InitNx2(platform->core.window_size.w, start.y);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
}
// NOTE: Draw entities =========================================================================
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
2023-09-16 07:32:25 +00:00
entity->alive_time_s += input->delta_s;
2023-09-16 02:21:24 +00:00
// NOTE: Render shapes in entity ===========================================================
2023-09-17 10:24:07 +00:00
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
for (FP_GameShape const &shape_ : entity->shapes) {
FP_GameShape const *shape = &shape_;
2023-09-16 02:21:24 +00:00
Dqn_V2 local_to_world_p1 = world_pos + shape->p1;
Dqn_V2 local_to_world_p2 = world_pos + shape->p2;
switch (shape->type) {
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_None: {
2023-09-16 02:21:24 +00:00
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Circle: {
2023-09-16 02:21:24 +00:00
TELY_Render_CircleColourV4(renderer, local_to_world_p1, shape->circle_radius, shape->render_mode, shape->colour);
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Rect: {
2023-09-16 02:21:24 +00:00
Dqn_Rect rect = Dqn_Rect_InitV2x2(local_to_world_p1, local_to_world_p2 - local_to_world_p1);
rect.pos -= rect.size * .5f;
TELY_Render_RectColourV4(renderer, rect, shape->render_mode, shape->colour);
} break;
2023-09-17 10:24:07 +00:00
case FP_GameShapeType_Line: {
2023-09-16 02:21:24 +00:00
TELY_Render_LineColourV4(renderer, local_to_world_p1, local_to_world_p2, shape->colour, shape->line_thickness);
} break;
}
}
// NOTE: Render entity sprites =============================================================
2023-09-18 13:00:30 +00:00
if (entity->sprite_sheet && entity->action.anim) {
2023-09-16 02:21:24 +00:00
TELY_AssetSpriteSheet const *sprite_sheet = entity->sprite_sheet;
2023-09-18 13:00:30 +00:00
FP_GameEntityAction const *action = &entity->action;
TELY_AssetSpriteAnimation const *sprite_anim = action->anim;
uint16_t anim_frame = DQN_CAST(uint16_t)(action->timer_s / sprite_anim->seconds_per_frame) % sprite_anim->count;
2023-09-16 02:21:24 +00:00
2023-09-18 13:00:30 +00:00
Dqn_usize sprite_index = sprite_anim->index + anim_frame;
2023-09-16 02:21:24 +00:00
Dqn_usize sprite_sheet_row = sprite_index / sprite_sheet->sprites_per_row;
Dqn_usize sprite_sheet_column = sprite_index % sprite_sheet->sprites_per_row;
Dqn_Rect src_rect = {};
src_rect.pos.x = DQN_CAST(Dqn_f32)(sprite_sheet_column * sprite_sheet->sprite_size.w);
src_rect.pos.y = DQN_CAST(Dqn_f32)(sprite_sheet_row * sprite_sheet->sprite_size.y);
src_rect.size.w = DQN_CAST(Dqn_f32)sprite_sheet->sprite_size.w;
src_rect.size.h = DQN_CAST(Dqn_f32)sprite_sheet->sprite_size.h;
Dqn_Rect dest_rect = {};
dest_rect.size = src_rect.size * entity->size_scale;
dest_rect.pos = world_pos - (dest_rect.size * .5f);
2023-09-16 02:21:24 +00:00
if (entity->facing_left)
dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally
TELY_Render_TextureColourV4(renderer, sprite_sheet->tex_handle, src_rect, dest_rect, TELY_COLOUR_WHITE_V4);
}
2023-09-16 07:32:25 +00:00
// NOTE: Render attack box =================================================================
{
2023-09-17 10:24:07 +00:00
Dqn_Rect attack_box = FP_Game_CalcEntityAttackWorldHitBox(game, entity->handle);
2023-09-16 07:32:25 +00:00
TELY_Render_RectColourV4(renderer, attack_box, TELY_RenderShapeMode_Line, TELY_COLOUR_RED_TOMATO_V4);
}
2023-09-16 02:21:24 +00:00
// NOTE: Render world position =============================================================
TELY_Render_CircleColourV4(renderer, world_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4);
// NOTE: Render hot/active entity ==========================================================
2023-09-17 10:24:07 +00:00
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
2023-09-16 02:21:24 +00:00
if (game->clicked_entity == entity->handle) {
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
2023-09-17 10:24:07 +00:00
} else if (game->hot_entity == entity->handle || (entity->flags & FP_EntityFlag_DrawHitBox)) {
2023-09-16 02:21:24 +00:00
Dqn_V4 hot_colour = game->hot_entity == entity->handle ? TELY_COLOUR_RED_TOMATO_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_YELLOW_SANDY_V4, .5f);
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, hot_colour);
2023-09-16 09:06:16 +00:00
}
2023-09-17 11:55:59 +00:00
if (game->hot_entity == entity->handle) {
2023-09-16 09:06:16 +00:00
if (entity->name.size) {
Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / FP_TILE_SIZE, world_pos.y / FP_TILE_SIZE);
2023-09-17 10:24:07 +00:00
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
2023-09-16 02:21:24 +00:00
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
Dqn_String8 label = Dqn_String8_InitF(scratch.allocator,
2023-09-16 09:06:16 +00:00
"%.*s (%.1f, %.1f) (%I32d, %I32d)",
2023-09-16 02:21:24 +00:00
DQN_STRING_FMT(entity->name),
entity_world_pos.x,
2023-09-16 09:06:16 +00:00
entity_world_pos.y,
player_tile.x,
player_tile.y);
2023-09-16 02:21:24 +00:00
TELY_Render_Text(renderer, world_mouse_p, Dqn_V2_InitNx2(0.f, 1), label);
}
}
}
2023-09-16 07:32:25 +00:00
}
extern "C" __declspec(dllexport)
void TELY_DLL_FrameUpdate(void *user_data)
{
TELY_Platform *platform = DQN_CAST(TELY_Platform *) user_data;
TELY_PlatformInput *input = &platform->input;
TELY_Assets *assets = &platform->assets;
TELY_Renderer *renderer = &platform->renderer;
2023-09-17 10:24:07 +00:00
FP_Game *game = DQN_CAST(FP_Game *) platform->user_data;
2023-09-20 13:35:38 +00:00
TELY_RFui *rfui = &game->rfui;
2023-09-16 07:32:25 +00:00
TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb);
2023-09-17 10:24:07 +00:00
TELY_Render_PushFont(renderer, game->jetbrains_mono_font);
2023-09-16 07:32:25 +00:00
2023-09-20 13:35:38 +00:00
TELY_RFui_FrameSetup(rfui, &platform->frame_arena);
TELY_RFui_PushFont(rfui, game->jetbrains_mono_font);
TELY_RFui_PushLabelColourV4(rfui, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
2023-09-16 07:32:25 +00:00
// =============================================================================================
game->prev_clicked_entity = game->clicked_entity;
game->prev_hot_entity = game->hot_entity;
game->prev_active_entity = game->active_entity;
game->hot_entity = {};
game->active_entity = {};
Dqn_FArray_Clear(&game->parent_entity_stack);
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
2023-09-17 10:24:07 +00:00
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
TELY_Render_PushTransform(renderer, model_view);
2023-09-16 07:32:25 +00:00
Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p);
// =============================================================================================
TELY_Audio *audio = &platform->audio;
if (audio->playback_size == 0) {
2023-09-17 10:24:07 +00:00
TELY_Audio_Play(audio, game->test_audio, 1.f /*volume*/);
2023-09-16 07:32:25 +00:00
}
// =============================================================================================
if (TELY_Platform_InputKeyWasDown(input->mouse_left) && TELY_Platform_InputKeyIsDown(input->mouse_left)) {
if (game->prev_active_entity.id)
game->active_entity = game->prev_active_entity;
} else {
2023-09-17 10:24:07 +00:00
for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity); ) {
FP_GameEntity *entity = it.entity;
2023-09-16 07:32:25 +00:00
if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0)
continue;
2023-09-17 10:24:07 +00:00
if ((entity->flags & FP_EntityFlag_Clickable) == 0)
2023-09-16 07:32:25 +00:00
continue;
2023-09-17 10:24:07 +00:00
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
2023-09-16 07:32:25 +00:00
if (!Dqn_Rect_ContainsPoint(world_hit_box, world_mouse_p))
continue;
game->hot_entity = entity->handle;
if (TELY_Platform_InputKeyIsPressed(input->mouse_left)) {
game->active_entity = entity->handle;
game->clicked_entity = entity->handle;
}
}
}
2023-09-16 02:21:24 +00:00
2023-09-16 09:06:16 +00:00
FP_GameUpdate(platform, game, renderer, input);
2023-09-16 07:32:25 +00:00
FP_GameRender(game, platform, renderer);
2023-09-20 13:35:38 +00:00
{
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); };
TELY_RFuiResult info_bar = TELY_RFui_Row(rfui, DQN_STRING8("Info Bar"));
info_bar.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute;
info_bar.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f;
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute;
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].value = 10.f;
TELY_RFui_PushParent(rfui, info_bar.widget);
DQN_DEFER { TELY_RFui_PopParent(rfui); };
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
Dqn_String8Builder builder = {};
builder.allocator = scratch.allocator;
TELY_RFui_TextF(rfui, "TELY");
Dqn_String8Builder_AppendF(&builder, "TELY");
if (Dqn_String8_IsValid(platform->core.os_name)) {
TELY_RFui_TextF(rfui, " | %.*s", DQN_STRING_FMT(platform->core.os_name));
Dqn_String8Builder_AppendF(&builder, " | %.*s", DQN_STRING_FMT(platform->core.os_name));
}
Dqn_String8Builder_AppendF(&builder,
" | %dx%d %.1fHz | TSC %.1f GHz",
platform->core.display.size.w,
platform->core.display.size.h,
platform->core.display.refresh_rate,
platform->core.tsc_per_second / 1'000'000'000.0);
TELY_RFui_TextF(rfui,
" | %dx%d %.1fHz | TSC %.1f GHz",
platform->core.display.size.w,
platform->core.display.size.h,
platform->core.display.refresh_rate,
platform->core.tsc_per_second / 1'000'000'000.0);
if (platform->core.ram_mb) {
Dqn_String8Builder_AppendF(&builder, " | RAM %.1fGB", platform->core.ram_mb / 1024.0);
TELY_RFui_TextF(rfui, " | RAM %.1fGB", platform->core.ram_mb / 1024.0);
}
Dqn_String8Builder_AppendF(&builder,
" | Work %04.1fms/f (%04.1f%%) | %05.1f FPS | Frame %'I64u | Timer %.1fs",
input->work_ms,
input->work_ms * 100.0 / input->delta_ms,
1000.0 / input->delta_ms,
input->frame_counter,
input->timer_s);
TELY_RFui_TextF(rfui,
" | Work %04.1fms/f (%04.1f%%) | %05.1f FPS | Frame %'I64u | Timer %.1fs",
input->work_ms,
input->work_ms * 100.0 / input->delta_ms,
1000.0 / input->delta_ms,
input->frame_counter,
input->timer_s);
}
TELY_RFui_Flush(rfui, renderer, input, assets);
2023-09-16 02:21:24 +00:00
TELY_Audio_MixPlaybackSamples(audio, assets);
}