1- /*
2- example_basic_5.cpp
3- - demonstrates loading APIs
4- - demonstrates loading extensions
5- - demonstrates hot reloading
6- - demonstrates minimal use of graphics extension
7- - demonstrates ui extension
8- */
9-
10- /*
11- Index of this file:
12- // [SECTION] quick notes
13- // [SECTION] includes
14- // [SECTION] structs
15- // [SECTION] apis
16- // [SECTION] pl_app_load
17- // [SECTION] pl_app_shutdown
18- // [SECTION] pl_app_resize
19- // [SECTION] pl_app_update
20- // [SECTION] full demo
21- // [SECTION] unity build
22- */
23-
24- // -----------------------------------------------------------------------------
25- // [SECTION] quick notes
26- // -----------------------------------------------------------------------------
27-
28- /*
29- This example is isn't really here to demonstrate anything (yet). It
30- utilizes an unstable extension and experimental backend but it can still
31- be used to experiment with Dear ImGui or used as a template to create tool
32- with this version of Pilot Light. For reference, the experimental backend
33- is currently "experimental" because it has some Dear ImGui setup code in
34- it. Once more features are exposed by the windows extension & backend,
35- the Dear ImGui code can be completely handled by users or extension. Until
36- then, it will be experimental.
37-
38- IMPORTANT:
39- To use this example, you must build Pilot Light and the examples with
40- "debug_experimental" or "release_experimental".
41-
42- ~~~> build.bat -c debug_release
43- or
44- ~~~> build.sh -c debug_release
45-
46- Dear ImGui is an immediate mode UI library similar to our UI extension (which
47- was actually inspired by both Dear ImGui and Nuklear). There are differences
48- so make sure you check the header files (documentation) for both before
49- assumming the APIs work the exact same way. In particular when it comes to
50- paired calls for things like begin/end widgets/windows/containers.
51- */
521
532// -----------------------------------------------------------------------------
543// [SECTION] includes
@@ -57,39 +6,94 @@ Index of this file:
576#include < stdlib.h> // malloc, free
587#include < stdio.h> // printf
598#include < string.h> // memset
60- #include " pl.h"
619
62- // extensions
63- #include " pl_ui_ext.h"
10+ // pilot light
11+ #include " pl.h"
12+ #include " pl_memory.h"
13+ #include " pl_string.h"
14+ #define PL_MATH_INCLUDE_FUNCTIONS
15+ #include " pl_math.h"
16+ #include " pl_icons.h"
17+ #include " pl_json.h"
18+
19+ // stable extensions
20+ #include " pl_image_ext.h"
21+ #include " pl_profile_ext.h"
22+ #include " pl_log_ext.h"
23+ #include " pl_stats_ext.h"
24+ #include " pl_graphics_ext.h"
25+ #include " pl_tools_ext.h"
26+ #include " pl_job_ext.h"
27+ #include " pl_draw_ext.h"
6428#include " pl_draw_backend_ext.h"
29+ #include " pl_ui_ext.h"
30+ #include " pl_shader_ext.h"
31+ #include " pl_string_intern_ext.h"
32+ #include " pl_platform_ext.h"
33+ #include " pl_console_ext.h"
34+ #include " pl_screen_log_ext.h"
6535#include " pl_starter_ext.h"
66- #include " pl_graphics_ext.h"
36+ #include " pl_pak_ext.h"
37+ #include " pl_datetime_ext.h"
38+ #include " pl_config_ext.h"
39+ #include " pl_vfs_ext.h"
40+ #include " pl_compress_ext.h"
6741
6842// dear imgui
6943#include " pl_dear_imgui_ext.h"
7044#include " imgui.h"
7145#include " implot.h"
7246
47+ // -----------------------------------------------------------------------------
48+ // [SECTION] global apis
49+ // -----------------------------------------------------------------------------
50+
51+ const plWindowI* gptWindows = nullptr ;
52+ const plStatsI* gptStats = nullptr ;
53+ const plGraphicsI* gptGfx = nullptr ;
54+ const plToolsI* gptTools = nullptr ;
55+ const plJobI* gptJobs = nullptr ;
56+ const plDrawI* gptDraw = nullptr ;
57+ const plDrawBackendI* gptDrawBackend = nullptr ;
58+ const plUiI* gptUI = nullptr ;
59+ const plIOI* gptIO = nullptr ;
60+ const plShaderI* gptShader = nullptr ;
61+ const plMemoryI* gptMemory = nullptr ;
62+ const plNetworkI* gptNetwork = nullptr ;
63+ const plStringInternI* gptString = nullptr ;
64+ const plProfileI* gptProfile = nullptr ;
65+ const plFileI* gptFile = nullptr ;
66+ const plConsoleI* gptConsole = nullptr ;
67+ const plScreenLogI* gptScreenLog = nullptr ;
68+ const plConfigI* gptConfig = nullptr ;
69+ const plStarterI* gptStarter = nullptr ;
70+ const plVfsI* gptVfs = nullptr ;
71+ const plPakI* gptPak = nullptr ;
72+ const plDateTimeI* gptDateTime = nullptr ;
73+ const plCompressI* gptCompress = nullptr ;
74+ const plDearImGuiI* gptDearImGui = nullptr ;
75+
76+ #define PL_ALLOC (x ) gptMemory->tracked_realloc (nullptr , (x), __FILE__, __LINE__)
77+ #define PL_REALLOC (x, y ) gptMemory->tracked_realloc ((x), (y), __FILE__, __LINE__)
78+ #define PL_FREE (x ) gptMemory->tracked_realloc ((x), 0, __FILE__, __LINE__)
79+
80+ #define PL_DS_ALLOC (x ) gptMemory->tracked_realloc (nullptr , (x), __FILE__, __LINE__)
81+ #define PL_DS_ALLOC_INDIRECT (x, FILE, LINE ) gptMemory->tracked_realloc (nullptr , (x), FILE, LINE)
82+ #define PL_DS_FREE (x ) gptMemory->tracked_realloc ((x), 0, __FILE__, __LINE__)
83+ #include " pl_ds.h"
84+
7385// -----------------------------------------------------------------------------
7486// [SECTION] structs
7587// -----------------------------------------------------------------------------
7688
7789typedef struct _plAppData
7890{
7991 plWindow* ptWindow;
80- } plAppData;
8192
82- // -----------------------------------------------------------------------------
83- // [SECTION] apis
84- // -----------------------------------------------------------------------------
85-
86- const plIOI* gptIO = NULL ;
87- const plWindowI* gptWindows = NULL ;
88- const plUiI* gptUi = NULL ;
89- const plDrawBackendI* gptDrawBackend = NULL ;
90- const plStarterI* gptStarter = NULL ;
91- const plDearImGuiI* gptDearImGui = NULL ;
92- const plGraphicsI* gptGfx = NULL ;
93+ // UI options
94+ bool bShowImGuiDemo;
95+ bool bShowImPlotDemo;
96+ } plAppData;
9397
9498// -----------------------------------------------------------------------------
9599// [SECTION] pl_app_load
@@ -128,13 +132,33 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
128132
129133 // re-retrieve the apis since we are now in
130134 // a different dll/so
131- gptIO = pl_get_api_latest (ptApiRegistry, plIOI);
132- gptWindows = pl_get_api_latest (ptApiRegistry, plWindowI);
133- gptDrawBackend = pl_get_api_latest (ptApiRegistry, plDrawBackendI);
134- gptUi = pl_get_api_latest (ptApiRegistry, plUiI);
135- gptStarter = pl_get_api_latest (ptApiRegistry, plStarterI);
136- gptDearImGui = pl_get_api_latest (ptApiRegistry, plDearImGuiI);
137- gptGfx = pl_get_api_latest (ptApiRegistry, plGraphicsI);
135+ gptWindows = pl_get_api_latest (ptApiRegistry, plWindowI);
136+ gptStats = pl_get_api_latest (ptApiRegistry, plStatsI);
137+ gptGfx = pl_get_api_latest (ptApiRegistry, plGraphicsI);
138+ gptTools = pl_get_api_latest (ptApiRegistry, plToolsI);
139+ gptJobs = pl_get_api_latest (ptApiRegistry, plJobI);
140+ gptDraw = pl_get_api_latest (ptApiRegistry, plDrawI);
141+ gptDrawBackend = pl_get_api_latest (ptApiRegistry, plDrawBackendI);
142+ gptUI = pl_get_api_latest (ptApiRegistry, plUiI);
143+ gptIO = pl_get_api_latest (ptApiRegistry, plIOI);
144+ gptShader = pl_get_api_latest (ptApiRegistry, plShaderI);
145+ gptMemory = pl_get_api_latest (ptApiRegistry, plMemoryI);
146+ gptNetwork = pl_get_api_latest (ptApiRegistry, plNetworkI);
147+ gptString = pl_get_api_latest (ptApiRegistry, plStringInternI);
148+ gptProfile = pl_get_api_latest (ptApiRegistry, plProfileI);
149+ gptFile = pl_get_api_latest (ptApiRegistry, plFileI);
150+ gptConsole = pl_get_api_latest (ptApiRegistry, plConsoleI);
151+ gptScreenLog = pl_get_api_latest (ptApiRegistry, plScreenLogI);
152+ gptConfig = pl_get_api_latest (ptApiRegistry, plConfigI);
153+ gptStarter = pl_get_api_latest (ptApiRegistry, plStarterI);
154+ gptDearImGui = pl_get_api_latest (ptApiRegistry, plDearImGuiI);
155+ gptDateTime = pl_get_api_latest (ptApiRegistry, plDateTimeI);
156+ gptVfs = pl_get_api_latest (ptApiRegistry, plVfsI);
157+ gptPak = pl_get_api_latest (ptApiRegistry, plPakI);
158+ gptDateTime = pl_get_api_latest (ptApiRegistry, plDateTimeI);
159+ gptCompress = pl_get_api_latest (ptApiRegistry, plCompressI);
160+
161+ gptScreenLog->add_message_ex (0 , 15.0 , PL_COLOR_32_MAGENTA , 1 .5f , " %s" , " App Hot Reloaded" );
138162
139163 ImPlot::SetCurrentContext ((ImPlotContext*)ptDataRegistry->get_data (" implot" ));
140164
@@ -150,29 +174,53 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
150174 const plExtensionRegistryI* ptExtensionRegistry = pl_get_api_latest (ptApiRegistry, plExtensionRegistryI);
151175
152176 // load extensions
153- // load extensions
154- // * first argument is the shared library name WITHOUT the extension
155- // * second & third argument is the load/unload functions names (use NULL for the default of "pl_load_ext" &
156- // "pl_unload_ext")
157- // * fourth argument indicates if the extension is reloadable (should we check for changes and reload if changed)
158177 ptExtensionRegistry->load (" pl_unity_ext" , NULL , NULL , true );
159- ptExtensionRegistry->load (" pl_platform_ext" , NULL , NULL , false ); // provides the file API used by the drawing ext
160- ptExtensionRegistry->load (" pl_dear_imgui_ext" , NULL , NULL , false ); // provides the imgui backend stuff
178+ ptExtensionRegistry->load (" pl_platform_ext" , NULL , NULL , false );
179+ ptExtensionRegistry->load (" pl_dear_imgui_ext" , NULL , NULL , false );
161180
162181 // load required apis
163- gptIO = pl_get_api_latest (ptApiRegistry, plIOI);
164- gptWindows = pl_get_api_latest (ptApiRegistry, plWindowI);
165- gptDrawBackend = pl_get_api_latest (ptApiRegistry, plDrawBackendI);
166- gptUi = pl_get_api_latest (ptApiRegistry, plUiI);
167- gptStarter = pl_get_api_latest (ptApiRegistry, plStarterI);
168- gptDearImGui = pl_get_api_latest (ptApiRegistry, plDearImGuiI);
169- gptGfx = pl_get_api_latest (ptApiRegistry, plGraphicsI);
182+ gptWindows = pl_get_api_latest (ptApiRegistry, plWindowI);
183+ gptStats = pl_get_api_latest (ptApiRegistry, plStatsI);
184+ gptGfx = pl_get_api_latest (ptApiRegistry, plGraphicsI);
185+ gptTools = pl_get_api_latest (ptApiRegistry, plToolsI);
186+ gptJobs = pl_get_api_latest (ptApiRegistry, plJobI);
187+ gptDraw = pl_get_api_latest (ptApiRegistry, plDrawI);
188+ gptDrawBackend = pl_get_api_latest (ptApiRegistry, plDrawBackendI);
189+ gptUI = pl_get_api_latest (ptApiRegistry, plUiI);
190+ gptIO = pl_get_api_latest (ptApiRegistry, plIOI);
191+ gptShader = pl_get_api_latest (ptApiRegistry, plShaderI);
192+ gptMemory = pl_get_api_latest (ptApiRegistry, plMemoryI);
193+ gptNetwork = pl_get_api_latest (ptApiRegistry, plNetworkI);
194+ gptString = pl_get_api_latest (ptApiRegistry, plStringInternI);
195+ gptProfile = pl_get_api_latest (ptApiRegistry, plProfileI);
196+ gptFile = pl_get_api_latest (ptApiRegistry, plFileI);
197+ gptConsole = pl_get_api_latest (ptApiRegistry, plConsoleI);
198+ gptScreenLog = pl_get_api_latest (ptApiRegistry, plScreenLogI);
199+ gptConfig = pl_get_api_latest (ptApiRegistry, plConfigI);
200+ gptStarter = pl_get_api_latest (ptApiRegistry, plStarterI);
201+ gptDearImGui = pl_get_api_latest (ptApiRegistry, plDearImGuiI);
202+ gptDateTime = pl_get_api_latest (ptApiRegistry, plDateTimeI);
203+ gptVfs = pl_get_api_latest (ptApiRegistry, plVfsI);
204+ gptPak = pl_get_api_latest (ptApiRegistry, plPakI);
205+ gptDateTime = pl_get_api_latest (ptApiRegistry, plDateTimeI);
206+ gptCompress = pl_get_api_latest (ptApiRegistry, plCompressI);
207+
208+ if (gptIO->get_io ()->bHotReloadActive )
209+ {
210+ gptVfs->mount_directory (" /shaders" , " ../shaders" , PL_VFS_MOUNT_FLAGS_NONE );
211+ gptVfs->mount_directory (" /shader-temp" , " ../shader-temp" , PL_VFS_MOUNT_FLAGS_NONE );
212+ gptFile->create_directory (" ../shader-temp" );
213+ }
214+ else
215+ {
216+ gptVfs->mount_pak (" /shader-temp" , " shaders.pak" , PL_VFS_MOUNT_FLAGS_NONE );
217+ }
170218
171219 // use window API to create a window
172220
173221 plWindowDesc tWindowDesc = {
174222 PL_WINDOW_FLAG_NONE ,
175- " Example Basic 5 " ,
223+ " Tool Template " ,
176224 500 ,
177225 500 ,
178226 200 ,
@@ -181,12 +229,21 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
181229 gptWindows->create (tWindowDesc, &ptAppData->ptWindow );
182230 gptWindows->show (ptAppData->ptWindow );
183231
232+ // setup starter extension
184233 plStarterInit tStarterInit = {};
185234 tStarterInit.tFlags = PL_STARTER_FLAGS_ALL_EXTENSIONS ;
186235 tStarterInit.ptWindow = ptAppData->ptWindow ;
187-
236+ tStarterInit. tFlags &= ~ PL_STARTER_FLAGS_SHADER_EXT ;
188237 gptStarter->initialize (tStarterInit);
189238
239+ // initialize shader compiler
240+ static plShaderOptions tDefaultShaderOptions = {};
241+ tDefaultShaderOptions.apcIncludeDirectories [0 ] = " /shaders/" ;
242+ tDefaultShaderOptions.apcDirectories [0 ] = " /shaders/" ;
243+ tDefaultShaderOptions.pcCacheOutputDirectory = " /shader-temp/" ;
244+ tDefaultShaderOptions.tFlags = PL_SHADER_FLAGS_AUTO_OUTPUT | PL_SHADER_FLAGS_INCLUDE_DEBUG ;
245+ gptShader->initialize (&tDefaultShaderOptions);
246+
190247 // wraps up (i.e. builds font atlas)
191248 gptStarter->finalize ();
192249
@@ -207,10 +264,7 @@ pl_app_shutdown(plAppData* ptAppData)
207264{
208265 plDevice* ptDevice = gptStarter->get_device ();
209266 gptGfx->flush_device (ptDevice);
210-
211- // cleanup backend resources
212267 gptDearImGui->cleanup ();
213-
214268 gptStarter->cleanup ();
215269 gptWindows->destroy (ptAppData->ptWindow );
216270 free (ptAppData);
@@ -240,31 +294,28 @@ pl_app_update(plAppData* ptAppData)
240294 gptDearImGui->new_frame (gptStarter->get_device (), gptStarter->get_render_pass ());
241295
242296 ImGui::DockSpaceOverViewport ();
243-
244- // create a window
245- if (ImGui::Begin (" Pilot Light" ))
297+ if (ImGui::BeginMainMenuBar ())
246298 {
247- if (ImGui::Button (" Print To Console" ))
299+ if (ImGui::BeginMenu (" File" , false )) ImGui::EndMenu ();
300+ if (ImGui::BeginMenu (" Edit" , false )) ImGui::EndMenu ();
301+ if (ImGui::BeginMenu (" Tools" , true ))
248302 {
249- printf (" Button Clicked" );
303+ ImGui::MenuItem (" Dear ImGui Demo" , nullptr , &ptAppData->bShowImGuiDemo );
304+ ImGui::MenuItem (" ImPlot Demo" , nullptr , &ptAppData->bShowImPlotDemo );
305+ ImGui::EndMenu ();
250306 }
307+ if (ImGui::BeginMenu (" Help" , false )) ImGui::EndMenu ();
308+ ImGui::EndMainMenuBar ();
251309 }
252- ImGui::End ();
253-
254- // just some provided demo windows
255- ImPlot::ShowDemoWindow (nullptr );
256- ImGui::ShowDemoWindow (nullptr );
257310
311+ if (ptAppData->bShowImPlotDemo )
312+ ImPlot::ShowDemoWindow (&ptAppData->bShowImPlotDemo );
313+ if (ptAppData->bShowImGuiDemo )
314+ ImGui::ShowDemoWindow (&ptAppData->bShowImGuiDemo );
258315
259316 // start main pass & return the encoder being used
260317 plRenderEncoder* ptEncoder = gptStarter->begin_main_pass ();
261-
262- // submit Dear ImGui stuff
263318 gptDearImGui->render (ptEncoder, gptGfx->get_encoder_command_buffer (ptEncoder));
264-
265- // allows the starter extension to handle some things then ends the main pass
266319 gptStarter->end_main_pass ();
267-
268- // must be the last function called when using the starter extension
269320 gptStarter->end_frame ();
270321}
0 commit comments