Optimize drawing board

This commit is contained in:
Trevor Slocum 2023-10-30 12:36:50 -07:00
parent 4a8d62c684
commit fe11f69409
4 changed files with 61 additions and 57 deletions

View file

@ -1,3 +1,6 @@
1.0.6:
- Optimize drawing board
1.0.5:
- Add sound effects

View file

@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/color"
"image/draw"
"log"
"strconv"
"sync"
@ -36,6 +37,7 @@ type board struct {
innerW, innerH int
backgroundImage *ebiten.Image
baseImage *image.RGBA
Sprites *Sprites
@ -274,41 +276,49 @@ func (b *board) updateBackgroundImage() {
frameW := b.w - int((b.horizontalBorderSize-borderSize)*2)
innerW := float64(b.w) - b.horizontalBorderSize*2 // Outer board width (including frame)
// Table
b.backgroundImage = ebiten.NewImage(b.w, b.h)
if b.backgroundImage == nil {
b.backgroundImage = ebiten.NewImage(b.w, b.h)
b.baseImage = image.NewRGBA(image.Rect(0, 0, b.w, b.h))
} else {
bounds := b.backgroundImage.Bounds()
if bounds.Dx() != b.w || bounds.Dy() != b.h {
b.backgroundImage = ebiten.NewImage(b.w, b.h)
b.baseImage = image.NewRGBA(image.Rect(0, 0, b.w, b.h))
}
}
// Draw table.
b.backgroundImage.Fill(tableColor)
// Frame
img := ebiten.NewImage(frameW, b.h)
img.Fill(frameColor)
// Draw frame.
{
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(b.horizontalBorderSize-borderSize), 0)
b.backgroundImage.DrawImage(img, op)
x, y := int(b.horizontalBorderSize-borderSize), 0
w, h := frameW, b.h
b.backgroundImage.SubImage(image.Rect(x, y, x+w, y+h)).(*ebiten.Image).Fill(frameColor)
}
// Face
img = ebiten.NewImage(int(innerW), b.h-int(b.verticalBorderSize*2))
img.Fill(faceColor)
// Draw face.
{
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(b.horizontalBorderSize), float64(b.verticalBorderSize))
b.backgroundImage.DrawImage(img, op)
x, y := int(b.horizontalBorderSize), int(b.verticalBorderSize)
w, h := int(innerW), b.h-int(b.verticalBorderSize*2)
b.backgroundImage.SubImage(image.Rect(x, y, x+w, y+h)).(*ebiten.Image).Fill(faceColor)
}
// Bar
img = ebiten.NewImage(int(b.barWidth), b.h)
img.Fill(frameColor)
// Draw bar.
{
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64((b.w/2)-int(b.barWidth/2)), 0)
b.backgroundImage.DrawImage(img, op)
x, y := int((b.w/2)-int(b.barWidth/2)), 0
w, h := int(b.barWidth), b.h
b.backgroundImage.SubImage(image.Rect(x, y, x+w, y+h)).(*ebiten.Image).Fill(frameColor)
}
// Draw triangles
baseImg := image.NewRGBA(image.Rect(0, 0, b.w-int(b.horizontalBorderSize*2), b.h-int(b.verticalBorderSize*2)))
gc := draw2dimg.NewGraphicContext(baseImg)
// Draw triangles.
draw.Draw(b.baseImage, image.Rect(0, 0, b.w, b.h), image.NewUniform(color.RGBA{0, 0, 0, 0}), image.ZP, draw.Src)
gc := draw2dimg.NewGraphicContext(b.baseImage)
offsetX, offsetY := float64(b.horizontalBorderSize), float64(b.verticalBorderSize)
for i := 0; i < 2; i++ {
if i == 1 {
offsetY = -offsetY - 1
}
triangleTip := (float64(b.h) - (b.verticalBorderSize * 2)) / 2
if i == 0 {
triangleTip -= b.triangleOffset
@ -332,26 +342,19 @@ func (b *board) updateBackgroundImage() {
if j >= 6 {
tx += b.barWidth
}
gc.MoveTo(float64(tx), float64(ty))
gc.LineTo(float64(tx+b.spaceWidth/2), triangleTip)
gc.LineTo(float64(tx+b.spaceWidth), float64(ty))
gc.MoveTo(offsetX+float64(tx), offsetY+float64(ty))
gc.LineTo(offsetX+float64(tx+b.spaceWidth/2), offsetY+triangleTip)
gc.LineTo(offsetX+float64(tx+b.spaceWidth), offsetY+float64(ty))
gc.Close()
gc.Fill()
}
}
img = ebiten.NewImageFromImage(baseImg)
{
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(b.horizontalBorderSize), float64(b.verticalBorderSize))
b.backgroundImage.DrawImage(img, op)
}
// Border
borderImage := image.NewRGBA(image.Rect(0, 0, b.w, b.h))
gc = draw2dimg.NewGraphicContext(borderImage)
// Draw border.
borderStrokeSize := 2.0
gc.SetStrokeColor(borderColor)
// - Center
gc.SetLineWidth(2)
gc.SetLineWidth(borderStrokeSize)
gc.MoveTo(float64(frameW/2), float64(0))
gc.LineTo(float64(frameW/2), float64(b.h))
gc.Stroke()
@ -360,7 +363,7 @@ func (b *board) updateBackgroundImage() {
gc.LineTo(float64(frameW), float64(b.h))
gc.Stroke()
// - Inside left
gc.SetLineWidth(1)
gc.SetLineWidth(borderStrokeSize / 2)
edge := float64((((innerW) - b.barWidth) / 2) + borderSize)
gc.MoveTo(float64(borderSize), float64(b.verticalBorderSize))
gc.LineTo(edge, float64(b.verticalBorderSize))
@ -392,12 +395,7 @@ func (b *board) updateBackgroundImage() {
gc.LineTo(float64(b.w), float64(b.h))
gc.Stroke()
}
img = ebiten.NewImageFromImage(borderImage)
{
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(b.horizontalBorderSize-borderSize, 0)
b.backgroundImage.DrawImage(img, op)
}
b.backgroundImage.DrawImage(ebiten.NewImageFromImage(b.baseImage), nil)
// Draw space numbers.
for space, r := range b.spaceRects {

View file

@ -35,7 +35,7 @@ import (
"golang.org/x/image/font/opentype"
)
const version = "v1.0.5"
const version = "v1.0.6"
const MaxDebug = 1
@ -201,23 +201,17 @@ func init() {
inputBuffer.Field.SetSuffix("")
}
var (
loadedCheckerWidth = -1
loadedDiceImages bool
)
var loadedCheckerWidth = -1
func loadImageAssets(width int) {
if width != loadedCheckerWidth {
imgCheckerLight = loadAsset("asset/image/checker_white.png", width)
imgCheckerDark = loadAsset("asset/image/checker_white.png", width)
//imgCheckerDark = loadAsset("assets/checker_black.png", width)
loadedCheckerWidth = width
}
if loadedDiceImages {
if width == loadedCheckerWidth {
return
}
loadedDiceImages = true
loadedCheckerWidth = width
imgCheckerLight = loadAsset("asset/image/checker_white.png", width)
imgCheckerDark = loadAsset("asset/image/checker_white.png", width)
//imgCheckerDark = loadAsset("assets/checker_black.png", width)
resizeDice := func(img image.Image) *ebiten.Image {
const maxSize = 70

View file

@ -2,6 +2,11 @@
package game
import (
"log"
"os"
)
const (
DefaultServerAddress = "wss://ws.bgammon.org"
OptimizeDraws = false
@ -9,3 +14,7 @@ const (
ShowServerSettings = true
APPNAME = "boxcars-android"
)
func init() {
log.SetOutput(os.Stdout)
}