summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2018-10-24 00:26:47 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2018-10-24 00:26:47 +0200
commitad0f2567240636a5ea6798c53c557d34620e662b (patch)
treeb62a13a6533756054644a55ba717577b51d195cc /src
parent23bee802a5bba0e9a7bd2775fb31bf0af29ba1e7 (diff)
downloadrpgedit-ad0f2567240636a5ea6798c53c557d34620e662b.tar
rpgedit-ad0f2567240636a5ea6798c53c557d34620e662b.zip
view/MapLoader: refactor texture creation into a method
Diffstat (limited to 'src')
-rw-r--r--src/view/MapLoader.ts28
1 files changed, 16 insertions, 12 deletions
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<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];
}