Mercurial > games > semicongine
annotate src/semicongine/engine.nim @ 647:6735f6a2de7a
add: working fullscreen for windows (tested)
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 03 May 2023 06:54:37 -0700 |
parents | 42ad7e6208e9 |
children | de9dff24c422 |
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 |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
13 import ./events |
588 | 14 import ./config |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
15 import ./math |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
16 import ./audio |
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 ) | |
632
42ad7e6208e9
add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents:
628
diff
changeset
|
95 startMixerThread() |
588 | 96 |
592
cda5874a8454
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
590
diff
changeset
|
97 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
|
98 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
|
99 engine.renderer = engine.device.initRenderer(renderPass) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
100 |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
101 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
|
102 assert engine.state != Destroyed |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
103 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
|
104 engine.renderer.setupDrawableBuffers(scene, vertexInput, transformAttribute=transformAttribute) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
105 |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
106 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
|
107 assert engine.state == Running |
601 | 108 assert engine.renderer.valid |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
109 if engine.state == Running: |
603
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
110 engine.renderer.refreshMeshData(scene) |
f494e291ff4a
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
601
diff
changeset
|
111 engine.renderer.render(scene) |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
112 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
113 proc updateInputs*(engine: var Engine): EngineState = |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
114 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
|
115 |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
116 engine.input.keyWasPressed = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
117 engine.input.keyWasReleased = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
118 engine.input.mouseWasPressed = {} |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
119 engine.input.mouseWasReleased = {} |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
120 engine.input.windowWasResized = engine.state == Starting |
605 | 121 |
122 var killed = false | |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
123 for event in engine.window.pendingEvents(): |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
124 inc engine.input.eventsProcessed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
125 if engine.eventHandler != nil: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
126 engine.eventHandler(engine, event) |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
127 case event.eventType: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
128 of Quit: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
129 killed = true |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
130 of ResizedWindow: |
605 | 131 engine.input.windowWasResized = true |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
132 of KeyPressed: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
133 engine.input.keyWasPressed.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
134 engine.input.keyIsDown.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
135 of KeyReleased: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
136 engine.input.keyWasReleased.incl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
137 engine.input.keyIsDown.excl event.key |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
138 of MousePressed: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
139 engine.input.mouseWasPressed.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
140 engine.input.mouseIsDown.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
141 of MouseReleased: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
142 engine.input.mouseWasReleased.incl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
143 engine.input.mouseIsDown.excl event.button |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
144 of MouseMoved: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
145 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
|
146 if engine.state == Starting: |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
147 engine.state = Running |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
148 if killed: |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
149 engine.state = Shutdown |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
150 if engine.exitHandler != nil: |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
151 engine.exitHandler(engine) |
605 | 152 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
|
153 engine.resizeHandler(engine) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
605
diff
changeset
|
154 return engine.state |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
155 |
626 | 156 # wrappers for internal things |
590
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
165 func framesRendered*(engine: Engine): auto = engine.renderer.framesRendered |
1edf3e16144e
add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents:
588
diff
changeset
|
166 func gpuDevice*(engine: Engine): Device = engine.device |
601 | 167 func getWindow*(engine: Engine): auto = engine.window |
605 | 168 func windowWasResized*(engine: Engine): auto = engine.input.windowWasResized |
626 | 169 func showSystemCursor*(engine: Engine) = engine.window.showSystemCursor() |
170 func hideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor() | |
647
6735f6a2de7a
add: working fullscreen for windows (tested)
Sam <sam@basx.dev>
parents:
632
diff
changeset
|
171 proc fullscreen*(engine: var Engine, enable: bool) = engine.window.fullscreen(enable) |