2017-02-10 19:44:29 +01:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-02-26 16:05:11 +01:00
|
|
|
package restorable
|
2017-02-10 19:44:29 +01:00
|
|
|
|
2017-05-01 16:23:58 +02:00
|
|
|
import (
|
2017-05-21 16:49:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2018-02-26 16:05:11 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2017-05-01 16:23:58 +02:00
|
|
|
)
|
|
|
|
|
2017-05-27 14:59:25 +02:00
|
|
|
var (
|
2018-02-26 16:05:11 +01:00
|
|
|
quadFloat32Num = graphics.QuadVertexSizeInBytes() / 4
|
2017-05-27 14:59:25 +02:00
|
|
|
theVerticesBackend = &verticesBackend{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type verticesBackend struct {
|
|
|
|
backend []float32
|
|
|
|
head int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *verticesBackend) get() []float32 {
|
|
|
|
const num = 256
|
|
|
|
if v.backend == nil {
|
|
|
|
v.backend = make([]float32, quadFloat32Num*num)
|
|
|
|
}
|
|
|
|
s := v.backend[v.head : v.head+quadFloat32Num]
|
|
|
|
v.head += quadFloat32Num
|
|
|
|
if v.head+quadFloat32Num > len(v.backend) {
|
|
|
|
v.backend = nil
|
|
|
|
v.head = 0
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2017-05-21 16:49:56 +02:00
|
|
|
|
2018-02-26 16:05:11 +01:00
|
|
|
func (i *Image) vertices(sx0, sy0, sx1, sy1 int, geo *affine.GeoM) []float32 {
|
2017-12-14 03:52:11 +01:00
|
|
|
if sx0 >= sx1 || sy0 >= sy1 {
|
2017-01-17 17:35:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-15 17:27:30 +01:00
|
|
|
if sx1 <= 0 || sy1 <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:49:56 +02:00
|
|
|
// TODO: This function should be in graphics package?
|
2017-05-27 14:59:25 +02:00
|
|
|
vs := theVerticesBackend.get()
|
2017-12-15 17:27:30 +01:00
|
|
|
|
|
|
|
if sx0 < 0 || sy0 < 0 {
|
|
|
|
dx := 0.0
|
|
|
|
dy := 0.0
|
|
|
|
if sx0 < 0 {
|
|
|
|
dx = -float64(sx0)
|
|
|
|
sx0 = 0
|
|
|
|
}
|
|
|
|
if sy0 < 0 {
|
|
|
|
dy = -float64(sy0)
|
|
|
|
sy0 = 0
|
|
|
|
}
|
2018-02-26 16:45:57 +01:00
|
|
|
var g *affine.GeoM
|
|
|
|
g = g.Translate(dx, dy)
|
|
|
|
g = g.Concat(geo)
|
|
|
|
geo = g
|
2017-12-15 17:27:30 +01:00
|
|
|
}
|
2018-01-14 08:05:18 +01:00
|
|
|
x0, y0 := 0.0, 0.0
|
|
|
|
x1, y1 := float64(sx1-sx0), float64(sy1-sy0)
|
2017-12-15 17:27:30 +01:00
|
|
|
|
2018-01-14 08:01:55 +01:00
|
|
|
// it really feels like we should be able to cache this computation
|
|
|
|
// but it may not matter.
|
2018-02-26 16:05:11 +01:00
|
|
|
width, height := i.Size()
|
2017-05-21 16:49:56 +02:00
|
|
|
w := 1
|
|
|
|
h := 1
|
|
|
|
for w < width {
|
2018-01-14 08:05:18 +01:00
|
|
|
w *= 2
|
2017-05-21 16:49:56 +02:00
|
|
|
}
|
|
|
|
for h < height {
|
2018-01-14 08:05:18 +01:00
|
|
|
h *= 2
|
2017-05-21 16:49:56 +02:00
|
|
|
}
|
|
|
|
wf := float32(w)
|
|
|
|
hf := float32(h)
|
2017-01-17 17:35:21 +01:00
|
|
|
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
|
2017-12-02 12:51:18 +01:00
|
|
|
|
2018-01-14 08:01:55 +01:00
|
|
|
x, y := geo.Apply32(x0, y0)
|
2017-12-02 12:51:18 +01:00
|
|
|
// Vertex coordinates
|
2018-01-14 08:01:55 +01:00
|
|
|
vs[0] = x
|
|
|
|
vs[1] = y
|
2017-12-02 12:51:18 +01:00
|
|
|
|
|
|
|
// Texture coordinates: first 2 values indicates the actual coodinate, and
|
|
|
|
// the second indicates diagonally opposite coodinates.
|
|
|
|
// The second is needed to calculate source rectangle size in shader programs.
|
2017-01-17 17:35:21 +01:00
|
|
|
vs[2] = u0
|
|
|
|
vs[3] = v0
|
2017-12-02 12:51:18 +01:00
|
|
|
vs[4] = u1
|
|
|
|
vs[5] = v1
|
|
|
|
|
2018-01-14 08:01:55 +01:00
|
|
|
// and the same for the other three coordinates
|
|
|
|
x, y = geo.Apply32(x1, y0)
|
|
|
|
vs[6] = x
|
|
|
|
vs[7] = y
|
|
|
|
vs[8] = u1
|
|
|
|
vs[9] = v0
|
|
|
|
vs[10] = u0
|
|
|
|
vs[11] = v1
|
|
|
|
|
|
|
|
x, y = geo.Apply32(x0, y1)
|
|
|
|
vs[12] = x
|
|
|
|
vs[13] = y
|
|
|
|
vs[14] = u0
|
|
|
|
vs[15] = v1
|
|
|
|
vs[16] = u1
|
|
|
|
vs[17] = v0
|
|
|
|
|
|
|
|
x, y = geo.Apply32(x1, y1)
|
|
|
|
vs[18] = x
|
|
|
|
vs[19] = y
|
|
|
|
vs[20] = u1
|
|
|
|
vs[21] = v1
|
|
|
|
vs[22] = u0
|
|
|
|
vs[23] = v0
|
2017-05-21 16:49:56 +02:00
|
|
|
|
|
|
|
return vs
|
|
|
|
}
|