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) w := int(float64(screenWidth) * screenScale)
h := int(float64(screenHeight) * screenScale) h := int(float64(screenHeight) * screenScale)
ox, oy := ui.ScreenOffset() px0, py0, px1, py1 := ui.ScreenPadding()
c.screen = newImageWithScreenFramebuffer(w, h, ox, oy) c.screen = newImageWithScreenFramebuffer(w, h, px0, py0, px1, py1)
_ = c.screen.Clear() _ = c.screen.Clear()
c.offscreen = offscreen c.offscreen = offscreen

View File

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

View File

@ -80,8 +80,10 @@ type Image struct {
// screen indicates whether the image is used as an actual screen. // screen indicates whether the image is used as an actual screen.
screen bool screen bool
offsetX float64 paddingX0 float64
offsetY float64 paddingY0 float64
paddingX1 float64
paddingY1 float64
} }
// NewImage creates an empty image with the given size. // 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. // 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{ i := &Image{
image: graphics.NewScreenFramebufferImage(width, height, offsetX, offsetY), image: graphics.NewScreenFramebufferImage(width, height, paddingX0, paddingY0),
volatile: true, volatile: true,
screen: true, screen: true,
offsetX: offsetX, paddingX0: paddingX0,
offsetY: offsetY, paddingY0: paddingY0,
paddingX1: paddingX1,
paddingY1: paddingY1,
} }
theImages.add(i) theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
@ -167,12 +171,10 @@ func (i *Image) clearIfVolatile() {
} }
w, h := i.image.Size() w, h := i.image.Size()
x0 := -float32(i.offsetX) x0 := -float32(i.paddingX0)
y0 := -float32(i.offsetY) y0 := -float32(i.paddingY0)
// TODO: This is an ad-hoc way to fix the problem #513. x1 := float32(w) + float32(i.paddingX1)
// Fix this for better calculation. y1 := float32(h) + float32(i.paddingY1)
x1 := float32(w) + float32(i.offsetX)
y1 := float32(h) + float32(i.offsetY)
// For the rule of values, see vertices.go. // For the rule of values, see vertices.go.
clearVertices := []float32{ clearVertices := []float32{
x0, y0, 0, 0, 1, 1, x0, y0, 0, 0, 1, 1,
@ -320,7 +322,7 @@ func (i *Image) restore() error {
if i.screen { if i.screen {
// The screen image should also be recreated because framebuffer might // The screen image should also be recreated because framebuffer might
// be changed. // 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.basePixels = nil
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false 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 u := currentUI
if !u.isRunning() { if !u.isRunning() {
return 0, 0 return 0, 0, 0, 0
} }
if !IsFullscreen() { if !IsFullscreen() {
if u.width == u.windowWidth { 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). // 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 ox := 0.0
oy := 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 oy = (float64(v.Height)*d/glfwScale() - float64(u.height)*u.actualScreenScale()) / 2
return nil return nil
}) })
return ox, oy return ox, oy, ox, oy
} }
func adjustCursorPosition(x, y int) (int, int) { func adjustCursorPosition(x, y int) (int, int) {
@ -330,7 +331,7 @@ func adjustCursorPosition(x, y int) (int, int) {
if !u.isRunning() { if !u.isRunning() {
return x, y return x, y
} }
ox, oy := ScreenOffset() ox, oy, _, _ := ScreenPadding()
s := 0.0 s := 0.0
_ = currentUI.runOnMainThread(func() error { _ = currentUI.runOnMainThread(func() error {
s = currentUI.actualScreenScale() s = currentUI.actualScreenScale()

View File

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

View File

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