mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
Remove GameContext
This commit is contained in:
parent
87bdd1a994
commit
d0c0dea2be
@ -30,8 +30,8 @@ type debugPrintState struct {
|
||||
|
||||
var defaultDebugPrintState = new(debugPrintState)
|
||||
|
||||
func DebugPrint(ga ebiten.GameContext, gr ebiten.GraphicsContext, str string) {
|
||||
defaultDebugPrintState.DebugPrint(ga, gr, str)
|
||||
func DebugPrint(gr ebiten.GraphicsContext, str string) {
|
||||
defaultDebugPrintState.DebugPrint(gr, str)
|
||||
}
|
||||
|
||||
func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y int, clr color.Color) {
|
||||
@ -61,13 +61,13 @@ func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y i
|
||||
gr.Texture(d.textTexture).Draw(parts, geom, clrm)
|
||||
}
|
||||
|
||||
func (d *debugPrintState) DebugPrint(ga ebiten.GameContext, gr ebiten.GraphicsContext, str string) {
|
||||
func (d *debugPrintState) DebugPrint(gr ebiten.GraphicsContext, str string) {
|
||||
if d.textTexture.IsNil() {
|
||||
img, err := assets.TextImage()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
d.textTexture, err = ga.NewTextureID(img, ebiten.FilterNearest)
|
||||
d.textTexture, err = ebiten.NewTextureID(img, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -75,7 +75,7 @@ func (d *debugPrintState) DebugPrint(ga ebiten.GameContext, gr ebiten.GraphicsCo
|
||||
if d.debugPrintRenderTarget.IsNil() {
|
||||
width, height := 256, 256
|
||||
var err error
|
||||
d.debugPrintRenderTarget, err = ga.NewRenderTargetID(width, height, ebiten.FilterNearest)
|
||||
d.debugPrintRenderTarget, err = ebiten.NewRenderTargetID(width, height, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Size struct {
|
||||
@ -38,7 +39,7 @@ type GameState struct {
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
gameContext ebiten.GameContext
|
||||
once sync.Once
|
||||
sceneManager *SceneManager
|
||||
input *Input
|
||||
textures *Textures
|
||||
@ -69,23 +70,20 @@ func (game *Game) isInitialized() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (game *Game) Initialize(g ebiten.GameContext) error {
|
||||
game.gameContext = g
|
||||
game.textures = NewTextures(g)
|
||||
for name, path := range texturePaths {
|
||||
game.textures.RequestTexture(name, path)
|
||||
}
|
||||
for name, size := range renderTargetSizes {
|
||||
game.textures.RequestRenderTarget(name, size)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (game *Game) Update() error {
|
||||
game.once.Do(func() {
|
||||
game.textures = NewTextures()
|
||||
for name, path := range texturePaths {
|
||||
game.textures.RequestTexture(name, path)
|
||||
}
|
||||
for name, size := range renderTargetSizes {
|
||||
game.textures.RequestRenderTarget(name, size)
|
||||
}
|
||||
})
|
||||
if !game.isInitialized() {
|
||||
return nil
|
||||
}
|
||||
game.input.Update(game.gameContext)
|
||||
game.input.Update()
|
||||
game.sceneManager.Update(&GameState{
|
||||
SceneManager: game.sceneManager,
|
||||
Input: game.input,
|
||||
|
@ -38,9 +38,9 @@ func (i *Input) StateForKey(key ebiten.Key) int {
|
||||
return i.states[key]
|
||||
}
|
||||
|
||||
func (i *Input) Update(g ebiten.GameContext) {
|
||||
func (i *Input) Update() {
|
||||
for key := range i.states {
|
||||
if !g.IsKeyPressed(key) {
|
||||
if !ebiten.IsKeyPressed(key) {
|
||||
i.states[key] = 0
|
||||
continue
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ type nameSize struct {
|
||||
}
|
||||
|
||||
type Textures struct {
|
||||
gameContext ebiten.GameContext
|
||||
texturePaths chan namePath
|
||||
renderTargetSizes chan nameSize
|
||||
textures map[string]ebiten.TextureID
|
||||
@ -43,9 +42,8 @@ type Textures struct {
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func NewTextures(g ebiten.GameContext) *Textures {
|
||||
func NewTextures() *Textures {
|
||||
textures := &Textures{
|
||||
gameContext: g,
|
||||
texturePaths: make(chan namePath),
|
||||
renderTargetSizes: make(chan nameSize),
|
||||
textures: map[string]ebiten.TextureID{},
|
||||
@ -83,7 +81,7 @@ func (t *Textures) loopMain() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
id, err := t.gameContext.NewTextureID(img, ebiten.FilterNearest)
|
||||
id, err := ebiten.NewTextureID(img, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -95,7 +93,7 @@ func (t *Textures) loopMain() {
|
||||
name := s.name
|
||||
size := s.size
|
||||
go func() {
|
||||
id, err := t.gameContext.NewRenderTargetID(size.Width, size.Height, ebiten.FilterNearest)
|
||||
id, err := ebiten.NewRenderTargetID(size.Width, size.Height, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"fmt"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/runner"
|
||||
"log"
|
||||
"runtime"
|
||||
)
|
||||
@ -15,15 +14,9 @@ const (
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
gameContext ebiten.GameContext
|
||||
canvasRenderTarget ebiten.RenderTargetID
|
||||
}
|
||||
|
||||
func (g *Game) Initialize(ga ebiten.GameContext) error {
|
||||
g.gameContext = ga
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Update() error {
|
||||
// TODO: Implement
|
||||
return nil
|
||||
@ -32,7 +25,7 @@ func (g *Game) Update() error {
|
||||
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
|
||||
if g.canvasRenderTarget.IsNil() {
|
||||
var err error
|
||||
g.canvasRenderTarget, err = g.gameContext.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest)
|
||||
g.canvasRenderTarget, err = ebiten.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -42,8 +35,8 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
|
||||
}
|
||||
ebiten.DrawWhole(gr.RenderTarget(g.canvasRenderTarget), screenWidth, screenHeight, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
|
||||
|
||||
mx, my := g.gameContext.CursorPosition()
|
||||
ebitenutil.DebugPrint(g.gameContext, gr, fmt.Sprintf("(%d, %d)", mx, my))
|
||||
mx, my := ebiten.CursorPosition()
|
||||
ebitenutil.DebugPrint(gr, fmt.Sprintf("(%d, %d)", mx, my))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -53,7 +46,7 @@ func init() {
|
||||
|
||||
func main() {
|
||||
game := new(Game)
|
||||
if err := runner.Run(game, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)", 60); err != nil {
|
||||
if err := ebiten.Run(game, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)", 60); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -21,15 +21,26 @@ import (
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Initialize(g GameContext) error
|
||||
Update() error
|
||||
Draw(gr GraphicsContext) error
|
||||
}
|
||||
|
||||
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)
|
||||
func IsKeyPressed(key Key) bool {
|
||||
return currentUI.canvas.input.IsKeyPressed(key)
|
||||
}
|
||||
|
||||
func CursorPosition() (x, y int) {
|
||||
return currentUI.canvas.input.CursorPosition()
|
||||
}
|
||||
|
||||
func IsMouseButtonPressed(mouseButton MouseButton) bool {
|
||||
return currentUI.canvas.input.IsMouseButtonPressed(mouseButton)
|
||||
}
|
||||
|
||||
func NewRenderTargetID(width, height int, filter Filter) (RenderTargetID, error) {
|
||||
return currentUI.canvas.NewRenderTargetID(width, height, filter)
|
||||
}
|
||||
|
||||
func NewTextureID(img image.Image, filter Filter) (TextureID, error) {
|
||||
return currentUI.canvas.NewTextureID(img, filter)
|
||||
}
|
||||
|
8
run.go
8
run.go
@ -23,9 +23,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var currentUI *ui
|
||||
|
||||
// Run runs the game.
|
||||
func Run(game Game, width, height, scale int, title string, fps int) error {
|
||||
ui := new(ui)
|
||||
|
||||
currentUI = ui
|
||||
defer func() {
|
||||
currentUI = nil
|
||||
}()
|
||||
|
||||
if err := ui.Start(game, width, height, scale, title); err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user