2022-03-19 17:24:47 +01:00
|
|
|
// Copyright 2022 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2022-10-19 17:46:44 +02:00
|
|
|
"fmt"
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2022-03-19 17:24:47 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
|
|
|
)
|
|
|
|
|
2022-03-20 08:51:23 +01:00
|
|
|
// panicOnErrorOnReadingPixels indicates whether reading pixels panics on an error or not.
|
2022-03-20 08:26:26 +01:00
|
|
|
// This value is set only on testing.
|
2022-03-20 08:51:23 +01:00
|
|
|
var panicOnErrorOnReadingPixels bool
|
2022-03-20 08:26:26 +01:00
|
|
|
|
2022-03-20 08:51:23 +01:00
|
|
|
func SetPanicOnErrorOnReadingPixelsForTesting(value bool) {
|
|
|
|
panicOnErrorOnReadingPixels = value
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:11:13 +02:00
|
|
|
const bigOffscreenScale = 2
|
|
|
|
|
2022-03-19 17:24:47 +01:00
|
|
|
type Image struct {
|
2022-10-19 17:46:44 +02:00
|
|
|
mipmap *mipmap.Mipmap
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
imageType atlas.ImageType
|
2022-09-28 16:44:27 +02:00
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
dotsBuffer map[[2]int][4]byte
|
2022-10-19 17:46:44 +02:00
|
|
|
|
|
|
|
// bigOffscreenBuffer is a double-sized offscreen for anti-alias rendering.
|
|
|
|
bigOffscreenBuffer *Image
|
|
|
|
bigOffscreenBufferBlend graphicsdriver.Blend
|
|
|
|
bigOffscreenBufferDirty bool
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-10-28 12:07:33 +02:00
|
|
|
// drawCallback is a callback called when DrawTriangles or WritePixels is called.
|
|
|
|
// drawCallback is useful to detect whether the image is manipulated or not after a certain time.
|
|
|
|
drawCallback func()
|
2022-12-02 13:04:03 +01:00
|
|
|
|
|
|
|
// These temporary vertices must not be reused until the vertices are sent to the graphics command queue.
|
|
|
|
|
|
|
|
tmpVerticesForFlushing []float32
|
|
|
|
tmpVerticesForCopying []float32
|
|
|
|
tmpVerticesForFill []float32
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func NewImage(width, height int, imageType atlas.ImageType) *Image {
|
2022-03-19 17:24:47 +01:00
|
|
|
return &Image{
|
2022-10-19 17:46:44 +02:00
|
|
|
mipmap: mipmap.New(width, height, imageType),
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) MarkDisposed() {
|
2022-04-02 17:21:21 +02:00
|
|
|
if i.mipmap == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-19 17:46:44 +02:00
|
|
|
if i.bigOffscreenBuffer != nil {
|
|
|
|
i.bigOffscreenBuffer.MarkDisposed()
|
|
|
|
i.bigOffscreenBuffer = nil
|
|
|
|
i.bigOffscreenBufferDirty = false
|
|
|
|
}
|
2022-03-19 17:24:47 +01:00
|
|
|
i.mipmap.MarkDisposed()
|
|
|
|
i.mipmap = nil
|
2022-10-16 12:47:00 +02:00
|
|
|
i.dotsBuffer = nil
|
2022-10-28 12:07:33 +02:00
|
|
|
i.drawCallback = nil
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 11:02:38 +01:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms [][]uint32, evenOdd bool, canSkipMipmap bool, antialias bool) {
|
2022-10-28 12:07:33 +02:00
|
|
|
if i.drawCallback != nil {
|
|
|
|
i.drawCallback()
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
if antialias {
|
|
|
|
// Flush the other buffer to make the buffers exclusive.
|
|
|
|
i.flushDotsBufferIfNeeded()
|
|
|
|
|
|
|
|
if i.bigOffscreenBufferBlend != blend {
|
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.bigOffscreenBuffer == nil {
|
|
|
|
var imageType atlas.ImageType
|
|
|
|
switch i.imageType {
|
|
|
|
case atlas.ImageTypeRegular, atlas.ImageTypeUnmanaged:
|
|
|
|
imageType = atlas.ImageTypeUnmanaged
|
|
|
|
case atlas.ImageTypeScreen, atlas.ImageTypeVolatile:
|
|
|
|
imageType = atlas.ImageTypeVolatile
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("ui: unexpected image type: %d", imageType))
|
|
|
|
}
|
2022-10-20 20:11:13 +02:00
|
|
|
i.bigOffscreenBuffer = NewImage(i.width*bigOffscreenScale, i.height*bigOffscreenScale, imageType)
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
i.bigOffscreenBufferBlend = blend
|
|
|
|
|
|
|
|
// Copy the current rendering result to get the correct blending result.
|
|
|
|
if blend != graphicsdriver.BlendSourceOver && !i.bigOffscreenBufferDirty {
|
|
|
|
srcs := [graphics.ShaderImageCount]*Image{i}
|
2022-12-02 13:04:03 +01:00
|
|
|
if len(i.tmpVerticesForCopying) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForCopying = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForCopying,
|
2022-10-19 17:46:44 +02:00
|
|
|
0, 0, float32(i.width), float32(i.height),
|
2022-10-20 20:11:13 +02:00
|
|
|
bigOffscreenScale, 0, 0, bigOffscreenScale, 0, 0,
|
2022-10-19 17:46:44 +02:00
|
|
|
1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
dstRegion := graphicsdriver.Region{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
2022-10-20 20:11:13 +02:00
|
|
|
Width: float32(i.width * bigOffscreenScale),
|
|
|
|
Height: float32(i.height * bigOffscreenScale),
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
2022-12-02 13:04:03 +01:00
|
|
|
i.bigOffscreenBuffer.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true, false)
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
|
2022-10-20 20:11:13 +02:00
|
|
|
vertices[i] *= bigOffscreenScale
|
|
|
|
vertices[i+1] *= bigOffscreenScale
|
2022-10-19 17:46:44 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:11:13 +02:00
|
|
|
dstRegion.X *= bigOffscreenScale
|
|
|
|
dstRegion.Y *= bigOffscreenScale
|
|
|
|
dstRegion.Width *= bigOffscreenScale
|
|
|
|
dstRegion.Height *= bigOffscreenScale
|
2022-10-19 17:46:44 +02:00
|
|
|
|
|
|
|
i.bigOffscreenBuffer.DrawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd, canSkipMipmap, false)
|
|
|
|
i.bigOffscreenBufferDirty = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2022-09-28 16:44:27 +02:00
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var srcMipmaps [graphics.ShaderImageCount]*mipmap.Mipmap
|
2022-03-19 17:24:47 +01:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-16 12:47:00 +02:00
|
|
|
src.flushBufferIfNeeded()
|
2022-03-19 17:24:47 +01:00
|
|
|
srcMipmaps[i] = src.mipmap
|
|
|
|
}
|
|
|
|
|
2022-10-15 11:58:56 +02:00
|
|
|
i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, blend, dstRegion, srcRegion, subimageOffsets, shader.shader, uniforms, evenOdd, canSkipMipmap)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:31:36 +02:00
|
|
|
func (i *Image) WritePixels(pix []byte, x, y, width, height int) {
|
2022-10-28 12:07:33 +02:00
|
|
|
if i.drawCallback != nil {
|
|
|
|
i.drawCallback()
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-09-28 16:44:27 +02:00
|
|
|
if width == 1 && height == 1 {
|
2022-10-19 17:46:44 +02:00
|
|
|
// Flush the other buffer to make the buffers exclusive.
|
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
if i.dotsBuffer == nil {
|
|
|
|
i.dotsBuffer = map[[2]int][4]byte{}
|
2022-09-28 16:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var clr [4]byte
|
|
|
|
copy(clr[:], pix)
|
2022-10-16 12:47:00 +02:00
|
|
|
i.dotsBuffer[[2]int{x, y}] = clr
|
2022-09-28 16:44:27 +02:00
|
|
|
|
|
|
|
// One square requires 6 indices (= 2 triangles).
|
2022-10-16 12:47:00 +02:00
|
|
|
if len(i.dotsBuffer) >= graphics.IndicesCount/6 {
|
2022-10-19 17:46:44 +02:00
|
|
|
i.flushDotsBufferIfNeeded()
|
2022-09-28 16:44:27 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2022-08-07 20:30:37 +02:00
|
|
|
i.mipmap.WritePixels(pix, x, y, width, height)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 19:48:23 +02:00
|
|
|
func (i *Image) ReadPixels(pixels []byte, x, y, width, height int) {
|
2022-03-20 08:26:26 +01:00
|
|
|
// Check the error existence and avoid unnecessary calls.
|
|
|
|
if theGlobalState.error() != nil {
|
2022-08-07 19:48:23 +02:00
|
|
|
return
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
|
2022-09-28 16:44:27 +02:00
|
|
|
if width == 1 && height == 1 {
|
2022-10-16 12:47:00 +02:00
|
|
|
if c, ok := i.dotsBuffer[[2]int{x, y}]; ok {
|
2022-09-28 16:44:27 +02:00
|
|
|
copy(pixels, c[:])
|
|
|
|
return
|
|
|
|
}
|
2022-10-19 17:46:44 +02:00
|
|
|
// Do not call flushDotsBufferIfNeeded here. This would slow (image/draw).Draw.
|
2022-09-28 16:44:27 +02:00
|
|
|
// See ebiten.TestImageDrawOver.
|
|
|
|
} else {
|
2022-10-19 17:46:44 +02:00
|
|
|
i.flushDotsBufferIfNeeded()
|
2022-09-28 16:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 19:48:23 +02:00
|
|
|
if err := theUI.readPixels(i.mipmap, pixels, x, y, width, height); err != nil {
|
2022-03-20 08:51:23 +01:00
|
|
|
if panicOnErrorOnReadingPixels {
|
2022-03-20 08:26:26 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
theGlobalState.setError(err)
|
|
|
|
}
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) DumpScreenshot(name string, blackbg bool) (string, error) {
|
2022-10-22 07:19:24 +02:00
|
|
|
i.flushBufferIfNeeded()
|
2022-03-21 14:47:13 +01:00
|
|
|
return theUI.dumpScreenshot(i.mipmap, name, blackbg)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
func (i *Image) flushBufferIfNeeded() {
|
2022-10-19 17:46:44 +02:00
|
|
|
// The buffers are exclusive and the order should not matter.
|
|
|
|
i.flushDotsBufferIfNeeded()
|
|
|
|
i.flushBigOffscreenBufferIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) flushDotsBufferIfNeeded() {
|
2022-10-16 12:47:00 +02:00
|
|
|
if len(i.dotsBuffer) == 0 {
|
2022-09-28 16:44:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:47:00 +02:00
|
|
|
l := len(i.dotsBuffer)
|
2022-12-02 13:04:03 +01:00
|
|
|
vs := make([]float32, l*4*graphics.VertexFloatCount)
|
2022-09-28 16:44:27 +02:00
|
|
|
is := make([]uint16, l*6)
|
|
|
|
sx, sy := float32(1), float32(1)
|
|
|
|
var idx int
|
2022-10-16 12:47:00 +02:00
|
|
|
for p, c := range i.dotsBuffer {
|
2022-09-28 16:44:27 +02:00
|
|
|
dx := float32(p[0])
|
|
|
|
dy := float32(p[1])
|
2022-10-02 06:26:33 +02:00
|
|
|
crf := float32(c[0]) / 0xff
|
|
|
|
cgf := float32(c[1]) / 0xff
|
|
|
|
cbf := float32(c[2]) / 0xff
|
|
|
|
caf := float32(c[3]) / 0xff
|
2022-09-28 16:44:27 +02:00
|
|
|
|
|
|
|
vs[graphics.VertexFloatCount*4*idx] = dx
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+1] = dy
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+2] = sx
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+3] = sy
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+4] = crf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+5] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+6] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+7] = caf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+8] = dx + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+9] = dy
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+10] = sx + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+11] = sy
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+12] = crf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+13] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+14] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+15] = caf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+16] = dx
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+17] = dy + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+18] = sx
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+19] = sy + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+20] = crf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+21] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+22] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+23] = caf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+24] = dx + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+25] = dy + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+26] = sx + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+27] = sy + 1
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+28] = crf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+29] = cgf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+30] = cbf
|
|
|
|
vs[graphics.VertexFloatCount*4*idx+31] = caf
|
|
|
|
|
|
|
|
is[6*idx] = uint16(4 * idx)
|
|
|
|
is[6*idx+1] = uint16(4*idx + 1)
|
|
|
|
is[6*idx+2] = uint16(4*idx + 2)
|
|
|
|
is[6*idx+3] = uint16(4*idx + 1)
|
|
|
|
is[6*idx+4] = uint16(4*idx + 2)
|
|
|
|
is[6*idx+5] = uint16(4*idx + 3)
|
|
|
|
|
|
|
|
idx++
|
|
|
|
}
|
2022-10-16 12:47:00 +02:00
|
|
|
i.dotsBuffer = nil
|
2022-09-28 16:44:27 +02:00
|
|
|
|
2022-10-21 08:23:09 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*mipmap.Mipmap{whiteImage.mipmap}
|
2022-09-28 16:44:27 +02:00
|
|
|
dr := graphicsdriver.Region{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(i.width),
|
|
|
|
Height: float32(i.height),
|
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
i.mipmap.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader.shader, nil, false, true)
|
2022-09-28 16:44:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
func (i *Image) flushBigOffscreenBufferIfNeeded() {
|
|
|
|
if !i.bigOffscreenBufferDirty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the offscreen clearn earlier to avoid recursive calls.
|
|
|
|
i.bigOffscreenBufferDirty = false
|
|
|
|
|
|
|
|
srcs := [graphics.ShaderImageCount]*Image{i.bigOffscreenBuffer}
|
2022-12-02 13:04:03 +01:00
|
|
|
if len(i.tmpVerticesForFlushing) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForFlushing = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForFlushing,
|
2022-10-20 20:11:13 +02:00
|
|
|
0, 0, float32(i.width*bigOffscreenScale), float32(i.height*bigOffscreenScale),
|
|
|
|
1.0/bigOffscreenScale, 0, 0, 1.0/bigOffscreenScale, 0, 0,
|
2022-10-19 17:46:44 +02:00
|
|
|
1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
dstRegion := graphicsdriver.Region{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(i.width),
|
|
|
|
Height: float32(i.height),
|
|
|
|
}
|
|
|
|
blend := graphicsdriver.BlendSourceOver
|
|
|
|
if i.bigOffscreenBufferBlend != graphicsdriver.BlendSourceOver {
|
|
|
|
blend = graphicsdriver.BlendCopy
|
|
|
|
}
|
2022-12-02 13:04:03 +01:00
|
|
|
i.DrawTriangles(srcs, i.tmpVerticesForFlushing, is, blend, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, LinearFilterShader, nil, false, true, false)
|
2022-10-19 17:46:44 +02:00
|
|
|
|
|
|
|
i.bigOffscreenBuffer.clear()
|
|
|
|
i.bigOffscreenBufferDirty = false
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func DumpImages(dir string) (string, error) {
|
2022-04-01 11:16:03 +02:00
|
|
|
return theUI.dumpImages(dir)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
var (
|
2022-10-21 08:23:09 +02:00
|
|
|
whiteImage = NewImage(3, 3, atlas.ImageTypeRegular)
|
2022-04-01 11:16:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-10-21 08:23:09 +02:00
|
|
|
pix := make([]byte, 4*whiteImage.width*whiteImage.height)
|
2022-04-01 11:16:03 +02:00
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
2022-10-21 08:23:09 +02:00
|
|
|
// As whiteImage is used at Fill, use WritePixels instead.
|
|
|
|
whiteImage.WritePixels(pix, 0, 0, whiteImage.width, whiteImage.height)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func (i *Image) clear() {
|
|
|
|
i.Fill(0, 0, 0, 0, 0, 0, i.width, i.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Fill(r, g, b, a float32, x, y, width, height int) {
|
|
|
|
dstRegion := graphicsdriver.Region{
|
|
|
|
X: float32(x),
|
|
|
|
Y: float32(y),
|
|
|
|
Width: float32(width),
|
|
|
|
Height: float32(height),
|
|
|
|
}
|
|
|
|
|
2022-12-02 13:04:03 +01:00
|
|
|
if len(i.tmpVerticesForFill) < 4*graphics.VertexFloatCount {
|
|
|
|
i.tmpVerticesForFill = make([]float32, 4*graphics.VertexFloatCount)
|
|
|
|
}
|
|
|
|
graphics.QuadVertices(
|
|
|
|
i.tmpVerticesForFill,
|
2022-10-21 08:23:09 +02:00
|
|
|
1, 1, float32(whiteImage.width-1), float32(whiteImage.height-1),
|
2022-04-01 11:16:03 +02:00
|
|
|
float32(i.width), 0, 0, float32(i.height), 0, 0,
|
|
|
|
r, g, b, a)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
|
2022-10-21 08:23:09 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*Image{whiteImage}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2022-12-02 13:04:03 +01:00
|
|
|
i.DrawTriangles(srcs, i.tmpVerticesForFill, is, graphicsdriver.BlendCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true, false)
|
2022-03-19 15:55:14 +01:00
|
|
|
}
|