Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active November 11, 2020 15:23
Show Gist options
  • Save peterhellberg/723946589855dc61fa45df81e3f70bfc to your computer and use it in GitHub Desktop.
Save peterhellberg/723946589855dc61fa45df81e3f70bfc to your computer and use it in GitHub Desktop.
Blocks rendered by Ebiten with gfx.
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/peterhellberg/gfx"
)
const (
title = "Ebiten GFX Blocks"
scale = 4
w, h = 256, 128
fw, fh = float64(w), float64(h)
fw2, fh2 = fw / 2, fh / 2
)
var (
pix = make([]uint8, w*h*4)
scaler = gfx.NewLinearScaler().Range(6, 0.5).Domain(0, fh)
flipY = gfx.V(1, -1)
bounds = gfx.R(0, 0, fw, fh)
defaultPos = gfx.V3(fw2, fh2, 0)
defaultOrigin = bounds.Center().ScaledXY(flipY).Vec3(1)
origin = defaultOrigin
pos = defaultPos
blocks = createBlocks()
)
func createBlocks() (blocks gfx.Blocks) {
for j := -6.0; j < 6; j++ {
for i := -6.0; i < 6; i++ {
aj, ai := gfx.MathAbs(j), gfx.MathAbs(i)
p := pos.AddXYZ(-fw2+(i*10), -fh2+(j*10), 0)
s := gfx.V3(3+aj, 3+ai, 3*ai+aj)
c := gfx.BlockColors[int(ai+aj)%8]
blocks.AddNewBlock(p, s, c)
blocks.AddNewBlock(p.AddXYZ(0, 0, -7*(ai+aj)), s.AddXYZ(-3, -3, 0), gfx.BlockColorBlack)
}
}
return blocks
}
func render() {
dst := gfx.NewImage(w, h, gfx.BlockColorBlack.Dark)
var drawCount int
for i := len(blocks) - 1; i >= 0; i-- {
b := blocks[i]
if shape, top, left, _ := b.Polygons(origin); bounds.Overlaps(shape.Rect()) {
drawCount += shape.Fill(dst, b.Color.Medium)
drawCount += left.Fill(dst, b.Color.Dark)
drawCount += top.Fill(dst, b.Color.Light)
}
drawCount++
}
gfx.Log("drawCount: %d", drawCount)
pix = dst.Pix
}
func main() {
render()
ebiten.SetWindowDecorated(false)
ebiten.Run(func(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyEscape) {
return gfx.ErrDone
}
if ebiten.IsDrawingSkipped() {
return nil
}
if updatePos(pos, origin) {
render()
}
return screen.ReplacePixels(pix)
}, w, h, scale, title)
}
func updatePos(oldPos, oldOrigin gfx.Vec3) bool {
if ebiten.IsKeyPressed(ebiten.KeyH) {
pos.Y += 0.6 // Diagonal up left
}
if ebiten.IsKeyPressed(ebiten.KeyL) {
pos.X += 0.6 // Diagonal up right
}
if ebiten.IsKeyPressed(ebiten.KeyJ) {
pos.X -= 0.6 // Diagonal down left
}
if ebiten.IsKeyPressed(ebiten.KeyK) {
pos.Y -= 0.6 // Diagonal down right
}
if ebiten.IsKeyPressed(ebiten.KeyW) {
pos.Z += 0.5 // Straight up
}
if ebiten.IsKeyPressed(ebiten.KeyS) {
pos.Z -= 0.5 // Straight down
}
if ebiten.IsKeyPressed(ebiten.KeyA) {
origin.X -= 1 // Straight left
}
if ebiten.IsKeyPressed(ebiten.KeyD) {
origin.X += 1 // Straight right
}
if ebiten.IsKeyPressed(ebiten.KeyC) {
pos, origin = defaultPos, defaultOrigin // Reset pos and origin
}
if ebiten.IsKeyPressed(ebiten.KeyX) && origin.Z > 0.5 {
origin.Z -= 0.05 // Scale down
}
if ebiten.IsKeyPressed(ebiten.KeyZ) && origin.Z < 10 {
origin.Z += 0.05 // Scale up
}
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
origin.X, origin.Y = gfx.IV(ebiten.CursorPosition()).ScaledXY(flipY).XY()
}
switch touches := ebiten.Touches(); len(touches) {
case 1:
origin.X, origin.Y = gfx.IV(touches[0].Position()).ScaledXY(flipY).XY()
case 2:
tv := gfx.IV(touches[1].Position())
origin.Z = scaler.ScaleFloat64(tv.Y)
origin.X, origin.Y = gfx.IV(touches[0].Position()).ScaledXY(flipY).XY()
}
return pos != oldPos || origin != oldOrigin
}
@peterhellberg
Copy link
Author

2560x1440-z12

gfx-many-blocks-2560x1440-z12

2560x1440-z24

gfx-many-blocks-2560x1440-z24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment