mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
Change API
This commit is contained in:
parent
9b05640ddf
commit
c039c13acd
@ -65,7 +65,7 @@ func (t *Textures) loopMain() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
id, err := graphics.CreateTexture(img, graphics.FilterNearest)
|
||||
id, err := graphics.NewTextureID(img, graphics.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -77,7 +77,7 @@ func (t *Textures) loopMain() {
|
||||
name := s.name
|
||||
size := s.size
|
||||
go func() {
|
||||
id, err := graphics.CreateRenderTarget(size.Width, size.Height, graphics.FilterNearest)
|
||||
id, err := graphics.NewRenderTargetID(size.Width, size.Height, graphics.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
package graphics
|
||||
|
||||
func OrthoProjectionMatrix(left, right, bottom, top int) [4][4]float64 {
|
||||
e11 := float64(2) / float64(right-left)
|
||||
e22 := float64(2) / float64(top-bottom)
|
||||
e14 := -1 * float64(right+left) / float64(right-left)
|
||||
e24 := -1 * float64(top+bottom) / float64(top-bottom)
|
||||
|
||||
return [4][4]float64{
|
||||
{e11, 0, 0, e14},
|
||||
{0, e22, 0, e24},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
}
|
||||
}
|
@ -26,11 +26,11 @@ var idsInstance = &ids{
|
||||
currentRenderTargetId: -1,
|
||||
}
|
||||
|
||||
func CreateRenderTarget(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
||||
func NewRenderTargetID(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
||||
return idsInstance.createRenderTarget(width, height, filter)
|
||||
}
|
||||
|
||||
func CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
||||
func NewTextureID(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
||||
return idsInstance.createTexture(img, filter)
|
||||
}
|
||||
|
||||
@ -138,7 +138,7 @@ func (i *ids) drawTexture(
|
||||
i.setViewportIfNeeded(target)
|
||||
r := i.renderTargetAt(target)
|
||||
projectionMatrix := r.projectionMatrix()
|
||||
quads := graphics.TextureQuads(parts, texture.width, texture.height)
|
||||
quads := shader.TextureQuads(parts, texture.width, texture.height)
|
||||
shader.DrawTexture(texture.native, projectionMatrix, quads, geo, color)
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package shader
|
||||
|
||||
import (
|
||||
"github.com/go-gl/gl"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"github.com/hajimehoshi/ebiten/graphics/matrix"
|
||||
"sync"
|
||||
)
|
||||
@ -19,7 +18,7 @@ func glMatrix(matrix [4][4]float64) [16]float32 {
|
||||
|
||||
var once sync.Once
|
||||
|
||||
func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []graphics.TextureQuad, geo matrix.Geometry, color matrix.Color) {
|
||||
func DrawTexture(native gl.Texture, projectionMatrix [4][4]float64, quads []TextureQuad, geo matrix.Geometry, color matrix.Color) {
|
||||
once.Do(func() {
|
||||
initialize()
|
||||
})
|
||||
|
@ -1,8 +1,7 @@
|
||||
package graphics
|
||||
package shader
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/draw"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
)
|
||||
|
||||
type TextureQuad struct {
|
||||
@ -31,29 +30,6 @@ func AdjustSizeForTexture(size int) int {
|
||||
return int(NextPowerOf2(uint64(size)))
|
||||
}
|
||||
|
||||
func AdjustImageForTexture(img image.Image) *image.NRGBA {
|
||||
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
|
||||
adjustedImageBounds := image.Rectangle{
|
||||
image.ZP,
|
||||
image.Point{
|
||||
AdjustSizeForTexture(width),
|
||||
AdjustSizeForTexture(height),
|
||||
},
|
||||
}
|
||||
if nrgba, ok := img.(*image.NRGBA); ok &&
|
||||
img.Bounds() == adjustedImageBounds {
|
||||
return nrgba
|
||||
}
|
||||
|
||||
adjustedImage := image.NewNRGBA(adjustedImageBounds)
|
||||
dstBounds := image.Rectangle{
|
||||
image.ZP,
|
||||
img.Bounds().Size(),
|
||||
}
|
||||
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
|
||||
return adjustedImage
|
||||
}
|
||||
|
||||
func u(x int, width int) float32 {
|
||||
return float32(x) / float32(AdjustSizeForTexture(width))
|
||||
}
|
||||
@ -62,7 +38,7 @@ func v(y int, height int) float32 {
|
||||
return float32(y) / float32(AdjustSizeForTexture(height))
|
||||
}
|
||||
|
||||
func TextureQuads(parts []TexturePart, width, height int) []TextureQuad {
|
||||
func TextureQuads(parts []graphics.TexturePart, width, height int) []TextureQuad {
|
||||
quads := []TextureQuad{}
|
||||
for _, part := range parts {
|
||||
x1 := float32(part.LocationX)
|
@ -1,4 +1,4 @@
|
||||
package graphics_test
|
||||
package shader_test
|
||||
|
||||
import (
|
||||
. "."
|
@ -3,9 +3,23 @@ package opengl
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-gl/gl"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"github.com/hajimehoshi/ebiten/graphics/opengl/internal/shader"
|
||||
)
|
||||
|
||||
func orthoProjectionMatrix(left, right, bottom, top int) [4][4]float64 {
|
||||
e11 := float64(2) / float64(right-left)
|
||||
e22 := float64(2) / float64(top-bottom)
|
||||
e14 := -1 * float64(right+left) / float64(right-left)
|
||||
e24 := -1 * float64(top+bottom) / float64(top-bottom)
|
||||
|
||||
return [4][4]float64{
|
||||
{e11, 0, 0, e14},
|
||||
{0, e22, 0, e24},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
}
|
||||
}
|
||||
|
||||
type RenderTarget struct {
|
||||
framebuffer gl.Framebuffer
|
||||
width int
|
||||
@ -41,19 +55,18 @@ func (r *RenderTarget) setAsViewport() {
|
||||
|
||||
gl.BlendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE)
|
||||
|
||||
width := graphics.AdjustSizeForTexture(r.width)
|
||||
height := graphics.AdjustSizeForTexture(r.height)
|
||||
width := shader.AdjustSizeForTexture(r.width)
|
||||
height := shader.AdjustSizeForTexture(r.height)
|
||||
gl.Viewport(0, 0, width, height)
|
||||
}
|
||||
|
||||
func (r *RenderTarget) projectionMatrix() [4][4]float64 {
|
||||
width := graphics.AdjustSizeForTexture(r.width)
|
||||
height := graphics.AdjustSizeForTexture(r.height)
|
||||
matrix := graphics.OrthoProjectionMatrix(0, width, 0, height)
|
||||
width := shader.AdjustSizeForTexture(r.width)
|
||||
height := shader.AdjustSizeForTexture(r.height)
|
||||
matrix := orthoProjectionMatrix(0, width, 0, height)
|
||||
if r.flipY {
|
||||
matrix[1][1] *= -1
|
||||
matrix[1][3] += float64(r.height) /
|
||||
float64(graphics.AdjustSizeForTexture(r.height)) * 2
|
||||
matrix[1][3] += float64(r.height) / float64(shader.AdjustSizeForTexture(r.height)) * 2
|
||||
}
|
||||
return matrix
|
||||
}
|
||||
|
@ -3,9 +3,34 @@ package opengl
|
||||
import (
|
||||
"github.com/go-gl/gl"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"github.com/hajimehoshi/ebiten/graphics/opengl/internal/shader"
|
||||
"image"
|
||||
"image/draw"
|
||||
)
|
||||
|
||||
func adjustImageForTexture(img image.Image) *image.NRGBA {
|
||||
width, height := img.Bounds().Size().X, img.Bounds().Size().Y
|
||||
adjustedImageBounds := image.Rectangle{
|
||||
image.ZP,
|
||||
image.Point{
|
||||
shader.AdjustSizeForTexture(width),
|
||||
shader.AdjustSizeForTexture(height),
|
||||
},
|
||||
}
|
||||
if nrgba, ok := img.(*image.NRGBA); ok &&
|
||||
img.Bounds() == adjustedImageBounds {
|
||||
return nrgba
|
||||
}
|
||||
|
||||
adjustedImage := image.NewNRGBA(adjustedImageBounds)
|
||||
dstBounds := image.Rectangle{
|
||||
image.ZP,
|
||||
img.Bounds().Size(),
|
||||
}
|
||||
draw.Draw(adjustedImage, dstBounds, img, image.ZP, draw.Src)
|
||||
return adjustedImage
|
||||
}
|
||||
|
||||
type Texture struct {
|
||||
native gl.Texture
|
||||
width int
|
||||
@ -43,14 +68,14 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter
|
||||
}
|
||||
|
||||
func createTexture(width, height int, filter graphics.Filter) (*Texture, error) {
|
||||
w := graphics.AdjustSizeForTexture(width)
|
||||
h := graphics.AdjustSizeForTexture(height)
|
||||
w := shader.AdjustSizeForTexture(width)
|
||||
h := shader.AdjustSizeForTexture(height)
|
||||
native := createNativeTexture(w, h, nil, filter)
|
||||
return &Texture{native, width, height}, nil
|
||||
}
|
||||
|
||||
func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
|
||||
adjustedImage := graphics.AdjustImageForTexture(img)
|
||||
adjustedImage := adjustImageForTexture(img)
|
||||
size := adjustedImage.Bounds().Size()
|
||||
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
|
||||
return &Texture{native, size.X, size.Y}, nil
|
||||
|
@ -20,24 +20,24 @@ type RenderTargetID int
|
||||
var currentTextureFactory TextureFactory
|
||||
|
||||
type TextureFactory interface {
|
||||
CreateRenderTarget(width, height int, filter Filter) (RenderTargetID, error)
|
||||
CreateTexture(img image.Image, filter Filter) (TextureID, error)
|
||||
NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error)
|
||||
NewTextureID(img image.Image, filter Filter) (TextureID, error)
|
||||
}
|
||||
|
||||
func SetTextureFactory(textureFactory TextureFactory) {
|
||||
currentTextureFactory = textureFactory
|
||||
}
|
||||
|
||||
func CreateRenderTarget(width, height int, filter Filter) (RenderTargetID, error) {
|
||||
func NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
|
||||
if currentTextureFactory == nil {
|
||||
panic("graphics.CreateRenderTarget: currentTextureFactory is not set.")
|
||||
panic("graphics.NewRenderTarget: currentTextureFactory is not set.")
|
||||
}
|
||||
return currentTextureFactory.CreateRenderTarget(width, height, filter)
|
||||
return currentTextureFactory.NewRenderTargetID(width, height, filter)
|
||||
}
|
||||
|
||||
func CreateTexture(img image.Image, filter Filter) (TextureID, error) {
|
||||
func NewTextureID(img image.Image, filter Filter) (TextureID, error) {
|
||||
if currentTextureFactory == nil {
|
||||
panic("graphics.CreateTexture: currentTextureFactory is not set")
|
||||
panic("graphics.NewTexture: currentTextureFactory is not set")
|
||||
}
|
||||
return currentTextureFactory.CreateTexture(img, filter)
|
||||
return currentTextureFactory.NewTextureID(img, filter)
|
||||
}
|
||||
|
@ -55,20 +55,20 @@ func (c *Canvas) IsClosed() bool {
|
||||
return c.window.ShouldClose()
|
||||
}
|
||||
|
||||
func (c *Canvas) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
||||
func (c *Canvas) NewTextureID(img image.Image, filter graphics.Filter) (graphics.TextureID, error) {
|
||||
var id graphics.TextureID
|
||||
var err error
|
||||
c.use(func() {
|
||||
id, err = opengl.CreateTexture(img, filter)
|
||||
id, err = opengl.NewTextureID(img, filter)
|
||||
})
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (c *Canvas) CreateRenderTarget(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
||||
func (c *Canvas) NewRenderTargetID(width, height int, filter graphics.Filter) (graphics.RenderTargetID, error) {
|
||||
var id graphics.RenderTargetID
|
||||
var err error
|
||||
c.use(func() {
|
||||
id, err = opengl.CreateRenderTarget(width, height, filter)
|
||||
id, err = opengl.NewRenderTargetID(width, height, filter)
|
||||
})
|
||||
return id, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user