2016-07-16 21:41:28 +02:00
|
|
|
// Copyright 2016 The Ebiten Authors
|
2016-07-13 19:35:20 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-09-03 15:05:05 +02:00
|
|
|
package restorable
|
2016-07-13 19:35:20 +02:00
|
|
|
|
|
|
|
import (
|
2018-02-28 16:27:55 +01:00
|
|
|
"fmt"
|
2021-07-29 09:09:25 +02:00
|
|
|
"image"
|
2016-07-13 19:35:20 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2016-07-13 19:35:20 +02:00
|
|
|
)
|
|
|
|
|
2022-06-06 02:12:57 +02:00
|
|
|
type ImageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ImageTypeRegular indicates the image is a regular image.
|
|
|
|
ImageTypeRegular ImageType = iota
|
|
|
|
|
2022-06-07 16:45:35 +02:00
|
|
|
// ImageTypeScreen indicates the image is used as an actual screen.
|
|
|
|
ImageTypeScreen
|
2022-06-06 02:12:57 +02:00
|
|
|
|
|
|
|
// ImageTypeVolatile indicates the image is cleared whenever a frame starts.
|
|
|
|
ImageTypeVolatile
|
|
|
|
)
|
|
|
|
|
2024-01-08 18:44:20 +01:00
|
|
|
// Image represents an image.
|
2016-09-03 15:05:05 +02:00
|
|
|
type Image struct {
|
2018-10-28 12:10:05 +01:00
|
|
|
image *graphicscommand.Image
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2019-08-15 19:28:59 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
|
2022-06-06 02:12:57 +02:00
|
|
|
imageType ImageType
|
2018-12-16 14:24:19 +01:00
|
|
|
}
|
2018-04-05 04:15:24 +02:00
|
|
|
|
2023-08-31 16:31:42 +02:00
|
|
|
// NewImage creates an emtpy image with the given size.
|
2018-12-01 20:28:38 +01:00
|
|
|
//
|
|
|
|
// The returned image is cleared.
|
2018-04-27 05:08:59 +02:00
|
|
|
//
|
|
|
|
// Note that Dispose is not called automatically.
|
2022-06-06 02:29:30 +02:00
|
|
|
func NewImage(width, height int, imageType ImageType) *Image {
|
2021-09-08 20:58:58 +02:00
|
|
|
if !graphicsDriverInitialized {
|
|
|
|
panic("restorable: graphics driver must be ready at NewImage but not")
|
|
|
|
}
|
|
|
|
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2022-06-07 16:45:35 +02:00
|
|
|
image: graphicscommand.NewImage(width, height, imageType == ImageTypeScreen),
|
2022-06-06 02:29:30 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
imageType: imageType,
|
2017-03-03 15:51:25 +01:00
|
|
|
}
|
2023-08-31 16:31:42 +02:00
|
|
|
|
|
|
|
// This needs to use 'InternalSize' to render the whole region, or edges are unexpectedly cleared on some
|
|
|
|
// devices.
|
|
|
|
iw, ih := i.image.InternalSize()
|
|
|
|
clearImage(i.image, image.Rect(0, 0, iw, ih))
|
2017-05-03 16:15:18 +02:00
|
|
|
theImages.add(i)
|
2018-04-05 17:36:15 +02:00
|
|
|
return i
|
|
|
|
}
|
2018-04-05 04:15:24 +02:00
|
|
|
|
2019-07-16 20:32:08 +02:00
|
|
|
// Extend extends the image by the given size.
|
|
|
|
// Extend creates a new image with the given size and copies the pixels of the given source image.
|
|
|
|
// Extend disposes itself after its call.
|
|
|
|
func (i *Image) Extend(width, height int) *Image {
|
2022-11-11 13:15:28 +01:00
|
|
|
if i.width >= width && i.height >= height {
|
|
|
|
return i
|
2019-07-16 20:32:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
newImg := NewImage(width, height, i.imageType)
|
2019-07-14 19:00:26 +02:00
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels
|
2020-08-15 08:11:56 +02:00
|
|
|
// information.
|
2022-07-12 18:46:02 +02:00
|
|
|
srcs := [graphics.ShaderImageCount]*Image{i}
|
2020-08-15 08:11:56 +02:00
|
|
|
sw, sh := i.image.InternalSize()
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1)
|
2020-08-15 08:11:56 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, sw, sh)
|
2023-11-04 10:09:47 +01:00
|
|
|
newImg.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll)
|
2019-07-16 20:32:08 +02:00
|
|
|
i.Dispose()
|
|
|
|
|
|
|
|
return newImg
|
2019-07-14 19:00:26 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:55:18 +02:00
|
|
|
// quadVertices returns vertices to render a quad. These values are passed to graphicscommand.Image.
|
2023-04-16 11:56:14 +02:00
|
|
|
func quadVertices(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, cr, cg, cb, ca float32) []float32 {
|
2019-09-19 18:55:18 +02:00
|
|
|
return []float32{
|
2023-04-16 11:56:14 +02:00
|
|
|
dx0, dy0, sx0, sy0, cr, cg, cb, ca,
|
|
|
|
dx1, dy0, sx1, sy0, cr, cg, cb, ca,
|
|
|
|
dx0, dy1, sx0, sy1, cr, cg, cb, ca,
|
|
|
|
dx1, dy1, sx1, sy1, cr, cg, cb, ca,
|
2019-09-19 18:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:31:42 +02:00
|
|
|
func clearImage(i *graphicscommand.Image, region image.Rectangle) {
|
|
|
|
vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0)
|
2019-08-24 19:39:59 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-11-04 10:09:47 +01:00
|
|
|
i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.shader, nil, graphicsdriver.FillAll)
|
2019-08-24 19:39:59 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// ClearPixels clears the specified region by WritePixels.
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) ClearPixels(region image.Rectangle) {
|
|
|
|
i.WritePixels(nil, region)
|
2019-07-09 19:36:59 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
// WritePixels replaces the image pixels with the given pixels slice.
|
2019-07-16 20:15:01 +02:00
|
|
|
//
|
2022-08-07 20:16:23 +02:00
|
|
|
// The specified region must not be overlapped with other regions by WritePixels.
|
2023-10-08 15:59:35 +02:00
|
|
|
func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangle) {
|
2023-04-27 17:12:42 +02:00
|
|
|
if region.Dx() <= 0 || region.Dy() <= 0 {
|
2018-02-28 16:27:55 +01:00
|
|
|
panic("restorable: width/height must be positive")
|
|
|
|
}
|
2019-09-20 22:40:05 +02:00
|
|
|
w, h := i.width, i.height
|
2023-04-27 17:12:42 +02:00
|
|
|
if !region.In(image.Rect(0, 0, w, h)) {
|
|
|
|
panic(fmt.Sprintf("restorable: out of range %v", region))
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
2018-02-28 18:49:45 +01:00
|
|
|
|
2019-07-17 16:03:14 +02:00
|
|
|
if pixels != nil {
|
2023-04-27 17:00:51 +02:00
|
|
|
i.image.WritePixels(pixels, region)
|
2019-07-17 16:03:14 +02:00
|
|
|
} else {
|
2023-08-31 16:31:42 +02:00
|
|
|
clearImage(i.image, region)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2016-07-26 03:51:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 18:55:18 +02:00
|
|
|
// DrawTriangles draws triangles with the given image.
|
|
|
|
//
|
|
|
|
// The vertex floats are:
|
|
|
|
//
|
2022-08-03 13:48:02 +02:00
|
|
|
// 0: Destination X in pixels
|
|
|
|
// 1: Destination Y in pixels
|
|
|
|
// 2: Source X in texels
|
|
|
|
// 3: Source Y in texels
|
|
|
|
// 4: Color R [0.0-1.0]
|
|
|
|
// 5: Color G
|
|
|
|
// 6: Color B
|
|
|
|
// 7: Color Y
|
2023-11-04 10:09:47 +01:00
|
|
|
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) {
|
2018-06-10 15:59:00 +02:00
|
|
|
if len(vertices) == 0 {
|
2018-02-26 16:05:11 +01:00
|
|
|
return
|
|
|
|
}
|
2020-07-05 18:55:46 +02:00
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*graphicscommand.Image
|
2022-10-02 16:49:07 +02:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
2020-07-17 18:09:58 +02:00
|
|
|
}
|
2022-10-02 16:49:07 +02:00
|
|
|
imgs[i] = src.image
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
2023-11-04 10:09:47 +01:00
|
|
|
i.image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (i *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, region image.Rectangle) error {
|
2024-01-08 18:43:02 +01:00
|
|
|
if err := i.image.ReadPixels(graphicsDriver, []graphicsdriver.PixelsArgs{
|
|
|
|
{
|
|
|
|
Pixels: pixels,
|
|
|
|
Region: region,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
return err
|
2020-01-18 17:18:56 +01:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
return nil
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 17:24:18 +02:00
|
|
|
// Dispose disposes the image.
|
|
|
|
//
|
2017-09-14 20:13:36 +02:00
|
|
|
// After disposing, calling the function of the image causes unexpected results.
|
2017-09-14 17:57:52 +02:00
|
|
|
func (i *Image) Dispose() {
|
2018-03-25 11:21:43 +02:00
|
|
|
theImages.remove(i)
|
2017-09-14 17:57:52 +02:00
|
|
|
i.image.Dispose()
|
|
|
|
i.image = nil
|
2016-07-13 19:35:20 +02:00
|
|
|
}
|
2016-09-03 17:07:06 +02:00
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (i *Image) Dump(graphicsDriver graphicsdriver.Graphics, path string, blackbg bool, rect image.Rectangle) (string, error) {
|
2022-03-19 15:55:14 +01:00
|
|
|
return i.image.Dump(graphicsDriver, path, blackbg, rect)
|
2019-07-19 18:42:19 +02:00
|
|
|
}
|
2021-09-09 05:57:47 +02:00
|
|
|
|
2022-04-01 17:32:16 +02:00
|
|
|
func (i *Image) InternalSize() (int, int) {
|
|
|
|
return i.image.InternalSize()
|
|
|
|
}
|