Mercurial > games > semicongine
annotate src/semicongine/engine.nim @ 627:34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 30 Apr 2023 01:02:33 +0700 |
parents | bf2f4a9cd962 |
children | 4d4f7a2eb6ca |
rev | line source |
---|---|
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
1 import std/sequtils |
588 | 2 import ./platform/window |
3 | |
4 import ./vulkan/api | |
5 import ./vulkan/instance | |
6 import ./vulkan/device | |
7 import ./vulkan/physicaldevice | |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
8 import ./vulkan/renderpass |
588 | 9 |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
10 import ./gpu_data |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
11 import ./entity |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
12 import ./renderer |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
13 import ./mesh |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
14 import ./events |
588 | 15 import ./config |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
16 import ./math |
588 | 17 |
18 type | |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
19 EngineState* = enum |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
20 Starting |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
21 Running |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
22 Shutdown |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
23 Destroyed |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
24 Input = object |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
25 keyIsDown: set[Key] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
26 keyWasPressed: set[Key] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
27 keyWasReleased: set[Key] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
28 mouseIsDown: set[MouseButton] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
29 mouseWasPressed: set[MouseButton] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
30 mouseWasReleased: set[MouseButton] |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
31 mousePosition: Vec2f |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
32 eventsProcessed: uint64 |
605 | 33 windowWasResized: bool |
588 | 34 Engine* = object |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
35 state*: EngineState |
588 | 36 device: Device |
37 debugger: Debugger | |
38 instance: Instance | |
39 window: NativeWindow | |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
40 renderer: Renderer |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
41 input: Input |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
42 exitHandler: proc(engine: var Engine) |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
43 resizeHandler: proc(engine: var Engine) |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
44 eventHandler: proc(engine: var Engine, event: Event) |
588 | 45 |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
46 |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
47 proc destroy*(engine: var Engine) = |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
48 checkVkResult engine.device.vk.vkDeviceWaitIdle() |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
49 engine.renderer.destroy() |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
50 engine.device.destroy() |
595
7f7e797335ed
add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents:
594
diff
changeset
|
51 if engine.debugger.messenger.valid: |
7f7e797335ed
add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents:
594
diff
changeset
|
52 engine.debugger.destroy() |
7f7e797335ed
add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents:
594
diff
changeset
|
53 engine.window.destroy() |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
54 engine.instance.destroy() |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
55 engine.state = Destroyed |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
56 |
588 | 57 |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
58 proc initEngine*( |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
59 applicationName: string, |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
60 debug=DEBUG, |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
61 exitHandler: proc(engine: var Engine) = nil, |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
62 resizeHandler: proc(engine: var Engine) = nil, |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
63 eventHandler: proc(engine: var Engine, event: Event) = nil |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
64 ): Engine = |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
65 result.state = Starting |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
66 result.exitHandler = exitHandler |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
67 result.resizeHandler = resizeHandler |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
68 result.eventHandler = eventHandler |
588 | 69 result.window = createWindow(applicationName) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
70 |
588 | 71 var |
72 instanceExtensions: seq[string] | |
73 enabledLayers: seq[string] | |
74 | |
75 if debug: | |
76 instanceExtensions.add "VK_EXT_debug_utils" | |
613 | 77 enabledLayers.add "VK_LAYER_KHRONOS_validation" |
78 if defined(linux): | |
79 enabledLayers.add "VK_LAYER_MESA_overlay" | |
588 | 80 result.instance = result.window.createInstance( |
81 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0), | |
82 instanceExtensions=instanceExtensions, | |
83 layers=enabledLayers, | |
84 ) | |
85 if debug: | |
86 result.debugger = result.instance.createDebugMessenger() | |
87 # create devices | |
88 let selectedPhysicalDevice = result.instance.getPhysicalDevices().filterBestGraphics() | |
89 result.device = result.instance.createDevice( | |
90 selectedPhysicalDevice, | |
91 enabledLayers = @[], | |
92 enabledExtensions = @[], | |
93 selectedPhysicalDevice.filterForGraphicsPresentationQueues() | |
94 ) | |
95 | |
592
cda5874a8454
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
590
diff
changeset
|
96 proc setRenderer*(engine: var Engine, renderPass: RenderPass) = |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
97 assert engine.state != Destroyed |
592
cda5874a8454
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
590
diff
changeset
|
98 engine.renderer = engine.device.initRenderer(renderPass) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
99 |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
100 proc addScene*(engine: var Engine, scene: Entity, vertexInput: seq[ShaderAttribute], transformAttribute="") = |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
101 assert engine.state != Destroyed |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
102 assert transformAttribute == "" or transformAttribute in map(vertexInput, proc(a: ShaderAttribute): string = a.name) |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
103 engine.renderer.setupDrawableBuffers(scene, vertexInput, transformAttribute=transformAttribute) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
104 |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
105 proc renderScene*(engine: var Engine, scene: Entity) = |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
106 assert engine.state == Running |
601 | 107 assert engine.renderer.valid |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
108 if engine.state == Running: |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
109 engine.renderer.refreshMeshData(scene) |
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
110 engine.renderer.render(scene) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
111 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
112 proc updateInputs*(engine: var Engine): EngineState = |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
113 assert engine.state in [Starting, Running] |
594
512d33d314c4
add: correct swapchain destruction, update 1st example to be working
Sam <sam@basx.dev>
parents:
592
diff
changeset
|
114 |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
115 engine.input.keyWasPressed = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
116 engine.input.keyWasReleased = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
117 engine.input.mouseWasPressed = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
118 engine.input.mouseWasReleased = {} |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
119 engine.input.windowWasResized = engine.state == Starting |
605 | 120 |
121 var killed = false | |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
122 for event in engine.window.pendingEvents(): |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
123 inc engine.input.eventsProcessed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
124 if engine.eventHandler != nil: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
125 engine.eventHandler(engine, event) |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
126 case event.eventType: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
127 of Quit: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
128 killed = true |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
129 of ResizedWindow: |
605 | 130 engine.input.windowWasResized = true |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
131 of KeyPressed: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
132 engine.input.keyWasPressed.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
133 engine.input.keyIsDown.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
134 of KeyReleased: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
135 engine.input.keyWasReleased.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
136 engine.input.keyIsDown.excl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
137 of MousePressed: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
138 engine.input.mouseWasPressed.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
139 engine.input.mouseIsDown.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
140 of MouseReleased: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
141 engine.input.mouseWasReleased.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
142 engine.input.mouseIsDown.excl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
143 of MouseMoved: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
144 engine.input.mousePosition = newVec2(float32(event.x), float32(event.y)) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
145 if engine.state == Starting: |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
146 engine.state = Running |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
147 if killed: |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
148 engine.state = Shutdown |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
149 if engine.exitHandler != nil: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
150 engine.exitHandler(engine) |
605 | 151 if engine.input.windowWasResized and engine.resizeHandler != nil: |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
152 engine.resizeHandler(engine) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
153 return engine.state |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
154 |
626 | 155 # wrappers for internal things |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
156 func keyIsDown*(engine: Engine, key: Key): auto = key in engine.input.keyIsDown |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
157 func keyWasPressed*(engine: Engine, key: Key): auto = key in engine.input.keyWasPressed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
158 func keyWasReleased*(engine: Engine, key: Key): auto = key in engine.input.keyWasReleased |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
159 func mouseIsDown*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseIsDown |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
160 func mouseWasPressed*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasPressed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
161 func mouseWasReleased*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasReleased |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
162 func mousePosition*(engine: Engine): auto = engine.input.mousePosition |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
163 func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
164 func framesRendered*(engine: Engine): auto = engine.renderer.framesRendered |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
165 func gpuDevice*(engine: Engine): Device = engine.device |
601 | 166 func getWindow*(engine: Engine): auto = engine.window |
605 | 167 func windowWasResized*(engine: Engine): auto = engine.input.windowWasResized |
626 | 168 func showSystemCursor*(engine: Engine) = engine.window.showSystemCursor() |
169 func hideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor() | |
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
170 proc fullscreen*(engine: Engine, enable: bool) = engine.window.fullscreen(enable) |