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/restorable"
"github.com/hajimehoshi/ebiten/internal/ui"
)
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)
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.offscreen = offscreen

View File

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

View File

@ -357,9 +357,11 @@ func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
}
type newScreenFramebufferImageCommand struct {
result *Image
width int
height int
result *Image
width int
height int
offsetX float64
offsetY float64
}
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
@ -370,8 +372,10 @@ func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
return errors.New("graphics: height must be equal or more than 1.")
}
f := &framebuffer{
native: opengl.GetContext().ScreenFramebuffer(),
flipY: true,
native: opengl.GetContext().ScreenFramebuffer(),
flipY: true,
offsetX: c.offsetX,
offsetY: c.offsetY,
}
c.result.framebuffer = f
return nil

View File

@ -36,6 +36,8 @@ type framebuffer struct {
native opengl.Framebuffer
flipY bool
proMatrix []float32
offsetX float64
offsetY float64
}
func newFramebufferFromTexture(texture *texture) (*framebuffer, error) {
@ -65,6 +67,8 @@ func (f *framebuffer) projectionMatrix(height int) []float32 {
m[4*1+1] *= -1
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
return f.proMatrix
}

View File

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

View File

@ -52,6 +52,9 @@ type Image struct {
// screen indicates whether the image is used as an actual screen.
screen bool
offsetX float64
offsetY float64
}
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
}
func NewScreenFramebufferImage(width, height int) *Image {
func NewScreenFramebufferImage(width, height int, offsetX, offsetY float64) *Image {
i := &Image{
image: graphics.NewScreenFramebufferImage(width, height),
image: graphics.NewScreenFramebufferImage(width, height, offsetX, offsetY),
volatile: true,
screen: true,
offsetX: offsetX,
offsetY: offsetY,
}
theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
@ -249,7 +254,7 @@ func (p *Image) restore() error {
if p.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
p.image = graphics.NewScreenFramebufferImage(w, h)
p.image = graphics.NewScreenFramebufferImage(w, h, p.offsetX, p.offsetY)
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil

View File

@ -194,6 +194,26 @@ func IsFullscreen() bool {
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) {
// This can be called before Run: change the state asyncly.
go func() {

View File

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

View File

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