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