-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·133 lines (92 loc) · 2.81 KB
/
main.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 main
import (
"errors"
_ "embed"
"github.com/solarlune/tetra3d"
"github.com/solarlune/tetra3d/colors"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type Game struct {
Library *tetra3d.Library
Scene *tetra3d.Scene
Camera *tetra3d.Camera
DrawDebugDepth bool
DrawDebugStats bool
}
// The goal of this example is to make a simple quickstart project for basing new projects off of.
// In this example, the Tetra3D icon spins in the center of the screen. It is shadeless, so you would
// either need to add a light to the scene, use other shadeless materials, or disable lighting on the
// scene to be able to see other new objects.
//go:embed startingScene.gltf
var startingGLTF []byte
func NewGame() *Game {
game := &Game{}
game.Init()
return game
}
func (g *Game) Init() {
if g.Library == nil {
library, err := tetra3d.LoadGLTFData(startingGLTF, nil)
if err != nil {
panic(err)
}
g.Library = library
}
g.Scene = g.Library.ExportedScene.Clone()
g.Camera = g.Scene.Root.Get("Camera").(*tetra3d.Camera)
}
func (g *Game) Update() error {
var err error
tetra := g.Scene.Root.Get("Tetra")
tetra.Rotate(0, 1, 0, tetra3d.ToRadians(1))
// Quit
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
err = errors.New("quit")
}
// Fullscreen
if inpututil.IsKeyJustPressed(ebiten.KeyF4) {
ebiten.SetFullscreen(!ebiten.IsFullscreen())
}
// Restart
if ebiten.IsKeyPressed(ebiten.KeyR) {
// Restarting is done here by simply recloning the scene; a better, more memory-efficient way to do this would be to simply
// re-place the player in his original location, for example.
g.Init()
}
// Debug stuff
if inpututil.IsKeyJustPressed(ebiten.KeyF1) {
g.DrawDebugStats = !g.DrawDebugStats
}
if inpututil.IsKeyJustPressed(ebiten.KeyF5) {
g.DrawDebugDepth = !g.DrawDebugDepth
}
return err
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(g.Scene.World.ClearColor.ToRGBA64())
g.Camera.Clear()
g.Camera.RenderScene(g.Scene)
if g.DrawDebugDepth {
screen.DrawImage(g.Camera.DepthTexture(), nil)
} else {
screen.DrawImage(g.Camera.ColorTexture(), nil)
}
if g.DrawDebugStats {
g.Camera.DrawDebugRenderInfo(screen, 1, colors.White())
}
}
func (g *Game) Layout(w, h int) (int, int) {
// This is a fixed aspect ratio; we can change this to, say, extend for wider displays by using the provided w argument and
// calculating the height from the aspect ratio, then calling Camera.Resize() on any / all cameras with the new width and height.
return g.Camera.Size()
}
func main() {
ebiten.SetWindowTitle("Tetra3d - Quickstart Project")
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
game := NewGame()
// An ungraceful quit
if err := ebiten.RunGame(game); err != nil && err.Error() != "quit" {
panic(err)
}
}