|
|
@ -47,6 +47,15 @@ export async function setup(params, wasmModule) {
|
|
|
|
gameState.width = Math.floor(parseInt(canvas.width) / pixelSize)
|
|
|
|
gameState.width = Math.floor(parseInt(canvas.width) / pixelSize)
|
|
|
|
gameState.height = Math.floor(parseInt(canvas.height) / pixelSize)
|
|
|
|
gameState.height = Math.floor(parseInt(canvas.height) / pixelSize)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const texture = gl.createTexture()
|
|
|
|
|
|
|
|
gl.activeTexture(gl.TEXTURE0)
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, texture)
|
|
|
|
|
|
|
|
gl.pixelStorei( gl.UNPACK_ALIGNMENT, 1 )
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
|
|
|
|
|
|
|
|
|
|
initialize()
|
|
|
|
initialize()
|
|
|
|
onThemeChange()
|
|
|
|
onThemeChange()
|
|
|
|
drawBoard()
|
|
|
|
drawBoard()
|
|
|
@ -84,12 +93,17 @@ export async function onThemeChange() {
|
|
|
|
const {gl, shader} = gameState
|
|
|
|
const {gl, shader} = gameState
|
|
|
|
|
|
|
|
|
|
|
|
const drawColorString = getComputedStyle(gameState.canvas).getPropertyValue('color')
|
|
|
|
const drawColorString = getComputedStyle(gameState.canvas).getPropertyValue('color')
|
|
|
|
const [_match, ...colorValues] = /rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)/.exec(drawColorString)
|
|
|
|
const drawColor = parseColorString(drawColorString)
|
|
|
|
const drawColor = colorValues.map(n => parseFloat(n) / 255)
|
|
|
|
|
|
|
|
|
|
|
|
const backgroundColorString = getComputedStyle(gameState.canvas).getPropertyValue('--background')
|
|
|
|
|
|
|
|
const backgroundColor = parseColorString(backgroundColorString)
|
|
|
|
|
|
|
|
|
|
|
|
const drawColorLocation = gl.getUniformLocation(shader, "drawColor")
|
|
|
|
const drawColorLocation = gl.getUniformLocation(shader, "drawColor")
|
|
|
|
gl.uniform3fv(drawColorLocation, drawColor)
|
|
|
|
gl.uniform3fv(drawColorLocation, drawColor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const backgroundColorLocation = gl.getUniformLocation(shader, "backgroundColor")
|
|
|
|
|
|
|
|
gl.uniform3fv(backgroundColorLocation, backgroundColor)
|
|
|
|
|
|
|
|
|
|
|
|
drawBoard()
|
|
|
|
drawBoard()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -159,20 +173,22 @@ function drawBoard() {
|
|
|
|
const boardLength = gameExports.getBoardLength()
|
|
|
|
const boardLength = gameExports.getBoardLength()
|
|
|
|
const boardData = (new Uint8Array(wasmBuffer)).slice(boardPointer, boardPointer + boardLength)
|
|
|
|
const boardData = (new Uint8Array(wasmBuffer)).slice(boardPointer, boardPointer + boardLength)
|
|
|
|
|
|
|
|
|
|
|
|
const texture = gl.createTexture()
|
|
|
|
|
|
|
|
gl.activeTexture(gl.TEXTURE0)
|
|
|
|
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, texture)
|
|
|
|
|
|
|
|
gl.pixelStorei( gl.UNPACK_ALIGNMENT, 1 )
|
|
|
|
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, width, height, 0, gl.ALPHA, gl.UNSIGNED_BYTE, boardData)
|
|
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, width, height, 0, gl.ALPHA, gl.UNSIGNED_BYTE, boardData)
|
|
|
|
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, 6)
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
|
|
|
}
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
|
|
|
|
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseColorString(string) {
|
|
|
|
|
|
|
|
const rgbMatch = /rgb\(([0-9]+), ?([0-9]+), ?([0-9]+)\)/i.exec(string)
|
|
|
|
|
|
|
|
if (rgbMatch)
|
|
|
|
|
|
|
|
return rgbMatch.slice(1).map(n => parseInt(n, 10) / 255)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hexMatch = /^\#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(string)
|
|
|
|
|
|
|
|
if (hexMatch)
|
|
|
|
|
|
|
|
return hexMatch.slice(1).map(n => parseInt(n, 16) / 255)
|
|
|
|
|
|
|
|
|
|
|
|
gl.clearColor(1, 1, 1, 0)
|
|
|
|
const shortHexMatch = /^\#([0-9a-f]){3}$/i.exec(string)
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
|
|
if (shortHexMatch)
|
|
|
|
gl.drawArrays(gl.TRIANGLES, 0, 6)
|
|
|
|
return shortHexMatch.slice(1).map(n => parseInt(n, 16) / 16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new Error(`Cannot parse color string "${string}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|