-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
133 lines (111 loc) · 2.71 KB
/
context.go
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
package wengine
import (
"errors"
)
type AssetMap map[string]Asset
type SceneMap map[string]*Scene
type Context struct {
scrWidth, scrHeight int
assets AssetMap
scenes SceneMap
currentScene *Scene
renderer Renderer
rendererName string
rendererSetting RendererSetting
input *Input
assetsToFinalize AssetMap
}
func NewContext() *Context {
renderer, exists := registeredRenderers["opengl"]
if !exists {
return nil
}
return &Context{assets: make(AssetMap), scenes: make(SceneMap), renderer: renderer, rendererName: "opengl", input: newInput()}
}
func (ctx *Context) Input() *Input {
return ctx.input
}
func (ctx *Context) AccessRenderSetting() *RendererSetting {
return &ctx.rendererSetting
}
func (ctx *Context) ScreenSize() (width, height int) {
width = ctx.scrWidth
height = ctx.scrHeight
return
}
func (ctx *Context) Assets() AssetMap {
return ctx.assets
}
func (ctx *Context) RegisterAsset(name string, asset Asset) {
ctx.assets[name] = asset
}
func (ctx *Context) RegisterScene(name string, scene *Scene) {
ctx.scenes[name] = scene
}
func (ctx *Context) CurrentScene() *Scene {
return ctx.currentScene
}
func (ctx *Context) ApplyScene(name string) {
scene, exists := ctx.scenes[name]
if !exists {
return
}
ctx.currentScene = scene
}
func (ctx *Context) SetScreenSize(width, height int) {
ctx.scrWidth = width
ctx.scrHeight = height
}
func (ctx *Context) LoadAssets(assets []string) error {
for _, name := range assets {
asset, exists := ctx.assets[name]
if !exists {
return errors.New("no such asset")
}
if asset.Loaded() {
continue
}
err := asset.load()
if err != nil {
return err
}
println("loaded asset: " + name)
}
ctx.renderer.NotifyInstall(assets)
return nil
}
func (ctx *Context) asyncLoadScene(scene *Scene) (chan error, error) {
// figure out all assets
assetsToLoad := []string{}
for _, obj := range scene.objects {
for _, compo := range obj.components {
switch compo.Type() {
case COMPO_MESH:
meshCompo, ok := compo.(*MeshComponent)
if !ok {
return nil, errors.New("found invalid component")
}
meshAsset := ctx.assets[meshCompo.Mesh]
if meshAsset != nil {
assetsToLoad = append(assetsToLoad, meshCompo.Mesh)
} else {
return nil, errors.New("found invalid component")
}
materialAsset := ctx.assets[meshCompo.Material]
if materialAsset != nil {
assetsToLoad = append(assetsToLoad, meshCompo.Material)
}
shaderAsset := ctx.assets[meshCompo.Shader]
if shaderAsset != nil {
assetsToLoad = append(assetsToLoad, meshCompo.Shader)
}
}
}
}
result := make(chan error, 1)
go func() {
err := ctx.LoadAssets(assetsToLoad)
result <- err
}()
return result, nil
}