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-09 15:16:04 +01:00
|
|
|
|
2014-12-30 18:55:17 +01:00
|
|
|
package graphics
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
import (
|
2014-12-22 17:26:44 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-12-06 07:47:48 +01:00
|
|
|
"github.com/go-gl/gl"
|
2014-12-19 21:22:10 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal"
|
2014-12-31 06:57:51 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
2013-06-19 03:31:44 +02:00
|
|
|
"image"
|
2014-12-07 11:25:49 +01:00
|
|
|
"image/draw"
|
2013-06-15 10:07:14 +02:00
|
|
|
)
|
|
|
|
|
2014-12-22 13:39:25 +01:00
|
|
|
func adjustImageForTexture(img image.Image) *image.RGBA {
|
2014-12-07 11:25:49 +01:00
|
|
|
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
|
|
|
|
adjustedImageBounds := image.Rectangle{
|
|
|
|
image.ZP,
|
|
|
|
image.Point{
|
2014-12-20 18:05:08 +01:00
|
|
|
internal.NextPowerOf2Int(width),
|
|
|
|
internal.NextPowerOf2Int(height),
|
2014-12-07 11:25:49 +01:00
|
|
|
},
|
|
|
|
}
|
2014-12-22 13:39:25 +01:00
|
|
|
if nrgba, ok := img.(*image.RGBA); ok && img.Bounds() == adjustedImageBounds {
|
2014-12-07 11:25:49 +01:00
|
|
|
return nrgba
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:39:25 +01:00
|
|
|
adjustedImage := image.NewRGBA(adjustedImageBounds)
|
2014-12-07 11:25:49 +01:00
|
|
|
dstBounds := image.Rectangle{
|
|
|
|
image.ZP,
|
|
|
|
img.Bounds().Size(),
|
|
|
|
}
|
|
|
|
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
|
|
|
|
return adjustedImage
|
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
type Texture struct {
|
2014-12-31 06:57:51 +01:00
|
|
|
native opengl.Texture
|
2014-01-08 10:03:21 +01:00
|
|
|
width int
|
|
|
|
height int
|
2014-01-08 06:37:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-20 17:04:49 +01:00
|
|
|
func (t *Texture) Size() (width, height int) {
|
|
|
|
return t.width, t.height
|
2014-12-14 07:26:10 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 06:57:51 +01:00
|
|
|
func NewTexture(c *opengl.Context, width, height int, filter opengl.Filter) (*Texture, error) {
|
2014-12-20 18:05:08 +01:00
|
|
|
w := internal.NextPowerOf2Int(width)
|
|
|
|
h := internal.NextPowerOf2Int(height)
|
2014-12-22 17:26:44 +01:00
|
|
|
if w < 4 {
|
|
|
|
return nil, errors.New("width must be equal or more than 4.")
|
|
|
|
}
|
|
|
|
if h < 4 {
|
|
|
|
return nil, errors.New("height must be equal or more than 4.")
|
|
|
|
}
|
2014-12-31 06:57:51 +01:00
|
|
|
native, err := c.NewTexture(w, h, nil, filter)
|
2014-12-28 16:21:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-14 07:26:10 +01:00
|
|
|
return &Texture{native, width, height}, nil
|
2013-10-17 04:21:57 +02:00
|
|
|
}
|
2013-10-27 11:27:59 +01:00
|
|
|
|
2014-12-31 06:57:51 +01:00
|
|
|
func NewTextureFromImage(c *opengl.Context, img image.Image, filter opengl.Filter) (*Texture, error) {
|
2014-12-17 14:50:44 +01:00
|
|
|
origSize := img.Bounds().Size()
|
2014-12-22 17:26:44 +01:00
|
|
|
if origSize.X < 4 {
|
|
|
|
return nil, errors.New("width must be equal or more than 4.")
|
|
|
|
}
|
|
|
|
if origSize.Y < 4 {
|
|
|
|
return nil, errors.New("height must be equal or more than 4.")
|
|
|
|
}
|
2014-12-07 11:25:49 +01:00
|
|
|
adjustedImage := adjustImageForTexture(img)
|
2013-12-18 10:05:28 +01:00
|
|
|
size := adjustedImage.Bounds().Size()
|
2014-12-31 06:57:51 +01:00
|
|
|
native, err := c.NewTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
2014-12-28 16:21:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-17 14:50:44 +01:00
|
|
|
return &Texture{native, origSize.X, origSize.Y}, nil
|
2013-10-27 11:27:59 +01:00
|
|
|
}
|
2014-01-08 10:03:21 +01:00
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
func (t *Texture) Dispose() {
|
2014-12-31 06:57:51 +01:00
|
|
|
gl.Texture(t.native).Delete()
|
2014-01-08 10:58:15 +01:00
|
|
|
}
|
2014-12-22 17:26:44 +01:00
|
|
|
|
|
|
|
func (t *Texture) Pixels() ([]uint8, error) {
|
|
|
|
w, h := internal.NextPowerOf2Int(t.width), internal.NextPowerOf2Int(t.height)
|
|
|
|
pixels := make([]uint8, 4*w*h)
|
2014-12-31 06:57:51 +01:00
|
|
|
gl.Texture(t.native).Bind(gl.TEXTURE_2D)
|
2014-12-22 17:26:44 +01:00
|
|
|
gl.GetTexImage(gl.TEXTURE_2D, 0, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
|
|
|
// TODO: Use glu.ErrorString
|
|
|
|
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
|
|
|
|
}
|
|
|
|
return pixels, nil
|
|
|
|
}
|