-
Notifications
You must be signed in to change notification settings - Fork 9
/
rendering.go
80 lines (68 loc) · 1.72 KB
/
rendering.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
package systems
import (
"github.com/andygeiss/ecs/core"
"github.com/andygeiss/ecs/example/components"
rl "github.com/gen2brain/raylib-go/raylib"
)
// Rendering ...
type Rendering struct {
err error
title string
width, height int32
plugins []core.Plugin
}
func (a *Rendering) Error() error {
return a.err
}
func (a *Rendering) Setup() {
rl.InitWindow(a.width, a.height, a.title)
}
func (a *Rendering) Process(em core.EntityManager) (state int) {
// First check if app should stop.
if rl.WindowShouldClose() {
return core.StateEngineStop
}
// Clear the screen
if rl.IsWindowReady() {
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
a.renderEntities(em)
rl.DrawFPS(10, 10)
rl.EndDrawing()
}
// Dispatch work to plugins.
for _, plugin := range a.plugins {
plugin(em)
}
return core.StateEngineContinue
}
func (a *Rendering) Teardown() {
rl.CloseWindow()
}
func (a *Rendering) WithHeight(height int) *Rendering {
a.height = int32(height)
return a
}
func (a *Rendering) WithPlugins(plugins ...core.Plugin) *Rendering {
a.plugins = plugins
return a
}
func (a *Rendering) WithTitle(title string) *Rendering {
a.title = title
return a
}
func (a *Rendering) WithWidth(width int) *Rendering {
a.width = int32(width)
return a
}
func (a *Rendering) renderEntities(em core.EntityManager) {
for _, e := range em.FilterByMask(components.MaskPosition | components.MaskSize) {
position := e.Get(components.MaskPosition).(*components.Position)
size := e.Get(components.MaskSize).(*components.Size)
rl.DrawRectangleRec(rl.Rectangle{X: position.X, Y: position.Y, Width: size.Width, Height: size.Height}, rl.Blue)
}
}
// NewRendering ...
func NewRendering() core.System {
return &Rendering{}
}