Bug fix: Creating a render target before running

This commit is contained in:
Hajime Hoshi 2014-12-17 20:21:27 +09:00
parent ebdee00dd2
commit 33cb234ae1
5 changed files with 22 additions and 21 deletions

View File

@ -22,7 +22,7 @@ import (
"os" "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) file, err := os.Open(path)
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -39,7 +39,7 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
func main() { func main() {
g := new(Game) g := new(Game)
id, err := ebitenutil.LoadImageAndCreateTexture("images/gophers.jpg", ebiten.FilterLinear) id, err := ebitenutil.NewTextureIDFromFile("images/gophers.jpg", ebiten.FilterLinear)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -23,6 +23,7 @@ import (
"image/color" "image/color"
"log" "log"
"math" "math"
"sync"
) )
const ( const (
@ -31,6 +32,7 @@ const (
) )
type Game struct { type Game struct {
init sync.Once
count int count int
brushRenderTarget ebiten.RenderTargetID brushRenderTarget ebiten.RenderTargetID
canvasRenderTarget ebiten.RenderTargetID canvasRenderTarget ebiten.RenderTargetID
@ -40,27 +42,15 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.count++ g.count++
} }
if g.brushRenderTarget.IsNil() { g.init.Do(func() {
var err error
g.brushRenderTarget, err = ebiten.NewRenderTargetID(1, 1, ebiten.FilterNearest)
if err != nil {
return err
}
gr.PushRenderTarget(g.brushRenderTarget) gr.PushRenderTarget(g.brushRenderTarget)
gr.Fill(0xff, 0xff, 0xff) gr.Fill(0xff, 0xff, 0xff)
gr.PopRenderTarget() 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.PushRenderTarget(g.canvasRenderTarget)
gr.Fill(0xff, 0xff, 0xff) gr.Fill(0xff, 0xff, 0xff)
gr.PopRenderTarget() gr.PopRenderTarget()
} })
mx, my := ebiten.CursorPosition() mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
@ -81,6 +71,15 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
func main() { func main() {
g := new(Game) 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 { if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -22,10 +22,6 @@ import (
) )
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { 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. // The defualt framebuffer should be 0.
r := opengl.NewRenderTarget(screenWidth*screenScale, screenHeight*screenScale, true) r := opengl.NewRenderTarget(screenWidth*screenScale, screenHeight*screenScale, true)

6
ui.go
View File

@ -18,6 +18,7 @@ package ebiten
import ( import (
"fmt" "fmt"
"github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"image" "image"
"runtime" "runtime"
@ -47,6 +48,11 @@ func init() {
funcs: make(chan func()), funcs: make(chan func()),
} }
currentUI.run() currentUI.run()
currentUI.use(func() {
gl.Init()
gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.BLEND)
})
} }
type ui struct { type ui struct {