Remove package glfw (once)

This commit is contained in:
Hajime Hoshi 2014-12-14 16:53:32 +09:00
parent 76b7da090c
commit 8319e0d41d
10 changed files with 124 additions and 139 deletions

View File

@ -14,36 +14,29 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package glfw package ebiten
import ( import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten"
"image" "image"
"runtime" "runtime"
) )
type GraphicsContext interface {
ebiten.GraphicsContext
PreUpdate()
PostUpdate()
}
type canvas struct { type canvas struct {
window *glfw.Window window *glfw.Window
scale int scale int
graphicsContext GraphicsContext graphicsContext *graphicsContext
input input input Input
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
} }
func (c *canvas) draw(game ebiten.Game) (err error) { func (c *canvas) draw(game Game) (err error) {
c.use(func() { c.use(func() {
c.graphicsContext.PreUpdate() c.graphicsContext.PreUpdate()
}) })
if err = game.Draw(&graphicsContext{c}); err != nil { if err = game.Draw(&syncGraphicsContext{c}); err != nil {
return return
} }
c.use(func() { c.use(func() {
@ -57,38 +50,38 @@ func (c *canvas) isClosed() bool {
return c.window.ShouldClose() return c.window.ShouldClose()
} }
func (c *canvas) NewTextureID(img image.Image, filter ebiten.Filter) (ebiten.TextureID, error) { func (c *canvas) NewTextureID(img image.Image, filter Filter) (TextureID, error) {
var id ebiten.TextureID var id TextureID
var err error var err error
c.use(func() { c.use(func() {
glFilter := 0 glFilter := 0
switch filter { switch filter {
case ebiten.FilterNearest: case FilterNearest:
glFilter = gl.NEAREST glFilter = gl.NEAREST
case ebiten.FilterLinear: case FilterLinear:
glFilter = gl.LINEAR glFilter = gl.LINEAR
default: default:
panic("not reached") panic("not reached")
} }
id, err = ebiten.NewTextureID(img, glFilter) id, err = newTextureID(img, glFilter)
}) })
return id, err return id, err
} }
func (c *canvas) NewRenderTargetID(width, height int, filter ebiten.Filter) (ebiten.RenderTargetID, error) { func (c *canvas) NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
var id ebiten.RenderTargetID var id RenderTargetID
var err error var err error
c.use(func() { c.use(func() {
glFilter := 0 glFilter := 0
switch filter { switch filter {
case ebiten.FilterNearest: case FilterNearest:
glFilter = gl.NEAREST glFilter = gl.NEAREST
case ebiten.FilterLinear: case FilterLinear:
glFilter = gl.LINEAR glFilter = gl.LINEAR
default: default:
panic("not reached") panic("not reached")
} }
id, err = ebiten.NewRenderTargetID(width, height, glFilter) id, err = newRenderTargetID(width, height, glFilter)
}) })
return id, err return id, err
} }
@ -111,14 +104,14 @@ func (c *canvas) use(f func()) {
} }
func (c *canvas) update() { func (c *canvas) update() {
c.input.update(c.window, c.scale) c.input.Update(c.window, c.scale)
} }
func (c *canvas) IsKeyPressed(key ebiten.Key) bool { func (c *canvas) IsKeyPressed(key Key) bool {
return c.input.IsKeyPressed(key) return c.input.IsKeyPressed(key)
} }
func (c *canvas) IsMouseButtonPressed(button ebiten.MouseButton) bool { func (c *canvas) IsMouseButtonPressed(button MouseButton) bool {
return c.input.IsMouseButtonPressed(button) return c.input.IsMouseButtonPressed(button)
} }

View File

@ -18,8 +18,8 @@ package main
import ( import (
"flag" "flag"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/example/blocks" "github.com/hajimehoshi/ebiten/example/blocks"
"github.com/hajimehoshi/ebiten/runner"
"log" "log"
"os" "os"
"runtime" "runtime"
@ -44,7 +44,7 @@ func main() {
} }
game := blocks.NewGame() game := blocks.NewGame()
if err := runner.Run(game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil { if err := ebiten.Run(game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -21,7 +21,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
func Initialize(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) { func initialize(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
gl.Init() gl.Init()
gl.Enable(gl.TEXTURE_2D) gl.Enable(gl.TEXTURE_2D)
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)

4
ids.go
View File

@ -41,11 +41,11 @@ var idsInstance = &ids{
currentRenderTargetId: -1, currentRenderTargetId: -1,
} }
func NewRenderTargetID(width, height int, filter int) (RenderTargetID, error) { func newRenderTargetID(width, height int, filter int) (RenderTargetID, error) {
return idsInstance.createRenderTarget(width, height, filter) return idsInstance.createRenderTarget(width, height, filter)
} }
func NewTextureID(img image.Image, filter int) (TextureID, error) { func newTextureID(img image.Image, filter int) (TextureID, error) {
return idsInstance.createTexture(img, filter) return idsInstance.createTexture(img, filter)
} }

View File

@ -16,23 +16,46 @@ limitations under the License.
package ebiten package ebiten
type Key int import (
glfw "github.com/go-gl/glfw3"
// TODO: Add more keys. "math"
const (
KeyUp Key = iota
KeyDown
KeyLeft
KeyRight
KeySpace
KeyMax
) )
type MouseButton int type Input struct {
keyPressed [KeyMax]bool
mouseButtonPressed [MouseButtonMax]bool
cursorX int
cursorY int
}
const ( func (i *Input) IsKeyPressed(key Key) bool {
MouseButtonLeft MouseButton = iota return i.keyPressed[key]
MouseButtonRight }
MouseButtonMiddle
MouseButtonMax func (i *Input) IsMouseButtonPressed(button MouseButton) bool {
) return i.mouseButtonPressed[button]
}
func (i *Input) CursorPosition() (x, y int) {
return i.cursorX, i.cursorY
}
var glfwKeyCodeToKey = map[glfw.Key]Key{
glfw.KeySpace: KeySpace,
glfw.KeyLeft: KeyLeft,
glfw.KeyRight: KeyRight,
glfw.KeyUp: KeyUp,
glfw.KeyDown: KeyDown,
}
func (i *Input) Update(window *glfw.Window, scale int) {
for g, u := range glfwKeyCodeToKey {
i.keyPressed[u] = window.GetKey(g) == glfw.Press
}
for b := MouseButtonLeft; b < MouseButtonMax; b++ {
i.mouseButtonPressed[b] = window.GetMouseButton(glfw.MouseButton(b)) == glfw.Press
}
x, y := window.GetCursorPosition()
i.cursorX = int(math.Floor(x)) / scale
i.cursorY = int(math.Floor(y)) / scale
}

View File

@ -1,62 +0,0 @@
/*
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 glfw
import (
glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten"
"math"
)
type input struct {
keyPressed [ebiten.KeyMax]bool
mouseButtonPressed [ebiten.MouseButtonMax]bool
cursorX int
cursorY int
}
func (i *input) IsKeyPressed(key ebiten.Key) bool {
return i.keyPressed[key]
}
func (i *input) IsMouseButtonPressed(button ebiten.MouseButton) bool {
return i.mouseButtonPressed[button]
}
func (i *input) CursorPosition() (x, y int) {
return i.cursorX, i.cursorY
}
var glfwKeyCodeToKey = map[glfw.Key]ebiten.Key{
glfw.KeySpace: ebiten.KeySpace,
glfw.KeyLeft: ebiten.KeyLeft,
glfw.KeyRight: ebiten.KeyRight,
glfw.KeyUp: ebiten.KeyUp,
glfw.KeyDown: ebiten.KeyDown,
}
func (i *input) update(window *glfw.Window, scale int) {
for g, u := range glfwKeyCodeToKey {
i.keyPressed[u] = window.GetKey(g) == glfw.Press
}
for b := ebiten.MouseButtonLeft; b < ebiten.MouseButtonMax; b++ {
i.mouseButtonPressed[b] = window.GetMouseButton(glfw.MouseButton(b)) == glfw.Press
}
x, y := window.GetCursorPosition()
i.cursorX = int(math.Floor(x)) / scale
i.cursorY = int(math.Floor(y)) / scale
}

38
keys.go Normal file
View File

@ -0,0 +1,38 @@
/*
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 ebiten
type Key int
// TODO: Add more keys.
const (
KeyUp Key = iota
KeyDown
KeyLeft
KeyRight
KeySpace
KeyMax
)
type MouseButton int
const (
MouseButtonLeft MouseButton = iota
MouseButtonRight
MouseButtonMiddle
MouseButtonMax
)

View File

@ -14,11 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package runner package ebiten
import ( import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/glfw"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -26,8 +24,8 @@ import (
) )
// Run runs the game. // Run runs the game.
func Run(game ebiten.Game, width, height, scale int, title string, fps int) error { func Run(game Game, width, height, scale int, title string, fps int) error {
ui := new(glfw.UI) ui := new(ui)
if err := ui.Start(game, width, height, scale, title); err != nil { if err := ui.Start(game, width, height, scale, title); err != nil {
return err return err
} }

View File

@ -14,31 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package glfw package ebiten
import ( type syncGraphicsContext struct {
"github.com/hajimehoshi/ebiten"
)
type graphicsContext struct {
canvas *canvas canvas *canvas
} }
var _ ebiten.GraphicsContext = new(graphicsContext) var _ GraphicsContext = new(syncGraphicsContext)
func (c *graphicsContext) Clear() { func (c *syncGraphicsContext) Clear() {
c.canvas.use(func() { c.canvas.use(func() {
c.canvas.graphicsContext.Clear() c.canvas.graphicsContext.Clear()
}) })
} }
func (c *graphicsContext) Fill(r, g, b uint8) { func (c *syncGraphicsContext) Fill(r, g, b uint8) {
c.canvas.use(func() { c.canvas.use(func() {
c.canvas.graphicsContext.Fill(r, g, b) c.canvas.graphicsContext.Fill(r, g, b)
}) })
} }
func (c *graphicsContext) Texture(id ebiten.TextureID) (d ebiten.Drawer) { func (c *syncGraphicsContext) Texture(id TextureID) (d Drawer) {
c.canvas.use(func() { c.canvas.use(func() {
d = &drawer{ d = &drawer{
canvas: c.canvas, canvas: c.canvas,
@ -48,7 +44,7 @@ func (c *graphicsContext) Texture(id ebiten.TextureID) (d ebiten.Drawer) {
return return
} }
func (c *graphicsContext) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawer) { func (c *syncGraphicsContext) RenderTarget(id RenderTargetID) (d Drawer) {
c.canvas.use(func() { c.canvas.use(func() {
d = &drawer{ d = &drawer{
canvas: c.canvas, canvas: c.canvas,
@ -58,13 +54,13 @@ func (c *graphicsContext) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawe
return return
} }
func (c *graphicsContext) PopRenderTarget() { func (c *syncGraphicsContext) PopRenderTarget() {
c.canvas.use(func() { c.canvas.use(func() {
c.canvas.graphicsContext.PopRenderTarget() c.canvas.graphicsContext.PopRenderTarget()
}) })
} }
func (c *graphicsContext) PushRenderTarget(id ebiten.RenderTargetID) { func (c *syncGraphicsContext) PushRenderTarget(id RenderTargetID) {
c.canvas.use(func() { c.canvas.use(func() {
c.canvas.graphicsContext.PushRenderTarget(id) c.canvas.graphicsContext.PushRenderTarget(id)
}) })
@ -72,12 +68,12 @@ func (c *graphicsContext) PushRenderTarget(id ebiten.RenderTargetID) {
type drawer struct { type drawer struct {
canvas *canvas canvas *canvas
innerDrawer ebiten.Drawer innerDrawer Drawer
} }
var _ ebiten.Drawer = new(drawer) var _ Drawer = new(drawer)
func (d *drawer) Draw(parts []ebiten.TexturePart, geo ebiten.GeometryMatrix, color ebiten.ColorMatrix) { func (d *drawer) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) {
d.canvas.use(func() { d.canvas.use(func() {
d.innerDrawer.Draw(parts, geo, color) d.innerDrawer.Draw(parts, geo, color)
}) })

View File

@ -14,13 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package glfw package ebiten
import ( import (
"errors" "errors"
"fmt" "fmt"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten"
) )
func init() { func init() {
@ -29,11 +28,11 @@ func init() {
}) })
} }
type UI struct { type ui struct {
canvas *canvas canvas *canvas
} }
func (u *UI) Start(game ebiten.Game, width, height, scale int, title string) error { func (u *ui) Start(game Game, width, height, scale int, title string) error {
if !glfw.Init() { if !glfw.Init() {
return errors.New("glfw.Init() fails") return errors.New("glfw.Init() fails")
} }
@ -56,7 +55,7 @@ func (u *UI) Start(game ebiten.Game, width, height, scale int, title string) err
windowWidth, _ := window.GetFramebufferSize() windowWidth, _ := window.GetFramebufferSize()
realScale := windowWidth / width realScale := windowWidth / width
c.use(func() { c.use(func() {
c.graphicsContext, err = ebiten.Initialize(width, height, realScale) c.graphicsContext, err = initialize(width, height, realScale)
}) })
if err != nil { if err != nil {
return err return err
@ -70,19 +69,19 @@ func (u *UI) Start(game ebiten.Game, width, height, scale int, title string) err
return nil return nil
} }
func (u *UI) DoEvents() { func (u *ui) DoEvents() {
glfw.PollEvents() glfw.PollEvents()
u.canvas.update() u.canvas.update()
} }
func (u *UI) Terminate() { func (u *ui) Terminate() {
glfw.Terminate() glfw.Terminate()
} }
func (u *UI) IsClosed() bool { func (u *ui) IsClosed() bool {
return u.canvas.isClosed() return u.canvas.isClosed()
} }
func (u *UI) DrawGame(game ebiten.Game) error { func (u *ui) DrawGame(game Game) error {
return u.canvas.draw(game) return u.canvas.draw(game)
} }