2016-06-11 18:45:40 +02:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
package graphics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Image struct {
|
|
|
|
texture *texture
|
|
|
|
framebuffer *framebuffer
|
2016-07-04 17:24:06 +02:00
|
|
|
width int
|
|
|
|
height int
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
|
2016-07-04 17:24:06 +02:00
|
|
|
i := &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2016-06-11 18:45:40 +02:00
|
|
|
c := &newImageCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
result: i,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
filter: filter,
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewImageFromImage(img *image.RGBA, filter opengl.Filter) (*Image, error) {
|
2016-07-04 17:24:06 +02:00
|
|
|
s := img.Bounds().Size()
|
|
|
|
i := &Image{
|
|
|
|
width: s.X,
|
|
|
|
height: s.Y,
|
|
|
|
}
|
2016-06-11 18:45:40 +02:00
|
|
|
c := &newImageFromImageCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
result: i,
|
|
|
|
img: img,
|
|
|
|
filter: filter,
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2016-06-17 21:46:33 +02:00
|
|
|
func NewScreenFramebufferImage(width, height int) (*Image, error) {
|
2016-07-04 17:24:06 +02:00
|
|
|
i := &Image{
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
c := &newScreenFramebufferImageCommand{
|
|
|
|
result: i,
|
2016-06-11 18:45:40 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return i, nil
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Dispose() error {
|
|
|
|
c := &disposeCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
target: i,
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-04 17:24:06 +02:00
|
|
|
func (i *Image) Size() (int, int) {
|
|
|
|
return i.width, i.height
|
|
|
|
}
|
|
|
|
|
2016-08-30 16:31:59 +02:00
|
|
|
func (i *Image) Fill(clr color.RGBA) error {
|
2016-08-22 17:05:23 +02:00
|
|
|
// TODO: Need to clone clr value
|
2016-06-11 18:45:40 +02:00
|
|
|
c := &fillCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
dst: i,
|
2016-06-11 18:45:40 +02:00
|
|
|
color: clr,
|
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:20:41 +02:00
|
|
|
func (i *Image) DrawImage(src *Image, vertices []int16, clr Matrix, mode opengl.CompositeMode) error {
|
2016-06-11 18:45:40 +02:00
|
|
|
c := &drawImageCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
dst: i,
|
|
|
|
src: src,
|
2016-06-11 18:45:40 +02:00
|
|
|
vertices: vertices,
|
|
|
|
color: clr,
|
|
|
|
mode: mode,
|
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) {
|
|
|
|
// Flush the enqueued commands so that pixels are certainly read.
|
|
|
|
if err := theCommandQueue.Flush(context); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f := i.framebuffer
|
2016-07-04 17:29:46 +02:00
|
|
|
return context.FramebufferPixels(f.native, i.width, i.height)
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) ReplacePixels(p []uint8) error {
|
2016-08-17 15:10:29 +02:00
|
|
|
pixels := make([]uint8, len(p))
|
|
|
|
copy(pixels, p)
|
2016-06-11 18:45:40 +02:00
|
|
|
c := &replacePixelsCommand{
|
2016-06-12 15:44:23 +02:00
|
|
|
dst: i,
|
2016-08-17 15:10:29 +02:00
|
|
|
pixels: pixels,
|
2016-06-11 18:45:40 +02:00
|
|
|
}
|
|
|
|
theCommandQueue.Enqueue(c)
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-12 16:54:36 +02:00
|
|
|
|
|
|
|
func (i *Image) IsInvalidated(context *opengl.Context) bool {
|
|
|
|
return !context.IsTexture(i.texture.native)
|
|
|
|
}
|