diff --git a/example/game/rects/rects.go b/example/game/rects/rects.go index 9acad78b7..dfb647c75 100644 --- a/example/game/rects/rects.go +++ b/example/game/rects/rects.go @@ -36,8 +36,15 @@ func New() *Rects { } func (game *Rects) InitTextures(tf graphics.TextureFactory) { - game.rectTextureID = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight) - game.offscreenID = tf.NewRenderTarget(offscreenWidth, offscreenHeight) + var err error + game.rectTextureID, err = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight) + if err != nil { + panic(err) + } + game.offscreenID, err = tf.NewRenderTarget(offscreenWidth, offscreenHeight) + if err != nil { + panic(err) + } } func (game *Rects) Update(context ebiten.GameContext) { diff --git a/graphics/graphics.go b/graphics/graphics.go index 1bb0d7389..9ab3424e6 100644 --- a/graphics/graphics.go +++ b/graphics/graphics.go @@ -35,7 +35,7 @@ type Context interface { } type TextureFactory interface { - NewRenderTarget(width, height int) RenderTargetID + NewRenderTarget(width, height int) (RenderTargetID, error) NewTextureFromImage(img image.Image) (TextureID, error) } diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 317b44a77..6f2a103fd 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -51,8 +51,12 @@ func (context *Context) Init() { initializeShaders() - context.screenId = context.NewRenderTarget( + var err error + context.screenId, err = context.NewRenderTarget( context.screenWidth, context.screenHeight) + if err != nil { + panic("initializing the offscreen failed: " + err.Error()) + } } func (context *Context) ToTexture(renderTargetID graphics.RenderTargetID) graphics.TextureID { @@ -262,8 +266,12 @@ func (context *Context) setShaderProgram( return } -func (context *Context) NewRenderTarget(width, height int) graphics.RenderTargetID { - renderTarget := newRenderTarget(width, height) +func (context *Context) NewRenderTarget(width, height int) ( + graphics.RenderTargetID, error) { + renderTarget, err := newRenderTarget(width, height) + if err != nil { + return 0, nil + } renderTargetId := graphics.RenderTargetID(<-newId) textureId := graphics.TextureID(<-newId) context.renderTargets[renderTargetId] = renderTarget @@ -274,7 +282,7 @@ func (context *Context) NewRenderTarget(width, height int) graphics.RenderTarget context.Clear() context.setMainFramebufferOffscreen() - return renderTargetId + return renderTargetId, nil } func (context *Context) NewTextureFromImage(img image.Image) ( diff --git a/graphics/opengl/texture.go b/graphics/opengl/texture.go index dbef31cd5..6d5fd5294 100644 --- a/graphics/opengl/texture.go +++ b/graphics/opengl/texture.go @@ -67,7 +67,16 @@ func createTexture(width, height int) *Texture { } } -func createTextureFromImage(img image.Image) *Texture { +func newRenderTarget(width, height int) (*RenderTarget, error) { + texture := createTexture(width, height) + framebuffer := createFramebuffer(texture.native) + return &RenderTarget{ + texture: texture, + framebuffer: framebuffer, + }, nil +} + +func newTextureFromImage(img image.Image) (*Texture, error) { size := img.Bounds().Size() width, height := size.X, size.Y @@ -93,20 +102,7 @@ func createTextureFromImage(img image.Image) *Texture { textureWidth: textureWidth, textureHeight: textureHeight, native: nativeTexture, - } -} - -func newRenderTarget(width, height int) *RenderTarget { - texture := createTexture(width, height) - framebuffer := createFramebuffer(texture.native) - return &RenderTarget{ - texture: texture, - framebuffer: framebuffer, - } -} - -func newTextureFromImage(img image.Image) (*Texture, error) { - return createTextureFromImage(img), nil + }, nil } func newRenderTargetWithFramebuffer(width, height int,