Created
June 6, 2020 20:53
-
-
Save peterhellberg/93e4e7a3bdaa54efa2f589b7d610a5f7 to your computer and use it in GitHub Desktop.
Bookmark SDF rendered by Ebiten using gfx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/hajimehoshi/ebiten" | |
"github.com/peterhellberg/gfx" | |
) | |
type Game struct { | |
gfx.Vec | |
Image *gfx.Paletted | |
N int | |
} | |
func (g *Game) Update(screen *ebiten.Image) error { | |
if ebiten.IsKeyPressed(ebiten.KeyEscape) || ebiten.IsKeyPressed(ebiten.KeyQ) { | |
return gfx.ErrDone | |
} | |
v := g.Vec | |
if ebiten.IsKeyPressed(ebiten.KeyLeft) { | |
g.X += 0.1 | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyRight) { | |
g.X -= 0.1 | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyUp) { | |
g.Y += 0.1 | |
} | |
if ebiten.IsKeyPressed(ebiten.KeyDown) { | |
g.Y -= 0.1 | |
} | |
n := g.N | |
if ebiten.IsKeyPressed(ebiten.Key1) { | |
g.N = 0 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key2) { | |
g.N = 1 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key3) { | |
g.N = 2 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key4) { | |
g.N = 3 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key5) { | |
g.N = 4 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key6) { | |
g.N = 5 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key7) { | |
g.N = 6 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key8) { | |
g.N = 7 | |
} | |
if ebiten.IsKeyPressed(ebiten.Key9) { | |
g.N = 8 | |
} | |
if !v.Eq(g.Vec) || v.Eq(gfx.ZV) || n != g.N { | |
p := Palettes[g.N] | |
m := gfx.NewPaletted(w, h, p, p[0]) | |
c := gfx.BoundsCenter(gfx.IR(0, 0, -w, -h)) | |
matrix := gfx.IM.Moved(c.Sub(gfx.V(0, -20))) | |
gfx.EachPixel(m.Bounds(), func(x, y int) { | |
pm := matrix.Project(gfx.IV(x, y)) | |
sd := gfx.SignedDistance{Vec: pm} | |
d := g.shape(sd) | |
switch { | |
case d < 0: | |
m.Set(x, y, p[1]) | |
case d < 1: | |
m.Set(x, y, p[2]) | |
case d < 2: | |
m.Set(x, y, p[3]) | |
} | |
}) | |
g.Image = m | |
} | |
return nil | |
} | |
func (g *Game) Draw(screen *ebiten.Image) { | |
screen.ReplacePixels(g.Image.Pixels()) | |
} | |
func (g *Game) Layout(_, _ int) (int, int) { | |
return 64, 64 | |
} | |
func (g *Game) shape(sd gfx.SignedDistance) float64 { | |
return sd.OpSubtraction( | |
sd.OpSmoothUnion( | |
sd.IsoscelesTriangle(gfx.V(16, 25+g.Y)), | |
sd.OpTx(gfx.IM.Moved(gfx.V(0, 26)), func(sd gfx.SignedDistance) float64 { | |
return sd.Rectangle(gfx.V(14, 10+g.X)) | |
}), 5, | |
), | |
sd.Rectangle(gfx.V(8, 30)), | |
) | |
} | |
var w, h = 64, 64 | |
func main() { | |
n := 7 | |
g := &Game{ | |
N: n, | |
Image: gfx.NewPaletted(w, h, Palettes[n]), | |
} | |
ebiten.SetWindowSize(1024, 1024) | |
ebiten.SetWindowDecorated(false) | |
ebiten.RunGame(g) | |
} | |
// Palettes with 4 colors | |
// from https://i.redd.it/0svby0ssa7s41.png | |
var Palettes = gfx.Palettes{ | |
PaletteIce, | |
PaletteBubblegum, | |
PaletteDream, | |
PaletteGreenApple, | |
PalettePurpleBerry, | |
PaletteDarkCherry, | |
PaletteCardboard, | |
PaletteOlive, | |
PaletteSteel, | |
} | |
var PaletteIce = gfx.Palette{ | |
{0x9b, 0xeb, 0xeb, 0xff}, | |
{0x6d, 0xa1, 0xdf, 0xff}, | |
{0x66, 0x53, 0xcb, 0xff}, | |
{0x50, 0x1a, 0x68, 0xff}, | |
} | |
var PaletteBubblegum = gfx.Palette{ | |
{0xf4, 0xe4, 0xd9, 0xff}, | |
{0xfd, 0x78, 0xad, 0xff}, | |
{0x05, 0x8e, 0xa8, 0xff}, | |
{0x2d, 0x2a, 0x01, 0xff}, | |
} | |
var PaletteDream = gfx.Palette{ | |
{0xff, 0xec, 0x8d, 0xff}, | |
{0x34, 0xd9, 0xce, 0xff}, | |
{0xf4, 0x1c, 0x7d, 0xff}, | |
{0x3f, 0x0d, 0x68, 0xff}, | |
} | |
var PaletteGreenApple = gfx.Palette{ | |
{0xf7, 0xff, 0xb7, 0xff}, | |
{0xa5, 0xd1, 0x45, 0xff}, | |
{0x2b, 0x80, 0x37, 0xff}, | |
{0x00, 0x1b, 0x27, 0xff}, | |
} | |
var PalettePurpleBerry = gfx.Palette{ | |
{0xde, 0xfe, 0xff, 0xff}, | |
{0x97, 0xb6, 0xff, 0xff}, | |
{0xdc, 0x05, 0xef, 0xff}, | |
{0x01, 0x1f, 0x11, 0xff}, | |
} | |
var PaletteDarkCherry = gfx.Palette{ | |
{0xff, 0xf5, 0xde, 0xff}, | |
{0xfd, 0x97, 0x85, 0xff}, | |
{0xf6, 0x08, 0x83, 0xff}, | |
{0x14, 0x02, 0x7a, 0xff}, | |
} | |
var PaletteCardboard = gfx.Palette{ | |
{0xc5, 0xfb, 0xe1, 0xff}, | |
{0xc6, 0xaa, 0x35, 0xff}, | |
{0xaf, 0x53, 0x14, 0xff}, | |
{0x3c, 0x1e, 0x15, 0xff}, | |
} | |
var PaletteOlive = gfx.Palette{ | |
{0xd4, 0xf1, 0xf0, 0xff}, | |
{0xa1, 0xb4, 0x67, 0xff}, | |
{0x7f, 0x6f, 0x00, 0xff}, | |
{0x2c, 0x25, 0x17, 0xff}, | |
} | |
var PaletteSteel = gfx.Palette{ | |
{0xdc, 0xed, 0xeb, 0xff}, | |
{0x90, 0xad, 0xbb, 0xff}, | |
{0x56, 0x68, 0x9d, 0xff}, | |
{0x26, 0x23, 0x38, 0xff}, | |
} |
Author
peterhellberg
commented
Jun 6, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment