3dexperiments/src/engine/index.js

92 lines
1.7 KiB
JavaScript

/* global performance */
import Screen from './screen'
import Input from './input'
import { ShaderManager } from './shader'
class Engine {
constructor () {
this.screen = new Screen()
this.input = new Input(this.screen.gl.canvas)
this.shaders = new ShaderManager()
this.running = false
// Queues
this.rst = []
this.ust = []
this.frameTime = 0
this.frameCount = 0
this.fps = 0
window.gl = this.screen.gl
}
get gl () {
return this.screen.gl
}
render (gl) {
// Set clear color to black, fully opaque
gl.clearColor(0.0, 0.7, 1.0, 1.0)
// Clear the color buffer with specified clear color
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Enable depth testing
gl.enable(gl.DEPTH_TEST)
// Enable back-face culling
gl.enable(gl.CULL_FACE)
gl.cullFace(gl.BACK)
// Render functions
for (let i in this.rst) {
this.rst[i](gl)
}
}
update (dt) {
this.input.update()
// Updates
for (let i in this.ust) {
this.ust[i](dt)
}
}
step () {
this.running && window.requestAnimationFrame(() => this.step())
this.render(this.gl)
let ts = performance.now()
let timeDiff = ts - this.frameTime // time difference in milliseconds
this.frameCount++
if (timeDiff > 0) {
this.fps = Math.floor(this.frameCount / timeDiff * 1000)
this.frameCount = 0
this.frameTime = ts
}
this.update(timeDiff / 1000)
}
addRenderFunction (fn) {
this.rst.push(fn)
}
addUpdateFunction (fn) {
this.ust.push(fn)
}
startGameLoop () {
if (this.running) throw new Error('Game Loop is already running!')
this.running = true
this.step()
}
}
export default Engine