mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Add ui.Run
This commit is contained in:
parent
d47dd79e1c
commit
f0f77c1a2f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
.DS_Store
|
||||
*~
|
||||
|
||||
|
@ -93,7 +93,7 @@ func (f *Field) Flush() {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Field) Draw(context graphics.Context, textures Textures, geo matrix.Geometry) {
|
||||
func (f *Field) Draw(context graphics.Context, textures *Textures, geo matrix.Geometry) {
|
||||
blocks := make([][]BlockType, len(f.blocks))
|
||||
for i, blockCol := range f.blocks {
|
||||
blocks[i] = make([]BlockType, len(blockCol))
|
||||
|
@ -17,12 +17,7 @@ func textWidth(str string) int {
|
||||
return charWidth * len(str)
|
||||
}
|
||||
|
||||
func drawText(
|
||||
context graphics.Context,
|
||||
textures Textures,
|
||||
str string,
|
||||
x, y, scale int,
|
||||
clr color.Color) {
|
||||
func drawText(context graphics.Context, textures *Textures, str string, x, y, scale int, clr color.Color) {
|
||||
fontTextureId := textures.GetTexture("font")
|
||||
parts := []graphics.TexturePart{}
|
||||
|
||||
@ -53,12 +48,7 @@ func drawText(
|
||||
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
|
||||
}
|
||||
|
||||
func drawTextWithShadow(
|
||||
context graphics.Context,
|
||||
textures Textures,
|
||||
str string,
|
||||
x, y, scale int,
|
||||
clr color.Color) {
|
||||
func drawTextWithShadow(context graphics.Context, textures *Textures, str string, x, y, scale int, clr color.Color) {
|
||||
drawText(context, textures, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80})
|
||||
drawText(context, textures, str, x, y, scale, clr)
|
||||
}
|
||||
|
@ -23,33 +23,28 @@ type GameState struct {
|
||||
Input *Input
|
||||
}
|
||||
|
||||
type Textures interface {
|
||||
RequestTexture(name string, path string)
|
||||
RequestRenderTarget(name string, size Size)
|
||||
Has(name string) bool
|
||||
GetTexture(name string) graphics.TextureId
|
||||
GetRenderTarget(name string) graphics.RenderTargetId
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
sceneManager *SceneManager
|
||||
input *Input
|
||||
textures Textures
|
||||
textures *Textures
|
||||
}
|
||||
|
||||
func NewGame(textures Textures) *Game {
|
||||
func NewGame() *Game {
|
||||
game := &Game{
|
||||
sceneManager: NewSceneManager(NewTitleScene()),
|
||||
input: NewInput(),
|
||||
textures: textures,
|
||||
}
|
||||
return game
|
||||
}
|
||||
|
||||
func (game *Game) SetTextureFactory(textureFactory graphics.TextureFactory) {
|
||||
game.textures = NewTextures(textureFactory)
|
||||
for name, path := range texturePaths {
|
||||
game.textures.RequestTexture(name, path)
|
||||
}
|
||||
for name, size := range renderTargetSizes {
|
||||
game.textures.RequestRenderTarget(name, size)
|
||||
}
|
||||
return game
|
||||
}
|
||||
|
||||
func (game *Game) isInitialized() bool {
|
||||
|
@ -97,7 +97,7 @@ func (s *GameScene) Update(state *GameState) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GameScene) Draw(context graphics.Context, textures Textures) {
|
||||
func (s *GameScene) Draw(context graphics.Context, textures *Textures) {
|
||||
context.Fill(0xff, 0xff, 0xff)
|
||||
|
||||
field := textures.GetTexture("empty")
|
||||
@ -108,24 +108,13 @@ func (s *GameScene) Draw(context graphics.Context, textures Textures) {
|
||||
geoMat.Translate(20, 20) // magic number?
|
||||
colorMat := matrix.ColorI()
|
||||
colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
|
||||
graphics.DrawWhole(
|
||||
context.Texture(field),
|
||||
emptyWidth,
|
||||
emptyHeight,
|
||||
geoMat,
|
||||
colorMat)
|
||||
graphics.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat)
|
||||
|
||||
geoMat = matrix.GeometryI()
|
||||
geoMat.Translate(20, 20)
|
||||
s.field.Draw(context, textures, geoMat)
|
||||
|
||||
if s.currentPiece != nil {
|
||||
s.currentPiece.Draw(
|
||||
context,
|
||||
textures,
|
||||
20, 20,
|
||||
s.currentPieceX,
|
||||
s.currentPieceY,
|
||||
s.currentPieceAngle)
|
||||
s.currentPiece.Draw(context, textures, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
|
||||
}
|
||||
}
|
||||
|
@ -123,11 +123,7 @@ const blockHeight = 10
|
||||
const fieldBlockNumX = 10
|
||||
const fieldBlockNumY = 20
|
||||
|
||||
func drawBlocks(
|
||||
context graphics.Context,
|
||||
textures Textures,
|
||||
blocks [][]BlockType,
|
||||
geo matrix.Geometry) {
|
||||
func drawBlocks(context graphics.Context, textures *Textures, blocks [][]BlockType, geo matrix.Geometry) {
|
||||
parts := []graphics.TexturePart{}
|
||||
for i, blockCol := range blocks {
|
||||
for j, block := range blockCol {
|
||||
@ -209,12 +205,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Piece) Draw(
|
||||
context graphics.Context,
|
||||
textures Textures,
|
||||
fieldX, fieldY int,
|
||||
pieceX, pieceY int,
|
||||
angle Angle) {
|
||||
func (p *Piece) Draw(context graphics.Context, textures *Textures, fieldX, fieldY int, pieceX, pieceY int, angle Angle) {
|
||||
size := len(p.blocks)
|
||||
blocks := make([][]BlockType, size)
|
||||
for i := range p.blocks {
|
||||
|
@ -14,7 +14,7 @@ func init() {
|
||||
|
||||
type Scene interface {
|
||||
Update(state *GameState)
|
||||
Draw(context graphics.Context, textures Textures)
|
||||
Draw(context graphics.Context, textures *Textures)
|
||||
}
|
||||
|
||||
const transitionMaxCount = 20
|
||||
@ -45,7 +45,7 @@ func (s *SceneManager) Update(state *GameState) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SceneManager) Draw(context graphics.Context, textures Textures) {
|
||||
func (s *SceneManager) Draw(context graphics.Context, textures *Textures) {
|
||||
if s.transitionCount == -1 {
|
||||
s.current.Draw(context, textures)
|
||||
return
|
||||
|
@ -1,7 +1,6 @@
|
||||
package main
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/example/blocks"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"image"
|
||||
"os"
|
||||
@ -15,7 +14,7 @@ type namePath struct {
|
||||
|
||||
type nameSize struct {
|
||||
name string
|
||||
size blocks.Size
|
||||
size Size
|
||||
}
|
||||
|
||||
type Textures struct {
|
||||
@ -94,7 +93,7 @@ func (t *Textures) RequestTexture(name string, path string) {
|
||||
t.texturePaths <- namePath{name, path}
|
||||
}
|
||||
|
||||
func (t *Textures) RequestRenderTarget(name string, size blocks.Size) {
|
||||
func (t *Textures) RequestRenderTarget(name string, size Size) {
|
||||
t.renderTargetSizes <- nameSize{name, size}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ func (s *TitleScene) Update(state *GameState) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TitleScene) Draw(context graphics.Context, textures Textures) {
|
||||
func (s *TitleScene) Draw(context graphics.Context, textures *Textures) {
|
||||
drawTitleBackground(context, textures, s.count)
|
||||
drawLogo(context, textures, "BLOCKS")
|
||||
|
||||
@ -36,7 +36,7 @@ func (s *TitleScene) Draw(context graphics.Context, textures Textures) {
|
||||
drawTextWithShadow(context, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff})
|
||||
}
|
||||
|
||||
func drawTitleBackground(context graphics.Context, textures Textures, c int) {
|
||||
func drawTitleBackground(context graphics.Context, textures *Textures, c int) {
|
||||
const textureWidth = 32
|
||||
const textureHeight = 32
|
||||
|
||||
@ -60,7 +60,7 @@ func drawTitleBackground(context graphics.Context, textures Textures, c int) {
|
||||
context.Texture(backgroundTextureId).Draw(parts, geo, clr)
|
||||
}
|
||||
|
||||
func drawLogo(context graphics.Context, textures Textures, str string) {
|
||||
func drawLogo(context graphics.Context, textures *Textures, str string) {
|
||||
scale := 4
|
||||
textWidth := textWidth(str) * scale
|
||||
x := (ScreenWidth - textWidth) / 2
|
||||
|
@ -2,57 +2,17 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/example/blocks"
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"github.com/hajimehoshi/ebiten/ui"
|
||||
"github.com/hajimehoshi/ebiten/ui/glfw"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Update(state ui.InputState)
|
||||
Draw(c graphics.Context)
|
||||
}
|
||||
|
||||
func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func main() {
|
||||
const screenWidth = blocks.ScreenWidth
|
||||
const screenHeight = blocks.ScreenHeight
|
||||
const screenScale = 2
|
||||
const fps = 60
|
||||
const frameTime = time.Duration(int64(time.Second) / int64(fps))
|
||||
const title = "Ebiten Demo"
|
||||
|
||||
u := new(glfw.UI)
|
||||
canvas := u.CreateCanvas(screenWidth, screenHeight, screenScale, title)
|
||||
|
||||
textureFactory := u.TextureFactory()
|
||||
game := blocks.NewGame(NewTextures(textureFactory))
|
||||
tick := time.Tick(frameTime)
|
||||
|
||||
sigterm := make(chan os.Signal, 1)
|
||||
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
u.Start()
|
||||
defer u.Terminate()
|
||||
for {
|
||||
u.DoEvents()
|
||||
select {
|
||||
default:
|
||||
canvas.Draw(game.Draw)
|
||||
case <-tick:
|
||||
game.Update(canvas.InputState())
|
||||
if canvas.IsClosed() {
|
||||
return
|
||||
}
|
||||
case <-sigterm:
|
||||
return
|
||||
}
|
||||
}
|
||||
game := blocks.NewGame()
|
||||
ui.Run(u, game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Ebiten Demo", 60)
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ changed easily.
|
||||
|
||||
```
|
||||
:; cd $GOHOME/src/github.com/hajimehoshi/ebiten/example
|
||||
:; go run *.go
|
||||
:; go run main.go
|
||||
```
|
||||
|
||||
## License
|
||||
|
@ -17,16 +17,13 @@ type UI struct {
|
||||
canvas *Canvas
|
||||
}
|
||||
|
||||
func (u *UI) CreateCanvas(width, height, scale int, title string) ui.Canvas {
|
||||
func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, graphics.TextureFactory) {
|
||||
if !glfw.Init() {
|
||||
panic("glfw.Init() fails")
|
||||
}
|
||||
glfw.WindowHint(glfw.Resizable, glfw.False)
|
||||
u.canvas = NewCanvas(width, height, scale, title)
|
||||
return u.canvas
|
||||
}
|
||||
|
||||
func (u *UI) Start() {
|
||||
return u.canvas, u.canvas
|
||||
}
|
||||
|
||||
func (u *UI) DoEvents() {
|
||||
@ -37,7 +34,3 @@ func (u *UI) DoEvents() {
|
||||
func (u *UI) Terminate() {
|
||||
glfw.Terminate()
|
||||
}
|
||||
|
||||
func (u *UI) TextureFactory() graphics.TextureFactory {
|
||||
return u.canvas
|
||||
}
|
||||
|
41
ui/run.go
Normal file
41
ui/run.go
Normal file
@ -0,0 +1,41 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/graphics"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Draw(context graphics.Context)
|
||||
Update(inputState InputState)
|
||||
SetTextureFactory(textureFactory graphics.TextureFactory)
|
||||
}
|
||||
|
||||
func Run(u UI, game Game, width, height, scale int, title string, fps int) {
|
||||
canvas, textureFactory := u.Start(width, height, scale, title)
|
||||
game.SetTextureFactory(textureFactory)
|
||||
|
||||
frameTime := time.Duration(int64(time.Second) / int64(fps))
|
||||
tick := time.Tick(frameTime)
|
||||
sigterm := make(chan os.Signal, 1)
|
||||
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
defer u.Terminate()
|
||||
for {
|
||||
u.DoEvents()
|
||||
select {
|
||||
default:
|
||||
canvas.Draw(game.Draw)
|
||||
case <-tick:
|
||||
game.Update(canvas.InputState())
|
||||
if canvas.IsClosed() {
|
||||
return
|
||||
}
|
||||
case <-sigterm:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user