Throw in tslint and fix style

This commit is contained in:
Matthias Schiffer 2018-10-24 01:26:15 +02:00
parent 06b2c5bec7
commit 9770eaf432
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
9 changed files with 303 additions and 170 deletions

View file

@ -1,63 +1,62 @@
import {mapValues, mapValuesAsync, nextPowerOf2} from '../util';
import Renderer from './Renderer';
import MapView from './MapView';
import MapData from '../model/MapData';
import MapView from './MapView';
import Renderer from './Renderer';
function loadImage(url: string): Promise<HTMLImageElement> {
return new Promise(function(resolve, reject) {
let img = new Image();
img.addEventListener('load', () => { resolve(img); });
img.addEventListener('error', () => { reject(Error('failed to load ' + url)); });
img.src = url;
});
return new Promise((resolve, reject) => {
const img = new Image();
img.addEventListener('load', () => { resolve(img); });
img.addEventListener('error', () => { reject(Error('failed to load ' + url)); });
img.src = url;
});
}
function loadImages(urls: Map<string, string>): Promise<Map<string, HTMLImageElement>> {
return mapValuesAsync(loadImage, urls);
return mapValuesAsync(loadImage, urls);
}
function loadTiles(tiles: Map<string, string>): Promise<Map<string, HTMLImageElement>> {
return loadImages(mapValues(t => `resources/sprite/tile/${t}.png`, tiles));
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');
const 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);
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;
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;
canvas.height = MapView.tileSize;
function mkTileTexture(gl: WebGLRenderingContext, tiles: Map<string, HTMLImageElement>):
[WebGLTexture, Map<string, number>] {
const canvas = document.createElement('canvas');
canvas.width = nextPowerOf2(tiles.size) * MapView.tileSize;
canvas.height = MapView.tileSize;
let i = 0;
let ret: Map<string, number> = new Map();
let ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
let i = 0;
const ret: Map<string, number> = new Map();
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
for (let [k, tile] of tiles) {
ctx.drawImage(tile, i * MapView.tileSize, 0);
ret.set(k, i++);
}
for (const [k, tile] of tiles) {
ctx.drawImage(tile, i * MapView.tileSize, 0);
ret.set(k, i++);
}
return [mkTexture(gl, canvas), ret];
return [mkTexture(gl, canvas), ret];
}
export async function loadMap(r: Renderer, mapData: MapData): Promise<MapView> {
let tiles = await loadTiles(mapData.tiles);
let [tileTexture, tileMap] = mkTileTexture(r.gl, tiles);
const tiles = await loadTiles(mapData.tiles);
const [tileTexture, tileMap] = mkTileTexture(r.gl, tiles);
return new MapView(r, mapData, tileTexture, tileMap);
return new MapView(r, mapData, tileTexture, tileMap);
}

View file

@ -1,49 +1,30 @@
import {nextPowerOf2} from '../util';
import Renderer from './Renderer';
import MapData from '../model/MapData';
import Renderer from './Renderer';
export default class MapView {
public static readonly tileSize: number = 32;
class MapView {
private redrawPending: boolean = false;
private redrawPending: boolean = false;
private vertexBuffer: WebGLBuffer;
private textureBuffer: WebGLBuffer;
constructor(
private r: Renderer,
private map: MapData,
private tileTexture: WebGLTexture,
private tileMap: Map<string, number>,
) {
const vertexData: number[] = [];
const textureData: number[] = [];
private addTile(vertexData: number[], textureData: number[], x: number, y: number, tile: string, tileCount: number) {
let tileID = this.tileMap.get(tile);
if (tileID === undefined)
throw new Error('invalid tile specifier in map data');
const tileCount = nextPowerOf2(tileMap.size);
vertexData.push(x); vertexData.push(y);
vertexData.push(x+1); vertexData.push(y);
vertexData.push(x); vertexData.push(y+1);
vertexData.push(x); vertexData.push(y+1);
vertexData.push(x+1); vertexData.push(y);
vertexData.push(x+1); vertexData.push(y+1);
textureData.push((tileID) / tileCount); textureData.push(0);
textureData.push((tileID+1) / tileCount); textureData.push(0);
textureData.push((tileID) / tileCount); textureData.push(1);
textureData.push((tileID) / tileCount); textureData.push(1);
textureData.push((tileID+1) / tileCount); textureData.push(0);
textureData.push((tileID+1) / tileCount); textureData.push(1);
}
constructor(private r: Renderer, private map: MapData, private tileTexture: WebGLTexture, private tileMap: Map<string, number>) {
let vertexData: number[] = [];
let textureData: number[] = [];
let tileCount = nextPowerOf2(tileMap.size);
for (let x = 0; x < map.width; x++) {
for (let y = 0; y < map.height; y++) {
this.addTile(vertexData, textureData, x, y, map.layers[0][y][x], tileCount);
}
}
for (let x = 0; x < map.width; x++)
for (let y = 0; y < map.height; y++)
this.addTile(vertexData, textureData, x, y, map.layers[0][y][x], tileCount);
this.vertexBuffer = r.createBuffer();
r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.vertexBuffer);
@ -52,9 +33,9 @@ class MapView {
this.textureBuffer = r.createBuffer();
r.gl.bindBuffer(r.gl.ARRAY_BUFFER, this.textureBuffer);
r.gl.bufferData(r.gl.ARRAY_BUFFER, new Float32Array(textureData), r.gl.STATIC_DRAW);
}
}
draw(): void {
public draw(): void {
this.r.gl.clear(this.r.gl.COLOR_BUFFER_BIT);
this.r.gl.activeTexture(this.r.gl.TEXTURE0);
@ -68,12 +49,27 @@ class MapView {
this.r.gl.vertexAttribPointer(this.r.textureCoordLoc, 2, this.r.gl.FLOAT, false, 0, 0);
this.r.gl.drawArrays(this.r.gl.TRIANGLES, 0, 6 * this.map.width * this.map.height);
}
}
private addTile(vertexData: number[], textureData: number[], x: number, y: number, tile: string, tileCount: number) {
const tileID = this.tileMap.get(tile);
if (tileID === undefined)
throw new Error('invalid tile specifier in map data');
vertexData.push(x); vertexData.push(y);
vertexData.push(x + 1); vertexData.push(y);
vertexData.push(x); vertexData.push(y + 1);
vertexData.push(x); vertexData.push(y + 1);
vertexData.push(x + 1); vertexData.push(y);
vertexData.push(x + 1); vertexData.push(y + 1);
textureData.push((tileID) / tileCount); textureData.push(0);
textureData.push((tileID + 1) / tileCount); textureData.push(0);
textureData.push((tileID) / tileCount); textureData.push(1);
textureData.push((tileID) / tileCount); textureData.push(1);
textureData.push((tileID + 1) / tileCount); textureData.push(0);
textureData.push((tileID + 1) / tileCount); textureData.push(1);
}
}
module MapView {
export const tileSize = 32;
}
export default MapView;

View file

@ -1,85 +1,62 @@
import {mat4} from 'gl-matrix';
import vertexShaderSrc from './default.vs';
import fragmentShaderSrc from './default.fs';
import vertexShaderSrc from './default.vs';
export default class Renderer {
public gl: WebGLRenderingContext;
public vertexPosLoc!: number;
public textureCoordLoc!: number;
private viewportLoc!: WebGLUniformLocation;
private translateLoc!: WebGLUniformLocation;
public samplerLoc!: WebGLUniformLocation;
private viewport: mat4 = mat4.create();
private mkContext(): WebGLRenderingContext {
let gl = (this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')) as WebGLRenderingContext|null;
if (!gl)
throw new Error('unable to initialize WebGL context');
private viewportLoc!: WebGLUniformLocation;
private translateLoc!: WebGLUniformLocation;
return gl;
constructor(private canvas: HTMLCanvasElement) {
this.gl = this.mkContext();
this.initShaders();
this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
this.setSize();
}
getAttribLocation(program: WebGLProgram, name: string): number {
let ret = this.gl.getAttribLocation(program, name);
if (ret < 0)
throw new Error("unable to get location of attribute '" + name + "'");
return ret;
}
getUniformLocation(program: WebGLProgram, name: string): WebGLUniformLocation {
let ret = this.gl.getUniformLocation(program, name);
if (!ret)
throw new Error("unable to get location of uniform '" + name + "'");
return ret;
}
createBuffer(): WebGLBuffer {
let ret = this.gl.createBuffer();
public createBuffer(): WebGLBuffer {
const ret = this.gl.createBuffer();
if (!ret)
throw new Error('unable to create buffer');
return ret;
}
compileShader(type: number, src: string): WebGLShader {
let shader = this.gl.createShader(type);
if (!shader)
throw new Error('Unable to create shader');
private mkContext(): WebGLRenderingContext {
const gl = (
this.canvas.getContext('webgl') || this.canvas.getContext('experimental-webgl')
) as WebGLRenderingContext|null;
if (!gl)
throw new Error('unable to initialize WebGL context');
this.gl.shaderSource(shader, src);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
let err = this.gl.getShaderInfoLog(shader);
this.gl.deleteShader(shader);
throw new Error('Unable to compile shader: ' + err);
}
return shader;
return gl;
}
private initShaders(): void {
let shaderProgram = this.gl.createProgram();
const shaderProgram = this.gl.createProgram();
if (!shaderProgram)
throw new Error('Unable to create shader program');
let vertexShader = this.compileShader(this.gl.VERTEX_SHADER, vertexShaderSrc);
let fragmentShader = this.compileShader(this.gl.FRAGMENT_SHADER, fragmentShaderSrc);
const vertexShader = this.compileShader(this.gl.VERTEX_SHADER, vertexShaderSrc);
const fragmentShader = this.compileShader(this.gl.FRAGMENT_SHADER, fragmentShaderSrc);
this.gl.attachShader(shaderProgram, vertexShader);
this.gl.attachShader(shaderProgram, fragmentShader);
this.gl.linkProgram(shaderProgram);
if (!this.gl.getProgramParameter(shaderProgram, this.gl.LINK_STATUS)) {
let err = this.gl.getProgramInfoLog(shaderProgram);
const err = this.gl.getProgramInfoLog(shaderProgram);
this.gl.deleteShader(vertexShader);
this.gl.deleteShader(fragmentShader);
@ -102,8 +79,8 @@ export default class Renderer {
}
private setSize(): void {
let w = this.canvas.width;
let h = this.canvas.height;
const w = this.canvas.width;
const h = this.canvas.height;
this.gl.viewport(0, 0, w, h);
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
@ -115,13 +92,36 @@ export default class Renderer {
this.gl.uniform2f(this.translateLoc, -5.0, -5.0);
}
constructor(private canvas: HTMLCanvasElement) {
this.gl = this.mkContext();
private getAttribLocation(program: WebGLProgram, name: string): number {
const ret = this.gl.getAttribLocation(program, name);
if (ret < 0)
throw new Error("unable to get location of attribute '" + name + "'");
this.initShaders();
return ret;
}
this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
private getUniformLocation(program: WebGLProgram, name: string): WebGLUniformLocation {
const ret = this.gl.getUniformLocation(program, name);
if (!ret)
throw new Error("unable to get location of uniform '" + name + "'");
this.setSize();
return ret;
}
private compileShader(type: number, src: string): WebGLShader {
const shader = this.gl.createShader(type);
if (!shader)
throw new Error('Unable to create shader');
this.gl.shaderSource(shader, src);
this.gl.compileShader(shader);
if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {
const err = this.gl.getShaderInfoLog(shader);
this.gl.deleteShader(shader);
throw new Error('Unable to compile shader: ' + err);
}
return shader;
}
}