graphics: Refactoring: Remove duplication of checking image size

This commit is contained in:
Hajime Hoshi 2018-03-09 11:03:55 +09:00
parent 4628154478
commit 0418ce0761
2 changed files with 17 additions and 33 deletions

View File

@ -444,7 +444,6 @@ type DrawImageOptions struct {
//
// Error returned by NewImage is always nil as of 1.5.0-alpha.
func NewImage(width, height int, filter Filter) (*Image, error) {
checkSize(width, height)
var i *Image
s := newSharedImagePart(width, height)
if s != nil {
@ -466,7 +465,6 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
// newImageWithoutInit creates an empty image without initialization.
func newImageWithoutInit(width, height int) *Image {
checkSize(width, height)
var i *Image
s := newSharedImagePart(width, height)
if s != nil {
@ -501,7 +499,6 @@ func newImageWithoutInit(width, height int) *Image {
//
// Error returned by newVolatileImage is always nil as of 1.5.0-alpha.
func newVolatileImage(width, height int, filter Filter) *Image {
checkSize(width, height)
r := restorable.NewImage(width, height, true)
i := &Image{
restorable: r,
@ -522,7 +519,6 @@ func newVolatileImage(width, height int, filter Filter) *Image {
// Error returned by NewImageFromImage is always nil as of 1.5.0-alpha.
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
size := source.Bounds().Size()
checkSize(size.X, size.Y)
width, height := size.X, size.Y
@ -547,7 +543,6 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
}
func newImageWithScreenFramebuffer(width, height int) *Image {
checkSize(width, height)
r := restorable.NewScreenFramebufferImage(width, height)
i := &Image{
restorable: r,
@ -559,18 +554,3 @@ func newImageWithScreenFramebuffer(width, height int) *Image {
// MaxImageSize represents the maximum width/height of an image.
const MaxImageSize = graphics.MaxImageSize
func checkSize(width, height int) {
if width <= 0 {
panic("ebiten: width must be more than 0")
}
if height <= 0 {
panic("ebiten: height must be more than 0")
}
if width > MaxImageSize {
panic(fmt.Sprintf("ebiten: width (%d) must be less than or equal to %d", width, MaxImageSize))
}
if height > MaxImageSize {
panic(fmt.Sprintf("ebiten: height (%d) must be less than or equal to %d", height, MaxImageSize))
}
}

View File

@ -15,7 +15,6 @@
package graphics
import (
"errors"
"fmt"
"github.com/hajimehoshi/ebiten/internal/affine"
@ -326,16 +325,26 @@ type newImageCommand struct {
height int
}
func checkSize(width, height int) {
if width < 1 {
panic(fmt.Sprintf("graphics: width (%d) must be equal or more than 1.", width))
}
if height < 1 {
panic(fmt.Sprintf("graphics: height (%d) must be equal or more than 1.", height))
}
if width > MaxImageSize {
panic(fmt.Sprintf("graphics: width (%d) must be less than or equal to %d", width, MaxImageSize))
}
if height > MaxImageSize {
panic(fmt.Sprintf("graphics: height (%d) must be less than or equal to %d", height, MaxImageSize))
}
}
// Exec executes a newImageCommand.
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
w := emath.NextPowerOf2Int(c.width)
h := emath.NextPowerOf2Int(c.height)
if w < 1 {
return errors.New("graphics: width must be equal or more than 1.")
}
if h < 1 {
return errors.New("graphics: height must be equal or more than 1.")
}
checkSize(w, h)
native, err := opengl.GetContext().NewTexture(w, h)
if err != nil {
return err
@ -359,12 +368,7 @@ type newScreenFramebufferImageCommand struct {
// Exec executes a newScreenFramebufferImageCommand.
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
if c.width < 1 {
return errors.New("graphics: width must be equal or more than 1.")
}
if c.height < 1 {
return errors.New("graphics: height must be equal or more than 1.")
}
checkSize(c.width, c.height)
// The (default) framebuffer size can't be converted to a power of 2.
// On browsers, c.width and c.height are used as viewport size and
// Edge can't treat a bigger viewport than the drawing area (#71).