Simplify API: Make loading textures enable before running a game (#24)

This commit is contained in:
Hajime Hoshi 2014-12-17 20:03:26 +09:00
parent a95ed2cd2a
commit ebdee00dd2
4 changed files with 90 additions and 57 deletions

36
ebitenutil/texture.go Normal file
View File

@ -0,0 +1,36 @@
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ebitenutil
import (
"github.com/hajimehoshi/ebiten"
"image"
"os"
)
func LoadImageAndCreateTexture(path string, filter ebiten.Filter) (ebiten.TextureID, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return 0, err
}
return ebiten.NewTextureID(img, filter)
}

View File

@ -18,10 +18,9 @@ package main
import ( import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"image" "github.com/hajimehoshi/ebiten/ebitenutil"
_ "image/jpeg" _ "image/jpeg"
"log" "log"
"os"
) )
const ( const (
@ -34,31 +33,17 @@ type Game struct {
} }
func (g *Game) Update(gr ebiten.GraphicsContext) error { func (g *Game) Update(gr ebiten.GraphicsContext) error {
if g.gophersTexture.IsNil() {
file, err := os.Open("images/gophers.jpg")
if err != nil {
return err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return err
}
id, err := ebiten.NewTextureID(img, ebiten.FilterLinear)
if err != nil {
return err
}
g.gophersTexture = id
}
if g.gophersTexture.IsNil() {
return nil
}
ebiten.DrawWhole(gr.Texture(g.gophersTexture), 500, 414, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) ebiten.DrawWhole(gr.Texture(g.gophersTexture), 500, 414, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
return nil return nil
} }
func main() { func main() {
g := new(Game) g := new(Game)
id, err := ebitenutil.LoadImageAndCreateTexture("images/gophers.jpg", ebiten.FilterLinear)
if err != nil {
log.Fatal(err)
}
g.gophersTexture = id
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Image (Ebiten Demo)"); err != nil { if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Image (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)
} }

13
run.go
View File

@ -16,23 +16,14 @@ limitations under the License.
package ebiten package ebiten
import (
"runtime"
)
var currentUI *ui
func init() {
runtime.LockOSThread()
}
// Run runs the game. // Run runs the game.
// This function must be called from the main thread. // This function must be called from the main thread.
func Run(f func(GraphicsContext) error, width, height, scale int, title string) error { func Run(f func(GraphicsContext) error, width, height, scale int, title string) error {
ui, err := newUI(width, height, scale, title) err := startUI(width, height, scale, title)
if err != nil { if err != nil {
return err return err
} }
ui := currentUI
defer ui.terminate() defer ui.terminate()
currentUI = ui currentUI = ui

71
ui.go
View File

@ -17,13 +17,38 @@ limitations under the License.
package ebiten package ebiten
import ( import (
"errors"
"fmt" "fmt"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"image" "image"
"runtime" "runtime"
) )
var currentUI *ui
func init() {
runtime.LockOSThread()
glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
panic(fmt.Sprintf("%v: %v\n", err, desc))
})
if !glfw.Init() {
panic("glfw.Init() fails")
}
glfw.WindowHint(glfw.Visible, glfw.False)
glfw.WindowHint(glfw.Resizable, glfw.False)
window, err := glfw.CreateWindow(16, 16, "", nil, nil)
if err != nil {
panic(err)
}
currentUI = &ui{
window: window,
funcs: make(chan func()),
}
currentUI.run()
}
type ui struct { type ui struct {
window *glfw.Window window *glfw.Window
scale int scale int
@ -32,37 +57,34 @@ type ui struct {
funcs chan func() funcs chan func()
} }
func newUI(width, height, scale int, title string) (*ui, error) { func startUI(width, height, scale int, title string) error {
glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) { monitor, err := glfw.GetPrimaryMonitor()
panic(fmt.Sprintf("%v: %v\n", err, desc))
})
if !glfw.Init() {
return nil, errors.New("glfw.Init() fails")
}
glfw.WindowHint(glfw.Resizable, glfw.False)
window, err := glfw.CreateWindow(width*scale, height*scale, title, nil, nil)
if err != nil { if err != nil {
return nil, err return err
} }
videoMode, err := monitor.GetVideoMode()
u := &ui{ if err != nil {
window: window, return err
scale: scale,
funcs: make(chan func()),
} }
x := (videoMode.Width - width*scale) / 2
y := (videoMode.Height - height*scale) / 3
u.run(width, height, scale) window := currentUI.window
window.SetSize(width*scale, height*scale)
window.SetTitle(title)
window.SetPosition(x, y)
window.Show()
ui := currentUI
ui.scale = scale
// For retina displays, recalculate the scale with the framebuffer size. // For retina displays, recalculate the scale with the framebuffer size.
windowWidth, _ := window.GetFramebufferSize() windowWidth, _ := window.GetFramebufferSize()
realScale := windowWidth / width realScale := windowWidth / width
u.use(func() { ui.use(func() {
u.graphicsContext, err = newGraphicsContext(width, height, realScale) ui.graphicsContext, err = newGraphicsContext(width, height, realScale)
}) })
if err != nil { return err
return nil, err
}
return u, nil
} }
func (u *ui) doEvents() { func (u *ui) doEvents() {
@ -117,11 +139,10 @@ func (u *ui) newRenderTargetID(width, height int, filter int) (RenderTargetID, e
return id, err return id, err
} }
func (u *ui) run(width, height, scale int) { func (u *ui) run() {
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
u.window.MakeContextCurrent() u.window.MakeContextCurrent()
glfw.SwapInterval(1)
for f := range u.funcs { for f := range u.funcs {
f() f()
} }