mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Add GameContext; Remove Input and TextureFactory
This commit is contained in:
parent
b313578a24
commit
343916ad29
35
gamecontext.go
Normal file
35
gamecontext.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GameContext interface {
|
||||||
|
IsKeyPressed(key Key) bool
|
||||||
|
CursorPosition() (x, y int)
|
||||||
|
IsMouseButtonPressed(mouseButton MouseButton) bool
|
||||||
|
NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error)
|
||||||
|
NewTextureID(img image.Image, filter Filter) (TextureID, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentGameContext GameContext
|
||||||
|
|
||||||
|
func SetGameContext(g GameContext) {
|
||||||
|
currentGameContext = g
|
||||||
|
}
|
30
input.go
30
input.go
@ -37,35 +37,23 @@ const (
|
|||||||
MouseButtonMax
|
MouseButtonMax
|
||||||
)
|
)
|
||||||
|
|
||||||
type Input interface {
|
|
||||||
IsKeyPressed(key Key) bool
|
|
||||||
CursorPosition() (x, y int)
|
|
||||||
IsMouseButtonPressed(mouseButton MouseButton) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentInput Input
|
|
||||||
|
|
||||||
func SetInput(input Input) {
|
|
||||||
currentInput = input
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsKeyPressed(key Key) bool {
|
func IsKeyPressed(key Key) bool {
|
||||||
if currentInput == nil {
|
if currentGameContext == nil {
|
||||||
panic("ebiten.IsKeyPressed: currentInput is not set")
|
panic("ebiten.IsKeyPressed: currentGameContext is not set")
|
||||||
}
|
}
|
||||||
return currentInput.IsKeyPressed(key)
|
return currentGameContext.IsKeyPressed(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CursorPosition() (x, y int) {
|
func CursorPosition() (x, y int) {
|
||||||
if currentInput == nil {
|
if currentGameContext == nil {
|
||||||
panic("ebiten.CurrentPosition: currentInput is not set")
|
panic("ebiten.CurrentPosition: currentGameContext is not set")
|
||||||
}
|
}
|
||||||
return currentInput.CursorPosition()
|
return currentGameContext.CursorPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsMouseButtonPressed(button MouseButton) bool {
|
func IsMouseButtonPressed(button MouseButton) bool {
|
||||||
if currentInput == nil {
|
if currentGameContext == nil {
|
||||||
panic("ebiten.IsMouseButtonPressed: currentInput is not set")
|
panic("ebiten.IsMouseButtonPressed: currentGameContext is not set")
|
||||||
}
|
}
|
||||||
return currentInput.IsMouseButtonPressed(button)
|
return currentGameContext.IsMouseButtonPressed(button)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ import (
|
|||||||
|
|
||||||
type canvas struct {
|
type canvas struct {
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
context *opengl.GraphicsContext
|
graphicsContext *opengl.GraphicsContext
|
||||||
input input
|
input input
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
@ -34,13 +34,13 @@ type canvas struct {
|
|||||||
|
|
||||||
func (c *canvas) draw(d GraphicsContextDrawer) (err error) {
|
func (c *canvas) draw(d GraphicsContextDrawer) (err error) {
|
||||||
c.use(func() {
|
c.use(func() {
|
||||||
c.context.PreUpdate()
|
c.graphicsContext.PreUpdate()
|
||||||
})
|
})
|
||||||
if err = d.Draw(&context{c}); err != nil {
|
if err = d.Draw(&graphicsContext{c}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.use(func() {
|
c.use(func() {
|
||||||
c.context.PostUpdate()
|
c.graphicsContext.PostUpdate()
|
||||||
c.window.SwapBuffers()
|
c.window.SwapBuffers()
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
@ -88,3 +88,15 @@ func (c *canvas) use(f func()) {
|
|||||||
func (c *canvas) update() {
|
func (c *canvas) update() {
|
||||||
c.input.update(c.window)
|
c.input.update(c.window)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *canvas) IsKeyPressed(key ebiten.Key) bool {
|
||||||
|
return c.input.IsKeyPressed(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *canvas) IsMouseButtonPressed(button ebiten.MouseButton) bool {
|
||||||
|
return c.input.IsMouseButtonPressed(button)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *canvas) CursorPosition() (x, y int) {
|
||||||
|
return c.input.CursorPosition()
|
||||||
|
}
|
||||||
|
@ -20,53 +20,53 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
)
|
)
|
||||||
|
|
||||||
type context struct {
|
type graphicsContext struct {
|
||||||
canvas *canvas
|
canvas *canvas
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ ebiten.GraphicsContext = new(context)
|
var _ ebiten.GraphicsContext = new(graphicsContext)
|
||||||
|
|
||||||
func (c *context) Clear() {
|
func (c *graphicsContext) Clear() {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
c.canvas.context.Clear()
|
c.canvas.graphicsContext.Clear()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Fill(r, g, b uint8) {
|
func (c *graphicsContext) Fill(r, g, b uint8) {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
c.canvas.context.Fill(r, g, b)
|
c.canvas.graphicsContext.Fill(r, g, b)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) Texture(id ebiten.TextureID) (d ebiten.Drawer) {
|
func (c *graphicsContext) Texture(id ebiten.TextureID) (d ebiten.Drawer) {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
d = &drawer{
|
d = &drawer{
|
||||||
canvas: c.canvas,
|
canvas: c.canvas,
|
||||||
innerDrawer: c.canvas.context.Texture(id),
|
innerDrawer: c.canvas.graphicsContext.Texture(id),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawer) {
|
func (c *graphicsContext) RenderTarget(id ebiten.RenderTargetID) (d ebiten.Drawer) {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
d = &drawer{
|
d = &drawer{
|
||||||
canvas: c.canvas,
|
canvas: c.canvas,
|
||||||
innerDrawer: c.canvas.context.RenderTarget(id),
|
innerDrawer: c.canvas.graphicsContext.RenderTarget(id),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) ResetOffscreen() {
|
func (c *graphicsContext) ResetOffscreen() {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
c.canvas.context.ResetOffscreen()
|
c.canvas.graphicsContext.ResetOffscreen()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetOffscreen(id ebiten.RenderTargetID) {
|
func (c *graphicsContext) SetOffscreen(id ebiten.RenderTargetID) {
|
||||||
c.canvas.use(func() {
|
c.canvas.use(func() {
|
||||||
c.canvas.context.SetOffscreen(id)
|
c.canvas.graphicsContext.SetOffscreen(id)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,8 +53,7 @@ func (u *UI) Start(width, height, scale int, title string) error {
|
|||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
ebiten.SetInput(&c.input)
|
ebiten.SetGameContext(c)
|
||||||
ebiten.SetTextureFactory(c)
|
|
||||||
|
|
||||||
c.run(width, height, scale)
|
c.run(width, height, scale)
|
||||||
|
|
||||||
@ -62,13 +61,14 @@ func (u *UI) Start(width, height, scale int, title string) error {
|
|||||||
windowWidth, _ := window.GetFramebufferSize()
|
windowWidth, _ := window.GetFramebufferSize()
|
||||||
realScale := windowWidth / width
|
realScale := windowWidth / width
|
||||||
c.use(func() {
|
c.use(func() {
|
||||||
c.context, err = opengl.Initialize(width, height, realScale)
|
c.graphicsContext, err = opengl.Initialize(width, height, realScale)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
u.canvas = c
|
u.canvas = c
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,33 +47,18 @@ func (i RenderTargetID) IsNil() bool {
|
|||||||
return i == 0
|
return i == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentTextureFactory TextureFactory
|
|
||||||
|
|
||||||
// A TextureFactory is the interface that creates a render target or a texture.
|
|
||||||
// This method is for the library and a game developer doesn't have to use this.
|
|
||||||
type TextureFactory interface {
|
|
||||||
NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error)
|
|
||||||
NewTextureID(img image.Image, filter Filter) (TextureID, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTextureFactory sets the current texture factory.
|
|
||||||
// This method is for the library and a game developer doesn't have to use this.
|
|
||||||
func SetTextureFactory(textureFactory TextureFactory) {
|
|
||||||
currentTextureFactory = textureFactory
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRenderTargetID returns an ID of a newly created render target.
|
// NewRenderTargetID returns an ID of a newly created render target.
|
||||||
func NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
|
func NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
|
||||||
if currentTextureFactory == nil {
|
if currentGameContext == nil {
|
||||||
panic("graphics.NewRenderTarget: currentTextureFactory is not set.")
|
panic("graphics.NewRenderTarget: currentGameContext is not set.")
|
||||||
}
|
}
|
||||||
return currentTextureFactory.NewRenderTargetID(width, height, filter)
|
return currentGameContext.NewRenderTargetID(width, height, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRenderTargetID returns an ID of a newly created texture.
|
// NewRenderTargetID returns an ID of a newly created texture.
|
||||||
func NewTextureID(img image.Image, filter Filter) (TextureID, error) {
|
func NewTextureID(img image.Image, filter Filter) (TextureID, error) {
|
||||||
if currentTextureFactory == nil {
|
if currentGameContext == nil {
|
||||||
panic("graphics.NewTexture: currentTextureFactory is not set")
|
panic("graphics.NewTexture: currentGameContext is not set")
|
||||||
}
|
}
|
||||||
return currentTextureFactory.NewTextureID(img, filter)
|
return currentGameContext.NewTextureID(img, filter)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user