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 (
"github.com/hajimehoshi/ebiten"
"image"
"github.com/hajimehoshi/ebiten/ebitenutil"
_ "image/jpeg"
"log"
"os"
)
const (
@ -34,31 +33,17 @@ type Game struct {
}
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())
return nil
}
func main() {
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 {
log.Fatal(err)
}

13
run.go
View File

@ -16,23 +16,14 @@ limitations under the License.
package ebiten
import (
"runtime"
)
var currentUI *ui
func init() {
runtime.LockOSThread()
}
// Run runs the game.
// This function must be called from the main thread.
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 {
return err
}
ui := currentUI
defer ui.terminate()
currentUI = ui

71
ui.go
View File

@ -17,13 +17,38 @@ limitations under the License.
package ebiten
import (
"errors"
"fmt"
glfw "github.com/go-gl/glfw3"
"image"
"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 {
window *glfw.Window
scale int
@ -32,37 +57,34 @@ type ui struct {
funcs chan func()
}
func newUI(width, height, scale int, title string) (*ui, error) {
glfw.SetErrorCallback(func(err glfw.ErrorCode, desc string) {
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)
func startUI(width, height, scale int, title string) error {
monitor, err := glfw.GetPrimaryMonitor()
if err != nil {
return nil, err
return err
}
u := &ui{
window: window,
scale: scale,
funcs: make(chan func()),
videoMode, err := monitor.GetVideoMode()
if err != nil {
return err
}
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.
windowWidth, _ := window.GetFramebufferSize()
realScale := windowWidth / width
u.use(func() {
u.graphicsContext, err = newGraphicsContext(width, height, realScale)
ui.use(func() {
ui.graphicsContext, err = newGraphicsContext(width, height, realScale)
})
if err != nil {
return nil, err
}
return u, nil
return err
}
func (u *ui) doEvents() {
@ -117,11 +139,10 @@ func (u *ui) newRenderTargetID(width, height int, filter int) (RenderTargetID, e
return id, err
}
func (u *ui) run(width, height, scale int) {
func (u *ui) run() {
go func() {
runtime.LockOSThread()
u.window.MakeContextCurrent()
glfw.SwapInterval(1)
for f := range u.funcs {
f()
}