Skip to content

Instantly share code, notes, and snippets.

@venning
Created May 30, 2022 12:29
Show Gist options
  • Save venning/3ac65978f793534dbc4a102b2274f494 to your computer and use it in GitHub Desktop.
Save venning/3ac65978f793534dbc4a102b2274f494 to your computer and use it in GitHub Desktop.
package main
import (
"image"
"image/color"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
var (
emptyImage = ebiten.NewImage(3, 3)
// emptySubImage is an internal sub image of emptyImage.
// Use emptySubImage at DrawTriangles instead of emptyImage in order to avoid bleeding edges.
emptySubImage = emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
)
func init() {
emptyImage.Fill(color.White)
}
const (
screenWidth = 640
screenHeight = 480
size = 100
)
type Game struct {
}
func (g *Game) Update() error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
containerImage := ebiten.NewImage(size, size)
containerImage.Fill(color.White)
var path vector.Path
path.MoveTo(0, 0)
path.LineTo(0, size - 1)
path.LineTo(size - 1, size - 1)
path.LineTo(size - 1, 0)
triOp := &ebiten.DrawTrianglesOptions{
FillRule: ebiten.EvenOdd,
}
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
for i := range vs {
vs[i].SrcX = 1
vs[i].SrcY = 1
vs[i].ColorR = 0xdb / float32(0xff)
vs[i].ColorG = 0x56 / float32(0xff)
vs[i].ColorB = 0x20 / float32(0xff)
}
containerImage.DrawTriangles(vs, is, emptySubImage, triOp)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(50, 50)
screen.DrawImage(containerImage, op)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Path Test")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment