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
|
|
|
)
|
|
|
|
|
2024-01-08 18:44:20 +01:00
|
|
|
// Image represents an image.
|
2016-09-03 15:05:05 +02:00
|
|
|
type Image struct {
|
2024-01-08 20:50:19 +01:00
|
|
|
// Image is the underlying image.
|
|
|
|
// This member is exported on purpose.
|
|
|
|
// TODO: Move the implementation to internal/atlas package (#805).
|
|
|
|
Image *graphicscommand.Image
|
2016-09-03 16:08:51 +02:00
|
|
|
|
2019-08-15 19:28:59 +02:00
|
|
|
width int
|
|
|
|
height int
|
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.
|
2024-01-13 10:03:36 +01:00
|
|
|
func NewImage(width, height int, screen bool) *Image {
|
2017-05-03 16:15:18 +02:00
|
|
|
i := &Image{
|
2024-01-13 10:03:36 +01:00
|
|
|
Image: graphicscommand.NewImage(width, height, screen),
|
|
|
|
width: width,
|
|
|
|
height: height,
|
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.
|
2024-01-08 20:50:19 +01:00
|
|
|
iw, ih := i.Image.InternalSize()
|
|
|
|
clearImage(i.Image, image.Rect(0, 0, iw, ih))
|
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
|
|
|
}
|
|
|
|
|
2024-01-13 10:03:36 +01:00
|
|
|
// Assume that the screen image is never extended.
|
|
|
|
newImg := NewImage(width, height, false)
|
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}
|
2024-01-08 20:50:19 +01: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)
|
2024-01-13 10:04:48 +01:00
|
|
|
i.Image.Dispose()
|
|
|
|
i.Image = nil
|
2019-07-16 20:32:08 +02:00
|
|
|
|
|
|
|
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()
|
2024-01-13 10:38:36 +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) {
|
2024-01-13 10:15:24 +01:00
|
|
|
if region.Dx() <= 0 || region.Dy() <= 0 {
|
|
|
|
panic("restorable: width/height must be positive")
|
|
|
|
}
|
|
|
|
clearImage(i.Image, region.Intersect(image.Rect(0, 0, i.width, i.height)))
|
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
|
|
|
|
2024-01-13 10:15:24 +01:00
|
|
|
i.Image.WritePixels(pixels, region)
|
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
|
|
|
}
|
2024-01-08 20:50:19 +01:00
|
|
|
imgs[i] = src.Image
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
2024-01-13 10:38:36 +01:00
|
|
|
i.Image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.Shader, uniforms, fillRule)
|
2016-09-03 16:42:44 +02:00
|
|
|
}
|