annotate src/semicongine/engine.nim @ 769:bb4c36d68444

add: some API improvments for vector, entity, and some other stuff
author Sam <sam@basx.dev>
date Sat, 08 Jul 2023 18:52:34 +0700
parents 0b5566fc214d
children 4921ec86dcb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
2 import std/options
653
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 650
diff changeset
3 import std/logging
649
c774b064def5 add: image/texture creation, refactoring of some unclean parts
Sam <sam@basx.dev>
parents: 647
diff changeset
4 import std/os
c774b064def5 add: image/texture creation, refactoring of some unclean parts
Sam <sam@basx.dev>
parents: 647
diff changeset
5
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
6 import ./platform/window
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
7
667
d3500ae5d235 did: small refactoring of module structure
Sam <sam@basx.dev>
parents: 658
diff changeset
8 import ./core
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./vulkan/instance
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./vulkan/device
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
11 import ./vulkan/physicaldevice
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
12 import ./vulkan/renderpass
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
13
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 699
diff changeset
14 import ./scene
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
15 import ./renderer
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
16 import ./events
632
42ad7e6208e9 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 628
diff changeset
17 import ./audio
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
18
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
19 type
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
20 EngineState* = enum
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
21 Starting
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
22 Running
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
23 Shutdown
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
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
32 mouseMove: Vec2f
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
33 eventsProcessed: uint64
605
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
34 windowWasResized: bool
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
35 mouseWheel: float32
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
36 Engine* = object
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
37 state*: EngineState
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
38 device: Device
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
39 debugger: Debugger
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
40 instance: Instance
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
41 window: NativeWindow
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
42 renderer: Option[Renderer]
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
43 input: Input
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
44 exitHandler: proc(engine: var Engine)
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
45 resizeHandler: proc(engine: var Engine)
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
46 eventHandler: proc(engine: var Engine, event: Event)
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
47
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
48
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
49 proc destroy*(engine: var Engine) =
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
50 checkVkResult engine.device.vk.vkDeviceWaitIdle()
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
51 if engine.renderer.isSome:
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
52 engine.renderer.get.destroy()
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
53 engine.device.destroy()
595
7f7e797335ed add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents: 594
diff changeset
54 if engine.debugger.messenger.valid:
7f7e797335ed add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents: 594
diff changeset
55 engine.debugger.destroy()
7f7e797335ed add: remove a few warning, improve a few things
Sam <sam@basx.dev>
parents: 594
diff changeset
56 engine.window.destroy()
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
57 engine.instance.destroy()
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
58
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
59
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
60 proc initEngine*(
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
61 applicationName: string,
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
62 debug=DEBUG,
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
63 exitHandler: proc(engine: var Engine) = nil,
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
64 resizeHandler: proc(engine: var Engine) = nil,
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
65 eventHandler: proc(engine: var Engine, event: Event) = nil
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
66 ): Engine =
653
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 650
diff changeset
67 echo "Set log level to ", ENGINE_LOGLEVEL
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 650
diff changeset
68 setLogFilter(ENGINE_LOGLEVEL)
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 650
diff changeset
69
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
70 result.state = Starting
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
71 result.exitHandler = exitHandler
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
72 result.resizeHandler = resizeHandler
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
73 result.eventHandler = eventHandler
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
74 result.window = createWindow(applicationName)
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
75
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
76 var
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
77 instanceExtensions: seq[string]
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
78 enabledLayers: seq[string]
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
79
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
80 if debug:
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
81 instanceExtensions.add "VK_EXT_debug_utils"
613
40f5d804eca6 fix: wrong layers activated
Sam <sam@basx.dev>
parents: 606
diff changeset
82 enabledLayers.add "VK_LAYER_KHRONOS_validation"
649
c774b064def5 add: image/texture creation, refactoring of some unclean parts
Sam <sam@basx.dev>
parents: 647
diff changeset
83 putEnv("VK_LAYER_ENABLES", "VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT")
c774b064def5 add: image/texture creation, refactoring of some unclean parts
Sam <sam@basx.dev>
parents: 647
diff changeset
84
769
bb4c36d68444 add: some API improvments for vector, entity, and some other stuff
Sam <sam@basx.dev>
parents: 749
diff changeset
85 if defined(linux) and DEBUG:
672
d7c61e6580ea did: refactor image handling
Sam <sam@basx.dev>
parents: 671
diff changeset
86 enabledLayers.add "VK_LAYER_MESA_overlay"
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
87 result.instance = result.window.createInstance(
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
88 vulkanVersion=VK_MAKE_API_VERSION(0, 1, 3, 0),
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
89 instanceExtensions=instanceExtensions,
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
90 layers=enabledLayers,
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
91 )
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
92 if debug:
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
93 result.debugger = result.instance.createDebugMessenger()
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
94 # create devices
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
95 let selectedPhysicalDevice = result.instance.getPhysicalDevices().filterBestGraphics()
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
96 result.device = result.instance.createDevice(
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
97 selectedPhysicalDevice,
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
98 enabledLayers = @[],
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
99 enabledExtensions = @[],
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
100 selectedPhysicalDevice.filterForGraphicsPresentationQueues()
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
101 )
632
42ad7e6208e9 add: audio subsystem, windows backend still missing
Sam <sam@basx.dev>
parents: 628
diff changeset
102 startMixerThread()
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
103
592
cda5874a8454 add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 590
diff changeset
104 proc setRenderer*(engine: var Engine, renderPass: RenderPass) =
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
105 if engine.renderer.isSome:
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
106 engine.renderer.get.destroy()
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
107 engine.renderer = some(engine.device.initRenderer(renderPass))
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
108
731
c12b11a5f112 did: overhaul some of the mesh-data uploading and transformation handling, added: text/font rendering
Sam <sam@basx.dev>
parents: 712
diff changeset
109 proc addScene*(engine: var Engine, scene: Scene, vertexInput: seq[ShaderAttribute], samplers: seq[ShaderAttribute], transformAttribute="transform") =
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
110 assert transformAttribute == "" or transformAttribute in map(vertexInput, proc(a: ShaderAttribute): string = a.name)
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
111 assert engine.renderer.isSome
712
7a7fb1a42811 fix: add descriptor pool per scene, use zippy instead of zip
sam <sam@basx.dev>
parents: 708
diff changeset
112 engine.renderer.get.setupDrawableBuffers(scene, vertexInput, samplers, transformAttribute=transformAttribute)
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
113
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 649
diff changeset
114 proc renderScene*(engine: var Engine, scene: var Scene) =
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
115 assert engine.state == Running
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
116 assert engine.renderer.isSome
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
117 engine.renderer.get.updateMeshData(scene)
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
118 engine.renderer.get.updateUniformData(scene)
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
119 engine.renderer.get.render(scene)
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
120
749
0b5566fc214d add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 731
diff changeset
121 proc updateAnimations*(engine: var Engine, scene: var Scene, dt: float32) =
0b5566fc214d add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 731
diff changeset
122 assert engine.state == Running
0b5566fc214d add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 731
diff changeset
123 assert engine.renderer.isSome
0b5566fc214d add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 731
diff changeset
124 engine.renderer.get.updateAnimations(scene, dt)
0b5566fc214d add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents: 731
diff changeset
125
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
126 proc updateInputs*(engine: var Engine): EngineState =
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
127 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
128
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
129 engine.input.keyWasPressed = {}
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
130 engine.input.keyWasReleased = {}
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
131 engine.input.mouseWasPressed = {}
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
132 engine.input.mouseWasReleased = {}
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
133 engine.input.mouseWheel = 0
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
134 engine.input.mouseMove = newVec2f()
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
135 if engine.state == Starting:
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
136 engine.input.windowWasResized = true
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
137 var mpos = engine.window.getMousePosition()
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
138 if mpos.isSome:
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
139 engine.input.mousePosition = mpos.get
605
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
140
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
141 var killed = false
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
142 for event in engine.window.pendingEvents():
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
143 inc engine.input.eventsProcessed
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
144 if engine.eventHandler != nil:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
145 engine.eventHandler(engine, event)
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
146 case event.eventType:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
147 of Quit:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
148 killed = true
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
149 of ResizedWindow:
605
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
150 engine.input.windowWasResized = true
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
151 of KeyPressed:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
152 engine.input.keyWasPressed.incl event.key
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
153 engine.input.keyIsDown.incl event.key
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
154 of KeyReleased:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
155 engine.input.keyWasReleased.incl event.key
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
156 engine.input.keyIsDown.excl event.key
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
157 of MousePressed:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
158 engine.input.mouseWasPressed.incl event.button
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
159 engine.input.mouseIsDown.incl event.button
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
160 of MouseReleased:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
161 engine.input.mouseWasReleased.incl event.button
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
162 engine.input.mouseIsDown.excl event.button
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
163 of MouseMoved:
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
164 let newPos = newVec2(float32(event.x), float32(event.y))
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
165 engine.input.mouseMove = newPos - engine.input.mousePosition
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
166 engine.input.mousePosition = newPos
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
167 of MouseWheel:
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
168 engine.input.mouseWheel = event.amount
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
169 if engine.state == Starting:
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
170 engine.state = Running
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
171 if killed:
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
172 engine.state = Shutdown
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
173 if engine.exitHandler != nil:
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
174 engine.exitHandler(engine)
605
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
175 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
176 engine.resizeHandler(engine)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 605
diff changeset
177 return engine.state
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
178
626
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 613
diff changeset
179 # wrappers for internal things
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
180 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
181 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
182 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
183 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
184 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
185 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
186 func mousePosition*(engine: Engine): auto = engine.input.mousePosition
699
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
187 func mouseMove*(engine: Engine): auto = engine.input.mouseMove
c29f3ad1604f did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 689
diff changeset
188 func mouseWheel*(engine: Engine): auto = engine.input.mouseWheel
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
189 func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed
658
8549373571aa did: improve stability for configuration without uniforms and settings the engine renderer
Sam <sam@basx.dev>
parents: 653
diff changeset
190 func framesRendered*(engine: Engine): uint64 = (if engine.renderer.isSome: engine.renderer.get.framesRendered else: 0)
590
1edf3e16144e add: input handling, small refactoring for renderer
Sam <sam@basx.dev>
parents: 588
diff changeset
191 func gpuDevice*(engine: Engine): Device = engine.device
601
7e54bbbcffba add: some helper functions
Sam <sam@basx.dev>
parents: 595
diff changeset
192 func getWindow*(engine: Engine): auto = engine.window
605
510c651cb27d add: window-resize check
Sam <sam@basx.dev>
parents: 603
diff changeset
193 func windowWasResized*(engine: Engine): auto = engine.input.windowWasResized
626
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 613
diff changeset
194 func showSystemCursor*(engine: Engine) = engine.window.showSystemCursor()
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 613
diff changeset
195 func hideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor()
647
6735f6a2de7a add: working fullscreen for windows (tested)
Sam <sam@basx.dev>
parents: 632
diff changeset
196 proc fullscreen*(engine: var Engine, enable: bool) = engine.window.fullscreen(enable)