mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
graphics: Refactoring: Remove duplication of checking image size
This commit is contained in:
parent
4628154478
commit
0418ce0761
20
image.go
20
image.go
@ -444,7 +444,6 @@ type DrawImageOptions struct {
|
|||||||
//
|
//
|
||||||
// Error returned by NewImage is always nil as of 1.5.0-alpha.
|
// Error returned by NewImage is always nil as of 1.5.0-alpha.
|
||||||
func NewImage(width, height int, filter Filter) (*Image, error) {
|
func NewImage(width, height int, filter Filter) (*Image, error) {
|
||||||
checkSize(width, height)
|
|
||||||
var i *Image
|
var i *Image
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
if s != nil {
|
if s != nil {
|
||||||
@ -466,7 +465,6 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
|
|||||||
|
|
||||||
// newImageWithoutInit creates an empty image without initialization.
|
// newImageWithoutInit creates an empty image without initialization.
|
||||||
func newImageWithoutInit(width, height int) *Image {
|
func newImageWithoutInit(width, height int) *Image {
|
||||||
checkSize(width, height)
|
|
||||||
var i *Image
|
var i *Image
|
||||||
s := newSharedImagePart(width, height)
|
s := newSharedImagePart(width, height)
|
||||||
if s != nil {
|
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.
|
// Error returned by newVolatileImage is always nil as of 1.5.0-alpha.
|
||||||
func newVolatileImage(width, height int, filter Filter) *Image {
|
func newVolatileImage(width, height int, filter Filter) *Image {
|
||||||
checkSize(width, height)
|
|
||||||
r := restorable.NewImage(width, height, true)
|
r := restorable.NewImage(width, height, true)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
restorable: r,
|
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.
|
// Error returned by NewImageFromImage is always nil as of 1.5.0-alpha.
|
||||||
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
|
||||||
size := source.Bounds().Size()
|
size := source.Bounds().Size()
|
||||||
checkSize(size.X, size.Y)
|
|
||||||
|
|
||||||
width, height := 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 {
|
func newImageWithScreenFramebuffer(width, height int) *Image {
|
||||||
checkSize(width, height)
|
|
||||||
r := restorable.NewScreenFramebufferImage(width, height)
|
r := restorable.NewScreenFramebufferImage(width, height)
|
||||||
i := &Image{
|
i := &Image{
|
||||||
restorable: r,
|
restorable: r,
|
||||||
@ -559,18 +554,3 @@ func newImageWithScreenFramebuffer(width, height int) *Image {
|
|||||||
|
|
||||||
// MaxImageSize represents the maximum width/height of an image.
|
// MaxImageSize represents the maximum width/height of an image.
|
||||||
const MaxImageSize = graphics.MaxImageSize
|
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
package graphics
|
package graphics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
||||||
@ -326,16 +325,26 @@ type newImageCommand struct {
|
|||||||
height int
|
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.
|
// Exec executes a newImageCommand.
|
||||||
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
w := emath.NextPowerOf2Int(c.width)
|
w := emath.NextPowerOf2Int(c.width)
|
||||||
h := emath.NextPowerOf2Int(c.height)
|
h := emath.NextPowerOf2Int(c.height)
|
||||||
if w < 1 {
|
checkSize(w, h)
|
||||||
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.")
|
|
||||||
}
|
|
||||||
native, err := opengl.GetContext().NewTexture(w, h)
|
native, err := opengl.GetContext().NewTexture(w, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -359,12 +368,7 @@ type newScreenFramebufferImageCommand struct {
|
|||||||
|
|
||||||
// Exec executes a newScreenFramebufferImageCommand.
|
// Exec executes a newScreenFramebufferImageCommand.
|
||||||
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
if c.width < 1 {
|
checkSize(c.width, c.height)
|
||||||
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.")
|
|
||||||
}
|
|
||||||
// The (default) framebuffer size can't be converted to a power of 2.
|
// 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
|
// 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).
|
// Edge can't treat a bigger viewport than the drawing area (#71).
|
||||||
|
Loading…
Reference in New Issue
Block a user