Refactoring

This commit is contained in:
Hajime Hoshi 2013-11-29 02:38:18 +09:00
parent c9e8036617
commit f0ef3384cb
14 changed files with 49 additions and 52 deletions

View File

@ -29,7 +29,7 @@ func (game *Input) InitTextures(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.textTextureId, err = tf.NewTextureFromImage(img); err != nil {
if game.textTextureId, err = tf.CreateTextureFromImage(img); err != nil {
panic(err)
}
}

View File

@ -40,7 +40,7 @@ func (game *Monochrome) InitTextures(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTextureId, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
panic(err)
}

View File

@ -37,11 +37,11 @@ func New() *Rects {
func (game *Rects) InitTextures(tf graphics.TextureFactory) {
var err error
game.rectTextureId, err = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight)
game.rectTextureId, err = tf.CreateRenderTarget(rectTextureWidth, rectTextureHeight)
if err != nil {
panic(err)
}
game.offscreenId, err = tf.NewRenderTarget(offscreenWidth, offscreenHeight)
game.offscreenId, err = tf.CreateRenderTarget(offscreenWidth, offscreenHeight)
if err != nil {
panic(err)
}

View File

@ -36,7 +36,7 @@ func (game *Rotating) InitTextures(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTextureId, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
panic(err)
}
}

View File

@ -86,7 +86,7 @@ func (game *Sprites) InitTextures(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.ebitenTextureId, err = tf.NewTextureFromImage(img); err != nil {
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
panic(err)
}
}

View File

@ -32,7 +32,7 @@ func (game *TestPattern) InitTextures(tf graphics.TextureFactory) {
if err != nil {
panic(err)
}
if game.textureId, err = tf.NewTextureFromImage(img); err != nil {
if game.textureId, err = tf.CreateTextureFromImage(img); err != nil {
panic(err)
}
size := img.Bounds().Size()

View File

@ -35,8 +35,8 @@ type Context interface {
}
type TextureFactory interface {
NewRenderTarget(width, height int) (RenderTargetId, error)
NewTextureFromImage(img image.Image) (TextureId, error)
CreateRenderTarget(width, height int) (RenderTargetId, error)
CreateTextureFromImage(img image.Image) (TextureId, error)
}
type TextureId int

View File

@ -101,12 +101,12 @@ func (context *Context) createRenderTarget(width, height int, filter texture.Fil
return renderTargetId, nil
}
func (context *Context) NewRenderTarget(width, height int) (
func (context *Context) CreateRenderTarget(width, height int) (
graphics.RenderTargetId, error) {
return context.createRenderTarget(width, height, texture.FilterLinear)
}
func (context *Context) NewTextureFromImage(img image.Image) (
func (context *Context) CreateTextureFromImage(img image.Image) (
graphics.TextureId, error) {
return context.ids.CreateTextureFromImage(img)
}

View File

@ -44,7 +44,7 @@ func (i *ids) ToTexture(id graphics.RenderTargetId) graphics.TextureId {
func (i *ids) CreateTextureFromImage(img image.Image) (
graphics.TextureId, error) {
texture, err := texture.NewFromImage(img)
texture, err := texture.CreateFromImage(img)
if err != nil {
return 0, err
}
@ -55,7 +55,7 @@ func (i *ids) CreateTextureFromImage(img image.Image) (
func (i *ids) CreateRenderTarget(width, height int, filter texture.Filter) (
graphics.RenderTargetId, error) {
renderTarget, texture, err := rendertarget.New(width, height, filter)
renderTarget, texture, err := rendertarget.Create(width, height, filter)
if err != nil {
return 0, err
}

View File

@ -34,7 +34,7 @@ func New(screenWidth, screenHeight, screenScale int) *Offscreen {
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
var err error
offscreen.mainFramebufferTexture, err = rendertarget.NewWithFramebuffer(
offscreen.mainFramebufferTexture, err = rendertarget.CreateWithFramebuffer(
screenWidth*screenScale,
screenHeight*screenScale,
rendertarget.Framebuffer(mainFramebuffer))

View File

@ -36,27 +36,23 @@ func createFramebuffer(nativeTexture C.GLuint) Framebuffer {
return Framebuffer(framebuffer)
}
// TODO: Rename them
func New(width, height int, filter texture.Filter) (
type framebufferCreator struct {
}
func (f *framebufferCreator) Create(native interface{}) interface{} {
return createFramebuffer(C.GLuint(native.(texture.Native)))
}
func Create(width, height int, filter texture.Filter) (
*gtexture.RenderTarget, *gtexture.Texture, error) {
tex, err := texture.New(width, height, filter)
tex, err := texture.Create(width, height, filter)
if err != nil {
return nil, nil, err
}
f := func(native interface{}) interface{} {
return createFramebuffer(C.GLuint(native.(texture.Native)))
}
return gtexture.NewRenderTarget(tex, f), tex, nil
return tex.NewRenderTarget(&framebufferCreator{}), tex, nil
}
func NewWithFramebuffer(width, height int, framebuffer Framebuffer) (
func CreateWithFramebuffer(width, height int, framebuffer Framebuffer) (
*gtexture.RenderTarget, error) {
tex, err := texture.NewEmpty(width, height)
if err != nil {
return nil, err
}
f := func(native interface{}) interface{} {
return framebuffer
}
return gtexture.NewRenderTarget(tex, f), nil
return gtexture.NewRenderTarget(framebuffer, width, height), nil
}

View File

@ -15,7 +15,7 @@ type Native C.GLuint
type Filter int
const (
FilterLinear = iota
FilterLinear Filter = iota
FilterNearest
)
@ -64,20 +64,16 @@ func createFromImage(img *image.NRGBA) (interface{}, error) {
return createNativeTexture(size.X, size.Y, img.Pix, FilterLinear), nil
}
// TODO: Rename them
func New(width, height int, filter Filter) (*gtexture.Texture, error) {
native, err := create(gtexture.AdjustSize(width), gtexture.AdjustSize(height), filter)
func Create(width, height int, filter Filter) (*gtexture.Texture, error) {
native, err := create(gtexture.AdjustSize(width),
gtexture.AdjustSize(height), filter)
if err != nil {
return nil, err
}
return gtexture.New(native, width, height), nil
}
func NewEmpty(width, height int) (*gtexture.Texture, error) {
return gtexture.New(nil, width, height), nil
}
func NewFromImage(img image.Image) (*gtexture.Texture, error) {
func CreateFromImage(img image.Image) (*gtexture.Texture, error) {
native, err := createFromImage(gtexture.AdjustImage(img))
if err != nil {
return nil, err

View File

@ -1,14 +1,16 @@
package texture
type RenderTarget struct {
texture *Texture
framebuffer interface{}
framebuffer interface{}
offscreenWidth int
offscreenHeight int
}
func NewRenderTarget(texture *Texture, create func(native interface{}) interface{}) *RenderTarget {
func NewRenderTarget(framebuffer interface{}, width, height int) *RenderTarget {
return &RenderTarget{
texture: texture,
framebuffer: create(texture.native),
framebuffer: framebuffer,
offscreenWidth: AdjustSize(width),
offscreenHeight: AdjustSize(height),
}
}
@ -16,8 +18,6 @@ type OffscreenSetter interface {
Set(framebuffer interface{}, x, y, width, height int)
}
func (renderTarget *RenderTarget) SetAsOffscreen(setter OffscreenSetter) {
renderTarget.texture.SetAsViewport(func(x, y, width, height int) {
setter.Set(renderTarget.framebuffer, x, y, width, height)
})
func (r *RenderTarget) SetAsOffscreen(setter OffscreenSetter) {
setter.Set(r.framebuffer, 0, 0, r.offscreenWidth, r.offscreenHeight)
}

View File

@ -62,10 +62,6 @@ func (texture *Texture) v(y int) float32 {
return float32(y) / float32(AdjustSize(texture.height))
}
func (texture *Texture) SetAsViewport(setter func(x, y, width, height int)) {
setter(0, 0, AdjustSize(texture.width), AdjustSize(texture.height))
}
type Quad struct {
VertexX1 float32
VertexX2 float32
@ -110,3 +106,12 @@ func (texture *Texture) DrawParts(parts []graphics.TexturePart, drawable Drawabl
}
drawable.Draw(texture.native, quads)
}
type FramebufferCreator interface {
Create(native interface{}) interface{}
}
func (texture *Texture) NewRenderTarget(creator FramebufferCreator) *RenderTarget {
return NewRenderTarget(creator.Create(texture.native), texture.width, texture.height)
}