annotate semicongine/engine.nim @ 905:a9d71d2c2461

did: same for panels
author Sam <sam@basx.dev>
date Sat, 24 Feb 2024 15:35:52 +0700
parents a253a691e0d6
children f4bc149a385c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
1 {.experimental: "codeReordering".}
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
2
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/options
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/logging
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/os
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
6
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import ./platform/window
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./core
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./vulkan/instance
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11 import ./vulkan/device
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 import ./vulkan/physicaldevice
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 import ./vulkan/shader
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15 import ./scene
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 import ./material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 import ./renderer
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 import ./events
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19 import ./audio
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 import ./text
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
21 import ./panel
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
23 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 EngineState* = enum
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 Starting
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 Running
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 Shutdown
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 Input = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29 keyIsDown: set[Key]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
30 keyWasPressed: set[Key]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
31 keyWasReleased: set[Key]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
32 mouseIsDown: set[MouseButton]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
33 mouseWasPressed: set[MouseButton]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34 mouseWasReleased: set[MouseButton]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
35 mousePosition: Vec2f
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36 mouseMove: Vec2f
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
37 eventsProcessed: uint64
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
38 windowWasResized: bool
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
39 mouseWheel: float32
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
40 Engine* = object
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
41 state*: EngineState
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
42 device: Device
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
43 debugger: Debugger
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
44 instance: Instance
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
45 window: NativeWindow
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
46 renderer: Option[Renderer]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 input: Input
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48 exitHandler: proc(engine: var Engine)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49 resizeHandler: proc(engine: var Engine)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
50 eventHandler: proc(engine: var Engine, event: Event)
859
a55410a08c63 add: better api for fullscreen management
sam <sam@basx.dev>
parents: 840
diff changeset
51 fullscreen: bool
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
52
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
53
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
54 proc destroy*(engine: var Engine) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
55 checkVkResult engine.device.vk.vkDeviceWaitIdle()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 if engine.renderer.isSome:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
57 engine.renderer.get.destroy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58 engine.device.destroy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59 if engine.debugger.messenger.valid:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 engine.debugger.destroy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
61 engine.window.destroy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 engine.instance.destroy()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
63
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
65 proc initEngine*(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
66 applicationName: string,
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
67 debug = DEBUG,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68 exitHandler: proc(engine: var Engine) = nil,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
69 resizeHandler: proc(engine: var Engine) = nil,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
70 eventHandler: proc(engine: var Engine, event: Event) = nil
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
71 ): Engine =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
72 echo "Set log level to ", ENGINE_LOGLEVEL
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 setLogFilter(ENGINE_LOGLEVEL)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
75 result.state = Starting
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
76 result.exitHandler = exitHandler
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
77 result.resizeHandler = resizeHandler
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
78 result.eventHandler = eventHandler
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
79 result.window = createWindow(applicationName)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
80
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81 var
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
82 instanceExtensions: seq[string]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
83 enabledLayers: seq[string]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
84
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
85 if debug:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
86 instanceExtensions.add "VK_EXT_debug_utils"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87 enabledLayers.add "VK_LAYER_KHRONOS_validation"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
88 # putEnv("VK_LAYER_ENABLES", "VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
89 putEnv("VK_LAYER_ENABLES", "")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
91 if defined(linux) and DEBUG:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 enabledLayers.add "VK_LAYER_MESA_overlay"
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 result.instance = result.window.createInstance(
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
94 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0),
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
95 instanceExtensions = instanceExtensions,
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
96 layers = enabledLayers,
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
97 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 if debug:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
99 result.debugger = result.instance.createDebugMessenger()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
100 # create devices
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 let selectedPhysicalDevice = result.instance.getPhysicalDevices().filterBestGraphics()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102 result.device = result.instance.createDevice(
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
103 selectedPhysicalDevice,
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
104 enabledLayers = @[],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
105 enabledExtensions = @[],
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
106 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
107 )
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
108 startMixerThread()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
109
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
110 proc initRenderer*(engine: var Engine, shaders: openArray[(MaterialType, ShaderConfiguration)], clearColor = Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32]), backFaceCulling = true) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
111
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
112 assert not engine.renderer.isSome
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
113 var allShaders = @shaders
897
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 895
diff changeset
114 allShaders.add (EMPTY_MATERIAL, EMPTY_SHADER)
fa54a8a1e3e4 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 895
diff changeset
115 allShaders.add (PANEL_MATERIAL_TYPE, PANEL_SHADER)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
116 allShaders.add (TEXT_MATERIAL_TYPE, TEXT_SHADER)
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
117 engine.renderer = some(engine.device.initRenderer(shaders = allShaders, clearColor = clearColor, backFaceCulling = backFaceCulling))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
118
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
119 proc initRenderer*(engine: var Engine, clearColor = Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32])) =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 engine.initRenderer(@[], clearColor)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
122 proc loadScene*(engine: var Engine, scene: var Scene) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
123 assert engine.renderer.isSome
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
124 assert not scene.loaded
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
125 scene.addShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.getAspectRatio)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
126 engine.renderer.get.setupDrawableBuffers(scene)
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
127 engine.renderer.get.updateMeshData(scene, forceAll = true)
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 872
diff changeset
128 engine.renderer.get.updateUniformData(scene, forceAll = true)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
129
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
130 proc unloadScene*(engine: var Engine, scene: Scene) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
131 engine.renderer.get.destroy(scene)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
132
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
133 proc renderScene*(engine: var Engine, scene: var Scene) =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
134 assert engine.state == Running
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
135 assert engine.renderer.isSome
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
136 scene.setShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.getAspectRatio)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
137 engine.renderer.get.updateMeshData(scene)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
138 engine.renderer.get.updateUniformData(scene)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
139 engine.renderer.get.render(scene)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
140
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
141 proc updateInputs*(engine: var Engine): EngineState =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
142 assert engine.state in [Starting, Running]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
143
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
144 # reset input states
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
145 engine.input.keyWasPressed = {}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146 engine.input.keyWasReleased = {}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
147 engine.input.mouseWasPressed = {}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
148 engine.input.mouseWasReleased = {}
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
149 engine.input.mouseWheel = 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
150 engine.input.mouseMove = newVec2f()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
151 engine.input.windowWasResized = false
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
152
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
153 if engine.state == Starting:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
154 engine.input.windowWasResized = true
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
155 var mpos = engine.window.getMousePosition()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
156 if mpos.isSome:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
157 engine.input.mousePosition = mpos.get
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
158
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
159 var killed = false
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
160 for event in engine.window.pendingEvents():
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
161 inc engine.input.eventsProcessed
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
162 if engine.eventHandler != nil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
163 engine.eventHandler(engine, event)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
164 case event.eventType:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
165 of Quit:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
166 killed = true
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
167 of ResizedWindow:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
168 engine.input.windowWasResized = true
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
169 of KeyPressed:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
170 engine.input.keyWasPressed.incl event.key
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
171 engine.input.keyIsDown.incl event.key
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
172 of KeyReleased:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
173 engine.input.keyWasReleased.incl event.key
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
174 engine.input.keyIsDown.excl event.key
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
175 of MousePressed:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
176 engine.input.mouseWasPressed.incl event.button
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
177 engine.input.mouseIsDown.incl event.button
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
178 of MouseReleased:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
179 engine.input.mouseWasReleased.incl event.button
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
180 engine.input.mouseIsDown.excl event.button
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
181 of MouseMoved:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
182 let newPos = newVec2(float32(event.x), float32(event.y))
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
183 engine.input.mouseMove = newPos - engine.input.mousePosition
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
184 engine.input.mousePosition = newPos
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
185 of MouseWheel:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
186 engine.input.mouseWheel = event.amount
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
187 if engine.state == Starting:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
188 engine.state = Running
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
189 if killed:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
190 engine.state = Shutdown
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
191 if engine.exitHandler != nil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
192 engine.exitHandler(engine)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
193 if engine.input.windowWasResized and engine.resizeHandler != nil:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
194 engine.resizeHandler(engine)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
195 return engine.state
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
196
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
197 # wrappers for internal things
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
198 func keyIsDown*(engine: Engine, key: Key): auto = key in engine.input.keyIsDown
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
199 func keyWasPressed*(engine: Engine, key: Key): auto = key in engine.input.keyWasPressed
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
200 func keyWasPressed*(engine: Engine): auto = engine.input.keyWasPressed.len > 0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
201 func keyWasReleased*(engine: Engine, key: Key): auto = key in engine.input.keyWasReleased
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
202 func mouseIsDown*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseIsDown
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
203 func mouseWasPressed*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasPressed
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
204 func mouseWasReleased*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasReleased
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
205 func mousePosition*(engine: Engine): auto = engine.input.mousePosition
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
206 func mousePositionNormalized*(engine: Engine): Vec2f =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
207 result.x = (engine.input.mousePosition.x / float32(engine.window.size[0])) * 2.0 - 1.0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
208 result.y = (engine.input.mousePosition.y / float32(engine.window.size[1])) * 2.0 - 1.0
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
209 func mouseMove*(engine: Engine): auto = engine.input.mouseMove
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
210 func mouseWheel*(engine: Engine): auto = engine.input.mouseWheel
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
211 func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
212 func framesRendered*(engine: Engine): uint64 = (if engine.renderer.isSome: engine.renderer.get.framesRendered else: 0)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
213 func gpuDevice*(engine: Engine): Device = engine.device
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
214 func getWindow*(engine: Engine): auto = engine.window
904
a253a691e0d6 fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents: 903
diff changeset
215 func getAspectRatio*(engine: Engine): float32 = engine.getWindow().size[0] / engine.getWindow().size[1]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
216 func windowWasResized*(engine: Engine): auto = engine.input.windowWasResized
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
217 func showSystemCursor*(engine: Engine) = engine.window.showSystemCursor()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
218 func hideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor()
859
a55410a08c63 add: better api for fullscreen management
sam <sam@basx.dev>
parents: 840
diff changeset
219 func fullscreen*(engine: Engine): bool = engine.fullscreen
a55410a08c63 add: better api for fullscreen management
sam <sam@basx.dev>
parents: 840
diff changeset
220 proc `fullscreen=`*(engine: var Engine, enable: bool) =
862
e5d7e3f6826e fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents: 859
diff changeset
221 if enable != engine.fullscreen:
e5d7e3f6826e fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents: 859
diff changeset
222 engine.fullscreen = enable
e5d7e3f6826e fix: do not try to change fullscreen if already in correct status
sam <sam@basx.dev>
parents: 859
diff changeset
223 engine.window.fullscreen(engine.fullscreen)
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 862
diff changeset
224
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 862
diff changeset
225 func limits*(engine: Engine): VkPhysicalDeviceLimits =
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 862
diff changeset
226 engine.gpuDevice().physicalDevice.properties.limits
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
227
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 904
diff changeset
228 proc processEvents*(engine: Engine, panel: var Panel) =
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 904
diff changeset
229 let hasMouseNow = panel.contains(engine.mousePositionNormalized, engine.getAspectRatio)
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
230
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
231 # enter/leave events
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
232 if hasMouseNow:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
233 if panel.hasMouse:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
234 if panel.onMouseMove != nil: panel.onMouseMove(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
235 else:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
236 if panel.onMouseEnter != nil: panel.onMouseEnter(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
237 else:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
238 if panel.hasMouse:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
239 if panel.onMouseLeave != nil: panel.onMouseLeave(panel)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
240
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
241 # button events
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
242 if hasMouseNow:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
243 if engine.input.mouseWasPressed.len > 0:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
244 if panel.onMouseDown != nil: panel.onMouseDown(panel, engine.input.mouseWasPressed)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
245 if engine.input.mouseWasReleased.len > 0:
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
246 if panel.onMouseUp != nil: panel.onMouseUp(panel, engine.input.mouseWasReleased)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
247
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
248 panel.hasMouse = hasMouseNow