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"
)
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

View File

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

View File

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

View File

@ -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)

6
ui.go
View File

@ -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 {