From 8319e0d41d3ec74c6dadd8bd72d459ff40d6fd7d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 14 Dec 2014 16:53:32 +0900 Subject: [PATCH] Remove package glfw (once) --- internal/glfw/canvas.go => canvas.go | 43 ++++++------- example/blocks.go | 4 +- graphicscontext.go | 2 +- ids.go | 4 +- input.go | 57 ++++++++++++----- internal/glfw/input.go | 62 ------------------- keys.go | 38 ++++++++++++ runner/run.go => run.go | 8 +-- .../glfw/context.go => syncgraphicscontext.go | 28 ++++----- internal/glfw/ui.go => ui.go | 17 +++-- 10 files changed, 124 insertions(+), 139 deletions(-) rename internal/glfw/canvas.go => canvas.go (66%) delete mode 100644 internal/glfw/input.go create mode 100644 keys.go rename runner/run.go => run.go (85%) rename internal/glfw/context.go => syncgraphicscontext.go (65%) rename internal/glfw/ui.go => ui.go (81%) diff --git a/internal/glfw/canvas.go b/canvas.go similarity index 66% rename from internal/glfw/canvas.go rename to canvas.go index 17c7da5f8..3cad49243 100644 --- a/internal/glfw/canvas.go +++ b/canvas.go @@ -14,36 +14,29 @@ See the License for the specific language governing permissions and limitations under the License. */ -package glfw +package ebiten import ( "github.com/go-gl/gl" glfw "github.com/go-gl/glfw3" - "github.com/hajimehoshi/ebiten" "image" "runtime" ) -type GraphicsContext interface { - ebiten.GraphicsContext - PreUpdate() - PostUpdate() -} - type canvas struct { window *glfw.Window scale int - graphicsContext GraphicsContext - input input + graphicsContext *graphicsContext + input Input funcs chan func() funcsDone chan struct{} } -func (c *canvas) draw(game ebiten.Game) (err error) { +func (c *canvas) draw(game Game) (err error) { c.use(func() { c.graphicsContext.PreUpdate() }) - if err = game.Draw(&graphicsContext{c}); err != nil { + if err = game.Draw(&syncGraphicsContext{c}); err != nil { return } c.use(func() { @@ -57,38 +50,38 @@ func (c *canvas) isClosed() bool { return c.window.ShouldClose() } -func (c *canvas) NewTextureID(img image.Image, filter ebiten.Filter) (ebiten.TextureID, error) { - var id ebiten.TextureID +func (c *canvas) NewTextureID(img image.Image, filter Filter) (TextureID, error) { + var id TextureID var err error c.use(func() { glFilter := 0 switch filter { - case ebiten.FilterNearest: + case FilterNearest: glFilter = gl.NEAREST - case ebiten.FilterLinear: + case FilterLinear: glFilter = gl.LINEAR default: panic("not reached") } - id, err = ebiten.NewTextureID(img, glFilter) + id, err = newTextureID(img, glFilter) }) return id, err } -func (c *canvas) NewRenderTargetID(width, height int, filter ebiten.Filter) (ebiten.RenderTargetID, error) { - var id ebiten.RenderTargetID +func (c *canvas) NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) { + var id RenderTargetID var err error c.use(func() { glFilter := 0 switch filter { - case ebiten.FilterNearest: + case FilterNearest: glFilter = gl.NEAREST - case ebiten.FilterLinear: + case FilterLinear: glFilter = gl.LINEAR default: panic("not reached") } - id, err = ebiten.NewRenderTargetID(width, height, glFilter) + id, err = newRenderTargetID(width, height, glFilter) }) return id, err } @@ -111,14 +104,14 @@ func (c *canvas) use(f func()) { } 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) } -func (c *canvas) IsMouseButtonPressed(button ebiten.MouseButton) bool { +func (c *canvas) IsMouseButtonPressed(button MouseButton) bool { return c.input.IsMouseButtonPressed(button) } diff --git a/example/blocks.go b/example/blocks.go index ddc7d0f6e..c4f35cb02 100644 --- a/example/blocks.go +++ b/example/blocks.go @@ -18,8 +18,8 @@ package main import ( "flag" + "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/example/blocks" - "github.com/hajimehoshi/ebiten/runner" "log" "os" "runtime" @@ -44,7 +44,7 @@ func main() { } 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) } } diff --git a/graphicscontext.go b/graphicscontext.go index 1f300b7d2..7145a96cb 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -21,7 +21,7 @@ import ( "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.Enable(gl.TEXTURE_2D) gl.Enable(gl.BLEND) diff --git a/ids.go b/ids.go index 870b39786..43bc54e25 100644 --- a/ids.go +++ b/ids.go @@ -41,11 +41,11 @@ var idsInstance = &ids{ 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) } -func NewTextureID(img image.Image, filter int) (TextureID, error) { +func newTextureID(img image.Image, filter int) (TextureID, error) { return idsInstance.createTexture(img, filter) } diff --git a/input.go b/input.go index f96cd6a36..aba338372 100644 --- a/input.go +++ b/input.go @@ -16,23 +16,46 @@ limitations under the License. package ebiten -type Key int - -// TODO: Add more keys. -const ( - KeyUp Key = iota - KeyDown - KeyLeft - KeyRight - KeySpace - KeyMax +import ( + glfw "github.com/go-gl/glfw3" + "math" ) -type MouseButton int +type Input struct { + keyPressed [KeyMax]bool + mouseButtonPressed [MouseButtonMax]bool + cursorX int + cursorY int +} -const ( - MouseButtonLeft MouseButton = iota - MouseButtonRight - MouseButtonMiddle - MouseButtonMax -) +func (i *Input) IsKeyPressed(key Key) bool { + return i.keyPressed[key] +} + +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 +} diff --git a/internal/glfw/input.go b/internal/glfw/input.go deleted file mode 100644 index 951fe8e73..000000000 --- a/internal/glfw/input.go +++ /dev/null @@ -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 -} diff --git a/keys.go b/keys.go new file mode 100644 index 000000000..f96cd6a36 --- /dev/null +++ b/keys.go @@ -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 +) diff --git a/runner/run.go b/run.go similarity index 85% rename from runner/run.go rename to run.go index 4bb12fc3c..58be47f1a 100644 --- a/runner/run.go +++ b/run.go @@ -14,11 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -package runner +package ebiten import ( - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/internal/glfw" "os" "os/signal" "syscall" @@ -26,8 +24,8 @@ import ( ) // Run runs the game. -func Run(game ebiten.Game, width, height, scale int, title string, fps int) error { - ui := new(glfw.UI) +func Run(game Game, width, height, scale int, title string, fps int) error { + ui := new(ui) if err := ui.Start(game, width, height, scale, title); err != nil { return err } diff --git a/internal/glfw/context.go b/syncgraphicscontext.go similarity index 65% rename from internal/glfw/context.go rename to syncgraphicscontext.go index 970fe6f74..14bbd8a0c 100644 --- a/internal/glfw/context.go +++ b/syncgraphicscontext.go @@ -14,31 +14,27 @@ See the License for the specific language governing permissions and limitations under the License. */ -package glfw +package ebiten -import ( - "github.com/hajimehoshi/ebiten" -) - -type graphicsContext struct { +type syncGraphicsContext struct { 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.graphicsContext.Clear() }) } -func (c *graphicsContext) Fill(r, g, b uint8) { +func (c *syncGraphicsContext) Fill(r, g, b uint8) { c.canvas.use(func() { 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() { d = &drawer{ canvas: c.canvas, @@ -48,7 +44,7 @@ func (c *graphicsContext) Texture(id ebiten.TextureID) (d ebiten.Drawer) { return } -func (c *graphicsContext) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawer) { +func (c *syncGraphicsContext) RenderTarget(id RenderTargetID) (d Drawer) { c.canvas.use(func() { d = &drawer{ canvas: c.canvas, @@ -58,13 +54,13 @@ func (c *graphicsContext) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawe return } -func (c *graphicsContext) PopRenderTarget() { +func (c *syncGraphicsContext) PopRenderTarget() { c.canvas.use(func() { c.canvas.graphicsContext.PopRenderTarget() }) } -func (c *graphicsContext) PushRenderTarget(id ebiten.RenderTargetID) { +func (c *syncGraphicsContext) PushRenderTarget(id RenderTargetID) { c.canvas.use(func() { c.canvas.graphicsContext.PushRenderTarget(id) }) @@ -72,12 +68,12 @@ func (c *graphicsContext) PushRenderTarget(id ebiten.RenderTargetID) { type drawer struct { 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.innerDrawer.Draw(parts, geo, color) }) diff --git a/internal/glfw/ui.go b/ui.go similarity index 81% rename from internal/glfw/ui.go rename to ui.go index 19d0f355d..01aa2d825 100644 --- a/internal/glfw/ui.go +++ b/ui.go @@ -14,13 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package glfw +package ebiten import ( "errors" "fmt" glfw "github.com/go-gl/glfw3" - "github.com/hajimehoshi/ebiten" ) func init() { @@ -29,11 +28,11 @@ func init() { }) } -type UI struct { +type ui struct { 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() { 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() realScale := windowWidth / width c.use(func() { - c.graphicsContext, err = ebiten.Initialize(width, height, realScale) + c.graphicsContext, err = initialize(width, height, realScale) }) if err != nil { return err @@ -70,19 +69,19 @@ func (u *UI) Start(game ebiten.Game, width, height, scale int, title string) err return nil } -func (u *UI) DoEvents() { +func (u *ui) DoEvents() { glfw.PollEvents() u.canvas.update() } -func (u *UI) Terminate() { +func (u *ui) Terminate() { glfw.Terminate() } -func (u *UI) IsClosed() bool { +func (u *ui) IsClosed() bool { return u.canvas.isClosed() } -func (u *UI) DrawGame(game ebiten.Game) error { +func (u *ui) DrawGame(game Game) error { return u.canvas.draw(game) }