-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
144 lines (117 loc) · 4.08 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fetching = require('./fetching')
const loopBody = (game, objects, f) => (context, time) => {
const a = f(game)
if (typeof a === 'function') {
a(context, time)
}
objects.forEach(obj => {
const b = f(obj)
if (typeof b === 'function') {
b(context, time)
}
})
}
const loop = (game, context, objects, time) => {
loopBody(game, objects, o => o.update)(context, time)
loopBody(game, objects, o => o.draw)(context, time)
const previous = time.totalGameTime
window.requestAnimationFrame(timestamp => {
const current = timestamp / 1000
const delta = current - previous
loop(game, context, objects, {
totalGameTime: current,
delta: delta,
fps: 1 / delta
})
})
}
const wgl = (canvasId, game) => {
const canvas = document.getElementById(canvasId)
const context = getContext(canvas)
if (!context) {
return null
}
const initialize = (context, content) => {
if (typeof game.initialize === 'function') {
game.initialize(context, content)
}
const objects = game.objects || []
return objects.map(obj => {
if (typeof obj.initialize === 'function') {
obj.initialize(context, content)
}
return {
update: (context, time) => typeof obj.update == 'function' ? obj.update(context, time) : () => { },
draw: (context, time) => typeof obj.draw == 'function' ? obj.draw(context, time) : () => { }
}
})
}
const loadShaders = (context, shaders) => Object.keys(shaders)
.map(key => Promise.all([fetching.fetchResource(context, {
src: shaders[key].vs,
type: context.VERTEX_SHADER
}), fetching.fetchResource(context, {
src: shaders[key].fs,
type: context.FRAGMENT_SHADER
})])
.then(values => {
const result = { }
result[key] = values.map(v => createShader(context, v.type, v.content))
return result
}))
const ls = Promise.all(loadShaders(context, game.config.shaders))
.then(result => result.reduce((o, c) => {
const key = Object.keys(c)[0]
o[key] = createProgram(context, c[key])
return o
}, { }))
const lr = Promise.all(Object.keys(game.config.resources)
.map(key => fetching.fetchResource(context, game.config.resources[key])
.then(r => {
const result = { }
result[key] = r
return result
})))
.then(result => result.reduce((o, c) => {
const key = Object.keys(c)[0]
o[key] = c[key]
return o
}, { }))
return Promise.all([ls, lr])
.then(result => initialize(context, {
programs: result[0],
resources: result[1]
}))
.then(objects => loop(game, context, objects, {
totalGameTime: 0,
delta: 0,
fps: 0
}))
}
const getContext = canvas => canvas.getContext('webgl') ||
canvas.getContext('experimental-webgl')
const createShader = (context, type, source) => {
let shader = context.createShader(type)
context.shaderSource(shader, source)
context.compileShader(shader)
if (context.getShaderParameter(shader, context.COMPILE_STATUS)) {
return shader
}
const e = context.getShaderInfoLog(shader)
context.deleteShader(shader)
throw new Error(`SHADER COMPILING ERROR: "${e}"\n${source}`)
}
const createProgram = (context, shaders) => {
let program = context.createProgram()
shaders.forEach(shader => {
context.attachShader(program, shader)
})
context.linkProgram(program)
if (context.getProgramParameter(program, context.LINK_STATUS)) {
return program
}
const e = context.getProgramInfoLog(program)
context.deleteProgram(program)
throw new Error(`SHADER LINKING ERROR: "${e}"`)
}
module.exports = wgl