-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (117 loc) · 3.29 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
134
135
136
137
138
139
package main
import (
"fmt"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/zeozeozeo/ebitengine-imgui-go/renderer"
"github.com/zeozeozeo/imgui"
)
func main() {
// create a new imgui context, you can provide your font atlas if you want,
// but we'll just pass nil for the default one
mgr := renderer.New(nil)
mgr.Ctx.IO.IniFilename = "" // disable imgui.ini
ebiten.SetWindowSize(1280, 720)
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
ebiten.SetTPS(ebiten.SyncWithFPS)
gg := &Game{
mgr: mgr,
deviceScale: ebiten.DeviceScaleFactor(),
demoWindowOpen: true,
metricsWindowOpen: true,
}
ebiten.RunGame(gg)
}
type Game struct {
mgr *renderer.Manager
deviceScale float64
retina bool
w, h int
demoWindowOpen bool
metricsWindowOpen bool
consoleOutput []string
}
func (game *Game) Draw(screen *ebiten.Image) {
ebitenutil.DebugPrintAt(
screen,
fmt.Sprintf(
"tps: %.2f\nfps: %.2f\n(c)lipmask: %t",
ebiten.ActualTPS(),
ebiten.ActualFPS(),
game.mgr.ClipMask,
),
10,
2,
)
game.mgr.Draw(screen)
}
func (game *Game) Update() error {
// update imgui state
game.mgr.Update(1.0 / float32(ebiten.ActualTPS()))
// disable or enable the clipmask when C is pressed
// usually you'd always have this enabled
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
game.mgr.ClipMask = !game.mgr.ClipMask
}
game.mgr.BeginFrame() // start a new imgui frame
// test window
imgui.Begin("Test window", nil, 0) // 0 = default window flags
{
imgui.Text("Hello, world!")
if imgui.Button("button") {
fmt.Println("button pressed")
game.consoleOutput = append(game.consoleOutput, "button pressed")
}
// draw a second button on the same line
imgui.SameLine(0, 4)
if imgui.Button("button 2") {
fmt.Println("button 2 pressed")
game.consoleOutput = append(game.consoleOutput, "button 2 pressed")
}
}
imgui.End()
// options window
imgui.SetNextWindowPos(imgui.NewImVec2(300, 300), imgui.ImGuiCond_FirstUseEver, *imgui.NewImVec2(0, 0))
imgui.Begin("Options", nil, 0)
{
imgui.Checkbox("Retina", &game.retina)
imgui.Checkbox("Demo window", &game.demoWindowOpen)
imgui.Checkbox("Metrics window", &game.metricsWindowOpen)
}
imgui.End()
// console window
imgui.SetNextWindowPos(imgui.NewImVec2(50, 200), imgui.ImGuiCond_FirstUseEver, *imgui.NewImVec2(0, 0))
imgui.SetNextWindowSize(imgui.NewImVec2(200, 300), imgui.ImGuiCond_FirstUseEver)
imgui.Begin("Console", nil, 0)
{
imgui.Text("console output:")
for idx, s := range game.consoleOutput {
imgui.PushID(int32(idx))
imgui.Text("%s", s)
imgui.PopID()
}
}
imgui.End()
// demo window (WARNING: this crashes a lot :p)
if game.demoWindowOpen {
imgui.ShowDemoWindow(&game.demoWindowOpen)
}
if game.metricsWindowOpen {
imgui.ShowMetricsWindow(&game.metricsWindowOpen)
}
game.mgr.EndFrame()
return nil
}
func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
if game.retina {
m := ebiten.DeviceScaleFactor()
game.w = int(float64(outsideWidth) * m)
game.h = int(float64(outsideHeight) * m)
} else {
game.w = outsideWidth
game.h = outsideHeight
}
game.mgr.SetDisplaySize(float32(game.w), float32(game.h))
return game.w, game.h
}