This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
530 lines (453 loc) · 11.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright (c) 2022 Elias Daler
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// crten - Cathode-Ray Tube ENgine
// Display/render pixel art with a CRT effect.
package main
import (
_ "embed"
"encoding/json"
"fmt"
"image/color"
"image/png"
"log"
"os"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/f64"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
)
var ScreenX = 256
var ScreenY = 240
const fontSize = 32
const version = "0.1" // TODO: don't hardcode
type ShaderParam struct {
Name string
Val float32
Min float32
Max float32
Step float32
}
//go:embed shaders/crt-lottes.go
var shaderSrc []byte
//go:embed assets/cat_and_wizard.png
var imgFile []byte
//go:embed m5x7.ttf
var fontFile []byte
type galleryImage struct {
Desc string
Image *ebiten.Image
}
type config struct {
InputPath string
OutputPath string
ConfigPath string
NoClose bool
}
type Game struct {
config config
shader *ebiten.Shader
ShaderParams []ShaderParam
DefaultValues []float32
img *ebiten.Image
windowSize f64.Vec2
defaultScale int
font font.Face
currMenuIndex int
showHelp bool
firstCursorMove bool
cursorAccum int
images []galleryImage
currImageIndex int
closeWindowOnTextFrame bool
}
type InputAction int
const (
InputActionCursorDown InputAction = iota
InputActionCursorUp
InputActionValueDown
InputActionValueUp
InputActionPrevImage
InputActionNextImage
InputActionResetParams
)
func (g *Game) loadFont() error {
tt, err := opentype.Parse(fontFile)
if err != nil {
return err
}
const dpi = 72
g.font, err = opentype.NewFace(tt, &opentype.FaceOptions{
Size: fontSize,
DPI: dpi,
Hinting: font.HintingFull,
})
if err != nil {
return err
}
return nil
}
func (g *Game) loadInputConfig() error {
cs, err := os.ReadFile(g.config.ConfigPath)
if err != nil {
log.Fatal(err)
}
var c struct {
Shader string `json:"shader"` // currently unused
Params map[string]float32 `json:"params"`
Scale int `json:"scale"`
}
if err := json.Unmarshal(cs, &c); err != nil {
log.Fatal(err)
}
for pn, v := range c.Params {
found := false
// not the most efficient way to find a param by name, but whatever
for i := 0; i < len(g.ShaderParams); i++ {
p := &g.ShaderParams[i]
if p.Name != pn {
continue
}
p.Val = v
found = true
}
if !found {
return fmt.Errorf("[error] unknown parameter name %q", pn)
}
}
if c.Scale != 0 {
g.defaultScale = c.Scale
}
return nil
}
func (g *Game) onInput(a InputAction) {
switch a {
case InputActionCursorDown:
g.currMenuIndex++
if g.currMenuIndex >= len(g.ShaderParams) {
g.currMenuIndex = 0
}
case InputActionCursorUp:
g.currMenuIndex--
if g.currMenuIndex < 0 {
g.currMenuIndex = len(g.ShaderParams) - 1
}
case InputActionValueDown:
p := &g.ShaderParams[g.currMenuIndex]
p.Val -= p.Step
if p.Val < p.Min {
p.Val = p.Min
}
case InputActionValueUp:
p := &g.ShaderParams[g.currMenuIndex]
p.Val += p.Step
if p.Val > p.Max {
p.Val = p.Max
}
case InputActionPrevImage:
g.currImageIndex--
if g.currImageIndex < 0 {
g.currImageIndex = len(g.images) - 1
}
g.onImageChanged()
case InputActionNextImage:
g.currImageIndex++
if g.currImageIndex >= len(g.images) {
g.currImageIndex = 0
}
g.onImageChanged()
case InputActionResetParams:
for i := 0; i < len(g.ShaderParams); i++ {
g.ShaderParams[i].Val = g.DefaultValues[i]
}
}
}
func (g *Game) onImageChanged() {
g.img = g.images[g.currImageIndex].Image
ScreenX, ScreenY = g.img.Size()
}
func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyF1) {
g.showHelp = !g.showHelp
}
if !g.showHelp {
return nil
}
// R - reset
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
g.onInput(InputActionResetParams)
}
if inpututil.IsKeyJustPressed(ebiten.KeyZ) {
g.onInput(InputActionPrevImage)
}
if inpututil.IsKeyJustPressed(ebiten.KeyX) {
g.onInput(InputActionNextImage)
}
// Down
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
g.firstCursorMove = true
g.cursorAccum = 0
g.onInput(InputActionCursorDown)
}
if inpututil.IsKeyJustReleased(ebiten.KeyDown) {
g.cursorAccum = 0
}
// Up
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
g.firstCursorMove = true
g.cursorAccum = 0
g.onInput(InputActionCursorUp)
}
if inpututil.IsKeyJustReleased(ebiten.KeyUp) {
g.cursorAccum = 0
}
// Left
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
g.firstCursorMove = true
g.cursorAccum = 0
g.onInput(InputActionValueDown)
}
if inpututil.IsKeyJustReleased(ebiten.KeyLeft) {
g.cursorAccum = 0
}
// Right
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
g.firstCursorMove = true
g.cursorAccum = 0
g.onInput(InputActionValueUp)
}
if inpututil.IsKeyJustReleased(ebiten.KeyRight) {
g.cursorAccum = 0
}
// Key repeat behaviour for cursor
if ebiten.IsKeyPressed(ebiten.KeyDown) ||
ebiten.IsKeyPressed(ebiten.KeyUp) ||
ebiten.IsKeyPressed(ebiten.KeyLeft) ||
ebiten.IsKeyPressed(ebiten.KeyRight) {
g.cursorAccum++
delay := 10
if g.firstCursorMove {
delay = 30
}
if g.cursorAccum == delay {
g.firstCursorMove = false
g.cursorAccum = 0
if ebiten.IsKeyPressed(ebiten.KeyDown) {
g.onInput(InputActionCursorDown)
} else if ebiten.IsKeyPressed(ebiten.KeyUp) {
g.onInput(InputActionCursorUp)
} else if ebiten.IsKeyPressed(ebiten.KeyLeft) {
g.onInput(InputActionValueDown)
} else if ebiten.IsKeyPressed(ebiten.KeyRight) {
g.onInput(InputActionValueUp)
}
}
}
if g.closeWindowOnTextFrame {
return fmt.Errorf("exiting...")
}
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
g.windowSize[0] = float64(outsideWidth)
g.windowSize[1] = float64(outsideHeight)
return outsideWidth, outsideHeight
}
func (g *Game) Draw(screen *ebiten.Image) {
g.drawCRTImage(screen)
if g.config.OutputPath != "" { // dump image to file
g.renderToImage(screen)
if g.config.NoClose {
g.closeWindowOnTextFrame = false
}
return
}
if g.showHelp {
g.drawUI(screen)
}
}
func (g *Game) drawCRTImage(screen *ebiten.Image) {
tw, th := g.img.Size()
l := CalculateLetterBox(g.windowSize, f64.Vec2{float64(tw), float64(th)})
if g.config.OutputPath != "" {
l.Scale = float64(g.defaultScale)
}
// draw at pixel perfect scale
// even if we have a bigger screen, we want to stay pixel perfect
w := tw * int(l.Scale)
h := th * int(l.Scale)
sop := &ebiten.DrawRectShaderOptions{}
sop.GeoM.Scale(l.Scale, l.Scale)
sop.Uniforms = map[string]any{
"ScreenSize": []float32{float32(w), float32(h)},
"TextureSize": []float32{float32(tw), float32(th)},
}
for _, p := range g.ShaderParams {
sop.Uniforms[p.Name] = p.Val
}
sop.Images[0] = g.img
// recalculate letter box - we are drawing a scaled texture now
l = CalculateLetterBox(g.windowSize, f64.Vec2{float64(w), float64(h)})
sop.GeoM.Concat(l.GetTransform())
screen.Clear()
screen.DrawRectShader(ScreenX, ScreenY, g.shader, sop)
}
func (g *Game) drawUI(screen *ebiten.Image) {
st := fontSize / 2
y := st * 2
x := 8
drawTextWithShadow(screen, "crten v"+version, g.font, x, y, color.White)
y += st
desc := g.images[g.currImageIndex].Desc
drawTextWithShadow(screen, "Art: "+desc, g.font, x, y, color.White)
y += st * 2
drawTextWithShadow(screen, "[F1 - show/hide UI]", g.font, x+1, y+1, color.White)
y += st
drawTextWithShadow(screen, "[Arrow keys - adjust params, R to reset]", g.font, x, y, color.White)
y += st
drawTextWithShadow(screen, "[Z/X - switch images]", g.font, x, y, color.White)
y += st * 2
for i, p := range g.ShaderParams {
isSelected := g.currMenuIndex == i
t := getParamText(p, isSelected)
drawTextWithShadow(screen, t, g.font, x, y, color.White)
if isSelected { // hack: highlight cursor and param name
c := color.RGBA{175, 233, 233, 255}
drawTextWithShadow(screen, " => "+p.Name, g.font, x, y, c)
}
y += st
}
}
func drawTextWithShadow(screen *ebiten.Image, str string, font font.Face, x int, y int, c color.Color) {
text.Draw(screen, str, font, x+1, y+1, color.Black)
text.Draw(screen, str, font, x, y, c)
}
func getParamText(p ShaderParam, isSelected bool) string {
pref := " "
arrLeft := " < "
arrRight := " >"
if isSelected {
pref = " => "
if p.Val == p.Min {
arrLeft = " "
}
if p.Val == p.Max {
arrRight = ""
}
} else {
arrLeft = " "
arrRight = " "
}
var desc string
if p.Name == "ShadowMask" {
switch p.Val {
case 0.0:
desc = "(None)"
case 1.0:
desc = "(Compressed TV style)"
case 2.0:
desc = "(Aperture-grille)"
case 3.0:
desc = "(Stretched VGA style)"
case 4.0:
desc = "(VGA style)"
default:
panic("??")
}
}
return fmt.Sprintf("%s%s:%s%.3f%s %s", pref, p.Name, arrLeft, p.Val, arrRight, desc)
}
func (g *Game) renderToImage(screen *ebiten.Image) error {
log.Printf("saving to %s...\n", g.config.OutputPath)
f, err := os.Create(g.config.OutputPath)
if err != nil {
return err
}
defer f.Close()
g.closeWindowOnTextFrame = true
return png.Encode(f, screen)
}
func main() {
config := parseConfig()
g := &Game{
config: config,
defaultScale: 4,
}
focus()
if config.InputPath != "" {
f, err := os.Open(config.InputPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
img, _, err := ebitenutil.NewImageFromReader(f)
if err != nil {
log.Fatal(err)
}
g.images = []galleryImage{{Desc: config.InputPath, Image: img}}
} else { // no path specified - show default images
var err error
g.images, err = getImages()
if err != nil {
log.Fatal(err)
}
}
g.onImageChanged()
shader, err := ebiten.NewShader(shaderSrc)
if err != nil {
log.Fatal(err)
}
g.shader = shader
g.ShaderParams = []ShaderParam{
{"HardScan", -10., -20., 0., 1.},
{"HardPix", -4., -20., 0., 1.},
{"WarpX", 0.01, 0.0, 0.125, 0.01},
{"WarpY", 0.02, 0.0, 0.125, 0.01},
{"MaskDark", 0.5, 0.0, 2.0, 0.1},
{"MaskLight", 1.5, 0.0, 2.0, 0.1},
{"ShadowMask", 0.0, 0.0, 4.0, 1.0},
{"BrightBoost", 1.0, 0.0, 2.0, 0.05},
{"HardBloomPix", -1.5, -2.0, -0.5, 0.1},
{"HardBloomScan", -2.0, -4.0, -1.0, 0.1},
{"BloomAmount", 0.05, 0.0, 1.0, 0.05},
{"Shape", 2.0, 0.0, 10.0, 0.05},
}
for _, p := range g.ShaderParams {
g.DefaultValues = append(g.DefaultValues, p.Val)
}
if g.config.ConfigPath != "" {
g.loadInputConfig()
}
g.showHelp = true
if err := g.loadFont(); err != nil {
log.Fatal(err)
}
if g.config.OutputPath != "" {
ebiten.SetScreenTransparent(true)
ebiten.SetWindowDecorated(false)
ebiten.SetWindowFloating(true)
} else {
ebiten.SetWindowTitle(fmt.Sprintf("crten v%s", version))
ebiten.SetWindowResizable(false)
}
ebiten.SetWindowSize(ScreenX*g.defaultScale, ScreenY*g.defaultScale)
if err := ebiten.RunGame(g); err != nil {
if err.Error() == "exiting..." {
return
}
log.Fatal(err)
}
}