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
|
|
|
)
|
|
|
|
|
|
|
|
type rectToPixels struct {
|
|
|
|
m map[image.Rectangle][]byte
|
|
|
|
|
2019-10-11 18:10:53 +02:00
|
|
|
lastR image.Rectangle
|
|
|
|
lastPix []byte
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *rectToPixels) addOrReplace(pixels []byte, x, y, width, height int) {
|
|
|
|
if len(pixels) != 4*width*height {
|
2020-12-05 11:56:00 +01:00
|
|
|
msg := fmt.Sprintf("restorable: len(pixels) must be 4*%d*%d = %d but %d", width, height, 4*width*height, len(pixels))
|
|
|
|
if pixels == nil {
|
|
|
|
msg += " (nil)"
|
|
|
|
}
|
|
|
|
panic(msg)
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if rtp.m == nil {
|
|
|
|
rtp.m = map[image.Rectangle][]byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
newr := image.Rect(x, y, x+width, y+height)
|
|
|
|
for r := range rtp.m {
|
|
|
|
if r == newr {
|
|
|
|
// Replace the region.
|
2019-11-15 18:10:51 +01:00
|
|
|
rtp.m[r] = pixels
|
2019-10-11 18:10:53 +02:00
|
|
|
if r == rtp.lastR {
|
2019-11-15 18:10:51 +01:00
|
|
|
rtp.lastPix = pixels
|
2019-10-03 19:02:33 +02:00
|
|
|
}
|
2019-07-17 16:03:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Overlaps(newr) {
|
|
|
|
panic(fmt.Sprintf("restorable: region (%#v) conflicted with the other region (%#v)", newr, r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the region.
|
2019-11-15 18:10:51 +01:00
|
|
|
rtp.m[newr] = pixels
|
2019-10-11 18:10:53 +02:00
|
|
|
if newr == rtp.lastR {
|
2019-11-15 18:10:51 +01:00
|
|
|
rtp.lastPix = pixels
|
2019-10-03 19:02:33 +02:00
|
|
|
}
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *rectToPixels) remove(x, y, width, height int) {
|
|
|
|
if rtp.m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newr := image.Rect(x, y, x+width, y+height)
|
|
|
|
for r := range rtp.m {
|
|
|
|
if r == newr {
|
|
|
|
delete(rtp.m, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *rectToPixels) at(i, j int) (byte, byte, byte, byte, bool) {
|
|
|
|
if rtp.m == nil {
|
|
|
|
return 0, 0, 0, 0, false
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:02:33 +02:00
|
|
|
var pix []byte
|
|
|
|
|
2019-07-17 16:03:14 +02:00
|
|
|
var r *image.Rectangle
|
2019-10-11 18:10:53 +02:00
|
|
|
if pt := image.Pt(i, j); pt.In(rtp.lastR) {
|
|
|
|
r = &rtp.lastR
|
|
|
|
pix = rtp.lastPix
|
2019-07-17 16:03:14 +02:00
|
|
|
} else {
|
|
|
|
for rr := range rtp.m {
|
|
|
|
if pt.In(rr) {
|
|
|
|
r = &rr
|
2019-10-11 18:10:53 +02:00
|
|
|
rtp.lastR = rr
|
2019-10-03 19:02:33 +02:00
|
|
|
pix = rtp.m[rr]
|
2019-10-11 18:10:53 +02:00
|
|
|
rtp.lastPix = pix
|
2019-07-17 16:03:14 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
return 0, 0, 0, 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := 4 * ((j-r.Min.Y)*r.Dx() + (i - r.Min.X))
|
|
|
|
return pix[idx], pix[idx+1], pix[idx+2], pix[idx+3], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rtp *rectToPixels) apply(img *graphicscommand.Image) {
|
|
|
|
// TODO: Isn't this too heavy? Can we merge the operations?
|
|
|
|
for r, pix := range rtp.m {
|
|
|
|
img.ReplacePixels(pix, r.Min.X, r.Min.Y, r.Dx(), r.Dy())
|
|
|
|
}
|
|
|
|
}
|