From 33cb234ae16aa6cf6112374f42d808feacabfce2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 17 Dec 2014 20:21:27 +0900 Subject: [PATCH] Bug fix: Creating a render target before running --- ebitenutil/texture.go | 2 +- example/image/main.go | 2 +- example/paint/main.go | 29 ++++++++++++++--------------- graphicscontext.go | 4 ---- ui.go | 6 ++++++ 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/ebitenutil/texture.go b/ebitenutil/texture.go index f92db2f8e..e07d8c1ac 100644 --- a/ebitenutil/texture.go +++ b/ebitenutil/texture.go @@ -22,7 +22,7 @@ import ( "os" ) -func LoadImageAndCreateTexture(path string, filter ebiten.Filter) (ebiten.TextureID, error) { +func NewTextureIDFromFile(path string, filter ebiten.Filter) (ebiten.TextureID, error) { file, err := os.Open(path) if err != nil { return 0, err diff --git a/example/image/main.go b/example/image/main.go index d338f4881..dad2edec7 100644 --- a/example/image/main.go +++ b/example/image/main.go @@ -39,7 +39,7 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error { func main() { g := new(Game) - id, err := ebitenutil.LoadImageAndCreateTexture("images/gophers.jpg", ebiten.FilterLinear) + id, err := ebitenutil.NewTextureIDFromFile("images/gophers.jpg", ebiten.FilterLinear) if err != nil { log.Fatal(err) } diff --git a/example/paint/main.go b/example/paint/main.go index fb5cf4929..22b029e49 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -23,6 +23,7 @@ import ( "image/color" "log" "math" + "sync" ) const ( @@ -31,6 +32,7 @@ const ( ) type Game struct { + init sync.Once count int brushRenderTarget ebiten.RenderTargetID canvasRenderTarget ebiten.RenderTargetID @@ -40,27 +42,15 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { g.count++ } - if g.brushRenderTarget.IsNil() { - var err error - g.brushRenderTarget, err = ebiten.NewRenderTargetID(1, 1, ebiten.FilterNearest) - if err != nil { - return err - } - + g.init.Do(func() { gr.PushRenderTarget(g.brushRenderTarget) gr.Fill(0xff, 0xff, 0xff) gr.PopRenderTarget() - } - if g.canvasRenderTarget.IsNil() { - var err error - g.canvasRenderTarget, err = ebiten.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest) - if err != nil { - return err - } gr.PushRenderTarget(g.canvasRenderTarget) gr.Fill(0xff, 0xff, 0xff) gr.PopRenderTarget() - } + }) + mx, my := ebiten.CursorPosition() if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { @@ -81,6 +71,15 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error { func main() { g := new(Game) + var err error + g.brushRenderTarget, err = ebiten.NewRenderTargetID(1, 1, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + g.canvasRenderTarget, err = ebiten.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { log.Fatal(err) } diff --git a/graphicscontext.go b/graphicscontext.go index 46f09c009..466c50fb5 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -22,10 +22,6 @@ import ( ) func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { - gl.Init() - gl.Enable(gl.TEXTURE_2D) - gl.Enable(gl.BLEND) - // The defualt framebuffer should be 0. r := opengl.NewRenderTarget(screenWidth*screenScale, screenHeight*screenScale, true) diff --git a/ui.go b/ui.go index b96eaa4aa..229453acd 100644 --- a/ui.go +++ b/ui.go @@ -18,6 +18,7 @@ package ebiten import ( "fmt" + "github.com/go-gl/gl" glfw "github.com/go-gl/glfw3" "image" "runtime" @@ -47,6 +48,11 @@ func init() { funcs: make(chan func()), } currentUI.run() + currentUI.use(func() { + gl.Init() + gl.Enable(gl.TEXTURE_2D) + gl.Enable(gl.BLEND) + }) } type ui struct {