mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 19:28:57 +01:00
examples/blend: clean up
This commit is contained in:
parent
80cbf7cbae
commit
903b5ab046
@ -30,8 +30,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
screenWidth = 780
|
screenWidth = 800
|
||||||
screenHeight = 600
|
screenHeight = 800
|
||||||
)
|
)
|
||||||
|
|
||||||
// mode is a blend mode with description.
|
// mode is a blend mode with description.
|
||||||
@ -59,13 +59,13 @@ func NewGame() (*Game, error) {
|
|||||||
return nil, fmt.Errorf("fail to load dest: %w", err)
|
return nil, fmt.Errorf("fail to load dest: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting up a grid for drawing.
|
// Set up a grid for drawing.
|
||||||
g := &Game{
|
g := &Game{
|
||||||
source: source,
|
source: source,
|
||||||
dest: dest,
|
dest: dest,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adding all known blend modes and their names.
|
// Add all known blend modes and their names.
|
||||||
g.modes = []mode{
|
g.modes = []mode{
|
||||||
{blend: ebiten.BlendCopy, name: "BlendCopy"},
|
{blend: ebiten.BlendCopy, name: "BlendCopy"},
|
||||||
{blend: ebiten.BlendSourceAtop, name: "BlendSourceAtop"},
|
{blend: ebiten.BlendSourceAtop, name: "BlendSourceAtop"},
|
||||||
@ -82,9 +82,9 @@ func NewGame() (*Game, error) {
|
|||||||
{blend: ebiten.BlendClear, name: "BlendClear"},
|
{blend: ebiten.BlendClear, name: "BlendClear"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjusting the tile size for the available images.
|
// Adjust the tile size for the available images.
|
||||||
g.tileSize = maxSide(g.source, g.dest)
|
g.tileSize = maxSide(g.source, g.dest)
|
||||||
g.offscreen = ebiten.NewImage(int(g.tileSize), int(g.tileSize))
|
g.offscreen = ebiten.NewImage(g.tileSize, g.tileSize)
|
||||||
|
|
||||||
return g, nil
|
return g, nil
|
||||||
}
|
}
|
||||||
@ -95,50 +95,42 @@ func (g *Game) Update() error {
|
|||||||
|
|
||||||
func (g *Game) Draw(screen *ebiten.Image) {
|
func (g *Game) Draw(screen *ebiten.Image) {
|
||||||
const (
|
const (
|
||||||
tileSpacing = 64
|
tileGap = 64
|
||||||
textSpacing = 16
|
textGap = 16
|
||||||
gridW = 4
|
gridW = 4
|
||||||
gridH = 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Clearing the screen.
|
// Clear the screen.
|
||||||
screen.Fill(color.White)
|
screen.Fill(color.White)
|
||||||
|
|
||||||
// Get an offset for center alignment.
|
// Get an offset for center alignment.
|
||||||
// Where sw, sh is the screen size in pixels.
|
// Where sw, sh is the screen size in pixels.
|
||||||
// And gw, gh is the grid size in pixels.
|
// And gw, gh is the grid size in pixels.
|
||||||
totalTileSize := g.tileSize + tileSpacing
|
totalTileSize := g.tileSize + tileGap
|
||||||
gw, gh := gridW*totalTileSize-tileSpacing, gridH*totalTileSize
|
gridH := (len(g.modes)-1)/gridW + 1
|
||||||
|
gw, gh := gridW*totalTileSize-tileGap, gridH*totalTileSize
|
||||||
sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
|
sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
|
||||||
cx, cy := float64(sw-gw)/2, float64(sh-gh)/2
|
ox, oy := float64(sw-gw)/2, float64(sh-gh)/2
|
||||||
|
|
||||||
|
// Draw a tilemap.
|
||||||
|
for i, m := range g.modes {
|
||||||
|
x := i % gridW
|
||||||
|
y := i / gridW
|
||||||
|
|
||||||
// Drawing a tilemap
|
|
||||||
for y := 0; y < gridH; y++ {
|
|
||||||
for x := 0; x < gridW; x++ {
|
|
||||||
px, py := x*totalTileSize, y*totalTileSize
|
px, py := x*totalTileSize, y*totalTileSize
|
||||||
if y > 0 {
|
|
||||||
// Making a place for the text.
|
|
||||||
py += textSpacing * y
|
|
||||||
}
|
|
||||||
|
|
||||||
// Getting the right mode for the coords or skipping the rendering.
|
// Making a place for the text.
|
||||||
m, ok := getMode(g.modes, x, y, gridW, gridH)
|
py += textGap * y
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drawing the blend mode and it's name.
|
// Drawing the blend mode and it's name.
|
||||||
tileSize := float64(g.tileSize)
|
alignedX, alignedY := float64(px)+ox, float64(py)+oy
|
||||||
alignedX, alignedY := float64(px)+cx, float64(py)+cy
|
|
||||||
halfTileSize, spacing := tileSize/2, float64(textSpacing)
|
|
||||||
g.drawBlendMode(screen, alignedX, alignedY, m.blend)
|
g.drawBlendMode(screen, alignedX, alignedY, m.blend)
|
||||||
drawCenteredText(screen, alignedX+halfTileSize, alignedY+tileSize+spacing, m.name)
|
drawCenteredText(screen, alignedX+float64(g.tileSize)/2, alignedY+float64(g.tileSize)+textGap, m.name)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) Layout(w, h int) (int, int) {
|
func (g *Game) Layout(w, h int) (int, int) {
|
||||||
return w, h
|
return screenWidth, screenHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) drawBlendMode(screen *ebiten.Image, x, y float64, mode ebiten.Blend) {
|
func (g *Game) drawBlendMode(screen *ebiten.Image, x, y float64, mode ebiten.Blend) {
|
||||||
@ -182,18 +174,6 @@ func maxSide(a, b *ebiten.Image) int {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMode returns a blend mode by index and found status.
|
|
||||||
func getMode(modes []mode, x, y, w, h int) (m mode, ok bool) {
|
|
||||||
idx := y*w + x
|
|
||||||
if idx > len(modes)-1 {
|
|
||||||
return mode{}, false
|
|
||||||
}
|
|
||||||
if x < 0 || x > w || y < 0 || y > h {
|
|
||||||
return mode{}, false
|
|
||||||
}
|
|
||||||
return modes[idx], true
|
|
||||||
}
|
|
||||||
|
|
||||||
// drawCenteredText is a util function for drawing blend mode description.
|
// drawCenteredText is a util function for drawing blend mode description.
|
||||||
func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) {
|
func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) {
|
||||||
bounds := text.BoundString(inconsolata.Bold8x16, s)
|
bounds := text.BoundString(inconsolata.Bold8x16, s)
|
||||||
|
Loading…
Reference in New Issue
Block a user