2014-12-24 03:04:10 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-12-10 15:52:37 +01:00
|
|
|
|
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2015-07-22 16:27:50 +02:00
|
|
|
audio "github.com/hajimehoshi/ebiten/exp/audio/inner"
|
2015-01-01 17:20:20 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2015-07-22 16:27:50 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
2015-01-01 17:20:20 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
2014-12-10 15:52:37 +01:00
|
|
|
"image"
|
|
|
|
)
|
|
|
|
|
2015-01-27 14:02:23 +01:00
|
|
|
var glContext *opengl.Context
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ui.Init()
|
|
|
|
ui.ExecOnUIThread(func() {
|
|
|
|
glContext = opengl.NewContext()
|
|
|
|
})
|
|
|
|
audio.Init()
|
|
|
|
}
|
|
|
|
|
2014-12-22 02:36:42 +01:00
|
|
|
// NewImage returns an empty image.
|
2015-01-20 15:58:58 +01:00
|
|
|
//
|
|
|
|
// NewImage generates a new texture and a new framebuffer.
|
|
|
|
// Be careful that image objects will never be released
|
|
|
|
// even though nothing refers the image object and GC works.
|
|
|
|
// It is because there is no way to define finalizers for Go objects if you use GopherJS.
|
2014-12-22 02:36:42 +01:00
|
|
|
func NewImage(width, height int, filter Filter) (*Image, error) {
|
2015-01-12 11:54:25 +01:00
|
|
|
var img *Image
|
2015-01-01 17:20:20 +01:00
|
|
|
var err error
|
2015-01-26 17:10:17 +01:00
|
|
|
useGLContext(func(c *opengl.Context) {
|
2015-01-01 17:20:20 +01:00
|
|
|
var texture *graphics.Texture
|
2015-01-12 11:54:25 +01:00
|
|
|
var framebuffer *graphics.Framebuffer
|
2015-01-01 17:20:20 +01:00
|
|
|
texture, err = graphics.NewTexture(c, width, height, glFilter(c, filter))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-12 11:54:25 +01:00
|
|
|
framebuffer, err = graphics.NewFramebufferFromTexture(c, texture)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
img = &Image{framebuffer: framebuffer, texture: texture}
|
2015-01-01 17:20:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-12 11:54:25 +01:00
|
|
|
if err := img.Clear(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return img, nil
|
2014-12-14 09:28:19 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 03:20:14 +01:00
|
|
|
// NewImageFromImage creates a new image with the given image (img).
|
2015-01-20 16:42:27 +01:00
|
|
|
//
|
|
|
|
// NewImageFromImage generates a new texture and a new framebuffer.
|
|
|
|
// Be careful that image objects will never be released
|
|
|
|
// even though nothing refers the image object and GC works.
|
|
|
|
// It is because there is no way to define finalizers for Go objects if you use GopherJS.
|
2014-12-22 02:36:42 +01:00
|
|
|
func NewImageFromImage(img image.Image, filter Filter) (*Image, error) {
|
2015-01-12 11:54:25 +01:00
|
|
|
var eimg *Image
|
2015-01-01 17:20:20 +01:00
|
|
|
var err error
|
2015-01-26 17:10:17 +01:00
|
|
|
useGLContext(func(c *opengl.Context) {
|
2015-01-01 17:20:20 +01:00
|
|
|
var texture *graphics.Texture
|
2015-01-12 11:54:25 +01:00
|
|
|
var framebuffer *graphics.Framebuffer
|
2015-01-01 17:20:20 +01:00
|
|
|
texture, err = graphics.NewTextureFromImage(c, img, glFilter(c, filter))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-01-12 11:54:25 +01:00
|
|
|
framebuffer, err = graphics.NewFramebufferFromTexture(c, texture)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
eimg = &Image{framebuffer: framebuffer, texture: texture}
|
2015-01-01 17:20:20 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-12 11:54:25 +01:00
|
|
|
return eimg, nil
|
2014-12-10 15:52:37 +01:00
|
|
|
}
|