Skip to content

Commit 8427907

Browse files
committed
feat: package backgrounds in spiral from map origin
1 parent 4d42718 commit 8427907

File tree

5 files changed

+86
-76
lines changed

5 files changed

+86
-76
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"backgroundColor":1445381,"backgroundResolution":512}
1+
{"backgroundColor":1445381,"backgroundResolution":512,"backgroundSize":100,"backgroundStart":[-1757,-1141],"backgroundCount":[35,20],"backgroundLength":286}

src/data-processed/backgrounds.pak

128 Bytes
Binary file not shown.

src/process/compress_backgrounds.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import sharp from 'sharp'
22
import * as fs from 'node:fs'
33
import { join } from 'node:path'
4-
import { backgroundColor } from '../data-raw/backgrounds/backgrounds.js'
4+
import * as B from '../data-raw/backgrounds/backgrounds.js'
55

6-
const bgr = parseInt(backgroundColor.slice(0, 2), 16)
7-
const bgg = parseInt(backgroundColor.slice(2, 4), 16)
8-
const bgb = parseInt(backgroundColor.slice(4, 6), 16)
6+
const bgr = parseInt(B.backgroundColor.slice(0, 2), 16)
7+
const bgg = parseInt(B.backgroundColor.slice(2, 4), 16)
8+
const bgb = parseInt(B.backgroundColor.slice(4, 6), 16)
99
const bgInt = bgr | (bgg << 8) | (bgb << 16)
1010

1111
const srcPath = join(import.meta.dirname, '../data-raw/backgrounds')
@@ -23,15 +23,6 @@ function updateDone() {
2323
if(done % 10 === 0) console.log('done', done, 'of ~' + filenames.length)
2424
}
2525

26-
function uint32ToString(value) {
27-
return String.fromCharCode(
28-
(value ) & 0xff,
29-
(value >> 8) & 0xff,
30-
(value >> 16) & 0xff,
31-
(value >> 24) & 0xff,
32-
);
33-
}
34-
3526
function findChunk(buffer, name) {
3627
const nameInt = name.charCodeAt(0) | (name.charCodeAt(1) << 8) | (name.charCodeAt(2) << 16) | (name.charCodeAt(3) << 24)
3728
const nameUint = nameInt >>> 0
@@ -152,5 +143,9 @@ for(let i = 0; i < filenames.length; i++) {
152143
const bgInfo = {}
153144
bgInfo.backgroundColor = bgInt
154145
bgInfo.backgroundResolution = 512
146+
bgInfo.backgroundSize = B.backgroundSize
147+
bgInfo.backgroundStart = B.backgroundStart
148+
bgInfo.backgroundCount = B.backgroundCount
149+
bgInfo.backgroundLength = B.backgrounds.length
155150

156151
fs.writeFileSync(dstInfo, JSON.stringify(bgInfo))

src/process/package_backgrounds.js

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
import { promises as fs } from 'node:fs'
22
import { createWriteStream } from 'node:fs'
33
import { join } from 'node:path'
4-
import { backgrounds } from '../data-raw/backgrounds/backgrounds.js'
4+
import * as B from '../data-raw/backgrounds/backgrounds.js'
55

66
const srcDir = join(import.meta.dirname, '../data-processed/backgrounds')
77
const dstFilename = join(import.meta.dirname, '../data-processed/backgrounds.pak')
88

9+
const bs = new Map()
910

10-
const filenames = await fs.readdir(srcDir)
11-
12-
const filesP = []
13-
for(let i = 0; i < filenames.length; i++) {
14-
filesP[i] = fs.readFile(join(srcDir, filenames[i]))
11+
for(let i = 0; i < B.backgrounds.length; i++) {
12+
const [x, y] = B.backgrounds[i]
13+
let col = bs.get(x)
14+
if(col == null) bs.set(x, col = new Map())
15+
col.set(y, fs.readFile(join(srcDir, x + '_' + y + '.png')))
1516
}
1617

17-
const header = []
18+
const len = B.backgrounds.length
19+
20+
const filesOrder = (() => {
21+
const order = []
22+
23+
let stepSize = 1
24+
let x = 18, y = 12
25+
let dx = -1, dy = 0
26+
27+
function add() {
28+
const col = bs.get(x)
29+
if(col == null) return
30+
const val = col.get(y)
31+
if(val != null) order.push({ x, y, fileP: val })
32+
}
33+
34+
function step() {
35+
for(let i = 0; i < stepSize; i++) {
36+
if(order.length >= len) return true
37+
x += dx
38+
y += dy
39+
add()
40+
}
41+
// 90 degrees counter clockwise
42+
const tmp = dx
43+
dx = -dy
44+
dy = tmp
45+
}
46+
47+
add()
48+
// go stepSize, turn, go stepSize, stepSize++
49+
while(true) {
50+
if(step()) break
51+
if(step()) break
52+
stepSize++
53+
}
1854

55+
return order
56+
})()
57+
58+
const header = []
1959
function writeUint(v) {
2060
var it = v
2161
do {
@@ -25,40 +65,14 @@ function writeUint(v) {
2565
it = div;
2666
} while(it != 0)
2767
}
28-
function writeString(v) {
29-
const buffer = Buffer.from(v, 'utf8')
30-
for(var i = 0; i < buffer.length; i++) if(buffer.readInt8(i) < 0) {
31-
throw new Exception(v)
32-
}
33-
34-
if(buffer.length == 0) header.push(1 << 7)
35-
else {
36-
if(buffer.length == 1 && buffer.readUint8(0) == (1 << 7)) throw new Exception()
37-
for(let i = 0; i < buffer.length-1; i++) {
38-
header.push(buffer[i])
39-
}
40-
header.push(buffer[buffer.length - 1] | (1 << 7))
41-
}
42-
}
43-
44-
writeUint(filenames.length)
45-
46-
const files = await Promise.all(filesP)
4768

48-
const nameRegex = /^(.+)_(.+)\.png$/
49-
50-
for(let i = 0; i < filenames.length; i++) {
51-
writeUint(files[i].length)
52-
const groups = filenames[i].match(nameRegex)
53-
const x = groups[1]
54-
const y = groups[2]
55-
let texI = 0
56-
while(true) {
57-
const coord = backgrounds[texI]
58-
if(coord[0] == x && coord[1] == y) break
59-
texI++
60-
}
61-
writeUint(texI)
69+
writeUint(len)
70+
for(let i = 0; i < filesOrder.length; i++) {
71+
const it = filesOrder[i]
72+
const file = await it.fileP
73+
writeUint(file.length)
74+
writeUint(it.x)
75+
writeUint(it.y)
6276
}
6377

6478
const dst = createWriteStream(dstFilename)
@@ -67,8 +81,8 @@ hLen.writeUint32LE(header.length)
6781
dst.write(hLen)
6882
dst.write(Buffer.from(header))
6983

70-
for(let i = 0; i < files.length; i++) {
71-
dst.write(files[i])
84+
for(let i = 0; i < filesOrder.length; i++) {
85+
dst.write(await filesOrder[i].fileP)
7286
}
7387

7488
dst.end(async() => {

src/renderBackground.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import * as bkg from '$/backgrounds.js'
21
import * as bkg2 from '$/backgrounds.json'
32
import { loadShader, checkProg } from './render_util.js'
43
import backgroundsUrl from '$/backgrounds.pak'
54

65
const actualResolution = bkg2.backgroundResolution
7-
const texturesC = bkg.backgrounds.length
6+
const texturesC = bkg2.backgroundLength
87

9-
// THIS LANGUAGE... IMAGINE TOT BEING ABLE TO PRINT A NUMBER WITH DECIMAL POINT
8+
// THIS LANGUAGE... IMAGINE NOT BEING ABLE TO PRINT A NUMBER WITH DECIMAL POINT
109
// NO, toFixed() ALSO ROUNDS THE NUMBER OR ADDS A MILLION ZEROS
1110
// NO, toString() PRINTS INTEGERS WITHOUT DECIMAL POINT
12-
const bgSize = bkg.backgroundSize + '.0'
11+
const bgSize = bkg2.backgroundSize + '.0'
1312

1413
const vsSource = `#version 300 es
1514
precision highp float;
@@ -111,11 +110,13 @@ function convToRGB565(gl, inputC) {
111110
return res
112111
}
113112

114-
function updateBackground(context, index, chunks) {
113+
function updateBackground(context, imageData, chunks) {
115114
const rd = context.backgrounds
116115
if(rd?.loadImages !== true) return
117116

118-
const imgData = rd.images[index]
117+
const imgData = rd.images[imageData.i]
118+
imgData.x = imageData.x
119+
imgData.y = imageData.y
119120

120121
const blob = new Blob(chunks, { type: 'image/png' })
121122
const url = URL.createObjectURL(blob)
@@ -127,7 +128,7 @@ function updateBackground(context, index, chunks) {
127128
// Technically can be the last texture, so this will make
128129
// mimpaps not appear. But only until the user moves the screen
129130
// or something else triggers a rerender, so shouldn't be a big deal
130-
context.backgrounds.changed.push(index)
131+
context.backgrounds.changed.push(imageData.i)
131132
imgData.done = true
132133
URL.revokeObjectURL(url)
133134
console.log('err')
@@ -138,13 +139,13 @@ function updateBackground(context, index, chunks) {
138139
gl.bindTexture(gl.TEXTURE_2D_ARRAY, rd.bgTextures)
139140
gl.texSubImage3D(
140141
gl.TEXTURE_2D_ARRAY, 0,
141-
0, 0, index,
142+
0, 0, imageData.i,
142143
actualResolution, actualResolution, 1,
143144
gl.RGB, gl.UNSIGNED_BYTE,
144145
img
145146
)
146147

147-
rd.changed.push(index)
148+
rd.changed.push(imageData.i)
148149
imgData.ok = true
149150
imgData.done = true
150151

@@ -223,15 +224,18 @@ async function downloadBackgrounds(context) {
223224
const imageDatas = []
224225
for(let i = 0; i < len; i++) {
225226
const size = parseCompressedInt()
226-
const ti = parseCompressedInt()
227-
imageDatas.push({ size, index: ti })
227+
const xi = parseCompressedInt()
228+
const yi = parseCompressedInt()
229+
const x = bkg2.backgroundStart[0] + xi * bkg2.backgroundSize
230+
const y = bkg2.backgroundStart[1] + yi * bkg2.backgroundSize
231+
imageDatas.push({ size, i, x, y })
228232
}
229233

230234
for(let i = 0; i < imageDatas.length; i++) {
231235
const id = imageDatas[i]
232236
var chunks = tryRead(id.size)
233237
if(chunks == null) chunks = await read(id.size)
234-
updateBackground(context, id.index, chunks)
238+
updateBackground(context, id, chunks)
235239
}
236240
}
237241

@@ -358,14 +362,11 @@ export function render(context) {
358362
var coordsCount = 0
359363
var done = true
360364
for(let i = 0; i < texturesC; i++) {
361-
done = done & rd.images[i].done
362-
if(!rd.images[i].ok) continue
363-
const bg = bkg.backgrounds[i]
364-
365-
const x = bkg.backgroundStart[0] + bg[0] * bkg.backgroundSize
366-
const y = bkg.backgroundStart[1] + bg[1] * bkg.backgroundSize
367-
dv.setFloat32(coordsCount * 12 , x, true)
368-
dv.setFloat32(coordsCount * 12 + 4, y, true)
365+
const it = rd.images[i]
366+
done = done & it.done
367+
if(!it.ok) continue
368+
dv.setFloat32(coordsCount * 12 , it.x, true)
369+
dv.setFloat32(coordsCount * 12 + 4, it.y, true)
369370
dv.setUint32 (coordsCount * 12 + 8, i, true)
370371
coordsCount++
371372
}

0 commit comments

Comments
 (0)