2019-07-17 16:03:14 +02:00
|
|
|
// Copyright 2019 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 restorable
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
|
2019-07-17 16:03:14 +02:00
|
|
|
)
|
|
|
|
|
2022-03-20 15:00:00 +01:00
|
|
|
type pixelsRecord struct {
|
|
|
|
rect image.Rectangle
|
|
|
|
pix []byte
|
|
|
|
}
|
|
|
|
|
2022-03-20 17:26:48 +01:00
|
|
|
func (p *pixelsRecord) clearIfOverlapped(rect image.Rectangle) {
|
2022-03-20 15:00:00 +01:00
|
|
|
r := p.rect.Intersect(rect)
|
|
|
|
ox := r.Min.X - p.rect.Min.X
|
|
|
|
oy := r.Min.Y - p.rect.Min.Y
|
|
|
|
w := p.rect.Dx()
|
|
|
|
for j := 0; j < r.Dy(); j++ {
|
|
|
|
for i := 0; i < r.Dx(); i++ {
|
|
|
|
idx := 4 * ((oy+j)*w + ox + i)
|
|
|
|
p.pix[idx] = 0
|
|
|
|
p.pix[idx+1] = 0
|
|
|
|
p.pix[idx+2] = 0
|
|
|
|
p.pix[idx+3] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (p *pixelsRecord) readPixels(pixels []byte, region image.Rectangle, imageWidth, imageHeight int) {
|
|
|
|
r := p.rect.Intersect(region.Intersect(image.Rect(0, 0, imageWidth, imageHeight)))
|
2022-08-05 14:40:38 +02:00
|
|
|
if r.Empty() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
dstBaseX := r.Min.X - region.Min.X
|
|
|
|
dstBaseY := r.Min.Y - region.Min.Y
|
2022-08-05 14:40:38 +02:00
|
|
|
srcBaseX := r.Min.X - p.rect.Min.X
|
|
|
|
srcBaseY := r.Min.Y - p.rect.Min.Y
|
|
|
|
lineWidth := 4 * r.Dx()
|
|
|
|
for j := 0; j < r.Dy(); j++ {
|
2023-04-27 17:12:42 +02:00
|
|
|
dstX := 4 * ((dstBaseY+j)*region.Dx() + dstBaseX)
|
2022-08-05 14:40:38 +02:00
|
|
|
srcX := 4 * ((srcBaseY+j)*p.rect.Dx() + srcBaseX)
|
|
|
|
copy(pixels[dstX:dstX+lineWidth], p.pix[srcX:srcX+lineWidth])
|
2022-03-20 17:26:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 15:00:00 +01:00
|
|
|
type pixelsRecords struct {
|
|
|
|
records []*pixelsRecord
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (pr *pixelsRecords) addOrReplace(pixels []byte, region image.Rectangle) {
|
|
|
|
if len(pixels) != 4*region.Dx()*region.Dy() {
|
|
|
|
msg := fmt.Sprintf("restorable: len(pixels) must be 4*%d*%d = %d but %d", region.Dx(), region.Dy(), 4*region.Dx()*region.Dy(), len(pixels))
|
2020-12-05 11:56:00 +01:00
|
|
|
if pixels == nil {
|
|
|
|
msg += " (nil)"
|
|
|
|
}
|
|
|
|
panic(msg)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 18:08:37 +01:00
|
|
|
// Remove or update the duplicated records first.
|
2022-03-20 15:00:00 +01:00
|
|
|
var n int
|
|
|
|
for _, r := range pr.records {
|
2023-04-27 17:12:42 +02:00
|
|
|
if r.rect.In(region) {
|
2022-03-20 15:00:00 +01:00
|
|
|
continue
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
pr.records[n] = r
|
|
|
|
n++
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
for i := n; i < len(pr.records); i++ {
|
|
|
|
pr.records[i] = nil
|
2019-10-03 19:02:33 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
pr.records = pr.records[:n]
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2022-03-20 15:00:00 +01:00
|
|
|
// Add the new record.
|
|
|
|
pr.records = append(pr.records, &pixelsRecord{
|
2023-04-27 17:12:42 +02:00
|
|
|
rect: region,
|
2022-03-20 15:00:00 +01:00
|
|
|
pix: pixels,
|
|
|
|
})
|
|
|
|
}
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (pr *pixelsRecords) clear(region image.Rectangle) {
|
2022-03-20 15:00:00 +01:00
|
|
|
var n int
|
|
|
|
for _, r := range pr.records {
|
2023-04-27 17:12:42 +02:00
|
|
|
if r.rect.In(region) {
|
2022-03-20 15:00:00 +01:00
|
|
|
continue
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
r.clearIfOverlapped(region)
|
2022-03-20 15:00:00 +01:00
|
|
|
pr.records[n] = r
|
|
|
|
n++
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
for i := n; i < len(pr.records); i++ {
|
|
|
|
pr.records[i] = nil
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
pr.records = pr.records[:n]
|
|
|
|
}
|
2019-07-17 16:03:14 +02:00
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
func (pr *pixelsRecords) readPixels(pixels []byte, region image.Rectangle, imageWidth, imageHeight int) {
|
2022-08-05 14:40:38 +02:00
|
|
|
for i := range pixels {
|
|
|
|
pixels[i] = 0
|
|
|
|
}
|
|
|
|
for _, r := range pr.records {
|
2023-04-27 17:12:42 +02:00
|
|
|
r.readPixels(pixels, region, imageWidth, imageHeight)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 15:00:00 +01:00
|
|
|
func (pr *pixelsRecords) apply(img *graphicscommand.Image) {
|
2019-07-17 16:03:14 +02:00
|
|
|
// TODO: Isn't this too heavy? Can we merge the operations?
|
2022-03-20 15:00:00 +01:00
|
|
|
for _, r := range pr.records {
|
2023-04-27 17:00:51 +02:00
|
|
|
img.WritePixels(r.pix, r.rect)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-27 14:22:31 +02:00
|
|
|
|
2023-02-24 13:49:33 +01:00
|
|
|
func (pr *pixelsRecords) appendRegions(regions []image.Rectangle) []image.Rectangle {
|
2022-08-27 14:22:31 +02:00
|
|
|
for _, r := range pr.records {
|
2023-02-24 13:49:33 +01:00
|
|
|
if r.rect.Empty() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
regions = append(regions, r.rect)
|
2022-08-27 14:22:31 +02:00
|
|
|
}
|
2023-02-24 13:49:33 +01:00
|
|
|
return regions
|
2022-08-27 14:22:31 +02:00
|
|
|
}
|