vn/code/vn.cpp

239 lines
7.2 KiB
C++
Raw Normal View History

2023-06-17 17:00:55 +00:00
#include "vn_platform.h"
#include "vn_platform.cpp"
2023-07-19 15:09:41 +00:00
#define STB_IMAGE_IMPLEMENTATION
#include "third_party/stb_image.h"
2023-06-19 17:12:26 +00:00
2023-07-19 15:09:41 +00:00
#if VN_INTERNAL
2023-06-19 17:12:26 +00:00
struct debug_settings
{
b32 RenderUIDebugRects;
b32 RenderFPSCounter;
2023-06-27 14:14:28 +00:00
b32 ListHotAndActive;
2023-07-19 15:09:41 +00:00
b32 ShowWelcomeMessage;
2023-06-19 17:12:26 +00:00
};
global debug_settings *DEBUG_DebugSettings = 0;
2023-06-19 17:12:26 +00:00
#endif
#include "generated/vn_character.meta.h"
2023-06-19 17:12:26 +00:00
#include "vn_tokenizer.h"
2023-07-19 15:09:41 +00:00
#include "vn_config.h"
2023-06-17 17:00:55 +00:00
#include "vn_font.h"
#include "vn_text_op.h"
#include "vn_ui.h"
2023-06-27 14:14:28 +00:00
#include "vn_ui_utils.h"
2023-08-22 03:19:51 +00:00
#include "vn_scene.h"
#include "vn_scene_view.h"
2023-06-17 17:00:55 +00:00
#include "vn_animation_curve.h"
2023-07-24 13:50:57 +00:00
#include "vn_theme_dark.h"
2023-06-17 17:00:55 +00:00
#if VN_INTERNAL
#include "vn_workspace.h"
#endif
#include "generated/vn_character.meta.c"
2023-07-19 15:09:41 +00:00
#include "vn_tokenizer.cpp"
2023-06-18 10:09:36 +00:00
#include "vn_config.cpp"
2023-06-17 17:00:55 +00:00
#include "vn_render.cpp"
#include "vn_font.cpp"
#include "vn_ui.cpp"
#include "vn_ui_utils.cpp"
#include "vn_animation_curve.cpp"
2023-07-19 15:09:41 +00:00
#include "vn_scene.cpp"
2023-08-22 03:19:51 +00:00
#include "vn_scene_view.cpp"
#if VN_INTERNAL
2023-08-22 03:19:51 +00:00
#include "vn_workspace.cpp"
#endif
2023-06-17 17:00:55 +00:00
struct vn_state
{
arena *Arena;
arena *FrameArena;
2023-08-22 03:19:51 +00:00
2023-06-19 17:12:26 +00:00
glyph_atlas *GlyphAtlas;
config *Config;
ui UI;
animation_curve_state AnimationCurveState;
2023-10-04 17:21:15 +00:00
b32 EditorMode;
2023-06-19 17:12:26 +00:00
2023-07-19 15:09:41 +00:00
render_handle BackgroundTexture;
2023-08-22 03:19:51 +00:00
scene_view SceneView;
2023-06-19 17:12:26 +00:00
#if VN_INTERNAL
workspace Workspace;
2023-06-19 17:12:26 +00:00
debug_settings DebugSettings;
#endif
2023-06-17 17:00:55 +00:00
};
2023-07-19 15:09:41 +00:00
static render_handle CreateTextureFromPath(vn_render_commands *Commands, string Path)
{
render_handle Result = {};
temporary_memory Scratch = GetScratch();
platform_file_handle File = Platform.OpenFile(Path, PlatformAccess_Read);
if(File.IsValid)
{
u64 DataCount = Platform.GetFileSize(File);
u8 *Data = PushArray(Scratch.Arena, u8, DataCount);
Platform.ReadFile(File, Data, 0, DataCount);
s32 Width, Height, BPP;
u8 *TextureData = stbi_load_from_memory(Data, DataCount, &Width, &Height, &BPP, 0);
render_texture_format TextureFormat = Render_TextureFormat_Invalid;
switch(BPP)
{
InvalidDefaultCase;
case 1: { TextureFormat = Render_TextureFormat_R8; } break;
case 3: { TextureFormat = Render_TextureFormat_RGB8; } break;
case 4: { TextureFormat = Render_TextureFormat_RGBA8; } break;
}
2023-10-04 17:21:15 +00:00
Result = Commands->AllocateTexture(V2S32(Width, Height), TextureFormat, true, TextureData);
2023-07-19 15:09:41 +00:00
stbi_image_free(TextureData);
Platform.CloseFile(File);
}
ReleaseScratch(Scratch);
return(Result);
}
2023-06-17 17:00:55 +00:00
VN_UPDATE_AND_RENDER(VN_UpdateAndRender)
{
2023-06-19 17:12:26 +00:00
SetThreadContext(ThreadContext);
Platform = Memory->PlatformAPI;
vn_state *State = Memory->State;
2023-07-19 15:09:41 +00:00
//- sixten: initialize application state
2023-06-19 17:12:26 +00:00
if(!Memory->State)
{
arena *Arena = ArenaAlloc(Kilobytes(24), true);
2023-07-19 15:09:41 +00:00
State = Memory->State = PushStruct(Arena, vn_state);
State->Arena = Arena;
State->FrameArena = ArenaAlloc(Megabytes(1), true);
2023-06-19 17:12:26 +00:00
State->GlyphAtlas = CreateGlyphAtlas(RenderCommands);
2023-07-19 15:09:41 +00:00
State->Config = CreateConfig();
//- sixten: load assets
State->BackgroundTexture = CreateTextureFromPath(RenderCommands, StrLit("data/backgrounds/ddlc-club.png"));
2023-06-19 17:12:26 +00:00
2023-07-19 15:09:41 +00:00
//- sixten: setup config binds and load current config
2023-06-19 17:12:26 +00:00
{
Config_BindS32(State->Config, StrLit("Platform/RefreshRate"), &Input->RefreshRate, 60);
#if VN_INTERNAL
2023-07-19 15:09:41 +00:00
DEBUG_DebugSettings = &State->DebugSettings;
Config_BindB32(State->Config, StrLit("Dev/RenderUIDebugRects"), &DEBUG_DebugSettings->RenderUIDebugRects, 0);
Config_BindB32(State->Config, StrLit("Dev/RenderFPSCounter"), &DEBUG_DebugSettings->RenderFPSCounter, 0);
Config_BindB32(State->Config, StrLit("Dev/ListHotAndActive"), &DEBUG_DebugSettings->ListHotAndActive, 0);
Config_BindB32(State->Config, StrLit("Dev/ShowWelcomeMessage"), &DEBUG_DebugSettings->ShowWelcomeMessage, 1);
2023-06-19 17:12:26 +00:00
#endif
Config_ReadFile(State->Config, StrLit("config.vn"));
}
//- sixten: load startup scene
2023-08-22 03:19:51 +00:00
scene_view *SceneView = &State->SceneView;
SV_Init(SceneView, State->Arena);
2023-10-04 17:21:15 +00:00
temporary_memory Scratch = GetScratch();
string SceneInput = Platform_ReadEntireFile(Scratch.Arena, StrLit("data/scene.vns"));
2023-10-04 17:21:15 +00:00
compiled_scene Scene = S_ScriptFromText(Scratch.Arena, SceneInput);
SV_SetCurrentSource(&Scene);
ReleaseScratch(Scratch);
2023-09-05 17:50:49 +00:00
2023-10-04 17:21:15 +00:00
SceneView->TestHappy = CreateTextureFromPath(RenderCommands, StrLit("data/characters/test_happy.png"));
SceneView->TestNormal = CreateTextureFromPath(RenderCommands, StrLit("data/characters/test_normal.png"));
SceneView->MonikaLeaning = CreateTextureFromPath(RenderCommands, StrLit("data/characters/monika_leaning.png"));
2023-10-04 17:21:15 +00:00
SceneView->BackgroundTexture = State->BackgroundTexture;
2023-09-05 17:50:49 +00:00
2023-07-19 15:09:41 +00:00
UI_Init(&State->UI);
2023-08-22 03:19:51 +00:00
AC_Init(&State->AnimationCurveState);
2023-10-04 17:21:15 +00:00
#if VN_INTERNAL
W_Init(&State->Workspace);
#endif
2023-10-04 17:21:15 +00:00
return;
2023-06-19 17:12:26 +00:00
}
#if VN_INTERNAL
DEBUG_DebugSettings = &State->DebugSettings;
#endif
2023-07-19 15:09:41 +00:00
//- sixten: begin new frame
2023-08-22 03:19:51 +00:00
ArenaClear(State->FrameArena);
AC_NewFrame(&State->AnimationCurveState, Input->dtForFrame);
2023-06-27 14:14:28 +00:00
UI_NewFrame(&State->UI, Input->EventList, Input->MouseP, State->GlyphAtlas);
2023-10-04 17:21:15 +00:00
SV_NewFrame(&State->SceneView, Input->EventList, Input->dtForFrame);
//- sixten: check for toggle between modes
if(Platform_KeyPress(Input->EventList, Key_Return, PlatformModifier_Ctrl))
{
State->EditorMode = !State->EditorMode;
}
2023-06-19 17:12:26 +00:00
2023-07-19 15:09:41 +00:00
//- sixten: build the ui
2023-06-23 12:38:15 +00:00
UI_BeginBuild(RenderCommands->RenderDim);
{
#if VN_INTERNAL
2023-10-04 17:21:15 +00:00
if(State->EditorMode)
2023-08-22 03:19:51 +00:00
{
W_Update(&State->Workspace, RenderCommands, Input, State->GlyphAtlas);
}
else
{
#endif
2023-08-22 03:19:51 +00:00
SV_BuildSceneView(Input);
#if VN_INTERNAL
2023-08-22 03:19:51 +00:00
}
#endif
2023-06-23 12:38:15 +00:00
}
2023-06-27 14:14:28 +00:00
UI_EndBuild();
2023-06-23 12:38:15 +00:00
2023-06-17 17:00:55 +00:00
2023-08-22 03:19:51 +00:00
//- sixten: update the scene
2023-10-04 17:21:15 +00:00
SV_Update(State->FrameArena);
2023-08-22 03:19:51 +00:00
2023-07-19 15:09:41 +00:00
//- sixten: consume all remaining evetns
2023-06-19 17:12:26 +00:00
for(platform_event *Event = Input->EventList->First;
Event != 0;
Event = Event->Next)
{
if(Event->Type == PlatformEvent_WindowClose)
{
2023-06-23 12:38:15 +00:00
Config_WriteFile(State->Config, StrLit("config.vn"));
2023-06-19 17:12:26 +00:00
Input->ExitRequested = true;
}
Platform_ConsumeEvent(Input->EventList, Event);
}
2023-06-17 17:00:55 +00:00
2023-07-19 15:09:41 +00:00
//- sixten: render the frame
2023-06-27 14:14:28 +00:00
{
2023-07-19 15:09:41 +00:00
render_group Group = BeginRenderGroup(RenderCommands);
PushClear(&Group, V3(0.1, 0.1, 0.1));
UI_RenderFrame(&Group, State->GlyphAtlas);
#if VN_INTERNAL
2023-07-19 15:09:41 +00:00
if(DEBUG_DebugSettings->ListHotAndActive)
{
PushText(&Group, State->GlyphAtlas, Font_Regular, V2(5, RenderCommands->RenderDim.y - 20), 15, Color_Grey,
2023-09-13 04:42:11 +00:00
PushFormat(State->UI.FrameArena, "Hot: %S:%llu", UI_BoxStringFromKey(UI_HotKey()), UI_HotKey()));
2023-07-19 15:09:41 +00:00
PushText(&Group, State->GlyphAtlas, Font_Regular, V2(5, RenderCommands->RenderDim.y - 40), 15, Color_Grey,
2023-09-13 04:42:11 +00:00
PushFormat(State->UI.FrameArena, "Active: %S:%llu", UI_BoxStringFromKey(UI_ActiveKey()), UI_ActiveKey()));
2023-07-19 15:09:41 +00:00
}
#endif
2023-06-27 14:14:28 +00:00
}
2023-06-17 17:00:55 +00:00
}