diff options
Diffstat (limited to 'src/view/util')
-rw-r--r-- | src/view/util/image.ts | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/view/util/image.ts b/src/view/util/image.ts index 73edbba..e01246c 100644 --- a/src/view/util/image.ts +++ b/src/view/util/image.ts @@ -1,3 +1,7 @@ +import { nextPowerOf2 } from '../../util'; +import { Renderer } from '../renderer/renderer'; +import { SpriteCoords } from '../sprite'; + export function loadImage(url: string): Promise<HTMLImageElement> { return new Promise((resolve, reject) => { const img = new Image(); @@ -7,17 +11,38 @@ export function loadImage(url: string): Promise<HTMLImageElement> { }); } -export function mkTexture(gl: WebGLRenderingContext, src: HTMLCanvasElement|HTMLImageElement): WebGLTexture { +export function mkTexture( + r: Renderer, + src: HTMLCanvasElement|HTMLImageElement, +): [WebGLTexture, [number, number], SpriteCoords] { + const gl = r.getContext(); const texture = gl.createTexture(); if (!texture) throw new Error('unable to create texture'); + const w = src.width, h = src.height; + const w2 = nextPowerOf2(w), h2 = nextPowerOf2(h); + + const canvas = document.createElement('canvas'); + canvas.width = w2; + canvas.height = h2; + + const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; + ctx.drawImage(src, 0, 0); + gl.bindTexture(gl.TEXTURE_2D, texture); - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, src); + 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; + const size: [number, number] = [ + w / r.coordScale, h / r.coordScale, + ]; + const coords: SpriteCoords = [ + 0, 0, w2 / r.coordScale, h2 / r.coordScale, + ]; + + return [texture, size, coords]; } |