view/MapLoader: refactor texture creation into a method
This commit is contained in:
parent
23bee802a5
commit
ad0f256724
1 changed files with 16 additions and 12 deletions
|
@ -22,6 +22,21 @@ function loadTiles(tiles: Map<string, string>): Promise<Map<string, HTMLImageEle
|
|||
return loadImages(mapValues(t => `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<string, HTMLImageElement>): [WebGLTexture, Map<string, number>] {
|
||||
let canvas = document.createElement('canvas');
|
||||
canvas.width = nextPowerOf2(tiles.size) * MapView.tileSize;
|
||||
|
@ -36,18 +51,7 @@ function mkTileTexture(gl: WebGLRenderingContext, tiles: Map<string, HTMLImageEl
|
|||
ret.set(k, i++);
|
||||
}
|
||||
|
||||
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, canvas);
|
||||
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, ret];
|
||||
return [mkTexture(gl, canvas), ret];
|
||||
}
|
||||
|
||||
|
||||
|
|
Reference in a new issue