ui: Center the screen on fullscreen mode (#374)

This commit is contained in:
Hajime Hoshi 2017-07-01 04:12:09 +09:00
parent 3168af1db4
commit c694851765
9 changed files with 60 additions and 15 deletions

View File

@ -19,6 +19,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/restorable" "github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/ui"
) )
func newGraphicsContext(f func(*Image) error) *graphicsContext { func newGraphicsContext(f func(*Image) error) *graphicsContext {
@ -64,7 +65,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)
c.screen = newImageWithScreenFramebuffer(w, h) ox, oy := ui.ScreenOffset()
c.screen = newImageWithScreenFramebuffer(w, h, ox, oy)
_ = c.screen.Clear() _ = c.screen.Clear()
c.offscreen = offscreen c.offscreen = offscreen

View File

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

View File

@ -360,6 +360,8 @@ type newScreenFramebufferImageCommand struct {
result *Image result *Image
width int width int
height int height int
offsetX float64
offsetY float64
} }
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error { func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
@ -372,6 +374,8 @@ func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
f := &framebuffer{ f := &framebuffer{
native: opengl.GetContext().ScreenFramebuffer(), native: opengl.GetContext().ScreenFramebuffer(),
flipY: true, flipY: true,
offsetX: c.offsetX,
offsetY: c.offsetY,
} }
c.result.framebuffer = f c.result.framebuffer = f
return nil return nil

View File

@ -36,6 +36,8 @@ type framebuffer struct {
native opengl.Framebuffer native opengl.Framebuffer
flipY bool flipY bool
proMatrix []float32 proMatrix []float32
offsetX float64
offsetY float64
} }
func newFramebufferFromTexture(texture *texture) (*framebuffer, error) { func newFramebufferFromTexture(texture *texture) (*framebuffer, error) {
@ -65,6 +67,8 @@ func (f *framebuffer) projectionMatrix(height int) []float32 {
m[4*1+1] *= -1 m[4*1+1] *= -1
m[4*3+1] += float32(height) / float32(viewportSize) * 2 m[4*3+1] += float32(height) / float32(viewportSize) * 2
} }
m[4*3+0] += float32(f.offsetX) / float32(viewportSize) * 2
m[4*3+1] += float32(f.offsetY) / float32(viewportSize) * 2
f.proMatrix = m f.proMatrix = m
return f.proMatrix return f.proMatrix
} }

View File

@ -107,7 +107,7 @@ func NewImageFromImage(img *image.RGBA, width, height int, filter opengl.Filter)
return i return i
} }
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int, offsetX, offsetY float64) *Image {
i := &Image{ i := &Image{
width: width, width: width,
height: height, height: height,
@ -116,6 +116,8 @@ func NewScreenFramebufferImage(width, height int) *Image {
result: i, result: i,
width: width, width: width,
height: height, height: height,
offsetX: offsetX,
offsetY: offsetY,
} }
theCommandQueue.Enqueue(c) theCommandQueue.Enqueue(c)
return i return i

View File

@ -52,6 +52,9 @@ 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
offsetY float64
} }
func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image { func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
@ -81,11 +84,13 @@ func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filt
return i return i
} }
func NewScreenFramebufferImage(width, height int) *Image { func NewScreenFramebufferImage(width, height int, offsetX, offsetY float64) *Image {
i := &Image{ i := &Image{
image: graphics.NewScreenFramebufferImage(width, height), image: graphics.NewScreenFramebufferImage(width, height, offsetX, offsetY),
volatile: true, volatile: true,
screen: true, screen: true,
offsetX: offsetX,
offsetY: offsetY,
} }
theImages.add(i) theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
@ -249,7 +254,7 @@ func (p *Image) restore() error {
if p.screen { if p.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.
p.image = graphics.NewScreenFramebufferImage(w, h) p.image = graphics.NewScreenFramebufferImage(w, h, p.offsetX, p.offsetY)
p.basePixels = nil p.basePixels = nil
p.baseColor = color.RGBA{} p.baseColor = color.RGBA{}
p.drawImageHistory = nil p.drawImageHistory = nil

View File

@ -194,6 +194,26 @@ func IsFullscreen() bool {
return f return f
} }
func ScreenOffset() (float64, float64) {
u := currentUI
if !u.isRunning() {
return 0, 0
}
if !IsFullscreen() {
return 0, 0
}
ox := 0.0
oy := 0.0
m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode()
_ = u.runOnMainThread(func() error {
ox = (float64(v.Width)*u.deviceScale/u.glfwScale - float64(u.width)*u.actualScreenScale()) / 2
oy = (float64(v.Height)*u.deviceScale/u.glfwScale - float64(u.height)*u.actualScreenScale()) / 2
return nil
})
return ox, oy
}
func SetCursorVisibility(visible bool) { func SetCursorVisibility(visible bool) {
// This can be called before Run: change the state asyncly. // This can be called before Run: change the state asyncly.
go func() { go func() {

View File

@ -66,6 +66,10 @@ func IsFullscreen() bool {
return false return false
} }
func ScreenOffset() (float64, float64) {
return 0, 0
}
func SetCursorVisibility(visibility bool) { func SetCursorVisibility(visibility bool) {
if visibility { if visibility {
canvas.Get("style").Set("cursor", "auto") canvas.Get("style").Set("cursor", "auto")

View File

@ -106,6 +106,10 @@ func ScreenScale() float64 {
return currentUI.scale return currentUI.scale
} }
func ScreenOffset() (float64, float64) {
return 0, 0
}
func SetCursorVisibility(visibility bool) { func SetCursorVisibility(visibility bool) {
// Do nothing // Do nothing
} }