-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmango.go
244 lines (174 loc) · 5.21 KB
/
mango.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package mango
import (
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/micahke/mango/core"
"github.com/micahke/mango/core/settings"
"github.com/micahke/mango/im"
"github.com/micahke/mango/input"
"github.com/micahke/mango/logging"
"github.com/micahke/mango/opengl"
"github.com/micahke/mango/system"
"github.com/micahke/mango/util"
"github.com/micahke/mango/util/loaders"
)
type Mango struct {
RenderMode core.RenderMode // The currently set render mode
Window *core.Window
LogPanel *logging.LogPanel
SceneEditor *core.SceneEditor
ShaderEditor *core.ShaderEditor
ImguiSandbox *core.ImguiSandbox
SystemManager *core.SystemManager
scene *core.Scene
}
// The main engine instance
var Engine *Mango
// RENDER MODES
var IM *im.IMMEDIATE_MODE
func init() {
runtime.LockOSThread()
}
// Initialization function for the engine
func Init(renderMode core.RenderMode, args ...any) {
// create a nre instance of Mango
Engine = new(Mango)
// DEBUGGING ENABLED AFTER THIS POINT
logging.DebugLog("LOGGING ENABLED")
// set the rendering mode
Engine.RenderMode = renderMode
if renderMode == core.RENDER_MODE_IM {
IM = im.Init()
}
// Enable GLFW
core.GLFWInit()
// Initialize resource loaders
loaders.InitPNGLoader()
// Load shaders
_, error := opengl.LoadShaders()
if error != nil {
logging.DebugLogError("Failed to load shaders: ", error)
}
err := loaders.Init()
if err != nil {
logging.DebugLogError(err)
}
Engine.SystemManager = core.NewSystemManager()
}
// Creates a new window
func CreateWindow(width, height int, title string, vsync bool) {
Engine.Window = core.CreateWindow(width, height, title, vsync)
Engine.Window.SetCursorPosCallback(input.CursorCallback)
Engine.Window.SetMouseButtonCallback(input.MouseButtonCallback)
Engine.Window.SetKeyCallback(input.KeyCallback)
// Initialize the keyboard input system
input.InitKeyboardInput(Engine.Window)
if Engine.RenderMode == core.RENDER_MODE_IM {
IM.InitProjectionMatrix(float32(width), float32(height))
}
// At this point, OpenGL is ready to be used anywhere in the program
util.InitImguiLayer(Engine.Window.Window)
// Create logging panel
Engine.LogPanel = logging.InitLogPanel(width, height)
util.ImguiRegisterPanel("logPanel", Engine.LogPanel)
if settings.Settings.CONSOLE_ON_STARTUP {
util.ImguiActivatePanel("logPanel")
}
// Scene Editor
if Engine.RenderMode == core.RENDER_MODE_DEFAULT {
Engine.SceneEditor = core.NewSceneEditor(Engine.scene, width, height)
util.ImguiRegisterPanel("sceneEditor", Engine.SceneEditor)
if settings.Settings.SCENE_EDITOR_STARTUP {
util.ImguiActivatePanel("sceneEditor")
}
Engine.SystemManager.AddSystem(&system.RenderSystem{
Entities: Engine.scene.ECS().GetEntities(),
})
}
// NON_DEFAULT TOOLS
Engine.ShaderEditor = core.NewShaderEditor()
util.ImguiRegisterPanel("shaderEditor", Engine.ShaderEditor)
if settings.Settings.SHADER_EDITOR_ON_STARTUP {
util.ImguiActivatePanel("shaderEditor")
}
Engine.ImguiSandbox = core.InitImguiSandbox()
util.ImguiRegisterPanel("imguiSandbox", Engine.ImguiSandbox)
if settings.Settings.IMGUI_SANDBOX_ON_STARTUP {
util.ImguiActivatePanel("imguiSandbox")
}
}
// Creates a scene and sets up an ECS
func CreateScene() *core.Scene {
logging.DebugLog("Scene creation requested: ")
scene := core.NewScene()
return scene
}
// Handles the creation of the core systems that the ECS uses
// These systems are the ones responsible to handling the entity's logic
func setupCoreSystems() {
}
func SetScene(scene *core.Scene) {
Engine.scene = scene
Engine.SystemManager.AddSystem(&system.EntitySystem{
Entities: scene.ECS().GetEntities(),
})
}
// Starts the main game loop
func Start() {
if Engine.Window == nil {
panic("No window initialized")
}
core.InitTimer()
for !Engine.Window.Window.ShouldClose() {
start := glfw.GetTime()
Engine.Window.SetMouseButtonCallback(input.MouseButtonCallback)
if util.ImguiWantsTextInput() {
util.ImguiSetDefaultKeyCallback()
} else {
Engine.Window.SetKeyCallback(input.KeyCallback)
}
glfw.PollEvents()
core.Timer.Update()
util.ImguiNewFrame()
// TODO: find a better place for this
processInput()
// fmt.Println(core.Timer.FPS())
// Clear the screen
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Check the rendermode and do appropriate stuff
if Engine.RenderMode == core.RENDER_MODE_IM {
IM.NewFrame(core.Timer.DeltaTime())
}
if Engine.RenderMode == core.RENDER_MODE_DEFAULT {
// Engine.scene.ECS().Update()
Engine.SystemManager.Update()
}
util.ImguiRender()
// Input handler reset
input.MouseInputCleanup()
input.ResetKeyInput()
Engine.Window.Window.SwapBuffers()
end := glfw.GetTime()
core.Timer.UpdateFrameData(start, end)
}
glfw.Terminate()
cleanup()
}
// TODO: move this
func GetWindow() *core.Window {
return Engine.Window
}
// handle any engine cleanup that needs to be done
func cleanup() {
}
func processInput() {
// On left CTRL, show log
if input.GetKeyDown(input.KEY_LEFT_CTRL) {
util.ImguiTogglePanel("logPanel")
}
// On left tab, open scene editor
if input.GetKeyDown(input.KEY_TAB) {
util.ImguiTogglePanel("sceneEditor")
}
}