2022-11-13 20:44:19 -07:00
|
|
|
package x
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/draw"
|
|
|
|
import "golang.org/x/image/math/fixed"
|
2022-11-15 22:29:23 -07:00
|
|
|
import "github.com/jezek/xgbutil/xgraphics"
|
2022-11-13 20:44:19 -07:00
|
|
|
|
|
|
|
import "git.tebibyte.media/sashakoshka/stone"
|
|
|
|
|
|
|
|
func (backend *Backend) Draw () {
|
2022-11-15 22:29:23 -07:00
|
|
|
backend.lock.Lock()
|
|
|
|
defer backend.lock.Unlock()
|
2022-11-13 20:44:19 -07:00
|
|
|
|
2022-11-16 09:14:30 -07:00
|
|
|
if backend.windowBoundsClean {
|
|
|
|
backend.canvas.XPaintRects (
|
|
|
|
backend.window.Id,
|
|
|
|
backend.drawCells(false)...)
|
|
|
|
} else {
|
2022-11-13 20:44:19 -07:00
|
|
|
backend.reallocateCanvas()
|
|
|
|
backend.drawCells(true)
|
|
|
|
backend.canvas.XDraw()
|
|
|
|
backend.canvas.XPaint(backend.window.Id)
|
2022-11-16 09:14:30 -07:00
|
|
|
backend.windowBoundsClean = true
|
2022-11-13 20:44:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 22:29:23 -07:00
|
|
|
func (backend *Backend) reallocateCanvas () {
|
|
|
|
if backend.canvas != nil {
|
|
|
|
backend.canvas.Destroy()
|
|
|
|
}
|
|
|
|
backend.canvas = xgraphics.New (
|
|
|
|
backend.connection,
|
|
|
|
image.Rect (
|
|
|
|
0, 0,
|
|
|
|
backend.metrics.windowWidth,
|
|
|
|
backend.metrics.windowHeight))
|
|
|
|
backend.canvas.For (func (x, y int) xgraphics.BGRA {
|
|
|
|
return backend.colors[stone.ColorBackground]
|
|
|
|
})
|
|
|
|
|
|
|
|
backend.canvas.XSurfaceSet(backend.window.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
|
|
|
|
width, height := backend.application.Size()
|
|
|
|
for y := 0; y < height; y ++ {
|
|
|
|
for x := 0; x < width; x ++ {
|
|
|
|
if !forceRedraw && backend.application.Clean(x, y) { continue }
|
|
|
|
|
2022-11-16 22:26:28 -07:00
|
|
|
cell := backend.application.GetForRendering(x, y)
|
2022-11-15 22:29:23 -07:00
|
|
|
content := cell.Rune()
|
|
|
|
|
|
|
|
if forceRedraw && content < 32 { continue }
|
|
|
|
|
|
|
|
areas = append(areas, backend.boundsOfCell(x, y))
|
|
|
|
backend.drawRune(x, y, content, cell.Color(), !forceRedraw)
|
|
|
|
}}
|
|
|
|
|
|
|
|
if backend.drawBufferBounds && forceRedraw {
|
|
|
|
strokeRectangle (
|
|
|
|
&image.Uniform {
|
|
|
|
C: backend.config.Color(stone.ColorForeground),
|
|
|
|
},
|
|
|
|
backend.canvas,
|
|
|
|
image.Rectangle {
|
|
|
|
Min: backend.originOfCell(0, 0),
|
|
|
|
Max: backend.originOfCell(width, height),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
2022-11-13 20:44:19 -07:00
|
|
|
}
|
|
|
|
|
2022-11-15 22:29:23 -07:00
|
|
|
func (backend *Backend) drawRune (
|
|
|
|
x, y int,
|
|
|
|
character rune,
|
|
|
|
runeColor stone.Color,
|
|
|
|
drawBackground bool,
|
|
|
|
) {
|
2022-11-13 20:44:19 -07:00
|
|
|
// TODO: cache these draws as non-transparent buffers with the
|
|
|
|
// application background color as the background. that way, we won't
|
|
|
|
// need to redraw the characters *or* composite them.
|
2022-11-15 22:29:23 -07:00
|
|
|
|
|
|
|
if drawBackground {
|
|
|
|
fillRectangle (
|
|
|
|
&image.Uniform {
|
|
|
|
C: backend.config.Color(stone.ColorBackground),
|
|
|
|
},
|
|
|
|
backend.canvas,
|
|
|
|
backend.boundsOfCell(x, y))
|
|
|
|
}
|
2022-11-13 20:44:19 -07:00
|
|
|
|
|
|
|
if character < 32 { return }
|
2022-11-17 19:19:23 -07:00
|
|
|
|
2022-11-13 20:44:19 -07:00
|
|
|
origin := backend.originOfCell(x, y + 1)
|
2022-11-17 19:19:23 -07:00
|
|
|
destinationRectangle, mask, _, _, ok := backend.font.face.Glyph (
|
|
|
|
fixed.Point26_6 { },
|
2022-11-13 20:44:19 -07:00
|
|
|
character)
|
2022-11-14 21:32:05 -07:00
|
|
|
|
2022-11-16 22:26:28 -07:00
|
|
|
if !ok {
|
|
|
|
strokeRectangle (
|
|
|
|
&image.Uniform {
|
|
|
|
C: backend.config.Color(stone.ColorForeground),
|
|
|
|
},
|
|
|
|
backend.canvas,
|
|
|
|
backend.boundsOfCell(x, y))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-17 19:19:23 -07:00
|
|
|
if backend.drawCellBounds {
|
2022-11-14 21:32:05 -07:00
|
|
|
strokeRectangle (
|
|
|
|
&image.Uniform {
|
|
|
|
C: backend.config.Color(stone.ColorForeground),
|
|
|
|
},
|
|
|
|
backend.canvas,
|
|
|
|
backend.boundsOfCell(x, y))
|
|
|
|
}
|
2022-11-17 19:19:23 -07:00
|
|
|
|
|
|
|
origin.Y += destinationRectangle.Min.Y - backend.metrics.descent
|
|
|
|
backend.sprayRuneMask(mask, origin, backend.colors[runeColor])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (backend *Backend) sprayRuneMask (
|
|
|
|
mask image.Image,
|
|
|
|
at image.Point,
|
|
|
|
fill xgraphics.BGRA,
|
|
|
|
) {
|
|
|
|
bounds := mask.Bounds().Max
|
|
|
|
for y := 0; y < bounds.Y; y ++ {
|
|
|
|
for x := 0; x < bounds.X; x ++ {
|
|
|
|
_, _, _, alpha := mask.At(x, y).RGBA()
|
|
|
|
backend.canvas.SetBGRA (
|
|
|
|
x + at.X,
|
|
|
|
y + at.Y,
|
|
|
|
xgraphics.BlendBGRA (
|
|
|
|
backend.colors[stone.ColorBackground],
|
|
|
|
xgraphics.BGRA {
|
|
|
|
R: fill.R,
|
|
|
|
G: fill.G,
|
|
|
|
B: fill.B,
|
|
|
|
A: uint8(alpha >> 8),
|
|
|
|
}))
|
|
|
|
}}
|
2022-11-13 20:44:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func fillRectangle (
|
|
|
|
source image.Image,
|
|
|
|
destination draw.Image,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
) {
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
|
|
|
destination.Set(x, y, source.At(x, y))
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strokeRectangle (
|
|
|
|
source image.Image,
|
|
|
|
destination draw.Image,
|
|
|
|
bounds image.Rectangle,
|
|
|
|
) {
|
|
|
|
x := 0
|
|
|
|
y := bounds.Min.Y
|
|
|
|
for x = bounds.Min.X; x < bounds.Max.X; x ++ {
|
|
|
|
destination.Set(x, y, source.At(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
y = bounds.Max.Y - 1
|
|
|
|
for x = bounds.Min.X; x < bounds.Max.X; x ++ {
|
|
|
|
destination.Set(x, y, source.At(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
x = bounds.Min.X
|
|
|
|
for y = bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
|
|
|
destination.Set(x, y, source.At(x, y))
|
|
|
|
}
|
|
|
|
|
|
|
|
x = bounds.Max.X - 1
|
|
|
|
for y = bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
|
|
|
destination.Set(x, y, source.At(x, y))
|
|
|
|
}
|
|
|
|
}
|