mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
Bug fix: Creating a render target before running
This commit is contained in:
parent
ebdee00dd2
commit
33cb234ae1
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
6
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user