From 0418ce076143d6daf95f3d7a977a29fe27459fa5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 9 Mar 2018 11:03:55 +0900 Subject: [PATCH] graphics: Refactoring: Remove duplication of checking image size --- image.go | 20 -------------------- internal/graphics/command.go | 30 +++++++++++++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/image.go b/image.go index 6c4bd53a5..0721da13f 100644 --- a/image.go +++ b/image.go @@ -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)) - } -} diff --git a/internal/graphics/command.go b/internal/graphics/command.go index 44b46b309..12b6aeef8 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -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).