-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
133 lines (106 loc) · 3.1 KB
/
game.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 walkie
import (
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/mathgl/mgl32"
"github.com/jakecoffman/walkie/eng"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type Game struct {
state int
Keys [1024]bool
fullscreen bool
window *eng.OpenGlWindow
gui *Gui
projection mgl32.Mat4
*eng.ResourceManager
ParticleGenerator *eng.ParticleGenerator
SpriteRenderer *eng.SpriteRenderer
TextRenderer *eng.TextRenderer
}
const (
worldWidth = 1920
worldHeight = 1080
)
// Game state
const (
stateActive = iota
statePause
)
func (g *Game) New(openGlWindow *eng.OpenGlWindow) {
g.window = openGlWindow
g.gui = NewGui(g)
g.Keys = [1024]bool{}
g.ResourceManager = eng.NewResourceManager()
g.LoadShader("assets/shaders/main.vs.glsl", "assets/shaders/main.fs.glsl", "sprite")
g.LoadShader("assets/shaders/particle.vs.glsl", "assets/shaders/particle.fs.glsl", "particle")
g.LoadShader("assets/shaders/text.vs.glsl", "assets/shaders/text.fs.glsl", "text")
//center := Vector{worldWidth / 2, worldHeight / 2}
g.projection = mgl32.Ortho(0, worldWidth, worldHeight, 0, -1, 1)
g.Shader("sprite").Use().SetInt("sprite", 0).SetMat4("projection", g.projection)
g.Shader("particle").Use().SetInt("sprite", 0).SetMat4("projection", g.projection)
g.SpriteRenderer = eng.NewSpriteRenderer(g.Shader("sprite"))
g.TextRenderer = eng.NewTextRenderer(g.Shader("text"), float32(openGlWindow.Width), float32(openGlWindow.Height), "assets/fonts/Roboto-Light.ttf", 24)
g.TextRenderer.SetColor(1, 1, 1, 1)
// Load all textures by name
_ = filepath.Walk("assets/textures", func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if info.IsDir() {
return nil
}
log.Println("Loading", info.Name())
g.LoadTexture(fmt.Sprintf("assets/textures/%v", info.Name()), strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())))
return nil
})
g.reset()
}
func (g *Game) Update(dt float64) {
if g.state == statePause {
return
}
}
func (g *Game) Render(alpha float64) {
if g.window.UpdateViewport {
g.window.UpdateViewport = false
g.window.ViewportWidth, g.window.ViewPortHeight = g.window.GetFramebufferSize()
gl.Viewport(0, 0, int32(g.window.ViewportWidth), int32(g.window.ViewPortHeight))
g.TextRenderer.Use().SetMat4("projection", mgl32.Ortho2D(0, float32(g.window.Width), float32(g.window.Height), 0))
log.Printf("update viewport %#v\n", g.window)
}
g.SpriteRenderer.DrawSprite(
g.Texture("background"),
mgl32.Vec2{worldWidth / 2, worldHeight / 2},
mgl32.Vec2{worldWidth, worldHeight}, 0, eng.White,
)
g.gui.Render()
}
func (g *Game) Close() {
g.gui.Destroy()
g.Clear()
}
func (g *Game) pause() {
g.state = statePause
}
func (g *Game) unpause() {
g.state = stateActive
}
func (g *Game) reset() {
}
func (g *Game) MouseToSpace(x, y, ww, wh float32) eng.Vector {
model := mgl32.Translate3D(0, 0, 0)
obj, err := mgl32.UnProject(mgl32.Vec3{x, wh - y, 0}, model, g.projection, 0, 0, int(ww), int(wh))
if err != nil {
panic(err)
}
return eng.Vec(obj.X(), obj.Y())
}