mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
Remove package glfw (once)
This commit is contained in:
parent
76b7da090c
commit
8319e0d41d
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
4
ids.go
4
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)
|
||||
}
|
||||
|
||||
|
57
input.go
57
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
|
||||
}
|
||||
|
@ -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
38
keys.go
Normal 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
|
||||
)
|
@ -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
|
||||
}
|
@ -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)
|
||||
})
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user