Repos / shark / 67903e6b57
commit 67903e6b5752ad2f38282a973c604c4c5b85cbfa
Author: Nhân <hi@imnhan.com>
Date: Sun Sep 17 11:23:52 2023 +0700
use `must`
diff --git a/main.go b/main.go
index d03848e..51dcdf5 100644
--- a/main.go
+++ b/main.go
@@ -6,13 +6,13 @@
"flag"
"image"
_ "image/png"
- "log"
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
+ "go.imnhan.com/shark/must"
)
const SPRITE_X = 100
@@ -221,15 +221,12 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (w, h int) {
}
func NewAnim(sprites embed.FS, subdir string) *Anim {
- files, err := sprites.ReadDir("sprites/" + subdir)
- PanicIfErr(err)
+ files := must.One(sprites.ReadDir("sprites/" + subdir))
var frames []*ebiten.Image
for _, direntry := range files {
fname := direntry.Name()
- frame, err := sprites.ReadFile("sprites/" + subdir + "/" + fname)
- PanicIfErr(err)
- img, _, err := ebitenutil.NewImageFromReader(bytes.NewReader(frame))
- PanicIfErr(err)
+ frame := must.One(sprites.ReadFile("sprites/" + subdir + "/" + fname))
+ img, _ := must.Two(ebitenutil.NewImageFromReader(bytes.NewReader(frame)))
frames = append(frames, img)
}
return &Anim{frames}
@@ -284,19 +281,11 @@ func main() {
ebiten.SetWindowDecorated(false)
ebiten.SetWindowFloating(true)
- AppIcon, _, iconerr := image.Decode(bytes.NewReader(IconFile))
- PanicIfErr(iconerr)
+ AppIcon, _ := must.Two(image.Decode(bytes.NewReader(IconFile)))
ebiten.SetWindowIcon([]image.Image{AppIcon})
- err := ebiten.RunGameWithOptions(
+ must.Zero(ebiten.RunGameWithOptions(
&game,
&ebiten.RunGameOptions{ScreenTransparent: true},
- )
- PanicIfErr(err)
-}
-
-func PanicIfErr(err error) {
- if err != nil {
- log.Fatal(err)
- }
+ ))
}
diff --git a/must/must.go b/must/must.go
new file mode 100644
index 0000000..6fc1354
--- /dev/null
+++ b/must/must.go
@@ -0,0 +1,34 @@
+// A.k.a "I just wanna write python"
+// Good for irrecoverable cases we absolutely want to fail fast with a
+// stacktrace.
+// Possibly useful for quick prototyping too, but ofc please treat every usage
+// of this package as a FIXME item and clean it up before running in
+// production.
+package must
+
+func Zero(err error) {
+ if err != nil {
+ panic(err)
+ }
+}
+
+func One[T any](val T, err error) T {
+ if err != nil {
+ panic(err)
+ }
+ return val
+}
+
+func Two[T any, U any](v1 T, v2 U, err error) (T, U) {
+ if err != nil {
+ panic(err)
+ }
+ return v1, v2
+}
+
+func Ok[T any](v T, ok bool) T {
+ if !ok {
+ panic("not ok")
+ }
+ return v
+}