-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
207 lines (173 loc) · 5.51 KB
/
main.lua
File metadata and controls
207 lines (173 loc) · 5.51 KB
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
local lume = require 'lib.lume'
local gameConfig = require 'game_config'
local ContentControl = require 'engine.content_control'
local AssetManager = require 'engine.asset_manager'
local tick = require 'lib.tick'
local DisplayHandler = require 'engine.display_handler'
love.inspect = require 'lib.inspect'
local IMGUI_EXISTS, imgui = pcall(require, 'imgui')
if not IMGUI_EXISTS then
imgui = nil
end
-- singletons
local Singletons = require 'engine.singletons'
-- logger
require 'engine.logger'
-- windows doesnt support color out of the box
love.log.useColor = love.system.getOS() ~= 'Windows'
love.log.outFile = string.format('love-oracle_%s_log.txt', os.date('%Y-%m-%d'))
-- time
require 'engine.time'
-- init quake console
require 'lib.console'
require 'engine.console_commands'
love.log.trace('Game Init')
love.log.debug('Ziggy Engine ' .. gameConfig.version)
print(' |\\|\\')
print(' .. \\ .')
print('o-- \\\\ / @)')
print(' v__///\\\\\\\\__/ @')
print(' { }')
print(' { } \\\\\\{ }')
print(' <_| <_|')
print()
love.log.debug("OS: " .. love.system.getOS())
love.log.debug(('Renderer: %s %s\nVendor: %s\nGPU: %s'):format(love.graphics.getRendererInfo()))
love.log.debug('Save Directory: ' .. love.filesystem.getSaveDirectory())
print()
--[[
Defining helper function used in data scripting
Hot reloading can't modify existing functions, but it works with tables.
To work around this, this function will create a metatable that is callable.
]]
function makeModuleFunction(func)
local function dropSelfArg(func)
return function(...)
return func(select(2, ...))
end
end
return setmetatable({}, {__call = dropSelfArg(func)})
end
---@type any
local screenManager = nil
local input = nil
function love.load(args)
-- graphics setup
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle(gameConfig.window.title)
-- set up tick rate
tick.rate = 1 / 60
tick.framerate = 60
local _, _, windowFlags = love.window.getMode()
if windowFlags.refreshrate then
love.log.debug(('Matching display refresh rate: %d'):format(windowFlags.refreshrate))
tick.framerate = math.max(tick.framerate, windowFlags.refreshrate)
end
-- set up display handler
DisplayHandler.init({
-- display handler arguments
canvasWidth = gameConfig.window.displayConfig.virtualWidth,
canvasHeight = gameConfig.window.displayConfig.virtualHeight,
-- resolution solution arguments
game_width = gameConfig.window.displayConfig.gameWidth,
game_height = gameConfig.window.displayConfig.gameHeight,
scale_mode = 1
})
-- build content here (need it for font)
ContentControl.buildContent()
love.graphics.setFont(AssetManager.getFont('base_screen_debug'))
--[[
Singleton Inits
]]
-- set up screen manager
screenManager = require('lib.roomy').new()
-- events we will handle ourselves
local excludeEvents = { 'update', 'draw', 'resize', 'load' }
if imgui then
-- we need to pass events to imgui (so handle everything)
excludeEvents = lume.concat(excludeEvents, {'textinput', 'keypressed', 'keyreleased', 'mousemoved', 'mousepressed', 'mousereleased', 'wheelmoved'})
end
screenManager:hook({ exclude = excludeEvents })
Singletons.screenManager = screenManager
-- set up input
input = require('lib.baton').new(gameConfig.controls)
Singletons.input = input
-- set up console
love.keyboard.setKeyRepeat(true)
--console.font = AssetManager.getFont('debugConsole')
love.log.debug(string.format('Launch args: %s', love.inspect(args)))
-- setup startup screen
love.log.trace('Startup Screen: ' .. gameConfig.startupScreen)
screenManager:enter(require(gameConfig.startupScreen)(), unpack(args))
end
function love.update(dt)
love.time.update(dt)
screenManager:emit('update', dt)
end
---@diagnostic disable-next-line: duplicate-set-field
function love.draw()
screenManager:emit('draw')
-- draw any imgui modules to support debugging/cheat menus
if imgui and lume.any(Singletons.imguiModules) then
imgui.NewFrame()
for _, module in ipairs(Singletons.imguiModules) do
module:draw()
end
imgui.Render()
end
end
function love.resize(w, h)
screenManager:emit('resize', w, h)
DisplayHandler.resize(w, h)
end
function love.quit()
if imgui then
imgui.ShutDown()
end
love.log.trace('Game Closed')
end
-- imgui stuff
if imgui then
function love.textinput(t)
imgui.TextInput(t)
if not imgui.GetWantCaptureKeyboard() then
screenManager:emit('textinput', t)
end
end
function love.keypressed(key)
imgui.KeyPressed(key)
if not imgui.GetWantCaptureKeyboard() then
screenManager:emit('keypressed', key)
end
end
function love.keyreleased(key)
imgui.KeyReleased(key)
if not imgui.GetWantCaptureKeyboard() then
screenManager:emit('keyreleased', key)
end
end
function love.mousemoved(x, y)
imgui.MouseMoved(x, y)
if not imgui.GetWantCaptureMouse() then
screenManager:emit('mousemoved', x, y)
end
end
function love.mousepressed(x, y, button)
imgui.MousePressed(button)
if not imgui.GetWantCaptureMouse() then
screenManager:emit('mousepressed', x, y, button)
end
end
function love.mousereleased(x, y, button)
imgui.MouseReleased(button)
if not imgui.GetWantCaptureMouse() then
screenManager:emit('mousereleased', x, y, button)
end
end
function love.wheelmoved(x, y)
imgui.WheelMoved(y)
if not imgui.GetWantCaptureMouse() then
screenManager:emit('wheelmoved', x, y)
end
end
end