#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); { 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; 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 = &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); sheet->sprite_count = 109; sheet->sprites_per_row = 7; sheet->sprite_size = Dqn_V2I_InitNx2(50, 37); TELY_AssetSpriteAnimation hero_anims[] = { {DQN_STRING8("Everything"), /*index*/ 0, /*count*/ sheet->sprite_count}, {DQN_STRING8("Idle"), /*index*/ 0, /*count*/ 3}, {DQN_STRING8("Run"), /*index*/ 8, /*count*/ 6}, {DQN_STRING8("Jump"), /*index*/ 14, /*count*/ 10}, {DQN_STRING8("Floor slide"), /*index*/ 24, /*count*/ 5}, {DQN_STRING8("Unknown"), /*index*/ 29, /*count*/ 9}, {DQN_STRING8("Attack A"), /*index*/ 38, /*count*/ 11}, {DQN_STRING8("Attack B"), /*index*/ 49, /*count*/ 4}, {DQN_STRING8("Attack C"), /*index*/ 53, /*count*/ 6}, {DQN_STRING8("Hurt A"), /*index*/ 59, /*count*/ 5}, {DQN_STRING8("Hurt B"), /*index*/ 64, /*count*/ 5}, {DQN_STRING8("Unsheath sword"), /*index*/ 69, /*count*/ 4}, {DQN_STRING8("Sheath sword"), /*index*/ 73, /*count*/ 4}, {DQN_STRING8("Air drift"), /*index*/ 77, /*count*/ 2}, {DQN_STRING8("Air drop"), /*index*/ 79, /*count*/ 2}, {DQN_STRING8("Ladder climb"), /*index*/ 81, /*count*/ 4}, {DQN_STRING8("Chi push"), /*index*/ 85, /*count*/ 8}, {DQN_STRING8("Leap slice A"), /*index*/ 93, /*count*/ 7}, {DQN_STRING8("Leap slice B"), /*index*/ 100, /*count*/ 3}, {DQN_STRING8("Leap slice C"), /*index*/ 103, /*count*/ 6}, }; 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); game->root_entity = Dqn_VArray_Make(&game->entities, 1, Dqn_ZeroMem_No); 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 ================================================================= TELY_GameEntity *f = TELY_Game_MakeEntityPointerF(game, "F"); TELY_Game_PushParentEntity(game, f->handle); TELY_GameEntity *b = TELY_Game_MakeEntityPointerF(game, "B"); TELY_GameEntity *g = TELY_Game_MakeEntityPointerF(game, "G"); TELY_Game_PushParentEntity(game, b->handle); TELY_GameEntity *a = TELY_Game_MakeEntityPointerF(game, "A"); TELY_GameEntity *d = TELY_Game_MakeEntityPointerF(game, "D"); TELY_Game_PushParentEntity(game, d->handle); TELY_GameEntity *c = TELY_Game_MakeEntityPointerF(game, "C"); TELY_GameEntity *e = TELY_Game_MakeEntityPointerF(game, "E"); TELY_Game_PopParentEntity(game); TELY_Game_PopParentEntity(game); TELY_Game_PushParentEntity(game, g->handle); TELY_GameEntity *i = TELY_Game_MakeEntityPointerF(game, "I"); TELY_Game_PushParentEntity(game, i->handle); TELY_GameEntity *h = TELY_Game_MakeEntityPointerF(game, "H"); TELY_Game_PopParentEntity(game); TELY_Game_PopParentEntity(game); TELY_Game_PopParentEntity(game); // NOTE: Pre order test ==================================================================== TELY_GameEntity *pre_order_walk[9] = {}; Dqn_usize pre_order_walk_count = 0; for (TELY_GameEntityIterator it = {}; TELY_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity);) { 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 =================================================================== TELY_GameEntity *post_order_walk[9] = {}; Dqn_usize post_order_walk_count = 0; for (TELY_GameEntityIterator it = {}; TELY_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity);) { 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 =========================================================================== TELY_Game_DeleteEntity(game, game->root_entity->handle); 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); } // NOTE: Test sprite animation entity { 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 = &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; } uint16_t font_size = 18; game->camera.scale = Dqn_V2_InitNx1(1); 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 ============================================================================ TELY_Audio *audio = &platform->audio; audio->chunk_pool = &game->chunk_pool; // NOTE: TELY ui =============================================================================== TELY_UI *ui = &game->ui; ui->arena.allocs_are_allowed_to_leak = true; } 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; 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, pona->jetbrains_mono_font); TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb); TELY_Render_PushFont(renderer, pona->jetbrains_mono_font); { Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_String8Builder builder = {}; builder.allocator = scratch.allocator; Dqn_String8Builder_AppendF(&builder, "TELY"); if (Dqn_String8_IsValid(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); if (platform->core.ram_mb) Dqn_String8Builder_AppendF(&builder, " | 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); Dqn_String8 info_label = Dqn_String8Builder_Build(&builder, scratch.allocator); TELY_Render_Text(renderer, /*position*/ Dqn_V2_InitNx1(10), /*align*/ Dqn_V2_InitNx1(0), info_label); } // ============================================================================================= 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); Dqn_M2x3 model_view = {}; { Dqn_V2 rotate_origin = game->camera.world_pos - (Dqn_V2_InitV2I(platform->core.window_size) * .5f); model_view = Dqn_M2x3_Identity(); model_view = Dqn_M2x3_Mul(model_view, Dqn_M2x3_Translate(rotate_origin)); model_view = Dqn_M2x3_Mul(model_view, Dqn_M2x3_Rotate(game->camera.rotate_rads)); model_view = Dqn_M2x3_Mul(model_view, Dqn_M2x3_Scale(game->camera.scale)); model_view = Dqn_M2x3_Mul(model_view, Dqn_M2x3_Translate((rotate_origin * -1) + game->camera.world_pos)); TELY_Render_PushTransform(renderer, model_view); } Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p); // ============================================================================================= TELY_Audio *audio = &platform->audio; if (audio->playback_size == 0) { TELY_Audio_Play(audio, pona->test_audio, 1.f /*volume*/); } // ============================================================================================= 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 { for (TELY_GameEntityIterator it = {}; TELY_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity); ) { TELY_GameEntity *entity = it.entity; if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0) continue; if ((entity->flags & TELY_EntityFlag_Clickable) == 0) continue; Dqn_Rect world_hit_box = TELY_Game_CalcEntityWorldHitBox(game, entity->handle); 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; } } } if (TELY_Platform_InputKeyIsReleased(input->mouse_left)) game->clicked_entity = game->prev_active_entity; Dqn_V2 dir_vector = {}; 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; if (game->clicked_entity.id) { if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Delete)) TELY_Game_DeleteEntity(game, game->clicked_entity); } else { game->camera.world_pos += dir_vector * 5.f; } for (TELY_GameEntityIterator it = {}; TELY_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) { TELY_GameEntity *entity = it.entity; entity->alive_time_s += input->delta_s; // NOTE: Move entity by keyboard =========================================================== if (game->clicked_entity == entity->handle) { if (entity->flags & TELY_EntityFlag_MoveByKeyboard) { entity->local_pos += dir_vector * DQN_CAST(Dqn_f32)(300.f * input->delta_s); if (dir_vector.x) entity->facing_left = dir_vector.x < 0.f; } } // NOTE: Move entity by mouse ============================================================== if (game->active_entity == entity->handle && entity->flags & TELY_EntityFlag_MoveByMouse) { if (entity->flags & TELY_EntityFlag_MoveByMouse) entity->local_pos += input->mouse_p_delta; } if (entity->flags & TELY_EntityFlag_DeriveHitBoxFromChildrenBoundingBox) { Dqn_Rect children_bbox = {}; // TODO(doyle): Is the hit box supposed to include the containing // entity itself? Not sure children_bbox.pos = TELY_Game_CalcEntityWorldPos(game, entity->handle); for (TELY_GameEntityIterator child_it = {}; TELY_Game_DFSPreOrderWalkEntityTree(game, &child_it, entity);) { TELY_GameEntity *child = child_it.entity; DQN_ASSERT(child != entity); Dqn_Rect bbox = TELY_Game_CalcEntityWorldBoundingBox(game, child->handle); 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: Render shapes in entity =========================================================== Dqn_V2 world_pos = TELY_Game_CalcEntityWorldPos(game, entity->handle); for (TELY_GameShape const &shape_ : entity->shapes) { TELY_GameShape const *shape = &shape_; Dqn_V2 local_to_world_p1 = world_pos + shape->p1; Dqn_V2 local_to_world_p2 = world_pos + shape->p2; switch (shape->type) { case TELY_GameShapeType_None: { } break; case TELY_GameShapeType_Circle: { TELY_Render_CircleColourV4(renderer, local_to_world_p1, shape->circle_radius, shape->render_mode, shape->colour); } break; case TELY_GameShapeType_Rect: { 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; case TELY_GameShapeType_Line: { TELY_Render_LineColourV4(renderer, local_to_world_p1, local_to_world_p2, shape->colour, shape->line_thickness); } break; } } // NOTE: Render entity sprites ============================================================= if (entity->sprite_sheet && entity->sprite_anims.size) { TELY_AssetSpriteSheet const *sprite_sheet = entity->sprite_sheet; TELY_AssetSpriteAnimation const *sprite_anim = entity->sprite_anims.data + entity->sprite_anim_index; uint32_t ticks_per_anim_frame = 10; uint64_t anim_frame_counter = input->frame_counter / ticks_per_anim_frame; Dqn_usize sprite_index = (sprite_anim->index + (anim_frame_counter % sprite_anim->count)) % sprite_sheet->sprite_count; 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); 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); } // 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 ========================================================== Dqn_Rect world_hit_box = TELY_Game_CalcEntityWorldHitBox(game, entity->handle); if (game->clicked_entity == entity->handle) { TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4); } else if (game->hot_entity == entity->handle || (entity->flags & TELY_EntityFlag_DrawHitBox)) { 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); if (game->hot_entity == entity->handle && (entity->name.size)) { Dqn_V2 entity_world_pos = TELY_Game_CalcEntityWorldPos(game, entity->handle); Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_String8 label = Dqn_String8_InitF(scratch.allocator, "%.*s (%.1f, %.1f)", DQN_STRING_FMT(entity->name), entity_world_pos.x, entity_world_pos.y); TELY_Render_Text(renderer, world_mouse_p, Dqn_V2_InitNx2(0.f, 1), label); } } } TELY_Audio_MixPlaybackSamples(audio, assets); }