From ad0f2567240636a5ea6798c53c557d34620e662b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 24 Oct 2018 00:26:47 +0200 Subject: view/MapLoader: refactor texture creation into a method --- src/view/MapLoader.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/view/MapLoader.ts b/src/view/MapLoader.ts index eca8b75..f39700e 100644 --- a/src/view/MapLoader.ts +++ b/src/view/MapLoader.ts @@ -22,6 +22,21 @@ function loadTiles(tiles: Map): Promise `resources/sprite/tile/${t}.png`, tiles)); } +function mkTexture(gl: WebGLRenderingContext, src: HTMLCanvasElement|HTMLImageElement): WebGLTexture { + let texture = gl.createTexture(); + if (!texture) + throw new Error('unable to create texture'); + + gl.bindTexture(gl.TEXTURE_2D, texture); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, src); + 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); + + return texture; +} + function mkTileTexture(gl: WebGLRenderingContext, tiles: Map): [WebGLTexture, Map] { let canvas = document.createElement('canvas'); canvas.width = nextPowerOf2(tiles.size) * MapView.tileSize; @@ -36,18 +51,7 @@ function mkTileTexture(gl: WebGLRenderingContext, tiles: Map