mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
parent
cd4a0ba489
commit
e16a4cd85c
@ -36,11 +36,13 @@ var (
|
||||
ebitenImage *ebiten.Image
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
type Game struct{}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
const (
|
||||
// The offset point to render the image.
|
||||
ox = 10
|
||||
@ -60,8 +62,10 @@ func update(screen *ebiten.Image) error {
|
||||
op.GeoM.Translate(ox+float64(w), oy)
|
||||
op.CompositeMode = ebiten.CompositeModeLighter
|
||||
screen.DrawImage(ebitenImage, op)
|
||||
}
|
||||
|
||||
return nil
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -80,7 +84,9 @@ func main() {
|
||||
}
|
||||
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Additive Blending (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Additive Blending (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -102,11 +102,13 @@ func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float
|
||||
ebitenutil.DebugPrintAt(screen, msg, int(x), int(y)-16)
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
type Game struct{}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
const ox, oy = 40, 60
|
||||
drawRect(screen, ebitenImage, ox, oy, 200, 100, ebiten.AddressClampToZero, "Regular")
|
||||
drawRect(screen, ebitenImage, 220+ox, oy, 200, 100, ebiten.AddressRepeat, "Regular, Repeat")
|
||||
@ -114,11 +116,16 @@ func update(screen *ebiten.Image) error {
|
||||
subImage := ebitenImage.SubImage(image.Rect(10, 5, 20, 30)).(*ebiten.Image)
|
||||
drawRect(screen, subImage, ox, 200+oy, 200, 100, ebiten.AddressClampToZero, "Subimage")
|
||||
drawRect(screen, subImage, 220+ox, 200+oy, 200, 100, ebiten.AddressRepeat, "Subimage, Repeat")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Sampler Address (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth, screenHeight)
|
||||
ebiten.SetWindowTitle("Sampler Address (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -38,12 +38,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
|
||||
thePlayer = &player{
|
||||
x16: 16 * 100,
|
||||
y16: 16 * 200,
|
||||
angle: maxAngle * 3 / 4,
|
||||
}
|
||||
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
|
||||
|
||||
gophersImage *ebiten.Image
|
||||
repeatedGophersImage *ebiten.Image
|
||||
groundImage *ebiten.Image
|
||||
@ -186,11 +182,11 @@ func (p *player) Angle() int {
|
||||
}
|
||||
|
||||
// updateGroundImage updates the ground image according to the current player's position.
|
||||
func updateGroundImage(ground *ebiten.Image) {
|
||||
func (g *Game) updateGroundImage(ground *ebiten.Image) {
|
||||
ground.Clear()
|
||||
|
||||
x16, y16 := thePlayer.Position()
|
||||
a := thePlayer.Angle()
|
||||
x16, y16 := g.player.Position()
|
||||
a := g.player.Angle()
|
||||
gw, gh := ground.Size()
|
||||
w, h := gophersImage.Size()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
@ -202,7 +198,7 @@ func updateGroundImage(ground *ebiten.Image) {
|
||||
}
|
||||
|
||||
// drawGroundImage draws the ground image to the given screen image.
|
||||
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
||||
func (g *Game) drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
||||
perspectiveGroundImage.Clear()
|
||||
gw, _ := ground.Size()
|
||||
pw, ph := perspectiveGroundImage.Size()
|
||||
@ -225,47 +221,65 @@ func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(-float64(pw)/2, 0)
|
||||
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
|
||||
op.GeoM.Rotate(-1 * float64(g.player.lean) / maxLean * math.Pi / 8)
|
||||
op.GeoM.Translate(float64(screenWidth)/2, screenHeight/3)
|
||||
screen.DrawImage(perspectiveGroundImage, op)
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
type Game struct {
|
||||
player *player
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
return &Game{
|
||||
player: &player{
|
||||
x16: 16 * 100,
|
||||
y16: 16 * 200,
|
||||
angle: maxAngle * 3 / 4,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
// Manipulate the player by the input.
|
||||
if ebiten.IsKeyPressed(ebiten.KeySpace) {
|
||||
thePlayer.MoveForward()
|
||||
g.player.MoveForward()
|
||||
}
|
||||
rotated := false
|
||||
if ebiten.IsKeyPressed(ebiten.KeyRight) {
|
||||
thePlayer.RotateRight()
|
||||
g.player.RotateRight()
|
||||
rotated = true
|
||||
}
|
||||
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
|
||||
thePlayer.RotateLeft()
|
||||
g.player.RotateLeft()
|
||||
rotated = true
|
||||
}
|
||||
if !rotated {
|
||||
thePlayer.Stabilize()
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
g.player.Stabilize()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
// Draw the ground image.
|
||||
screen.Fill(skyColor)
|
||||
updateGroundImage(groundImage)
|
||||
drawGroundImage(screen, groundImage)
|
||||
g.updateGroundImage(groundImage)
|
||||
g.drawGroundImage(screen, groundImage)
|
||||
|
||||
// Draw the message.
|
||||
tutrial := "Space: Move forward\nLeft/Right: Rotate"
|
||||
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\n%s", ebiten.CurrentTPS(), ebiten.CurrentFPS(), tutrial)
|
||||
ebitenutil.DebugPrint(screen, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Air Ship (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Air Ship (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(NewGame()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -33,38 +33,48 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
count int
|
||||
ebitenImage *ebiten.Image
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
count++
|
||||
count %= ebiten.MaxTPS() * 10
|
||||
diff := float64(count) * 0.2
|
||||
type Game struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
g.count++
|
||||
g.count %= ebiten.MaxTPS() * 10
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) offset() float64 {
|
||||
v := float64(g.count) * 0.2
|
||||
switch {
|
||||
case 480 < count:
|
||||
diff = 0
|
||||
case 240 < count:
|
||||
diff = float64(480-count) * 0.2
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
case 480 < g.count:
|
||||
v = 0
|
||||
case 240 < g.count:
|
||||
v = float64(480-g.count) * 0.2
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
||||
|
||||
// Draw 100 Ebitens
|
||||
v := g.offset()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
|
||||
for i := 0; i < 10*10; i++ {
|
||||
op.GeoM.Reset()
|
||||
x := float64(i%10)*diff + 15
|
||||
y := float64(i/10)*diff + 20
|
||||
x := float64(i%10)*v + 15
|
||||
y := float64(i/10)*v + 20
|
||||
op.GeoM.Translate(x, y)
|
||||
screen.DrawImage(ebitenImage, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -83,7 +93,9 @@ func main() {
|
||||
}
|
||||
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Alpha Blending (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -38,24 +38,29 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
count = 0
|
||||
runnerImage *ebiten.Image
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
count++
|
||||
type Game struct {
|
||||
count int
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
g.count++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
|
||||
op.GeoM.Translate(screenWidth/2, screenHeight/2)
|
||||
i := (count / 5) % frameNum
|
||||
i := (g.count / 5) % frameNum
|
||||
sx, sy := frameOX+i*frameWidth, frameOY
|
||||
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -74,7 +79,9 @@ func main() {
|
||||
}
|
||||
runnerImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Animation (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Animation (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -256,28 +256,46 @@ Press U to switch the runnable-on-unfocused state
|
||||
Press A to switch Ogg and MP3
|
||||
Current Time: %s
|
||||
Current Volume: %d/128
|
||||
Type: %s`, ebiten.CurrentTPS(), currentTimeStr, int(p.audioPlayer.Volume()*128), musicPlayer.musicType)
|
||||
Type: %s`, ebiten.CurrentTPS(), currentTimeStr, int(p.audioPlayer.Volume()*128), p.musicType)
|
||||
ebitenutil.DebugPrint(screen, msg)
|
||||
}
|
||||
|
||||
var (
|
||||
type Game struct {
|
||||
musicPlayer *Player
|
||||
musicPlayerCh = make(chan *Player)
|
||||
errCh = make(chan error)
|
||||
)
|
||||
musicPlayerCh chan *Player
|
||||
errCh chan error
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
func NewGame() (*Game, error) {
|
||||
audioContext, err := audio.NewContext(sampleRate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m, err := NewPlayer(audioContext, typeOgg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Game{
|
||||
musicPlayer: m,
|
||||
musicPlayerCh: make(chan *Player),
|
||||
errCh: make(chan error),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
select {
|
||||
case p := <-musicPlayerCh:
|
||||
musicPlayer = p
|
||||
case err := <-errCh:
|
||||
case p := <-g.musicPlayerCh:
|
||||
g.musicPlayer = p
|
||||
case err := <-g.errCh:
|
||||
return err
|
||||
default:
|
||||
}
|
||||
|
||||
if musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
||||
if g.musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
||||
var t musicType
|
||||
switch musicPlayer.musicType {
|
||||
switch g.musicPlayer.musicType {
|
||||
case typeOgg:
|
||||
t = typeMP3
|
||||
case typeMP3:
|
||||
@ -286,46 +304,45 @@ func update(screen *ebiten.Image) error {
|
||||
panic("not reached")
|
||||
}
|
||||
|
||||
musicPlayer.Close()
|
||||
musicPlayer = nil
|
||||
g.musicPlayer.Close()
|
||||
g.musicPlayer = nil
|
||||
|
||||
go func() {
|
||||
p, err := NewPlayer(audio.CurrentContext(), t)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
g.errCh <- err
|
||||
return
|
||||
}
|
||||
musicPlayerCh <- p
|
||||
g.musicPlayerCh <- p
|
||||
}()
|
||||
}
|
||||
|
||||
if musicPlayer != nil {
|
||||
if err := musicPlayer.update(); err != nil {
|
||||
if g.musicPlayer != nil {
|
||||
if err := g.musicPlayer.update(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if musicPlayer != nil {
|
||||
musicPlayer.draw(screen)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
audioContext, err := audio.NewContext(sampleRate)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
if g.musicPlayer != nil {
|
||||
g.musicPlayer.draw(screen)
|
||||
}
|
||||
}
|
||||
|
||||
musicPlayer, err = NewPlayer(audioContext, typeOgg)
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Audio (Ebiten Demo)")
|
||||
g, err := NewGame()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
|
||||
if err := ebiten.RunGame(g); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -47,37 +47,40 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
var player *audio.Player
|
||||
type Game struct {
|
||||
player *audio.Player
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
if player == nil {
|
||||
// Decode the wav file.
|
||||
// wavS is a decoded io.ReadCloser and io.Seeker.
|
||||
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create an infinite loop stream from the decoded bytes.
|
||||
// s is still an io.ReadCloser and io.Seeker.
|
||||
s := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*4*sampleRate, loopLengthInSecond*4*sampleRate)
|
||||
|
||||
player, err = audio.NewPlayer(audioContext, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Play the infinite-length stream. This never ends.
|
||||
player.Play()
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
if g.player != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
pos := player.Current()
|
||||
// Decode the wav file.
|
||||
// wavS is a decoded io.ReadCloser and io.Seeker.
|
||||
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create an infinite loop stream from the decoded bytes.
|
||||
// s is still an io.ReadCloser and io.Seeker.
|
||||
s := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*4*sampleRate, loopLengthInSecond*4*sampleRate)
|
||||
|
||||
g.player, err = audio.NewPlayer(audioContext, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Play the infinite-length stream. This never ends.
|
||||
g.player.Play()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
pos := g.player.Current()
|
||||
if pos > 5*time.Second {
|
||||
pos = (player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second
|
||||
pos = (g.player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second
|
||||
}
|
||||
msg := fmt.Sprintf(`TPS: %0.2f
|
||||
This is an example using
|
||||
@ -87,11 +90,16 @@ Intro: 0[s] - %[2]d[s]
|
||||
Loop: %[2]d[s] - %[3]d[s]
|
||||
Current: %0.2[4]f[s]`, ebiten.CurrentTPS(), introLengthInSecond, introLengthInSecond+loopLengthInSecond, float64(pos)/float64(time.Second))
|
||||
ebitenutil.DebugPrint(screen, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio Infinite Loop (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Audio Infinite Loop (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -35,11 +35,13 @@ var (
|
||||
gophersImage *ebiten.Image
|
||||
)
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
}
|
||||
type Game struct{}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(0, 0)
|
||||
screen.DrawImage(gophersImage, op)
|
||||
@ -60,8 +62,10 @@ func update(screen *ebiten.Image) error {
|
||||
screen.DrawImage(gophersImage, op)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -80,7 +84,9 @@ func main() {
|
||||
}
|
||||
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Blur (Ebiten Demo)"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth, screenHeight)
|
||||
ebiten.SetWindowTitle("Blur (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -27,9 +27,13 @@ import (
|
||||
"github.com/jakecoffman/cp"
|
||||
)
|
||||
|
||||
const (
|
||||
screenWidth = 600
|
||||
screenHeight = 480
|
||||
)
|
||||
|
||||
var (
|
||||
space *cp.Space
|
||||
dot *ebiten.Image
|
||||
dot *ebiten.Image
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -37,26 +41,77 @@ func init() {
|
||||
dot.Fill(color.White)
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
space.Step(1.0 / float64(ebiten.MaxTPS()))
|
||||
type Game struct {
|
||||
space *cp.Space
|
||||
}
|
||||
|
||||
if ebiten.IsDrawingSkipped() {
|
||||
return nil
|
||||
func NewGame() *Game {
|
||||
const (
|
||||
imageWidth = 188
|
||||
imageHeight = 35
|
||||
)
|
||||
|
||||
space := cp.NewSpace()
|
||||
space.Iterations = 1
|
||||
|
||||
// The space will contain a very large number of similarly sized objects.
|
||||
// This is the perfect candidate for using the spatial hash.
|
||||
// Generally you will never need to do this.
|
||||
space.UseSpatialHash(2.0, 10000)
|
||||
|
||||
var body *cp.Body
|
||||
var shape *cp.Shape
|
||||
|
||||
for y := 0; y < imageHeight; y++ {
|
||||
for x := 0; x < imageWidth; x++ {
|
||||
if getPixel(uint(x), uint(y)) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
xJitter := 0.05 * rand.Float64()
|
||||
yJitter := 0.05 * rand.Float64()
|
||||
|
||||
shape = makeBall(2.0*(float64(x)+imageWidth/2+xJitter)-75, 2*(imageHeight/2.0+float64(y)+yJitter)+150)
|
||||
space.AddBody(shape.Body())
|
||||
space.AddShape(shape)
|
||||
}
|
||||
}
|
||||
|
||||
body = space.AddBody(cp.NewBody(1e9, cp.INFINITY))
|
||||
body.SetPosition(cp.Vector{X: -1000, Y: 225})
|
||||
body.SetVelocity(400, 0)
|
||||
|
||||
shape = space.AddShape(cp.NewCircle(body, 8, cp.Vector{}))
|
||||
shape.SetElasticity(0)
|
||||
shape.SetFriction(0)
|
||||
|
||||
return &Game{
|
||||
space: space,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
g.space.Step(1.0 / float64(ebiten.MaxTPS()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
screen.Fill(color.Black)
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.ColorM.Scale(200.0/255.0, 200.0/255.0, 200.0/255.0, 1)
|
||||
|
||||
space.EachBody(func(body *cp.Body) {
|
||||
g.space.EachBody(func(body *cp.Body) {
|
||||
op.GeoM.Reset()
|
||||
op.GeoM.Translate(body.Position().X, body.Position().Y)
|
||||
screen.DrawImage(dot, op)
|
||||
})
|
||||
|
||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return screenWidth, screenHeight
|
||||
}
|
||||
|
||||
func getPixel(x, y uint) int {
|
||||
@ -112,46 +167,9 @@ var imageBitmap = []int{
|
||||
}
|
||||
|
||||
func main() {
|
||||
const (
|
||||
imageWidth = 188
|
||||
imageHeight = 35
|
||||
)
|
||||
|
||||
space = cp.NewSpace()
|
||||
space.Iterations = 1
|
||||
|
||||
// The space will contain a very large number of similarly sized objects.
|
||||
// This is the perfect candidate for using the spatial hash.
|
||||
// Generally you will never need to do this.
|
||||
space.UseSpatialHash(2.0, 10000)
|
||||
|
||||
var body *cp.Body
|
||||
var shape *cp.Shape
|
||||
|
||||
for y := 0; y < imageHeight; y++ {
|
||||
for x := 0; x < imageWidth; x++ {
|
||||
if getPixel(uint(x), uint(y)) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
xJitter := 0.05 * rand.Float64()
|
||||
yJitter := 0.05 * rand.Float64()
|
||||
|
||||
shape = makeBall(2.0*(float64(x)+imageWidth/2+xJitter)-75, 2*(imageHeight/2.0+float64(y)+yJitter)+150)
|
||||
space.AddBody(shape.Body())
|
||||
space.AddShape(shape)
|
||||
}
|
||||
}
|
||||
|
||||
body = space.AddBody(cp.NewBody(1e9, cp.INFINITY))
|
||||
body.SetPosition(cp.Vector{X: -1000, Y: 225})
|
||||
body.SetVelocity(400, 0)
|
||||
|
||||
shape = space.AddShape(cp.NewCircle(body, 8, cp.Vector{}))
|
||||
shape.SetElasticity(0)
|
||||
shape.SetFriction(0)
|
||||
|
||||
if err := ebiten.Run(update, 600, 480, 1, "Ebiten"); err != nil {
|
||||
ebiten.SetWindowSize(screenWidth, screenHeight)
|
||||
ebiten.SetWindowTitle("Ebiten")
|
||||
if err := ebiten.RunGame(NewGame()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -32,9 +32,12 @@ const (
|
||||
screenHeight = 240
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
count int
|
||||
var (
|
||||
gophersImage *ebiten.Image
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
count int
|
||||
}
|
||||
|
||||
func (g *Game) Update(screen *ebiten.Image) error {
|
||||
@ -43,7 +46,7 @@ func (g *Game) Update(screen *ebiten.Image) error {
|
||||
}
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
w, h := g.gophersImage.Size()
|
||||
w, h := gophersImage.Size()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
|
||||
// Move the image's center to the screen's upper-left corner.
|
||||
@ -58,7 +61,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
// Move the image to the screen's center.
|
||||
op.GeoM.Translate(screenWidth/2, screenHeight/2)
|
||||
|
||||
screen.DrawImage(g.gophersImage, op)
|
||||
screen.DrawImage(gophersImage, op)
|
||||
}
|
||||
|
||||
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
@ -79,12 +82,11 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
g := &Game{}
|
||||
g.gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
||||
|
||||
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
|
||||
ebiten.SetWindowTitle("Rotate (Ebiten Demo)")
|
||||
if err := ebiten.RunGame(g); err != nil {
|
||||
if err := ebiten.RunGame(&Game{}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user