Change API

This commit is contained in:
Hajime Hoshi 2014-12-07 19:25:49 +09:00
parent 9b05640ddf
commit c039c13acd
10 changed files with 71 additions and 73 deletions

View File

@ -65,7 +65,7 @@ func (t *Textures) loopMain() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
id, err := graphics.CreateTexture(img, graphics.FilterNearest) id, err := graphics.NewTextureID(img, graphics.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -77,7 +77,7 @@ func (t *Textures) loopMain() {
name := s.name name := s.name
size := s.size size := s.size
go func() { 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 { if err != nil {
panic(err) panic(err)
} }

View File

@ -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},
}
}

View File

@ -26,11 +26,11 @@ var idsInstance = &ids{
currentRenderTargetId: -1, 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) 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) return idsInstance.createTexture(img, filter)
} }
@ -138,7 +138,7 @@ func (i *ids) drawTexture(
i.setViewportIfNeeded(target) i.setViewportIfNeeded(target)
r := i.renderTargetAt(target) r := i.renderTargetAt(target)
projectionMatrix := r.projectionMatrix() 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) shader.DrawTexture(texture.native, projectionMatrix, quads, geo, color)
} }

View File

@ -2,7 +2,6 @@ package shader
import ( import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix" "github.com/hajimehoshi/ebiten/graphics/matrix"
"sync" "sync"
) )
@ -19,7 +18,7 @@ func glMatrix(matrix [4][4]float64) [16]float32 {
var once sync.Once 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() { once.Do(func() {
initialize() initialize()
}) })

View File

@ -1,8 +1,7 @@
package graphics package shader
import ( import (
"image" "github.com/hajimehoshi/ebiten/graphics"
"image/draw"
) )
type TextureQuad struct { type TextureQuad struct {
@ -31,29 +30,6 @@ func AdjustSizeForTexture(size int) int {
return int(NextPowerOf2(uint64(size))) 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 { func u(x int, width int) float32 {
return float32(x) / float32(AdjustSizeForTexture(width)) return float32(x) / float32(AdjustSizeForTexture(width))
} }
@ -62,7 +38,7 @@ func v(y int, height int) float32 {
return float32(y) / float32(AdjustSizeForTexture(height)) 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{} quads := []TextureQuad{}
for _, part := range parts { for _, part := range parts {
x1 := float32(part.LocationX) x1 := float32(part.LocationX)

View File

@ -1,4 +1,4 @@
package graphics_test package shader_test
import ( import (
. "." . "."

View File

@ -3,9 +3,23 @@ package opengl
import ( import (
"fmt" "fmt"
"github.com/go-gl/gl" "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 { type RenderTarget struct {
framebuffer gl.Framebuffer framebuffer gl.Framebuffer
width int width int
@ -41,19 +55,18 @@ func (r *RenderTarget) setAsViewport() {
gl.BlendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE) gl.BlendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE)
width := graphics.AdjustSizeForTexture(r.width) width := shader.AdjustSizeForTexture(r.width)
height := graphics.AdjustSizeForTexture(r.height) height := shader.AdjustSizeForTexture(r.height)
gl.Viewport(0, 0, width, height) gl.Viewport(0, 0, width, height)
} }
func (r *RenderTarget) projectionMatrix() [4][4]float64 { func (r *RenderTarget) projectionMatrix() [4][4]float64 {
width := graphics.AdjustSizeForTexture(r.width) width := shader.AdjustSizeForTexture(r.width)
height := graphics.AdjustSizeForTexture(r.height) height := shader.AdjustSizeForTexture(r.height)
matrix := graphics.OrthoProjectionMatrix(0, width, 0, height) matrix := orthoProjectionMatrix(0, width, 0, height)
if r.flipY { if r.flipY {
matrix[1][1] *= -1 matrix[1][1] *= -1
matrix[1][3] += float64(r.height) / matrix[1][3] += float64(r.height) / float64(shader.AdjustSizeForTexture(r.height)) * 2
float64(graphics.AdjustSizeForTexture(r.height)) * 2
} }
return matrix return matrix
} }

View File

@ -3,9 +3,34 @@ package opengl
import ( import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/graphics" "github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/opengl/internal/shader"
"image" "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 { type Texture struct {
native gl.Texture native gl.Texture
width int width int
@ -43,14 +68,14 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, filter
} }
func createTexture(width, height int, filter graphics.Filter) (*Texture, error) { func createTexture(width, height int, filter graphics.Filter) (*Texture, error) {
w := graphics.AdjustSizeForTexture(width) w := shader.AdjustSizeForTexture(width)
h := graphics.AdjustSizeForTexture(height) h := shader.AdjustSizeForTexture(height)
native := createNativeTexture(w, h, nil, filter) native := createNativeTexture(w, h, nil, filter)
return &Texture{native, width, height}, nil return &Texture{native, width, height}, nil
} }
func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) { func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) {
adjustedImage := graphics.AdjustImageForTexture(img) adjustedImage := adjustImageForTexture(img)
size := adjustedImage.Bounds().Size() size := adjustedImage.Bounds().Size()
native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter) native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter)
return &Texture{native, size.X, size.Y}, nil return &Texture{native, size.X, size.Y}, nil

View File

@ -20,24 +20,24 @@ type RenderTargetID int
var currentTextureFactory TextureFactory var currentTextureFactory TextureFactory
type TextureFactory interface { type TextureFactory interface {
CreateRenderTarget(width, height int, filter Filter) (RenderTargetID, error) NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error)
CreateTexture(img image.Image, filter Filter) (TextureID, error) NewTextureID(img image.Image, filter Filter) (TextureID, error)
} }
func SetTextureFactory(textureFactory TextureFactory) { func SetTextureFactory(textureFactory TextureFactory) {
currentTextureFactory = textureFactory currentTextureFactory = textureFactory
} }
func CreateRenderTarget(width, height int, filter Filter) (RenderTargetID, error) { func NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
if currentTextureFactory == nil { 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 { 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)
} }

View File

@ -55,20 +55,20 @@ func (c *Canvas) IsClosed() bool {
return c.window.ShouldClose() 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 id graphics.TextureID
var err error var err error
c.use(func() { c.use(func() {
id, err = opengl.CreateTexture(img, filter) id, err = opengl.NewTextureID(img, filter)
}) })
return id, err 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 id graphics.RenderTargetID
var err error var err error
c.use(func() { c.use(func() {
id, err = opengl.CreateRenderTarget(width, height, filter) id, err = opengl.NewRenderTargetID(width, height, filter)
}) })
return id, err return id, err
} }