mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
affine: Remove GeoM to avoid GeoM allocations on the heap
This commit is contained in:
parent
460c47a9eb
commit
b4ace53596
153
geom.go
153
geom.go
@ -16,54 +16,61 @@ package ebiten
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GeoMDim is a dimension of a GeoM.
|
// GeoMDim is a dimension of a GeoM.
|
||||||
const GeoMDim = affine.GeoMDim
|
const GeoMDim = 3
|
||||||
|
|
||||||
// A GeoM represents a matrix to transform geometry when rendering an image.
|
// A GeoM represents a matrix to transform geometry when rendering an image.
|
||||||
//
|
//
|
||||||
// The initial value is identity.
|
// The initial value is identity.
|
||||||
type GeoM struct {
|
type GeoM struct {
|
||||||
impl *affine.GeoM
|
a_1 float64 // The actual 'a' value minus 1
|
||||||
|
b float64
|
||||||
|
c float64
|
||||||
|
d_1 float64 // The actual 'd' value minus 1
|
||||||
|
tx float64
|
||||||
|
ty float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a string representation of GeoM.
|
// String returns a string representation of GeoM.
|
||||||
func (g *GeoM) String() string {
|
func (g *GeoM) String() string {
|
||||||
a, b, c, d, tx, ty := g.impl.Elements()
|
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", g.a_1+1, g.b, g.tx, g.c, g.d_1+1, g.ty)
|
||||||
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", a, b, tx, c, d, ty)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset resets the GeoM as identity.
|
// Reset resets the GeoM as identity.
|
||||||
func (g *GeoM) Reset() {
|
func (g *GeoM) Reset() {
|
||||||
g.impl = nil
|
g.a_1 = 0
|
||||||
|
g.b = 0
|
||||||
|
g.c = 0
|
||||||
|
g.d_1 = 0
|
||||||
|
g.tx = 0
|
||||||
|
g.ty = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply pre-multiplies a vector (x, y, 1) by the matrix.
|
// Apply pre-multiplies a vector (x, y, 1) by the matrix.
|
||||||
// In other words, Apply calculates GeoM * (x, y, 1)^T.
|
// In other words, Apply calculates GeoM * (x, y, 1)^T.
|
||||||
// The return value is x and y values of the result vector.
|
// The return value is x and y values of the result vector.
|
||||||
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
|
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
|
||||||
return g.impl.Apply(x, y)
|
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Element returns a value of a matrix at (i, j).
|
// Element returns a value of a matrix at (i, j).
|
||||||
func (g *GeoM) Element(i, j int) float64 {
|
func (g *GeoM) Element(i, j int) float64 {
|
||||||
a, b, c, d, tx, ty := g.impl.Elements()
|
|
||||||
switch {
|
switch {
|
||||||
case i == 0 && j == 0:
|
case i == 0 && j == 0:
|
||||||
return a
|
return g.a_1 + 1
|
||||||
case i == 0 && j == 1:
|
case i == 0 && j == 1:
|
||||||
return b
|
return g.b
|
||||||
case i == 0 && j == 2:
|
case i == 0 && j == 2:
|
||||||
return tx
|
return g.tx
|
||||||
case i == 1 && j == 0:
|
case i == 1 && j == 0:
|
||||||
return c
|
return g.c
|
||||||
case i == 1 && j == 1:
|
case i == 1 && j == 1:
|
||||||
return d
|
return g.d_1 + 1
|
||||||
case i == 1 && j == 2:
|
case i == 1 && j == 2:
|
||||||
return ty
|
return g.ty
|
||||||
default:
|
default:
|
||||||
panic("ebiten: i or j is out of index")
|
panic("ebiten: i or j is out of index")
|
||||||
}
|
}
|
||||||
@ -72,46 +79,126 @@ func (g *GeoM) Element(i, j int) float64 {
|
|||||||
// Concat multiplies a geometry matrix with the other geometry matrix.
|
// Concat multiplies a geometry matrix with the other geometry matrix.
|
||||||
// This is same as muptiplying the matrix other and the matrix g in this order.
|
// This is same as muptiplying the matrix other and the matrix g in this order.
|
||||||
func (g *GeoM) Concat(other GeoM) {
|
func (g *GeoM) Concat(other GeoM) {
|
||||||
g.impl = g.impl.Concat(other.impl)
|
a := (other.a_1+1)*(g.a_1+1) + other.b*g.c
|
||||||
|
b := (other.a_1+1)*g.b + other.b*(g.d_1+1)
|
||||||
|
tx := (other.a_1+1)*g.tx + other.b*g.ty + other.tx
|
||||||
|
c := other.c*(g.a_1+1) + (other.d_1+1)*g.c
|
||||||
|
d := other.c*g.b + (other.d_1+1)*(g.d_1+1)
|
||||||
|
ty := other.c*g.tx + (other.d_1+1)*g.ty + other.ty
|
||||||
|
|
||||||
|
g.a_1 = a - 1
|
||||||
|
g.b = b
|
||||||
|
g.c = c
|
||||||
|
g.d_1 = d - 1
|
||||||
|
g.tx = tx
|
||||||
|
g.ty = ty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add is deprecated as of 1.5.0-alpha.
|
// Add is deprecated as of 1.5.0-alpha.
|
||||||
// Note that this doesn't make sense as an operation for affine matrices.
|
// Note that this doesn't make sense as an operation for affine matrices.
|
||||||
func (g *GeoM) Add(other GeoM) {
|
func (g *GeoM) Add(other GeoM) {
|
||||||
g.impl = g.impl.Add(other.impl)
|
g.a_1 += other.a_1
|
||||||
|
g.b += other.b
|
||||||
|
g.c += other.c
|
||||||
|
g.d_1 += other.d_1
|
||||||
|
g.tx += other.tx
|
||||||
|
g.ty += other.ty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scale scales the matrix by (x, y).
|
// Scale scales the matrix by (x, y).
|
||||||
func (g *GeoM) Scale(x, y float64) {
|
func (g *GeoM) Scale(x, y float64) {
|
||||||
g.impl = g.impl.Scale(x, y)
|
a := (g.a_1 + 1) * x
|
||||||
|
b := g.b * x
|
||||||
|
tx := g.tx * x
|
||||||
|
c := g.c * y
|
||||||
|
d := (g.d_1 + 1) * y
|
||||||
|
ty := g.ty * y
|
||||||
|
|
||||||
|
g.a_1 = a - 1
|
||||||
|
g.b = b
|
||||||
|
g.c = c
|
||||||
|
g.d_1 = d - 1
|
||||||
|
g.tx = tx
|
||||||
|
g.ty = ty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate translates the matrix by (tx, ty).
|
// Translate translates the matrix by (tx, ty).
|
||||||
func (g *GeoM) Translate(tx, ty float64) {
|
func (g *GeoM) Translate(tx, ty float64) {
|
||||||
g.impl = g.impl.Translate(tx, ty)
|
g.tx += tx
|
||||||
}
|
g.ty += ty
|
||||||
|
|
||||||
// IsInvertible returns a boolean value indicating
|
|
||||||
// whether the matrix g is invertible or not.
|
|
||||||
func (g *GeoM) IsInvertible() bool {
|
|
||||||
return g.impl.IsInvertible()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invert inverts the matrix.
|
|
||||||
// If g is not invertible, Invert panics.
|
|
||||||
func (g *GeoM) Invert() {
|
|
||||||
g.impl = g.impl.Invert()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotate rotates the matrix by theta.
|
// Rotate rotates the matrix by theta.
|
||||||
// The unit is radian.
|
// The unit is radian.
|
||||||
func (g *GeoM) Rotate(theta float64) {
|
func (g *GeoM) Rotate(theta float64) {
|
||||||
g.impl = g.impl.Rotate(theta)
|
sin, cos := math.Sincos(theta)
|
||||||
|
|
||||||
|
a := cos*(g.a_1+1) - sin*g.c
|
||||||
|
b := cos*g.b - sin*(g.d_1+1)
|
||||||
|
tx := cos*g.tx - sin*g.ty
|
||||||
|
c := sin*(g.a_1+1) + cos*g.c
|
||||||
|
d := sin*g.b + cos*(g.d_1+1)
|
||||||
|
ty := sin*g.tx + cos*g.ty
|
||||||
|
|
||||||
|
g.a_1 = a - 1
|
||||||
|
g.b = b
|
||||||
|
g.c = c
|
||||||
|
g.d_1 = d - 1
|
||||||
|
g.tx = tx
|
||||||
|
g.ty = ty
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GeoM) det() float64 {
|
||||||
|
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsInvertible returns a boolean value indicating
|
||||||
|
// whether the matrix g is invertible or not.
|
||||||
|
func (g *GeoM) IsInvertible() bool {
|
||||||
|
return g.det() != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invert inverts the matrix.
|
||||||
|
// If g is not invertible, Invert panics.
|
||||||
|
func (g *GeoM) Invert() {
|
||||||
|
det := g.det()
|
||||||
|
if det == 0 {
|
||||||
|
panic("ebiten: g is not invertible")
|
||||||
|
}
|
||||||
|
|
||||||
|
a := (g.d_1 + 1) / det
|
||||||
|
b := -g.b / det
|
||||||
|
c := -g.c / det
|
||||||
|
d := (g.a_1 + 1) / det
|
||||||
|
tx := (-(g.d_1+1)*g.tx + g.b*g.ty) / det
|
||||||
|
ty := (g.c*g.tx + -(g.a_1+1)*g.ty) / det
|
||||||
|
|
||||||
|
g.a_1 = a - 1
|
||||||
|
g.b = b
|
||||||
|
g.c = c
|
||||||
|
g.d_1 = d - 1
|
||||||
|
g.tx = tx
|
||||||
|
g.ty = ty
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
// SetElement sets an element at (i, j).
|
||||||
func (g *GeoM) SetElement(i, j int, element float64) {
|
func (g *GeoM) SetElement(i, j int, element float64) {
|
||||||
g.impl = g.impl.SetElement(i, j, element)
|
switch {
|
||||||
|
case i == 0 && j == 0:
|
||||||
|
g.a_1 = element - 1
|
||||||
|
case i == 0 && j == 1:
|
||||||
|
g.b = element
|
||||||
|
case i == 0 && j == 2:
|
||||||
|
g.tx = element
|
||||||
|
case i == 1 && j == 0:
|
||||||
|
g.c = element
|
||||||
|
case i == 1 && j == 1:
|
||||||
|
g.d_1 = element - 1
|
||||||
|
case i == 1 && j == 2:
|
||||||
|
g.ty = element
|
||||||
|
default:
|
||||||
|
panic("ebiten: i or j is out of index")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.
|
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.
|
||||||
|
10
image.go
10
image.go
@ -19,7 +19,6 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
|
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
@ -212,7 +211,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
|||||||
sy1 = r.Max.Y
|
sy1 = r.Max.Y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
geom := options.GeoM.impl
|
geom := &options.GeoM
|
||||||
if sx0 < 0 || sy0 < 0 {
|
if sx0 < 0 || sy0 < 0 {
|
||||||
dx := 0.0
|
dx := 0.0
|
||||||
dy := 0.0
|
dy := 0.0
|
||||||
@ -224,10 +223,9 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
|
|||||||
dy = -float64(sy0)
|
dy = -float64(sy0)
|
||||||
sy0 = 0
|
sy0 = 0
|
||||||
}
|
}
|
||||||
var g *affine.GeoM
|
geom = &GeoM{}
|
||||||
g = g.Translate(dx, dy)
|
geom.Translate(dx, dy)
|
||||||
g = g.Concat(geom)
|
geom.Concat(options.GeoM)
|
||||||
geom = g
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mode := opengl.CompositeMode(options.CompositeMode)
|
mode := opengl.CompositeMode(options.CompositeMode)
|
||||||
|
@ -1,214 +0,0 @@
|
|||||||
// 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.
|
|
||||||
|
|
||||||
package affine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GeoMDim is a dimension of a GeoM.
|
|
||||||
const GeoMDim = 3
|
|
||||||
|
|
||||||
// A GeoM represents a matrix to transform geometry when rendering an image.
|
|
||||||
//
|
|
||||||
// The nil and initial value is identity.
|
|
||||||
type GeoM struct {
|
|
||||||
a_1 float64 // The actual 'a' value minus 1
|
|
||||||
b float64
|
|
||||||
c float64
|
|
||||||
d_1 float64 // The actual 'd' value minus 1
|
|
||||||
tx float64
|
|
||||||
ty float64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
|
|
||||||
if g == nil {
|
|
||||||
return x, y
|
|
||||||
}
|
|
||||||
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) {
|
|
||||||
if g == nil {
|
|
||||||
return float32(x), float32(y)
|
|
||||||
}
|
|
||||||
return float32((g.a_1+1)*x + g.b*y + g.tx), float32(g.c*x + (g.d_1+1)*y + g.ty)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) Elements() (a, b, c, d, tx, ty float64) {
|
|
||||||
if g == nil {
|
|
||||||
return 1, 0, 0, 1, 0, 0
|
|
||||||
}
|
|
||||||
return g.a_1 + 1, g.b, g.c, g.d_1 + 1, g.tx, g.ty
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
|
||||||
func (g *GeoM) SetElement(i, j int, element float64) *GeoM {
|
|
||||||
a, b, c, d, tx, ty := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
|
|
||||||
if g != nil {
|
|
||||||
a, b, c, d, tx, ty = g.a_1+1, g.b, g.c, g.d_1+1, g.tx, g.ty
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case i == 0 && j == 0:
|
|
||||||
a = element
|
|
||||||
case i == 0 && j == 1:
|
|
||||||
b = element
|
|
||||||
case i == 0 && j == 2:
|
|
||||||
tx = element
|
|
||||||
case i == 1 && j == 0:
|
|
||||||
c = element
|
|
||||||
case i == 1 && j == 1:
|
|
||||||
d = element
|
|
||||||
case i == 1 && j == 2:
|
|
||||||
ty = element
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: a - 1,
|
|
||||||
b: b,
|
|
||||||
c: c,
|
|
||||||
d_1: d - 1,
|
|
||||||
tx: tx,
|
|
||||||
ty: ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Concat multiplies a geometry matrix with the other geometry matrix.
|
|
||||||
// This is same as muptiplying the matrix other and the matrix g in this order.
|
|
||||||
func (g *GeoM) Concat(other *GeoM) *GeoM {
|
|
||||||
if g == nil {
|
|
||||||
return other
|
|
||||||
}
|
|
||||||
if other == nil {
|
|
||||||
return g
|
|
||||||
}
|
|
||||||
|
|
||||||
return &GeoM{
|
|
||||||
a_1: (other.a_1+1)*(g.a_1+1) + other.b*g.c - 1,
|
|
||||||
b: (other.a_1+1)*g.b + other.b*(g.d_1+1),
|
|
||||||
tx: (other.a_1+1)*g.tx + other.b*g.ty + other.tx,
|
|
||||||
c: other.c*(g.a_1+1) + (other.d_1+1)*g.c,
|
|
||||||
d_1: other.c*g.b + (other.d_1+1)*(g.d_1+1) - 1,
|
|
||||||
ty: other.c*g.tx + (other.d_1+1)*g.ty + other.ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add is deprecated.
|
|
||||||
func (g *GeoM) Add(other *GeoM) *GeoM {
|
|
||||||
if g == nil {
|
|
||||||
g = &GeoM{}
|
|
||||||
}
|
|
||||||
if other == nil {
|
|
||||||
other = &GeoM{}
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: (g.a_1 + 1) + (other.a_1 + 1) - 1,
|
|
||||||
b: g.b + other.b,
|
|
||||||
c: g.c + other.c,
|
|
||||||
d_1: (g.d_1 + 1) + (other.d_1 + 1) - 1,
|
|
||||||
tx: g.tx + other.tx,
|
|
||||||
ty: g.ty + other.ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scale scales the matrix by (x, y).
|
|
||||||
func (g *GeoM) Scale(x, y float64) *GeoM {
|
|
||||||
if g == nil {
|
|
||||||
return &GeoM{
|
|
||||||
a_1: x - 1,
|
|
||||||
b: 0,
|
|
||||||
c: 0,
|
|
||||||
d_1: y - 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: (g.a_1+1)*x - 1,
|
|
||||||
b: g.b * x,
|
|
||||||
tx: g.tx * x,
|
|
||||||
c: g.c * y,
|
|
||||||
d_1: (g.d_1+1)*y - 1,
|
|
||||||
ty: g.ty * y,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translate translates the matrix by (x, y).
|
|
||||||
func (g *GeoM) Translate(tx, ty float64) *GeoM {
|
|
||||||
if g == nil {
|
|
||||||
return &GeoM{
|
|
||||||
tx: tx,
|
|
||||||
ty: ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: g.a_1,
|
|
||||||
b: g.b,
|
|
||||||
c: g.c,
|
|
||||||
d_1: g.d_1,
|
|
||||||
tx: g.tx + tx,
|
|
||||||
ty: g.ty + ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rotate rotates the matrix by theta.
|
|
||||||
func (g *GeoM) Rotate(theta float64) *GeoM {
|
|
||||||
sin, cos := math.Sincos(theta)
|
|
||||||
if g == nil {
|
|
||||||
return &GeoM{
|
|
||||||
a_1: cos - 1,
|
|
||||||
b: -sin,
|
|
||||||
c: sin,
|
|
||||||
d_1: cos - 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: cos*(g.a_1+1) - sin*g.c - 1,
|
|
||||||
b: cos*g.b - sin*(g.d_1+1),
|
|
||||||
tx: cos*g.tx - sin*g.ty,
|
|
||||||
c: sin*(g.a_1+1) + cos*g.c,
|
|
||||||
d_1: sin*g.b + cos*(g.d_1+1) - 1,
|
|
||||||
ty: sin*g.tx + cos*g.ty,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) det() float64 {
|
|
||||||
if g == nil {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) IsInvertible() bool {
|
|
||||||
return g.det() != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GeoM) Invert() *GeoM {
|
|
||||||
if g == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
det := g.det()
|
|
||||||
if det == 0 {
|
|
||||||
panic("affine: g is not invertible")
|
|
||||||
}
|
|
||||||
return &GeoM{
|
|
||||||
a_1: ((g.d_1 + 1) / det) - 1,
|
|
||||||
b: -g.b / det,
|
|
||||||
c: -g.c / det,
|
|
||||||
d_1: ((g.a_1 + 1) / det) - 1,
|
|
||||||
tx: (-(g.d_1+1)*g.tx + g.b*g.ty) / det,
|
|
||||||
ty: (g.c*g.tx + -(g.a_1+1)*g.ty) / det,
|
|
||||||
}
|
|
||||||
}
|
|
@ -15,7 +15,6 @@
|
|||||||
package graphicsutil
|
package graphicsutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
)
|
)
|
||||||
@ -44,7 +43,11 @@ func (v *verticesBackend) sliceForOneQuad() []float32 {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) []float32 {
|
type GeoM interface {
|
||||||
|
Apply(x, y float64) (x2, y2 float64)
|
||||||
|
}
|
||||||
|
|
||||||
|
func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geom GeoM) []float32 {
|
||||||
if sx0 >= sx1 || sy0 >= sy1 {
|
if sx0 >= sx1 || sy0 >= sy1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -71,10 +74,13 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) [
|
|||||||
hf := float32(h)
|
hf := float32(h)
|
||||||
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
|
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
|
||||||
|
|
||||||
x, y := geo.Apply32(x0, y0)
|
x, y := x0, y0
|
||||||
|
if geom != nil {
|
||||||
|
x, y = geom.Apply(x, y)
|
||||||
|
}
|
||||||
// Vertex coordinates
|
// Vertex coordinates
|
||||||
vs[0] = x
|
vs[0] = float32(x)
|
||||||
vs[1] = y
|
vs[1] = float32(y)
|
||||||
|
|
||||||
// Texture coordinates: first 2 values indicates the actual coodinate, and
|
// Texture coordinates: first 2 values indicates the actual coodinate, and
|
||||||
// the second indicates diagonally opposite coodinates.
|
// the second indicates diagonally opposite coodinates.
|
||||||
@ -85,25 +91,34 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) [
|
|||||||
vs[5] = v1
|
vs[5] = v1
|
||||||
|
|
||||||
// and the same for the other three coordinates
|
// and the same for the other three coordinates
|
||||||
x, y = geo.Apply32(x1, y0)
|
x, y = x1, y0
|
||||||
vs[6] = x
|
if geom != nil {
|
||||||
vs[7] = y
|
x, y = geom.Apply(x, y)
|
||||||
|
}
|
||||||
|
vs[6] = float32(x)
|
||||||
|
vs[7] = float32(y)
|
||||||
vs[8] = u1
|
vs[8] = u1
|
||||||
vs[9] = v0
|
vs[9] = v0
|
||||||
vs[10] = u0
|
vs[10] = u0
|
||||||
vs[11] = v1
|
vs[11] = v1
|
||||||
|
|
||||||
x, y = geo.Apply32(x0, y1)
|
x, y = x0, y1
|
||||||
vs[12] = x
|
if geom != nil {
|
||||||
vs[13] = y
|
x, y = geom.Apply(x, y)
|
||||||
|
}
|
||||||
|
vs[12] = float32(x)
|
||||||
|
vs[13] = float32(y)
|
||||||
vs[14] = u0
|
vs[14] = u0
|
||||||
vs[15] = v1
|
vs[15] = v1
|
||||||
vs[16] = u1
|
vs[16] = u1
|
||||||
vs[17] = v0
|
vs[17] = v0
|
||||||
|
|
||||||
x, y = geo.Apply32(x1, y1)
|
x, y = x1, y1
|
||||||
vs[18] = x
|
if geom != nil {
|
||||||
vs[19] = y
|
x, y = geom.Apply(x, y)
|
||||||
|
}
|
||||||
|
vs[18] = float32(x)
|
||||||
|
vs[19] = float32(y)
|
||||||
vs[20] = u1
|
vs[20] = u1
|
||||||
vs[21] = v1
|
vs[21] = v1
|
||||||
vs[22] = u0
|
vs[22] = u0
|
||||||
|
@ -122,6 +122,17 @@ var (
|
|||||||
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
|
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type geoM struct {
|
||||||
|
scaleX float64
|
||||||
|
scaleY float64
|
||||||
|
tx float64
|
||||||
|
ty float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *geoM) Apply(x, y float64) (float64, float64) {
|
||||||
|
return x*g.scaleX + g.tx, y*g.scaleY + g.ty
|
||||||
|
}
|
||||||
|
|
||||||
// ReplacePixels replaces the image pixels with the given pixels slice.
|
// ReplacePixels replaces the image pixels with the given pixels slice.
|
||||||
//
|
//
|
||||||
// If pixels is nil, ReplacePixels clears the specified reagion.
|
// If pixels is nil, ReplacePixels clears the specified reagion.
|
||||||
@ -146,10 +157,13 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
|
|||||||
// However, that's ok since this image will be stale or have updated pixel data
|
// However, that's ok since this image will be stale or have updated pixel data
|
||||||
// and this image can be restored without dummyImage.
|
// and this image can be restored without dummyImage.
|
||||||
w, h := dummyImage.Size()
|
w, h := dummyImage.Size()
|
||||||
geom := (*affine.GeoM)(nil).Scale(float64(width)/float64(w), float64(height)/float64(h))
|
|
||||||
geom = geom.Translate(float64(x), float64(y))
|
|
||||||
colorm := (*affine.ColorM)(nil).Scale(0, 0, 0, 0)
|
colorm := (*affine.ColorM)(nil).Scale(0, 0, 0, 0)
|
||||||
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, geom)
|
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{
|
||||||
|
scaleX: float64(width) / float64(w),
|
||||||
|
scaleY: float64(height) / float64(h),
|
||||||
|
tx: float64(x),
|
||||||
|
ty: float64(y),
|
||||||
|
})
|
||||||
i.image.DrawImage(dummyImage.image, vs, quadIndices, colorm, opengl.CompositeModeCopy, graphics.FilterNearest)
|
i.image.DrawImage(dummyImage.image, vs, quadIndices, colorm, opengl.CompositeModeCopy, graphics.FilterNearest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
|
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
@ -257,6 +256,15 @@ func TestRestoreOverrideSource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type geoM struct {
|
||||||
|
tx float64
|
||||||
|
ty float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *geoM) Apply(x, y float64) (x2, y2 float64) {
|
||||||
|
return x + g.tx, y + g.ty
|
||||||
|
}
|
||||||
|
|
||||||
func TestRestoreComplexGraph(t *testing.T) {
|
func TestRestoreComplexGraph(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
w = 4
|
w = 4
|
||||||
@ -299,23 +307,23 @@ func TestRestoreComplexGraph(t *testing.T) {
|
|||||||
img1.Dispose()
|
img1.Dispose()
|
||||||
img0.Dispose()
|
img0.Dispose()
|
||||||
}()
|
}()
|
||||||
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
|
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
|
||||||
img3.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img3.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
|
||||||
img3.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img3.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
|
||||||
img4.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img4.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(2, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{2, 0})
|
||||||
img4.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img4.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
|
||||||
img5.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img5.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
|
||||||
img6.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img6.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
|
||||||
img6.DrawImage(img4, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img6.DrawImage(img4, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
|
||||||
img7.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img7.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(2, 0))
|
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{2, 0})
|
||||||
img7.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img7.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
if err := ResolveStaleImages(); err != nil {
|
if err := ResolveStaleImages(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -408,7 +416,7 @@ func TestRestoreRecursive(t *testing.T) {
|
|||||||
img1.Dispose()
|
img1.Dispose()
|
||||||
img0.Dispose()
|
img0.Dispose()
|
||||||
}()
|
}()
|
||||||
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
|
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
|
||||||
img1.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img1.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
img0.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
img0.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
|
||||||
if err := ResolveStaleImages(); err != nil {
|
if err := ResolveStaleImages(); err != nil {
|
||||||
|
@ -135,7 +135,7 @@ func (i *Image) Size() (width, height int) {
|
|||||||
return i.width, i.height
|
return i.width, i.height
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
|
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom graphicsutil.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
|
|
||||||
|
@ -21,7 +21,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/internal/affine"
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||||
. "github.com/hajimehoshi/ebiten/internal/shareable"
|
. "github.com/hajimehoshi/ebiten/internal/shareable"
|
||||||
@ -46,6 +45,15 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
const bigSize = 2049
|
const bigSize = 2049
|
||||||
|
|
||||||
|
type geoM struct {
|
||||||
|
tx float64
|
||||||
|
ty float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *geoM) Apply(x, y float64) (x2, y2 float64) {
|
||||||
|
return x + g.tx, y + g.ty
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnsureNotShared(t *testing.T) {
|
func TestEnsureNotShared(t *testing.T) {
|
||||||
// Create img1 and img2 with this size so that the next images are allocated
|
// Create img1 and img2 with this size so that the next images are allocated
|
||||||
// with non-upper-left location.
|
// with non-upper-left location.
|
||||||
@ -85,7 +93,7 @@ func TestEnsureNotShared(t *testing.T) {
|
|||||||
dy1 = size * 3 / 4
|
dy1 = size * 3 / 4
|
||||||
)
|
)
|
||||||
// img4.ensureNotShared() should be called.
|
// img4.ensureNotShared() should be called.
|
||||||
geom := (*affine.GeoM)(nil).Translate(size/4, size/4)
|
geom := &geoM{size / 4, size / 4}
|
||||||
img4.DrawImage(img3, 0, 0, size/2, size/2, geom, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
img4.DrawImage(img3, 0, 0, size/2, size/2, geom, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
||||||
|
|
||||||
for j := 0; j < size; j++ {
|
for j := 0; j < size; j++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user