1+ ########################################################################################################################
2+ # Pilot Light - 2D App Template
3+ ########################################################################################################################
4+
5+
6+ import os
7+ from pathlib import Path
8+
9+ # pilot light stuff
10+ import pilotlight .pilotlight as pl
11+ from pilotlight .pilotlight import *
12+ from pilotlight .imgui import *
13+ from pilotlight .enums import *
14+ from pilotlight .types import *
15+
16+ class App :
17+
18+ def __init__ (self ):
19+ self .show_imgui_demo = False
20+ self .show_implot_demo = False
21+ self .some_string_array = "pizza"
22+
23+ # -------------------------------------------------------------------------
24+ # Application lifetime
25+ # -------------------------------------------------------------------------
26+
27+ def pl_app_load (self ): # called on load
28+
29+ # mount directories used by the shader extension
30+ package_dir = os .path .dirname (os .path .abspath (pl .__file__ ))
31+
32+ plVfsI .mount_directory ("/shaders" , package_dir + "/shaders" )
33+ plVfsI .mount_directory ("/cache" , str (Path .cwd ()) + "/cache" )
34+ plVfsI .mount_directory ("/shader-temp" , str (Path .cwd ()) + "/shader-temp" )
35+ plVfsI .mount_directory ("/assets" , str (Path .cwd ()) + "/assets" )
36+
37+ # create & show the OS window
38+ window_desc = plWindowDesc ()
39+ window_desc .pcTitle = "Pilot Light - 2D App Template"
40+ window_desc .iXPos = 100
41+ window_desc .iYPos = 100
42+ window_desc .uWidth = 1280
43+ window_desc .uHeight = 720
44+ _ , self .ptWindow = plWindowI .create (window_desc )
45+ plWindowI .show (self .ptWindow )
46+
47+ # initialize starter extension
48+ starterInit = plStarterInit (plStarterFlag .PL_STARTER_FLAGS_ALL_EXTENSIONS | plStarterFlag .PL_STARTER_FLAGS_MSAA , self .ptWindow )
49+
50+ # we need to handle the shader extension since we need to find
51+ # the shaders provided with the python package
52+ starterInit .eFlags &= ~ plStarterFlag .PL_STARTER_FLAGS_SHADER_EXT
53+ starterInit .eFlags &= ~ plStarterFlag .PL_STARTER_FLAGS_DRAW_EXT
54+ plStarterI .initialize (starterInit )
55+
56+ ptDevice = plStarterI .get_device ()
57+ tDrawInit = plDrawInit (ptDevice )
58+ plDrawI .initialize (tDrawInit )
59+
60+ self .ptFontAtlas = plDrawI .create_font_atlas ()
61+ plDrawI .set_font_atlas (self .ptFontAtlas )
62+
63+ tFontRange = plFontRange (0x0020 , 0x00FF - 0x0020 )
64+
65+ tFontConfig = plFontConfig ()
66+ tFontConfig .fSize = 18.0
67+ tFontConfig .uHOverSampling = 1
68+ tFontConfig .uVOverSampling = 1
69+ tFontConfig .ptRanges = [tFontRange ]
70+ self .ptFont = plDrawI .add_font_from_file_ttf (self .ptFontAtlas , tFontConfig , "/assets/fonts/Cousine-Regular.ttf" );
71+
72+ self .drawlist = plDrawI .request_2d_drawlist ()
73+ self .ptFGLayer = plDrawI .request_2d_layer (self .drawlist )
74+
75+ # shader system
76+ shader_options = plShaderOptions ()
77+ shader_options .pcCacheOutputDirectory = "/shader-temp/"
78+ shader_options .apcDirectories = ["/shaders/" ]
79+ shader_options .apcIncludeDirectories = ["/shaders/" ]
80+ shader_options .eFlags = plShaderFlags .PL_SHADER_FLAGS_AUTO_OUTPUT
81+
82+ plShaderI .initialize (shader_options )
83+
84+ # finish starter setup (since we are done setting up shader extension)
85+ plStarterI .finalize ()
86+
87+ ptCmdBuffer = plStarterI .get_raw_command_buffer () # not recording
88+ plDrawI .build_font_atlas (ptCmdBuffer , self .ptFontAtlas ) # actually record
89+ plStarterI .return_raw_command_buffer (ptCmdBuffer ) # will submit & wait
90+
91+ tRenderAttachmentInfo = plRenderAttachmentInfo ()
92+ plStarterI .get_render_attachment_info (tRenderAttachmentInfo ) # out parameter
93+ plDearImGuiI .initialize (plStarterI .get_device (), plStarterI .get_swapchain (), tRenderAttachmentInfo )
94+
95+ def pl_app_shutdown (self ): # called on shutdown
96+ plGraphicsI .flush_device (plStarterI .get_device ())
97+ plDearImGuiI .cleanup ()
98+ plDrawI .return_2d_layer (self .ptFGLayer )
99+ plDrawI .return_2d_drawlist (self .drawlist )
100+ plDrawI .cleanup_font_atlas (self .ptFontAtlas )
101+ plDrawI .cleanup ()
102+ plStarterI .cleanup ()
103+ plWindowI .destroy (self .ptWindow )
104+
105+ def pl_app_resize (self ): # called on resize
106+ plStarterI .resize ()
107+
108+ def pl_app_update (self ): # called every frame
109+
110+ if not plStarterI .begin_frame ():
111+ return
112+
113+ plDrawI .new_frame ()
114+ plDearImGuiI .new_frame (plStarterI .get_device ())
115+
116+ if self .show_imgui_demo :
117+ self .show_imgui_demo = ImGui .ShowDemoWindow (self .show_imgui_demo )
118+
119+ if self .show_implot_demo :
120+ self .show_implot_demo = ImPlot .ShowDemoWindow (self .show_implot_demo )
121+
122+ # outer frame
123+ plDrawI .add_rect (
124+ self .ptFGLayer ,
125+ [40.0 , 40.0 ],
126+ [1240.0 , 680.0 ],
127+ plDrawLineOptions (PL_COLOR_32_YELLOW , 2.0 )
128+ )
129+
130+ # text
131+ plDrawI .add_text (self .ptFGLayer , [300 , 300 ], "Hello Python" , plDrawTextOptions (self .ptFont , 18.0 , PL_COLOR_32_CYAN ))
132+
133+ # io API
134+ if plIOI .is_key_pressed (plKey .PL_KEY_P ):
135+ print ("P key pressed!" )
136+
137+ # ui API
138+ if plUiI .begin_window ("Debug Window" ):
139+
140+ _ , self .some_string_array = plUiI .input_text ("Input" , self .some_string_array )
141+ if plUiI .button ("Press me" ):
142+ print ("Button Pressed" )
143+
144+ if plUiI .button ("Add Message" ):
145+ plScreenLogI .add_message (1.0 , "Logging from python!" )
146+
147+ _ , self .show_imgui_demo = plUiI .checkbox ("Show ImGui Demo" , self .show_imgui_demo )
148+ _ , self .show_implot_demo = plUiI .checkbox ("Show ImPlot Demo" , self .show_implot_demo )
149+
150+ plUiI .end_window ()
151+
152+ # dear imgui
153+ if ImGui .BeginMainMenuBar ():
154+ if ImGui .BeginMenu ("File" ):
155+ ImGui .EndMenu ()
156+ if ImGui .BeginMenu ("Edit" , False ):
157+ ImGui .EndMenu ()
158+ if ImGui .BeginMenu ("Tools" ):
159+ _ , self .show_imgui_demo = ImGui .MenuItem ("Show ImGui Demo" , "" , self .show_imgui_demo )
160+ _ , self .show_implot_demo = ImGui .MenuItem ("Show ImPlot Demo" , "" , self .show_implot_demo )
161+ ImGui .EndMenu ()
162+ if ImGui .BeginMenu ("Help" ):
163+ ImGui .MenuItemSimple ("Check For Update" )
164+ ImGui .MenuItemSimple ("About" , "-a" )
165+ ImGui .EndMenu ()
166+ ImGui .EndMainMenuBar ()
167+ if ImGui .Begin ("ImGui Window" ):
168+ if ImGui .Button ("Press Me" ):
169+ print ("Pressed Imgui Button" )
170+ ImGui .End ()
171+
172+ # begin render pass
173+ cmdBuffer = plStarterI .begin_main_pass ()
174+
175+ plDrawI .submit_2d_layer (self .ptFGLayer )
176+ io = plIOI .get_io ()
177+ tMainViewportSize = io .tMainViewportSize
178+ tRenderAttachmentInfo = plRenderAttachmentInfo ()
179+ plStarterI .get_render_attachment_info (tRenderAttachmentInfo )
180+ plDrawI .submit_2d_drawlist (self .drawlist , cmdBuffer , tMainViewportSize .x , tMainViewportSize .y , plGraphicsI .get_swapchain_info (plStarterI .get_swapchain ()).eSampleCount , tRenderAttachmentInfo )
181+ plDearImGuiI .render (cmdBuffer )
182+
183+ plStarterI .end_main_pass ()
184+
185+ plStarterI .end_frame ()
186+
187+ # let pilot light run application
188+ pl_run (App ())
0 commit comments