-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
305 lines (279 loc) · 8.63 KB
/
main.lua
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
--PROF_CAPTURE = true
--prof = require("jprof")
baton = require("lib.baton.baton")
require("game.engine_images")
require("game.engine_LL")
require("game.engine_gfx_LL")
require("game.ll_build")
require("game.matrices")
require("game.global_structures")
require("game.utility")
log = require("lib.log.log")
log.usecolor = false
log.level = "fatal"
function love.load()
initScreen()
initScale()
initPaletteShader()
initDraw()
initTimer()
initCache()
initInput()
init_vector_pool()
init_vector_pair_pool()
init_tile_quad_pool()
init_quad_pool()
init_main()
ll_global = create_ll_system()
init_splash()
engine_init()
ll_main_entry()
--prof.connect()
end
function love.draw()
startDrawing()
local success, result = coroutine.resume(main_crt)
if not success then
error(debug.traceback(main_crt, result))
end
doneDrawing()
end
function love.resize(w, h)
scaleOptions[scaleOption]()
end
function love.keypressed(key, scancode, isrepeat)
if not key:find("shift") then
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then
table.insert(keybuffer, key:upper())
else
table.insert(keybuffer, key)
end
end
if love.keyboard.isDown("f12") then
scaleOption = scaleOption + 1
if scaleOption > #scaleOptions then
scaleOption = 1
end
scaleOptions[scaleOption]()
end
if (love.keyboard.isDown("ralt") and key == "return") or
love.keyboard.isDown("f11") then
if not fullscreen then
love.window.setFullscreen(true, "desktop")
fullscreen = true
else
love.window.setFullscreen(false, "desktop")
fullscreen = false
end
scaleOptions[scaleOption]()
end
end
function love.quit()
--prof.write("prof.mpack")
end
function init_main()
main_crt = coroutine.create(main)
end
function main()
repeat
--prof.enabled(true)
--prof.push("frame")
for u = 1, loops do
reset_vector_pool()
reset_vector_pair_pool()
reset_tile_quad_pool()
reset_quad_pool()
input:update()
timerUpdate()
--timer = timer + .005
--prof.push("enemy_main")
enemy_main()
--prof.pop("enemy_main")
--prof.push("hero_main")
hero_main()
--prof.pop("hero_main")
--prof.push("play_sequence")
play_sequence(ll_global)
--prof.pop("play_sequence")
--prof.push("blit_scene")
draw = u == loops and love.graphics.draw or noop
blit_scene()
--prof.pop("blit_scene")
end
--prof.pop("frame")
--prof.enabled(false)
if not stillPlaying() then break end
coroutine.yield()
until false
LL_RollCredits()
love.event.quit()
end
--Initializes the window, sets up some defaults and
--initializes the main drawing canvas, which is a 320x200
--canvas to simulate SCREEN 13.
function initScreen()
love.window.setTitle("Lynn's Legacy")
love.window.setMode(640, 400, {resizable = true, minwidth = 320, minheight = 200})
love.window.setFullscreen(true, "desktop")
fullscreen = true
love.window.setVSync(1)
love.mouse.setVisible(false)
love.graphics.setDefaultFilter("nearest", "nearest", 1)
canvas = love.graphics.newCanvas(320, 200)
savedCanvas = love.graphics.newCanvas(320, 200)
clearmode = true
--used by screenQuake
tripPosition = 0
tripOffset = create_vector()
end
function initScale()
scaleOptions = {}
table.insert(scaleOptions,
function()
retrieveDimensions()
scale = math.min(screenWidth / canvasWidth, screenHeight / canvasHeight)
end)
for s = 1, 6 do
table.insert(scaleOptions, function() scale = s end)
end
scaleOption = 1
scaleOptions[scaleOption]()
end
function initTimer()
timer = love.timer.getTime()
if love.window.getVSync() ~= 0 then
loops = 16
timerUpdate =
function()
width, height, flags = love.window.getMode()
if refreshrate ~= flags.refreshrate then
refreshrate = flags.refreshrate
timerInc = 1 / (loops * flags.refreshrate)
end
timer = timer + timerInc
end
else
loops = 4
timerUpdate = function() timer = love.timer.getTime() end
end
end
--Initializes our global palette, paletteCanvas and
--palette shader. This shader simulates a 256 color graphics mode.
--Palette effects can be achieved simply by writing new colors to the
--palette canvas. The palette canvas is a 256x1 texture where each
--pixel is a color. Then, all graphics that are loaded from spritesheets
--are converted such that the red component is an index into this palette
--texture.
function initPaletteShader()
masterPalette, palette = loadPalette("data/palette/ll.pal")
paletteCanvas = love.graphics.newCanvas(256, 1)
shader = love.graphics.newShader("shader/palette_shader.fs")
shader:send("paletteTexture", paletteCanvas)
end
function initCache()
imageHeaderCache = {}
objectXmlCache = {}
end
function initDraw()
--Allows us to temporarily make all drawing no-op if needed
draw = love.graphics.draw
end
function initInput()
keybuffer = {}
input = baton.new {
controls = {
left = {'key:left', 'key:a', 'axis:leftx-', 'button:dpleft'},
right = {'key:right', 'key:d', 'axis:leftx+', 'button:dpright'},
up = {'key:up', 'key:w', 'axis:lefty-', 'button:dpup'},
down = {'key:down', 'key:s', 'axis:lefty+', 'button:dpdown'},
attack = {'key:x', 'button:a'},
item = {'key:z', 'button:x'},
action = {'key:space', 'button:b'},
map = {'key:m', 'button:y'},
pause = {'key:escape', 'button:start'},
cycleleft = {'key:[', 'button:leftshoulder'},
cycleright = {'key:]', 'button:rightshoulder'}
},
pairs = {
move = {'left', 'right', 'up', 'down'}
},
joystick = love.joystick.getJoysticks()[1],
}
end
--Should be called before drawing anything to the main canvas.
function startDrawing()
love.graphics.setCanvas(canvas)
if clearmode then love.graphics.clear() end
end
--Should be called after drawing everything to the main canvas.
function doneDrawing()
love.graphics.setCanvas(paletteCanvas)
for x = 0, 255 do
love.graphics.setColor(palette[x][0], palette[x][1], palette[x][2])
love.graphics.points(x + .5, .5)
end
love.graphics.setColor(1, 1, 1)
love.graphics.setCanvas()
love.graphics.setShader(shader)
love.graphics.push()
--Move to the appropiate top left corner.
retrieveDimensions()
love.graphics.translate(
math.floor((screenWidth - canvasWidth * scale) / 2),
math.floor((screenHeight - canvasHeight * scale) / 2))
love.graphics.scale(scale, scale)
love.graphics.draw(canvas, tripOffset.x, tripOffset.y)
love.graphics.pop()
love.graphics.setShader()
love.graphics.setCanvas(savedCanvas)
love.graphics.clear()
love.graphics.draw(canvas)
love.graphics.setCanvas()
end
function retrieveDimensions()
screenWidth, screenHeight = love.graphics.getDimensions()
canvasWidth, canvasHeight = canvas:getDimensions()
end
--Returns a loaded imageHeader from the global imageHeaderCache
--cache, or, if there is no image header for the specified file
--name, loads the image header.
function getImageHeader(fileName)
local imageHeader = imageHeaderCache[fileName]
if not imageHeader then
imageHeader = LLSystem_ImageLoad(fileName)
imageHeaderCache[fileName] = imageHeader
end
return imageHeader
end
--Returns a loaded xml object from the global objects xml
--cache, or, if there is no xml obect for the specified file
--name, loads it.
function getObjectXml(fileName)
local objectXml = objectXmlCache[fileName]
if not objectXml then
objectXml = love.filesystem.read(fileName)
objectXmlCache[fileName] = objectXml
end
return objectXml
end
--Loads a palette file and returns a table with all of the rgb triplets
--of the file as tables. Each r,g,b component is transformed into the
--proper float 0 to 1 range that Love2D expects. If there is a problem
--loading the file, nil is returned. The palette file stores rgb triplets
--as b,g,r (backwards), and this is taken care of by this function.
function loadPalette(fileName)
local paletteBlob = loadBlob(fileName)
if paletteBlob then
local palette, masterPalette = {}, {}
for x = 0, 255 do
local b,g,r = readByte(paletteBlob) / 255,
readByte(paletteBlob) / 255,
readByte(paletteBlob) / 255
masterPalette[x] = {[0] = r, g, b}
palette[x] = {[0] = r, g, b}
end
return masterPalette, palette
else
return nil
end
end