mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 03:58:55 +01:00
graphics: Better scaling with a real number (#236)
This commit is contained in:
parent
bb511b2c13
commit
5983e94cb8
@ -15,6 +15,8 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
@ -29,6 +31,7 @@ func newGraphicsContext(f func(*Image) error) *graphicsContext {
|
|||||||
type graphicsContext struct {
|
type graphicsContext struct {
|
||||||
f func(*Image) error
|
f func(*Image) error
|
||||||
offscreen *Image
|
offscreen *Image
|
||||||
|
offscreen2 *Image // TODO: better name
|
||||||
screen *Image
|
screen *Image
|
||||||
screenScale float64
|
screenScale float64
|
||||||
initialized bool
|
initialized bool
|
||||||
@ -45,14 +48,22 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w := int(float64(screenWidth) * screenScale)
|
intScreenScale := int(math.Floor(screenScale))
|
||||||
h := int(float64(screenHeight) * screenScale)
|
w := screenWidth * intScreenScale
|
||||||
|
h := screenHeight * intScreenScale
|
||||||
|
offscreen2, err := NewImage(w, h, FilterLinear)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w = int(float64(screenWidth) * screenScale)
|
||||||
|
h = int(float64(screenHeight) * screenScale)
|
||||||
c.screen, err = newImageWithScreenFramebuffer(w, h)
|
c.screen, err = newImageWithScreenFramebuffer(w, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.screen.Clear()
|
c.screen.Clear()
|
||||||
c.offscreen = offscreen
|
c.offscreen = offscreen
|
||||||
|
c.offscreen2 = offscreen2
|
||||||
c.screenScale = screenScale
|
c.screenScale = screenScale
|
||||||
ui.GLContext().ResetViewportSize()
|
ui.GLContext().ResetViewportSize()
|
||||||
return nil
|
return nil
|
||||||
@ -89,14 +100,24 @@ func (c *graphicsContext) initializeIfNeeded() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func drawWithFittingScale(dst *Image, src *Image) error {
|
||||||
|
wd, hd := dst.Size()
|
||||||
|
ws, hs := src.Size()
|
||||||
|
sw := float64(wd) / float64(ws)
|
||||||
|
sh := float64(hd) / float64(hs)
|
||||||
|
op := &DrawImageOptions{}
|
||||||
|
op.GeoM.Scale(sw, sh)
|
||||||
|
if err := dst.DrawImage(src, op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *graphicsContext) drawToDefaultRenderTarget() error {
|
func (c *graphicsContext) drawToDefaultRenderTarget() error {
|
||||||
if err := c.screen.Clear(); err != nil {
|
if err := c.screen.Clear(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
scale := float64(c.screenScale)
|
if err := drawWithFittingScale(c.screen, c.offscreen2); err != nil {
|
||||||
options := &DrawImageOptions{}
|
|
||||||
options.GeoM.Scale(scale, scale)
|
|
||||||
if err := c.screen.DrawImage(c.offscreen, options); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := c.flush(); err != nil {
|
if err := c.flush(); err != nil {
|
||||||
@ -118,11 +139,18 @@ func (c *graphicsContext) UpdateAndDraw() error {
|
|||||||
if IsRunningSlowly() {
|
if IsRunningSlowly() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if err := c.offscreen2.Clear(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := c.drawToDefaultRenderTarget(); err != nil {
|
if err := c.drawToDefaultRenderTarget(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
exceptions := map[*imageImpl]struct{}{
|
exceptions := map[*imageImpl]struct{}{
|
||||||
c.offscreen.impl: {},
|
c.offscreen.impl: {},
|
||||||
|
c.offscreen2.impl: {},
|
||||||
c.screen.impl: {},
|
c.screen.impl: {},
|
||||||
}
|
}
|
||||||
if err := theImages.savePixels(ui.GLContext(), exceptions); err != nil {
|
if err := theImages.savePixels(ui.GLContext(), exceptions); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user