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 (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/affine"
|
|
|
|
"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-03-19 17:24:47 +01:00
|
|
|
type Image struct {
|
|
|
|
mipmap *mipmap.Mipmap
|
2022-04-01 11:16:03 +02:00
|
|
|
width int
|
|
|
|
height int
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImage(width, height int) *Image {
|
|
|
|
return &Image{
|
|
|
|
mipmap: mipmap.New(width, height),
|
2022-04-01 11:16:03 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func newScreenFramebufferImage(width, height int) *Image {
|
2022-03-19 17:24:47 +01:00
|
|
|
return &Image{
|
|
|
|
mipmap: mipmap.NewScreenFramebufferMipmap(width, height),
|
2022-04-01 11:16:03 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
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-03-19 17:24:47 +01:00
|
|
|
i.mipmap.MarkDisposed()
|
|
|
|
i.mipmap = nil
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {
|
2022-03-19 17:24:47 +01:00
|
|
|
var srcMipmaps [graphics.ShaderImageNum]*mipmap.Mipmap
|
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
srcMipmaps[i] = src.mipmap
|
|
|
|
}
|
|
|
|
|
|
|
|
var s *mipmap.Shader
|
|
|
|
if shader != nil {
|
|
|
|
s = shader.shader
|
|
|
|
}
|
|
|
|
|
2022-03-21 09:48:47 +01:00
|
|
|
i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd, canSkipMipmap)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 07:19:06 +01:00
|
|
|
func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
|
|
|
|
i.mipmap.ReplacePixels(pix, x, y, width, height)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 10:38:29 +01:00
|
|
|
func (i *Image) At(x, y int) (r, g, b, a byte) {
|
2022-03-20 08:26:26 +01:00
|
|
|
// Check the error existence and avoid unnecessary calls.
|
|
|
|
if theGlobalState.error() != nil {
|
2022-03-20 10:38:29 +01:00
|
|
|
return 0, 0, 0, 0
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:47:13 +01:00
|
|
|
r, g, b, a, err := theUI.imageAt(i.mipmap, x, y)
|
2022-03-20 10:38:29 +01:00
|
|
|
if 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-20 10:38:29 +01:00
|
|
|
return 0, 0, 0, 0
|
2022-03-20 08:26:26 +01:00
|
|
|
}
|
2022-03-20 10:38:29 +01:00
|
|
|
return r, g, b, a
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) DumpScreenshot(name string, blackbg bool) error {
|
2022-03-21 14:47:13 +01:00
|
|
|
return theUI.dumpScreenshot(i.mipmap, name, blackbg)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
func DumpImages(dir string) error {
|
|
|
|
return theUI.dumpImages(dir)
|
2022-03-19 17:24:47 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 11:16:03 +02:00
|
|
|
var (
|
|
|
|
emptyImage = NewImage(3, 3)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pix := make([]byte, 4*emptyImage.width*emptyImage.height)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
|
|
|
// As emptyImage is used at Fill, use ReplacePixels instead.
|
|
|
|
emptyImage.ReplacePixels(pix, 0, 0, emptyImage.width, emptyImage.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),
|
|
|
|
}
|
|
|
|
|
|
|
|
vs := graphics.QuadVertices(
|
|
|
|
1, 1, float32(emptyImage.width-1), float32(emptyImage.height-1),
|
|
|
|
float32(i.width), 0, 0, float32(i.height), 0, 0,
|
|
|
|
r, g, b, a)
|
|
|
|
is := graphics.QuadIndices()
|
|
|
|
|
|
|
|
srcs := [graphics.ShaderImageNum]*Image{emptyImage}
|
|
|
|
|
|
|
|
i.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false, true)
|
2022-03-19 15:55:14 +01:00
|
|
|
}
|