diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-10-31 21:12:22 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2018-10-31 21:12:22 +0100 |
commit | 31c131289c6b37a08ea66e988eaded1cd9c9c72c (patch) | |
tree | 087ec092b7ad242abd7242e239406c3af5b824fa /src/view | |
parent | 69be8d2ef8121661a1b3f5a0ea42d534585f3e73 (diff) | |
download | rpgedit-31c131289c6b37a08ea66e988eaded1cd9c9c72c.tar rpgedit-31c131289c6b37a08ea66e988eaded1cd9c9c72c.zip |
Add loader for simple (single-sprint) entities, use direction input
Diffstat (limited to 'src/view')
-rw-r--r-- | src/view/entity.ts | 15 | ||||
-rw-r--r-- | src/view/renderer/Renderer.ts | 24 | ||||
-rw-r--r-- | src/view/sprite.ts | 2 |
3 files changed, 33 insertions, 8 deletions
diff --git a/src/view/entity.ts b/src/view/entity.ts new file mode 100644 index 0000000..d95d924 --- /dev/null +++ b/src/view/entity.ts @@ -0,0 +1,15 @@ +import Renderer from './renderer/Renderer'; +import { SpriteCoords, SpriteView, SpriteViewBuilder } from './sprite'; +import { loadImage, mkTexture } from './util/image'; + +export async function loadSimpleEntity(r: Renderer, sprite: string): Promise<SpriteView> { + const tile = await loadImage(`resources/sprite/entity/${sprite}.png`); + const texture = mkTexture(r.getContext(), tile); + + const builder = new SpriteViewBuilder(r, texture); + + const coords: SpriteCoords = [0, 0, 1, 1]; + builder.addSprite(coords, coords); + + return builder.build(); +} diff --git a/src/view/renderer/Renderer.ts b/src/view/renderer/Renderer.ts index bf29797..344bccf 100644 --- a/src/view/renderer/Renderer.ts +++ b/src/view/renderer/Renderer.ts @@ -1,10 +1,13 @@ -import {mat4} from 'gl-matrix'; +import { mat4, vec2 } from 'gl-matrix'; import Shaders from './Shaders'; export default class Renderer { private readonly gl: WebGLRenderingContext; private readonly shaders: Shaders; + + private readonly center: vec2 = vec2.create(); + private readonly translation: vec2 = vec2.create(); private readonly viewport: mat4 = mat4.create(); constructor(private readonly canvas: HTMLCanvasElement) { @@ -45,8 +48,19 @@ export default class Renderer { return this.shaders.samplerLoc; } - public setTranslation(v: Float32List) { - this.gl.uniform2fv(this.shaders.translateLoc, v); + public setCenter(v: vec2|number[]) { + vec2.copy(this.center, v); + } + + public setTranslation(v: vec2|number[]) { + vec2.sub(this.translation, v, this.center); + this.gl.uniform2fv(this.shaders.translateLoc, this.translation); + } + + public clear(): void { + this.gl.clear(this.gl.COLOR_BUFFER_BIT); + + this.setTranslation([0, 0]); } private mkContext(): WebGLRenderingContext { @@ -64,12 +78,10 @@ export default class Renderer { const h = this.canvas.height; this.gl.viewport(0, 0, w, h); - this.gl.clear(this.gl.COLOR_BUFFER_BIT); + this.clear(); mat4.identity(this.viewport); mat4.scale(this.viewport, this.viewport, [2 * 64 / w, -2 * 64 / h, 1.0]); this.gl.uniformMatrix4fv(this.shaders.viewportLoc, false, this.viewport); - - this.setTranslation([-5, -5]); } } diff --git a/src/view/sprite.ts b/src/view/sprite.ts index 18c91c7..02fa77a 100644 --- a/src/view/sprite.ts +++ b/src/view/sprite.ts @@ -57,8 +57,6 @@ export class SpriteView { public render(): void { const gl = this.r.getContext(); - gl.clear(gl.COLOR_BUFFER_BIT); - gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, this.texture); gl.uniform1i(this.r.getSamplerLoc(), 0); |