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 (
|
2013-06-19 03:31:44 +02:00
|
|
|
"image"
|
2014-12-07 11:25:49 +01:00
|
|
|
"image/draw"
|
2016-05-12 04:06:12 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
2013-06-15 10:07:14 +02:00
|
|
|
)
|
|
|
|
|
2016-05-12 04:06:12 +02:00
|
|
|
func adjustImageForTexture(img *image.RGBA) *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{
|
2016-02-26 18:35:05 +01:00
|
|
|
int(NextPowerOf2Int32(int32(width))),
|
|
|
|
int(NextPowerOf2Int32(int32(height))),
|
2014-12-07 11:25:49 +01:00
|
|
|
},
|
|
|
|
}
|
2016-05-12 04:06:12 +02:00
|
|
|
if img.Bounds() == adjustedImageBounds {
|
|
|
|
return img
|
2014-12-07 11:25:49 +01:00
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
}
|
2016-05-12 04:06:12 +02:00
|
|
|
draw.Draw(adjustedImage, dstBounds, img, img.Bounds().Min, draw.Src)
|
2014-12-07 11:25:49 +01:00
|
|
|
return adjustedImage
|
|
|
|
}
|
|
|
|
|
2016-06-11 18:34:21 +02:00
|
|
|
type Image struct {
|
2016-06-11 18:41:50 +02:00
|
|
|
texture *texture
|
|
|
|
framebuffer *framebuffer
|
2016-06-11 18:34:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-11 18:41:50 +02: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
|
|
|
}
|
|
|
|
|
2016-06-11 18:34:21 +02:00
|
|
|
func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
|
|
|
|
i := &Image{
|
2016-06-11 18:41:50 +02:00
|
|
|
texture: &texture{},
|
|
|
|
framebuffer: &framebuffer{},
|
2016-06-11 18:34:21 +02:00
|
|
|
}
|
2016-06-11 17:23:26 +02:00
|
|
|
c := &newImageCommand{
|
2016-06-11 18:34:21 +02:00
|
|
|
texture: i.texture,
|
|
|
|
framebuffer: i.framebuffer,
|
2016-06-11 17:23:26 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
filter: filter,
|
2014-12-22 17:26:44 +01:00
|
|
|
}
|
2016-06-11 17:23:26 +02:00
|
|
|
theCommandQueue.Enqueue(c)
|
2016-06-11 18:34:21 +02:00
|
|
|
return i, nil
|
2013-10-17 04:21:57 +02:00
|
|
|
}
|
2013-10-27 11:27:59 +01:00
|
|
|
|
2016-06-11 18:34:21 +02:00
|
|
|
func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) {
|
|
|
|
i := &Image{
|
2016-06-11 18:41:50 +02:00
|
|
|
texture: &texture{},
|
|
|
|
framebuffer: &framebuffer{},
|
2016-06-11 18:34:21 +02:00
|
|
|
}
|
2016-06-11 17:23:26 +02:00
|
|
|
c := &newImageFromImageCommand{
|
2016-06-11 18:34:21 +02:00
|
|
|
texture: i.texture,
|
|
|
|
framebuffer: i.framebuffer,
|
2016-06-11 17:23:26 +02:00
|
|
|
img: img,
|
|
|
|
filter: filter,
|
2014-12-28 16:21:40 +01:00
|
|
|
}
|
2016-06-11 17:23:26 +02:00
|
|
|
theCommandQueue.Enqueue(c)
|
2016-06-11 18:34:21 +02:00
|
|
|
return i, nil
|
2013-10-27 11:27:59 +01:00
|
|
|
}
|