restorable: Better calculation to clear restorable.Image

This commit is contained in:
Hajime Hoshi 2018-02-24 21:16:30 +09:00
parent da20b3f49a
commit a644e92298
6 changed files with 32 additions and 29 deletions

View File

@ -55,8 +55,8 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
w := int(float64(screenWidth) * screenScale)
h := int(float64(screenHeight) * screenScale)
ox, oy := ui.ScreenOffset()
c.screen = newImageWithScreenFramebuffer(w, h, ox, oy)
px0, py0, px1, py1 := ui.ScreenPadding()
c.screen = newImageWithScreenFramebuffer(w, h, px0, py0, px1, py1)
_ = c.screen.Clear()
c.offscreen = offscreen

View File

@ -382,9 +382,9 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
return i, nil
}
func newImageWithScreenFramebuffer(width, height int, offsetX, offsetY float64) *Image {
func newImageWithScreenFramebuffer(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image {
checkSize(width, height)
r := restorable.NewScreenFramebufferImage(width, height, offsetX, offsetY)
r := restorable.NewScreenFramebufferImage(width, height, paddingX0, paddingY0, paddingX1, paddingY1)
i := &Image{r, FilterDefault}
runtime.SetFinalizer(i, (*Image).Dispose)
return i

View File

@ -80,8 +80,10 @@ type Image struct {
// screen indicates whether the image is used as an actual screen.
screen bool
offsetX float64
offsetY float64
paddingX0 float64
paddingY0 float64
paddingX1 float64
paddingY1 float64
}
// NewImage creates an empty image with the given size.
@ -115,13 +117,15 @@ func NewImageFromImage(source image.Image) *Image {
}
// NewScreenFramebufferImage creates a special image that framebuffer is one for the screen.
func NewScreenFramebufferImage(width, height int, offsetX, offsetY float64) *Image {
func NewScreenFramebufferImage(width, height int, paddingX0, paddingY0, paddingX1, paddingY1 float64) *Image {
i := &Image{
image: graphics.NewScreenFramebufferImage(width, height, offsetX, offsetY),
image: graphics.NewScreenFramebufferImage(width, height, paddingX0, paddingY0),
volatile: true,
screen: true,
offsetX: offsetX,
offsetY: offsetY,
paddingX0: paddingX0,
paddingY0: paddingY0,
paddingX1: paddingX1,
paddingY1: paddingY1,
}
theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
@ -167,12 +171,10 @@ func (i *Image) clearIfVolatile() {
}
w, h := i.image.Size()
x0 := -float32(i.offsetX)
y0 := -float32(i.offsetY)
// TODO: This is an ad-hoc way to fix the problem #513.
// Fix this for better calculation.
x1 := float32(w) + float32(i.offsetX)
y1 := float32(h) + float32(i.offsetY)
x0 := -float32(i.paddingX0)
y0 := -float32(i.paddingY0)
x1 := float32(w) + float32(i.paddingX1)
y1 := float32(h) + float32(i.paddingY1)
// For the rule of values, see vertices.go.
clearVertices := []float32{
x0, y0, 0, 0, 1, 1,
@ -320,7 +322,7 @@ func (i *Image) restore() error {
if i.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
i.image = graphics.NewScreenFramebufferImage(w, h, i.offsetX, i.offsetY)
i.image = graphics.NewScreenFramebufferImage(w, h, i.paddingX0, i.paddingY0)
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false

View File

@ -300,17 +300,18 @@ func SetWindowIcon(iconImages []image.Image) {
})
}
func ScreenOffset() (float64, float64) {
func ScreenPadding() (x0, y0, x1, y1 float64) {
u := currentUI
if !u.isRunning() {
return 0, 0
return 0, 0, 0, 0
}
if !IsFullscreen() {
if u.width == u.windowWidth {
return 0, 0
return 0, 0, 0, 0
}
// The window width can be bigger than the game screen width (#444).
return (float64(u.windowWidth)*u.actualScreenScale() - float64(u.width)*u.actualScreenScale()) / 2, 0
ox := (float64(u.windowWidth)*u.actualScreenScale() - float64(u.width)*u.actualScreenScale()) / 2
return ox, 0, ox, 0
}
ox := 0.0
oy := 0.0
@ -322,7 +323,7 @@ func ScreenOffset() (float64, float64) {
oy = (float64(v.Height)*d/glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
return nil
})
return ox, oy
return ox, oy, ox, oy
}
func adjustCursorPosition(x, y int) (int, int) {
@ -330,7 +331,7 @@ func adjustCursorPosition(x, y int) (int, int) {
if !u.isRunning() {
return x, y
}
ox, oy := ScreenOffset()
ox, oy, _, _ := ScreenPadding()
s := 0.0
_ = currentUI.runOnMainThread(func() error {
s = currentUI.actualScreenScale()

View File

@ -73,8 +73,8 @@ func IsRunnableInBackground() bool {
return currentUI.runnableInBackground
}
func ScreenOffset() (float64, float64) {
return 0, 0
func ScreenPadding() (x0, y0, x1, y1 float64) {
return 0, 0, 0, 0
}
func adjustCursorPosition(x, y int) (int, int) {

View File

@ -156,8 +156,8 @@ func ScreenScale() float64 {
return s
}
func ScreenOffset() (float64, float64) {
return 0, 0
func ScreenPadding() (x0, y0, x1, y1 float64) {
return 0, 0, 0, 0
}
func adjustCursorPosition(x, y int) (int, int) {