-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
231 lines (200 loc) · 7.45 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"fmt"
"github.com/BigJk/crt"
teadapter "github.com/BigJk/crt/bubbletea"
"github.com/BigJk/crt/shader"
"github.com/BigJk/end_of_eden/cmd/internal/testargs"
"github.com/BigJk/end_of_eden/internal/fs"
"github.com/BigJk/end_of_eden/system/audio"
"github.com/BigJk/end_of_eden/system/gen"
"github.com/BigJk/end_of_eden/system/gen/faces"
"github.com/BigJk/end_of_eden/system/localization"
"github.com/BigJk/end_of_eden/system/settings"
"github.com/BigJk/end_of_eden/system/settings/viper"
"github.com/BigJk/end_of_eden/ui/menus/mainmenu"
uiset "github.com/BigJk/end_of_eden/ui/menus/settings"
"github.com/BigJk/end_of_eden/ui/root"
"github.com/BigJk/end_of_eden/ui/style"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/hajimehoshi/ebiten/v2"
zone "github.com/lrstanley/bubblezone"
"image/color"
"math"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"
)
var (
loadStyle = lipgloss.NewStyle().Bold(true).Italic(true).Foreground(style.BaseGray)
)
func initSystems(hasAudio bool) {
// Init settings
fmt.Println(loadStyle.Render("Initializing Settings. Please wait..."))
{
if err := settings.LoadSettings(); err != nil {
panic(err)
}
}
fmt.Println(loadStyle.Render("Done!"))
// Init audio
if hasAudio {
fmt.Println(loadStyle.Render("Initializing Audio. Please wait..."))
audio.InitAudio()
fmt.Println(loadStyle.Render("Done!"))
}
// Init generators
fmt.Println(loadStyle.Render("Initializing Proc-Gen. Please wait..."))
{
// Init face generator
if err := faces.InitGlobal("./assets/gen/faces"); err != nil {
panic(err)
}
// Init other gens
gen.InitGen()
}
fmt.Println(loadStyle.Render("Done!"))
// Init Localization
fmt.Println(loadStyle.Render("Initializing Localization. Please wait..."))
{
if err := localization.Global.AddFolder("./assets/locals"); err != nil {
panic(err)
}
localization.SetCurrent(settings.GetString("language"))
}
fmt.Println(loadStyle.Render("Done!"))
}
func main() {
testArgs := testargs.New()
flag.Parse()
// Start profiling server
if env := os.Getenv("EOE_PROFILE"); env != "" {
go func() {
http.ListenAndServe(":8080", nil)
}()
}
vi := viper.Viper{
SettingsName: "settings_gl",
}
vi.SetDefault("audio", true)
vi.SetDefault("volume", 1)
vi.SetDefault("language", "en")
vi.SetDefault("font_size", 12)
vi.SetDefault("font_normal", "IosevkaTermNerdFontMono-Regular.ttf")
vi.SetDefault("font_italic", "IosevkaTermNerdFontMono-Italic.ttf")
vi.SetDefault("font_bold", "IosevkaTermNerdFontMono-Bold.ttf")
vi.SetDefault("width", 1100)
vi.SetDefault("height", 900)
vi.SetDefault("crt", false)
vi.SetDefault("grain", false)
vi.SetDefault("show_fps", false)
vi.SetDefault("fps", 30)
settings.SetSettings(vi)
fmt.Println(lipgloss.NewStyle().Bold(true).Foreground(style.BaseRed).Render("End Of Eden"))
initSystems(settings.GetBool("audio"))
fonts, err := crt.LoadFaces("./assets/fonts/"+settings.GetString("font_normal"), "./assets/fonts/"+settings.GetString("font_bold"), "./assets/fonts/"+settings.GetString("font_italic"), crt.GetFontDPI(), settings.GetFloat("font_size"))
if err != nil {
panic(err)
}
uiSettings := []uiset.Value{
{Key: "audio", Name: "Audio", Description: "Enable or disable audio", Type: uiset.Bool, Val: settings.GetBool("audio"), Min: nil, Max: nil},
{Key: "volume", Name: "Volume", Description: "Change the volume", Type: uiset.Float, Val: settings.GetFloat("volume"), Min: 0.0, Max: 2.0},
{Key: "language", Name: "Language", Description: fmt.Sprintf("Change the language (supported: %s)", strings.Join(localization.Global.GetLocales(), ", ")), Type: uiset.String, Val: settings.GetString("language")},
{Key: "font_size", Name: "Font Size", Description: "Change the font size", Type: uiset.Float, Val: settings.GetFloat("font_size"), Min: 6.0, Max: 64.0},
{Key: "width", Name: "Width", Description: "Change the window width", Type: uiset.Float, Val: settings.GetFloat("width"), Min: 450.0, Max: 5000.0},
{Key: "height", Name: "Height", Description: "Change the window height", Type: uiset.Float, Val: settings.GetFloat("height"), Min: 450.0, Max: 5000.0},
{Key: "crt", Name: "CRT", Description: "Enable or disable CRT shader. Increases GPU usage.", Type: uiset.Bool, Val: settings.GetBool("crt"), Min: nil, Max: nil},
{Key: "grain", Name: "Grain", Description: "Enable or disable grain shader. Matches well with the CRT shader. Increases GPU usage.", Type: uiset.Bool, Val: settings.GetBool("grain"), Min: nil, Max: nil},
{Key: "show_fps", Name: "Show FPS", Description: "Show the current FPS", Type: uiset.Bool, Val: settings.GetBool("show_fps"), Min: nil, Max: nil},
{Key: "fps", Name: "FPS", Description: "Change the FPS", Type: uiset.Float, Val: settings.GetFloat("fps"), Min: 10.0, Max: 144.0},
{Key: "font_normal", Name: "Normal Font", Description: "Change the normal font", Type: uiset.String, Val: settings.GetString("font_normal"), Min: nil, Max: nil},
{Key: "font_bold", Name: "Bold Font", Description: "Change the bold font", Type: uiset.String, Val: settings.GetString("font_bold"), Min: nil, Max: nil},
{Key: "font_italic", Name: "Italic Font", Description: "Change the italic font", Type: uiset.String, Val: settings.GetString("font_italic"), Min: nil, Max: nil},
}
// Create base model
var baseModel tea.Model
zones := zone.New()
baseModel = root.New(zones, mainmenu.NewModel(zones, settings.GetGlobal(), uiSettings, func(values []uiset.Value) error {
for i := range values {
settings.Set(values[i].Key, values[i].Val)
}
localization.SetCurrent(settings.GetString("language"))
return settings.SaveSettings()
}))
// Apply test args if given
baseModel = testArgs.ApplyArgs(baseModel, zones)
// Create window
win, _, err := teadapter.Window(settings.GetInt("width"), settings.GetInt("height"), fonts, baseModel, color.RGBA{R: 34, G: 36, B: 41, A: 255}, tea.WithAltScreen())
if err != nil {
panic(err)
}
var loadedShader []shader.Shader
// Setup CRT shader
if settings.GetBool("crt") {
crtLotte, err := shader.NewCrtLotte()
if err != nil {
panic(err)
}
crtLotte.Uniforms["WarpX"] = float32(0)
crtLotte.Uniforms["WarpY"] = float32(0)
// TODO: This is a bad hack to get the shader to change its state!
go func() {
warp := 0.0
for {
time.Sleep(time.Millisecond * 50)
warp += 0.005
win.Lock()
{
crtLotte.Uniforms["WarpX"] = float32(math.Abs(math.Sin(warp)*0.01) * 0.5)
crtLotte.Uniforms["WarpY"] = float32(math.Abs(math.Sin(warp)*0.01) * 0.5)
}
win.Unlock()
}
}()
loadedShader = append(loadedShader, crtLotte)
}
// Setup grain shader
if settings.GetBool("grain") {
res, _ := fs.ReadFile("./assets/shader/grain.go")
grain, err := ebiten.NewShader(res)
if err != nil {
panic(err)
}
w, h := win.Layout(ebiten.WindowSize())
s := &shader.BaseShader{
Shader: grain,
Uniforms: map[string]any{
"ScreenSize": []float32{float32(w), float32(h)},
"Tick": float32(0),
"Strength": float32(0.05),
},
}
// TODO: This is a bad hack to get the shader to change its state!
go func() {
cur := float32(0)
for {
time.Sleep(time.Millisecond * 50)
cur += 1
win.Lock()
{
s.Uniforms["Tick"] = cur
}
win.Unlock()
}
}()
loadedShader = append(loadedShader, s)
}
if len(loadedShader) > 0 {
win.SetShader(loadedShader...)
}
// Run game
win.ShowTPS(settings.GetBool("show_fps"))
ebiten.SetTPS(settings.GetInt("fps"))
if err := win.Run("End Of Eden"); err != nil {
panic(err)
}
}