-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.lua
More file actions
352 lines (296 loc) · 13 KB
/
Copy pathbootstrap.lua
File metadata and controls
352 lines (296 loc) · 13 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
--[[ ********************************************************************** ]]--
--[[ [CODE NEXUS] ]]--
--[[ ]]--
--[[ ---------------------------------------------------------------------- ]]--
--[[ Copyright (c) 2012-2013 CODE NEXUS Development Team ]]--
--[[ ]]--
--[[ This software is provided 'as-is', without any express or implied ]]--
--[[ warranty. In no event will the authors be held liable for any damages ]]--
--[[ arising from the use of this software. ]]--
--[[ ]]--
--[[ Permission is granted to anyone to use this software for any purpose, ]]--
--[[ including commercial applications, and to alter it and redistribute it ]]--
--[[ freely, subject to the following restrictions: ]]--
--[[ ]]--
--[[ 1. The origin of this software must not be misrepresented; you must not]]--
--[[ claim that you wrote the original software. If you use this software]]--
--[[ in a product, an acknowledgment in the product documentation would ]]--
--[[ be appreciated but is not required. ]]--
--[[ ]]--
--[[ 2. Altered source versions must be plainly marked as such, and must not]]--
--[[ be misrepresented as being the original software. ]]--
--[[ ]]--
--[[ 3. This notice may not be removed or altered from any source ]]--
--[[ distribution. ]]--
--[[ ********************************************************************** ]]--
-- / ---------------------------------------------------------------------- \ --
-- | Import modules | --
-- \ ---------------------------------------------------------------------- / --
local os = os
local type = type
local math = math
local pairs = pairs
local pcall = pcall
local error = error
local table = table
local ipairs = ipairs
local string = string
local unpack = unpack
local tostring = tostring
local coroutine = coroutine
local setmetatable = setmetatable
local getmetatable = getmetatable
local collectgarbage = collectgarbage
local Nexus = nexus
local Core = Nexus.core
local Constants = Nexus.constants
local Configures = Nexus.configures
local KEYS = Constants.KEYS
local VERSION = Constants.VERSION
local DEBUG_MODE = Constants.DEBUG_MODE
local EMPTY_FUNCTION = Constants.EMPTY_FUNCTION
-- / ---------------------------------------------------------------------- \ --
-- | Local variables | --
-- \ ---------------------------------------------------------------------- / --
local m_love_extended = false
local f_coroutine_resume = coroutine.resume
-- / ---------------------------------------------------------------------- \ --
-- | Extend bulit-in modules and functions of lua and LÖVE | --
-- \ ---------------------------------------------------------------------- / --
function math.clamp(low, n, high)
return math.min(math.max(n, low), high)
end
function table.first(t)
return t[1]
end
function table.last(t)
return t[#t]
end
function table.indexOf(t, v)
for key, value in pairs(t) do
if type(v) == 'function' then
if v(value) then return key end
else
if value == v then return key end
end
end
return nil
end
function table.removeValue(t, v)
local index = table.indexOf(t, v)
if index then table.remove(t, index) end
return t
end
function coroutine.resume(...)
local results = { f_coroutine_resume(...) }
if not results[1] then error(tostring(results[2]), 2) end
return unpack(results)
end
function extend_love_functions()
if not m_love_extended then
local l = love
local lf = l.filesystem
local lfload = lf.load
function lf.loadChunk(path)
return lfload(path)
end
function lf.load(path, failed)
local ok
local chunk
local result
if not failed then failed = error end
ok, chunk = pcall(lfload, path)
if not ok then
failed(tostring(chunk))
else
ok, result = pcall(chunk)
if not ok then failed(tostring(result)) end
end
return result
end
m_love_extended = true
end
end
-- / ---------------------------------------------------------------------- \ --
-- | Debug functions | --
-- \ ---------------------------------------------------------------------- / --
if DEBUG_MODE then
local lggm = nil
local lgsm = nil
local sc = nil
local sg = nil
local se = nil
local sl = nil
local update = nil
local render = nil
return function(instance, enable)
-- / ------------------------------------------------------------------ \ --
-- | Import modules | --
-- \ ------------------------------------------------------------------ / --
local l = love
local lt = l.timer
local lg = l.graphics
local Data = Core.import 'nexus.core.data'
local Game = Core.import 'nexus.core.game'
local Graphics = Core.import 'nexus.core.graphics'
local Input = Core.import 'nexus.core.input'
local Scene = Core.import 'nexus.core.scene'
local Color = Core.import 'nexus.base.color'
local Font = Core.import 'nexus.base.font'
local GameConsole = Core.import 'nexus.game.console'
-- / ------------------------------------------------------------------ \ --
-- | Declare object | --
-- \ ------------------------------------------------------------------ / --
local Debug = {
messages = nil,
scenes = {}
}
-- / ------------------------------------------------------------------ \ --
-- | Local variables | --
-- \ ------------------------------------------------------------------ / --
local m_x_offset = 8
local m_y_offset = 8
local m_line_height = 24
local t_title_color = Color.new(255, 255, 0, 255)
local t_default_color = Color.new(255, 255, 255, 255)
-- / ------------------------------------------------------------------ \ --
-- | Member functions | --
-- \ ------------------------------------------------------------------ / --
function Debug.profiler()
end
if not enable then
lg.isSupported = lgis
lg.getMode = lggm
lg.setMode = lgsm
Scene.change = sc
Scene.goto = sg
Scene.enter = se
Scene.leave = sl
Graphics.update = update
Graphics.render = render
return EMPTY_FUNCTION
end
extend_love_functions()
-- / ------------------------------------------------------------------ \ --
-- | Game hooks in debug mode | --
-- \ ------------------------------------------------------------------ / --
lgis = lg.isSupported
lggm = lg.getMode
lgsm = lg.setMode
sc = Scene.change
sg = Scene.goto
se = Scene.enter
sl = Scene.leave
update = Graphics.update
render = Graphics.render
if l._version ~= '0.9.0' then
function lg.setDefaultFilter(...)
return lg.setDefaultImageFilter(...)
end
function lg.newShader(vertexcode, pixelcode)
return lg.newPixelEffect(pixelcode)
end
function lg.setShader(...)
return lg.setPixelEffect(...)
end
function lg.getShader(...)
return lg.getPixelEffect(...)
end
function lg._shaderCodeToGLSL(...)
return lg._effectCodeToGLSL(...)
end
function lg.isSupported(...)
local args = {...}
for index, value in ipairs(args) do
if value == 'shader' then args[index] = 'pixeleffect' end
end
return lgis(unpack(args))
end
function lg.getMode()
local width, height, fullscreen, vsync, fsaa = lggm()
return width, height, {
fullscreen = fullscreen,
vsync = vsync,
fsaa = fsaa,
resizeable = false,
borderless = false,
centered = true
}
end
function lg.setMode(width, height, flags)
lgsm(width, height, flags.fullscreen, flags.vsync, flags.fsaa)
end
end
function Scene.change(scene)
Debug.scenes[#Debug.scenes] = scene
return sc(scene)
end
function Scene.goto(scene)
if #Debug.scenes > 0 then table.remove(Debug.scenes) end
table.insert(Debug.scenes, scene)
return sg(scene)
end
function Scene.enter(scene)
table.insert(Debug.scenes, scene)
return se(scene)
end
function Scene.leave(scene)
if #Debug.scenes == 0 then return end
table.remove(Debug.scenes)
return sl(scene)
end
function Graphics.update(...)
update(...)
Debug.messages = {}
for _, scene in ipairs(Debug.scenes) do
if scene.__debug then
local messages = scene.__debug(scene);
table.insert(Debug.messages, 1, {
table.remove(messages, 1),
#messages,
table.concat(messages, '\n')
})
end
end
end
function Graphics.render(...)
local position = 0
local width, height, flags = lg.getMode()
render(...)
if not GameConsole.isConsoleEnabled() then
local font = Data.getFont('debug')
local size = font.size
font.color = t_default_color
Font.text(font, string.format('FPS: %d', lt.getFPS()), m_x_offset, m_y_offset, width, m_line_height)
Font.text(font, string.format('Lua Memory Usage: %d KB', collectgarbage('count')), m_x_offset, m_y_offset + m_line_height, width, m_line_height)
Font.text(font, string.format('Screen: %d x %d (%s, vsync %s, fsaa %d)', width, height, flags.fullscreen and 'fullscreen' or 'windowed', flags.vsync and 'enabled' or 'disabled', flags.fsaa), m_x_offset, m_y_offset + 2 * m_line_height, width, m_line_height)
Font.text(font, string.format('Date: %s', os.date(Data.getFormat('datetime'))), m_x_offset, m_y_offset + 3 * m_line_height, width, m_line_height)
font.size = 48
font.bold = true
Font.text(font, string.format('NOT FINAL GAME'), 0, 60, width, 2 * m_line_height, Font.ALIGN_CENTER)
font.size = size
font.bold = false
Font.text(font, string.format('CODE NEXUS %s (%s) on %s.', Game.getVersionString(), VERSION.STAGE, l._os), 0, height - 24, width - 8, m_line_height, Font.ALIGN_RIGHT)
for _, message in ipairs(Debug.messages) do
local name, lines, messages = unpack(message)
font.color = t_title_color
Font.text(font, name, 24, 120 + position * m_line_height, width, m_line_height)
font.color = t_default_color
Font.text(font, messages, 32, 120 + (position + 1) * m_line_height, width, m_line_height * lines)
position = position + lines + 1
end
end
end
Input.bindKeyEvent('debug.quickstart', Input.TRIGGER, KEYS.F5, function()
local SceneNewGame = Core.import 'nexus.scene.newgame'
Scene.goto(SceneNewGame.new())
end)
return Debug
end
end
return function(instance, enable)
extend_love_functions()
return setmetatable({}, {
__index = EMPTY_FUNCTION
})
end