Skip to content

Commit 4286570

Browse files
refac: prep for new project helpers
1 parent 0260b89 commit 4286570

14 files changed

Lines changed: 432 additions & 59 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pilotlight/*.so
2121
pilotlight/*.rdi
2222
pilotlight/*.dSYM
2323
pilotlight/shaders/
24+
pilotlight/assets/
2425

2526
# debug files
2627
src/*.pdb

examples/example_basic_0.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ def pl_app_load(self):
5252
# Initialize the starter extension.
5353
# We explicitly disable the shader ext from the starter flags because
5454
# we are going to initialize the shader system ourselves below.
55-
starter_flags = plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS
56-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
57-
starter_flags |= plStarterFlag.PL_STARTER_FLAGS_MSAA
58-
59-
plStarterI.initialize(self.ptWindow, starter_flags)
55+
starterInit = plStarterInit(plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS | plStarterFlag.PL_STARTER_FLAGS_MSAA, self.ptWindow)
56+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
57+
plStarterI.initialize(starterInit)
6058

6159
# Initialize shader system.
6260
# This is required even for simple drawing examples because the

examples/example_basic_1.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def pl_app_load(self):
3939
plVfsI.mount_directory("/cache", str(Path.cwd()) + "/../cache")
4040
plVfsI.mount_directory("/shaders", os.path.dirname(os.path.abspath(pl.__file__)) + "/shaders")
4141
plVfsI.mount_directory("/shader-temp", str(Path.cwd()) + "/../shader-temp")
42-
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../../pilotlight/assets")
42+
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../pilotlight/assets")
4343

4444
# Create and show the OS window.
4545
window_desc = plWindowDesc()
@@ -54,12 +54,10 @@ def pl_app_load(self):
5454
# Initialize the starter extension.
5555
# We explicitly disable the shader ext from the starter flags because
5656
# we are going to initialize the shader system ourselves below.
57-
starter_flags = plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS
58-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
59-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_DRAW_EXT
60-
# starter_flags |= plStarterFlag.PL_STARTER_FLAGS_MSAA
61-
62-
plStarterI.initialize(self.ptWindow, starter_flags)
57+
starterInit = plStarterInit(plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS, self.ptWindow)
58+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
59+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_DRAW_EXT
60+
plStarterI.initialize(starterInit)
6361

6462
ptDevice = plStarterI.get_device()
6563
tDrawInit = plDrawInit(ptDevice)

examples/example_basic_2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def pl_app_load(self):
3535
plVfsI.mount_directory("/cache", str(Path.cwd()) + "/../cache")
3636
plVfsI.mount_directory("/shaders", os.path.dirname(os.path.abspath(pl.__file__)) + "/shaders")
3737
plVfsI.mount_directory("/shader-temp", str(Path.cwd()) + "/../shader-temp")
38-
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../../pilotlight/assets")
38+
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../pilotlight/assets")
3939

4040
# Create window.
4141
window_desc = plWindowDesc()
@@ -50,13 +50,12 @@ def pl_app_load(self):
5050

5151
# Initialize starter, but manually initialize shader/draw so this example
5252
# clearly shows what is required.
53-
starter_flags = plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS
54-
starter_flags |= plStarterFlag.PL_STARTER_FLAGS_DEPTH_BUFFER
55-
starter_flags |= plStarterFlag.PL_STARTER_FLAGS_MSAA
56-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
57-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_DRAW_EXT
58-
59-
plStarterI.initialize(self.ptWindow, starter_flags)
53+
starterInit = plStarterInit(plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS, self.ptWindow)
54+
starterInit.eFlags |= plStarterFlag.PL_STARTER_FLAGS_DEPTH_BUFFER
55+
starterInit.eFlags |= plStarterFlag.PL_STARTER_FLAGS_MSAA
56+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
57+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_DRAW_EXT
58+
plStarterI.initialize(starterInit)
6059

6160
# Draw system.
6261
ptDevice = plStarterI.get_device()

examples/example_basic_3.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def pl_app_load(self):
3838
plVfsI.mount_directory("/cache", str(Path.cwd()) + "/../cache")
3939
plVfsI.mount_directory("/shaders", os.path.dirname(os.path.abspath(pl.__file__)) + "/shaders")
4040
plVfsI.mount_directory("/shader-temp", str(Path.cwd()) + "/../shader-temp")
41-
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../../pilotlight/assets")
41+
plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../pilotlight/assets")
4242

4343
# Create and show the OS window.
4444
window_desc = plWindowDesc()
@@ -53,11 +53,9 @@ def pl_app_load(self):
5353
# Initialize the starter extension.
5454
# We explicitly disable the shader ext from the starter flags because
5555
# we are going to initialize the shader system ourselves below.
56-
starter_flags = plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS
57-
starter_flags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
58-
starter_flags |= plStarterFlag.PL_STARTER_FLAGS_MSAA
59-
60-
plStarterI.initialize(self.ptWindow, starter_flags)
56+
starterInit = plStarterInit(plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS | plStarterFlag.PL_STARTER_FLAGS_MSAA, self.ptWindow)
57+
starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT
58+
plStarterI.initialize(starterInit)
6159

6260
# Initialize shader system.
6361
# This is required even for simple drawing examples because the

extensions/pl_starter_ext_m.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,22 @@ Index of this file:
2020
//-----------------------------------------------------------------------------
2121

2222
PyObject*
23-
starter_initialize(PyObject* self, PyObject* args, PyObject* kwargs)
23+
starter_initialize(PyObject* self, PyObject* ptInit)
2424
{
25-
PyObject* ptWindow = NULL;
2625

27-
static const char* apcKeywords[] = {
28-
"window",
29-
"flags",
30-
NULL,
31-
};
26+
plStarterInit tStarterInit = {0};
3227

33-
int iFlags = 0;
34-
if (!pl_parse_args("Oi", (const char**)apcKeywords, args, kwargs, __FUNCTION__,
35-
&ptWindow, &iFlags))
36-
return NULL;
37-
38-
plWindow* ptWindowPtr = PyCapsule_GetPointer(ptWindow, "plWindow");
28+
{
29+
PyObject* ptPythonOption = PyObject_GetAttrString(ptInit, "eFlags");
30+
tStarterInit.eFlags = PyLong_AsInt(ptPythonOption);
31+
Py_DECREF(ptPythonOption);
32+
}
3933

40-
// initialize the starter API (handles alot of boilerplate)
41-
plStarterInit tStarterInit = {
42-
.eFlags = iFlags,
43-
.ptWindow = ptWindowPtr
44-
};
34+
{
35+
PyObject* ptPythonOption = PyObject_GetAttrString(ptInit, "ptWindow");
36+
tStarterInit.ptWindow = PyCapsule_GetPointer(ptPythonOption, "plWindow");
37+
Py_DECREF(ptPythonOption);
38+
}
4539
gptStarter->initialize(tStarterInit);
4640
Py_RETURN_NONE;
4741
}
@@ -200,6 +194,7 @@ plPythonIntConstantPair gatStarterIntPairs[] = {
200194
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_SHADER_EXT),
201195
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_SCREEN_LOG_EXT),
202196
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_GRAPHICS_EXT),
197+
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_RESOURCE_EXT),
203198
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_TOOLS_EXT),
204199
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_DEPTH_BUFFER),
205200
PL_ADD_INT_CONSTANT(PL_STARTER_FLAGS_MSAA),
@@ -211,7 +206,7 @@ plPythonIntConstantPair gatStarterIntPairs[] = {
211206
static PyMethodDef gatCommandsplStarterI[] =
212207
{
213208
{"begin_frame", (PyCFunction)starter_begin_frame, METH_NOARGS | METH_STATIC, NULL},
214-
{"initialize", (PyCFunction)starter_initialize, METH_VARARGS | METH_STATIC, NULL},
209+
{"initialize", (PyCFunction)starter_initialize, METH_O | METH_STATIC, NULL},
215210
{"finalize", (PyCFunction)starter_finalize, METH_NOARGS | METH_STATIC, NULL},
216211
{"cleanup", (PyCFunction)starter_cleanup, METH_NOARGS | METH_STATIC, NULL},
217212
{"resize", (PyCFunction)starter_resize, METH_NOARGS | METH_STATIC, NULL},

pilotlight/data/template_2d_app.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

Comments
 (0)