-
Notifications
You must be signed in to change notification settings - Fork 0
/
rivneco.nelua
463 lines (426 loc) · 15 KB
/
rivneco.nelua
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
-- Minimal framework for WASM4
require 'riv'
require 'math'
require 'memory'
## function neco_setup(opts)
local opt_scalar_concept = #[concept(function(x) return x.type.is_niltype or x.type.is_scalar end)]#
local scalar_concept = #[concept(function(x) return x.type.is_scalar end)]#
-- initialize
##[==[
local function read_indexed_ppm_image(filename)
local file, open_err = io.open(filename,'rb')
if not file then return nil, string.format('file %s: %s', filename, open_err) end
local filedata,read_err = file:read('*a')
file:close()
if not filedata then return nil, read_err end
if not filedata then return string.format('file %s: %s', filename, read_err) end
local ppm_patt = require'nelua.thirdparty.lpegrex'.compile([[
file <- {| 'P6' skip
{:width: %d+ :} skip
{:height: %d+ :} skip
{:maxval: %d+ :} %s
{:data: .* :} |}
skip <- (%s / '#' (!%nl .)* %nl)+
]])
local ppm = ppm_patt:match(filedata)
if not ppm then return nil, string.format('file %s: unsupported PPM file format', filename) end
ppm.width, ppm.height, ppm.maxval = tonumber(ppm.width), tonumber(ppm.height), tonumber(ppm.maxval)
assert(ppm.maxval == 255)
local pos = 1
local data = ppm.data
local size = ppm.height*ppm.width
local usedcolors = {[0] = true}
local pixels = {}
local colors = {}
for i=1,size do
local color
color, pos = string.unpack('>I3', data, pos)
pixels[i] = color
if not usedcolors[color] then
colors[#colors+1] = color
usedcolors[color] = true
end
end
table.sort(colors)
local index2color = {[0]=0}
local color2index = {[0]=0}
for i,color in ipairs(colors) do
index2color[i] = color
color2index[color] = i
end
local indexdata = {}
for i=1,size do
indexdata[i] = string.char(color2index[pixels[i]])
end
return table.concat(indexdata), ppm.width, ppm.height, index2color
end
local function read_tiled_map(filename)
local tiledmap = dofile(opts.map)
local tileddata = tiledmap.layers[1].data
local mapdata = {}
for i=1,#tileddata do
local id = tileddata[i]
if id ~= 0 then id = id - 1 end
mapdata[#mapdata+1] = string.pack('I1', id)
end
return table.concat(mapdata), tiledmap.width, tiledmap.height
end
local function read_font(fontconf)
fontconf.image_data, fontconf.image_width, fontconf.image_height = assert(read_indexed_ppm_image(fontconf.image))
return fontconf
end
-- default configs
neco_tilesize = opts.tilesize or 8
-- gfx and palette
neco_gfxdata, neco_gfxwidth, neco_gfxheight, neco_palette = assert(read_indexed_ppm_image(opts.gfx))
neco_palette_size = #neco_palette+1
-- map
if opts.map then
neco_mapdata, neco_mapwidth, neco_mapheight = read_tiled_map(opts.map)
else
neco_mapdata, neco_mapwidth, neco_mapheight = '', 0, 0
end
-- font
neco_font = read_font(opts.font)
local function conv_data_to_8bit(data)
local res = {}
for i=1,#data do
local v = data:sub(i,i):byte()
table.insert(res, v)
end
return res
end
]==]
global neco = @record{}
global neco.riv: *riv_context = riv
global neco.tilesize <comptime> = #[neco_tilesize]#
global neco.gfxwidth <comptime> = #[neco_gfxwidth]#
global neco.gfxheight <comptime> = #[neco_gfxheight]#
global neco.mapwidth <comptime> = #[neco_mapwidth]#
global neco.mapheight <comptime> = #[neco_mapheight]#
global neco.altpalette: [#[neco_palette_size]#]byte
global neco.camera: record{x: integer, y: integer}
global neco.gfxpixels: []byte = #[conv_data_to_8bit(neco_gfxdata)]#
global neco.map: []byte = #[conv_data_to_8bit(neco_mapdata)]#
global neco.fontpixels: []byte = #[conv_data_to_8bit(neco_font.image_data)]#
global SCREEN_WIDTH <comptime> = #[opts.width]#
global SCREEN_HEIGHT <comptime> = #[opts.height]#
global SCREEN_TARGET_FPS <comptime> = #[opts.target_fps]#
global BTN_LEFT: integer <comptime> = 0
global BTN_RIGHT: integer <comptime> = 1
global BTN_UP: integer <comptime> = 2
global BTN_DOWN: integer <comptime> = 3
global BTN_O: integer <comptime> = 4
global BTN_X: integer <comptime> = 5
global BTN_MOUSE_LEFT: integer <comptime> = 6
global BTN_MOUSE_RIGHT: integer <comptime> = 7
global BTN_MOUSE_MIDDLE: integer <comptime> = 8
global C4: float32 <comptime> = 261.63
global Cs4: float32 <comptime> = 277.18
global D4: float32 <comptime> = 293.66
global Ds4: float32 <comptime> = 311.13
global E4: float32 <comptime> = 329.63
global F4: float32 <comptime> = 349.23
global Fs4: float32 <comptime> = 369.99
global G4: float32 <comptime> = 392.00
global Gs4: float32 <comptime> = 415.30
global A4: float32 <comptime> = 440.00
global As4: float32 <comptime> = 466.16
global B4: float32 <comptime> = 493.88
global C5: float32 <comptime> = C4*2
global Cs5: float32 <comptime> = Cs4*2
global D5: float32 <comptime> = D4*2
global Ds5: float32 <comptime> = Ds4*2
global E5: float32 <comptime> = E4*2
global F5: float32 <comptime> = F4*2
global Fs5: float32 <comptime> = Fs4*2
global G5: float32 <comptime> = G4*2
global Gs5: float32 <comptime> = Gs4*2
global A5: float32 <comptime> = A4*2
global As5: float32 <comptime> = As4*2
global B5: float32 <comptime> = B4*2
-- Clear screen
global function cls(col: opt_scalar_concept)
memory.set(&neco.riv.framebuffer[0], col, SCREEN_WIDTH * SCREEN_HEIGHT)
end
global function quit()
riv.quit = true
end
-- Change current drawing color palette
global function pal(col: opt_scalar_concept, newcol: opt_scalar_concept)
## if not col.type.is_niltype then
local col: byte = math.ifloor(col) % #neco.altpalette
## end
## if not newcol.type.is_niltype then
local newcol: byte = math.ifloor(newcol) % #neco.altpalette
## end
## if col.type.is_niltype then
for i=0,<#neco.altpalette do
neco.altpalette[i] = i
end
## else
neco.altpalette[col] = newcol
## end
end
-- Set a pixel in the screen
local function _pset(x: int32, y: int32, col: uint8) <inline>
local i = (@uint32)(y*SCREEN_WIDTH + x)
neco.riv.framebuffer[i] = col
end
--[[
Draw sprite n (0..255) at position x, y.
Color 0 drawn as transparent by default.
flip_x=true to flip horizontally
flip_y=true to flip vertically
]]
global function spr(n: integer,
x: scalar_concept, y: scalar_concept,
w: scalar_concept, h: scalar_concept,
flip_x: boolean,
flip_y: boolean)
if unlikely(n < 0 or n >= neco.gfxwidth*neco.gfxheight) then return end
local x, y = (@integer)(x), (@integer)(y)
local w = (@integer)(w)*neco.tilesize
local h = (@integer)(h)*neco.tilesize
local hcount: uinteger <comptime> = (neco.gfxwidth // neco.tilesize)
local isx = ((@uinteger)(n) % hcount) * neco.tilesize
local isy = ((@uinteger)(n) // hcount) * neco.tilesize
local idx = x + neco.camera.x
local idy = y + neco.camera.y
-- horizontal flip
local sxf = 1
if flip_x then
sxf = -1
isx = isx + w - 1
end
-- vertical flip
local syf = 1
if flip_y then
syf = -1
isy = isy + h - 1
end
-- clip out of bounds width
if idx < 0 then w = w+idx; isx = isx-idx*sxf; idx = 0 end
local idxa = idx+w - SCREEN_WIDTH
if idxa > 0 then w = w - idxa end
-- clip out of bounds height
if idy < 0 then h = h+idy; isy = isy-idy*syf; idy = 0 end
local idya = idy+h - SCREEN_HEIGHT
if idya > 0 then h = h - idya end
-- -- clipped away?
if h < 0 or w < 0 then return end
-- blit
for iy=0,<h do
local sy = isy + iy*syf
local dy = idy + iy
for ix=0,<w do
local sx = isx + ix*sxf
local dx = idx + ix
local i = sy * neco.gfxwidth + sx
local c = neco.gfxpixels[i]
if c ~= 0 then
_pset(dx, dy, neco.altpalette[c])
end
end
end
end
global WaveType = @enum(byte) {
SINE = RIV_WAVEFORM_SINE,
SQUARE = RIV_WAVEFORM_SQUARE,
TRIANGLE = RIV_WAVEFORM_TRIANGLE,
SAWTOOTH = RIV_WAVEFORM_SAWTOOTH,
PULSE = RIV_WAVEFORM_PULSE,
NOISE = RIV_WAVEFORM_NOISE,
}
global ToneDesc = @riv_waveform_desc
-- Play a tone
global function tonex(tone: ToneDesc): uint64
tone.attack = tone.attack / 60
tone.decay = tone.decay / 60
tone.sustain = tone.sustain / 60
tone.release = tone.release / 60
return riv_waveform(&tone)
end
global function stoptone(tone_id: uint64)
return riv_sound(&riv_sound_desc{id=tone_id, seek=-1})
end
-- Draws a pixel
global function pset(x: scalar_concept, y: scalar_concept, col: byte)
local x, y = neco.camera.x+(@integer)(x), neco.camera.y+(@integer)(y)
if unlikely(x < 0 or y < 0 or x >= SCREEN_WIDTH or y >= SCREEN_HEIGHT) then return end
col = neco.altpalette[col % #neco.altpalette]
_pset(x, y, col)
end
-- Change camera drawing offset
global function camera(x: scalar_concept, y: scalar_concept)
neco.camera.x = -(@integer)(x)
neco.camera.y = -(@integer)(y)
end
-- Draws a rectangle
global function rectfill(x0: scalar_concept, y0: scalar_concept, x1: scalar_concept, y1: scalar_concept, col: byte)
local x0, y0 = math.clamp((@integer)(neco.camera.x+x0), 0, SCREEN_WIDTH-1), math.clamp((@integer)(neco.camera.y+y0), 0, SCREEN_HEIGHT-1)
local x1, y1 = math.clamp((@integer)(neco.camera.x+x1), 0, SCREEN_WIDTH-1), math.clamp((@integer)(neco.camera.y+y1), 0, SCREEN_HEIGHT-1)
local pitch = x1+1-x0
col = neco.altpalette[col % #neco.altpalette]
if pitch == SCREEN_WIDTH then
local h = y1+1-y0
memory.set(&neco.riv.framebuffer[y0*SCREEN_WIDTH + x0], col, SCREEN_WIDTH*h)
else
for y=y0,y1 do
for x=x0,x1 do
_pset(x, y, col)
end
end
end
end
-- Draws a circle
global function circfill(x: number, y: number, r: number, col: byte)
if unlikely(r < 0) then return end
if r < 1 then
pset(x, y, col)
return
end
col = neco.altpalette[col % #neco.altpalette]
x, y = neco.camera.x+x, neco.camera.y+y
local sy, sx = -r, -r
local ey, ex = r, r
if y < r then sy = -y end
if x < r then sx = -x end
if y+r >= SCREEN_HEIGHT then ey = SCREEN_HEIGHT - y end
if x+r >= SCREEN_WIDTH then ex = SCREEN_WIDTH - x end
sy, sx, ey, ex = sy+0.5, sx+0.5, ey-0.5, ex-0.5
local r2 = r*r
for iy=sy,ey do
local iy2 = iy*iy
for ix=sx,ex do
local ix2 = ix*ix
if ix2+iy2 <= r2 then
local dx, dy = x + ix, y +iy
_pset((@integer)(dx), (@integer)(dy), col)
end
end
end
end
-- Draws a font glyph (character)
global function glyph(n: byte, x: scalar_concept, y: scalar_concept, col: byte, size: integer)
local x, y = (@integer)(x), (@integer)(y)
if unlikely(n < 32 or n >= 128) then return end
n = n - 32
local hcount <comptime> = #[neco_font.image_width // neco_font.glyph_xadvance]#
local isx, isy = (n % hcount) * #[neco_font.glyph_xadvance]#, (n // hcount) * #[neco_font.glyph_yadvance]#
local idx, idy = x + neco.camera.x, y + neco.camera.y
local w = #[neco_font.glyph_width]#
local h = #[neco_font.glyph_yadvance]#
local dcol = neco.altpalette[col % #neco.altpalette]
if size == 1 then
-- clip out of bounds width
if idx < 0 then w, isx, idx = w+idx, isx-idx, 0 end
local idxa = idx+w - SCREEN_WIDTH
if idxa > 0 then w = w - idxa end
-- clip out of bounds height
if idy < 0 then h, isy, idy = h+idy, isy-idy, 0 end
local idya = idy+h - SCREEN_HEIGHT
if idya > 0 then h = h - idya end
-- -- clipped away?
if h < 0 or w < 0 then return end
-- blit
for iy=0,<h do
local sy = isy + iy
local dy = idy + iy
for ix=0,<w do
local sx = isx + ix
local dx = idx + ix
if neco.fontpixels[sy*#[neco_font.image_width]# + sx] ~= 0 then
_pset(dx, dy, dcol)
end
end
end
else
for iy=0,<h do
local sy = isy + iy
local dy = idy + iy*size
for ix=0,<w do
local sx = isx + ix
local dx = idx + ix*size
if neco.fontpixels[sy*#[neco_font.image_width]# + sx] ~= 0 then
for py=dy,<dy+size do
if unlikely(py < 0 or py >= SCREEN_HEIGHT) then continue end
for px=dx,<dx+size do
if unlikely(px < 0 or px >= SCREEN_WIDTH) then continue end
_pset(px, py, dcol)
end
end
end
end
end
end
end
-- Draw a text
global function ptext(msg: string, x: scalar_concept, y: scalar_concept, col: byte, size: integer)
for i=0,<msg.size do
local n = msg.data[i]
glyph(n, x, y, col, size)
x = x + #[neco_font.glyph_width + neco_font.glyph_xspacing]#*size
end
end
-- Get map tile id at (x,y)
global function mget(x: scalar_concept, y: scalar_concept): uint8
local x, y = (@integer)(x), (@integer)(y)
if unlikely(x < 0 or y < 0 or x >= neco.mapwidth or y >= neco.mapheight) then return 0 end
return neco.map[y*neco.mapwidth + x]
end
-- Checks if a button is pressed.
global function btn(key: integer): boolean
switch key do
case 0 then return neco.riv.keys[RIV_GAMEPAD_LEFT].down
case 1 then return neco.riv.keys[RIV_GAMEPAD_RIGHT].down
case 2 then return neco.riv.keys[RIV_GAMEPAD_UP].down
case 3 then return neco.riv.keys[RIV_GAMEPAD_DOWN].down
case 4 then return neco.riv.keys[RIV_GAMEPAD_A1].down or neco.riv.keys[RIV_GAMEPAD_A4].down
case 5 then return neco.riv.keys[RIV_GAMEPAD_A2].down or neco.riv.keys[RIV_GAMEPAD_A3].down
end
return false
end
-- Checks if a button was just pressed.
global function btnp(key: integer): boolean
switch key do
case 0 then return neco.riv.keys[RIV_GAMEPAD_LEFT].press
case 1 then return neco.riv.keys[RIV_GAMEPAD_RIGHT].press
case 2 then return neco.riv.keys[RIV_GAMEPAD_UP].press
case 3 then return neco.riv.keys[RIV_GAMEPAD_DOWN].press
case 4 then return neco.riv.keys[RIV_GAMEPAD_A1].press or neco.riv.keys[RIV_GAMEPAD_A4].press
case 5 then return neco.riv.keys[RIV_GAMEPAD_A2].press or neco.riv.keys[RIV_GAMEPAD_A3].press
end
return false
end
## end
## function neco_run()
local function init()
## for i=0,#neco_palette do
local c = #[neco_palette[i]]#
c = (((c >> 0) & 0xff) << 16) |
(((c >> 8) & 0xff) << 8) |
(((c >> 16) & 0xff) << 0)
neco.riv.palette[#[i]#] = c
neco.altpalette[#[i]#] = #[i]#
## end
neco.riv.width = SCREEN_WIDTH
neco.riv.height = SCREEN_HEIGHT
neco.riv.target_fps = SCREEN_TARGET_FPS
neco.riv.pixel_format = RIV_PIXELFORMAT_PLT256
## if symbols._init then
_init()
## end
end
local function frame()
## if symbols._update then
_update()
## end
## if symbols._draw then
_draw()
## end
end
init()
repeat frame() until not riv_present()
-- ## nostartfiles()
## end