diff --git a/_docs/gen.go b/_docs/gen.go index ffda4b3b2..891509b07 100644 --- a/_docs/gen.go +++ b/_docs/gen.go @@ -185,7 +185,7 @@ func (e *example) Source() string { panic(err) } str := regexp.MustCompile("(?s)^.*?\n\n").ReplaceAllString(string(b), "") - str = strings.Replace(str, "\t", " ", -1) + str = strings.Replace(str, "\t", " ", -1) return str } diff --git a/_docs/index.tmpl.html b/_docs/index.tmpl.html index b7a8f7b03..9a9e1e9d9 100644 --- a/_docs/index.tmpl.html +++ b/_docs/index.tmpl.html @@ -70,17 +70,17 @@ the main.go file:

package main
 
 import (
-    "github.com/hajimehoshi/ebiten"
-    "github.com/hajimehoshi/ebiten/ebitenutil"
+        "github.com/hajimehoshi/ebiten"
+        "github.com/hajimehoshi/ebiten/ebitenutil"
 )
 
 func update(screen *ebiten.Image) error {
-    ebitenutil.DebugPrint(screen, "Hello world!")
-    return nil
+        ebitenutil.DebugPrint(screen, "Hello world!")
+        return nil
 }
 
 func main() {
-    ebiten.Run(update, 320, 240, 2, "Hello world!")
+        ebiten.Run(update, 320, 240, 2, "Hello world!")
 }
 
diff --git a/_docs/public/examples/alphablending.html b/_docs/public/examples/alphablending.html index 93c0ee847..1e3c87fd7 100644 --- a/_docs/public/examples/alphablending.html +++ b/_docs/public/examples/alphablending.html @@ -27,78 +27,78 @@ package main import ( - "image/color" - _ "image/png" - "log" + "image/color" + _ "image/png" + "log" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - count int - ebitenImage *ebiten.Image + count int + ebitenImage *ebiten.Image ) type imageParts struct { - diff float64 + diff float64 } func (p *imageParts) Src(i int) (int, int, int, int) { - w, h := ebitenImage.Size() - return 0, 0, w, h + w, h := ebitenImage.Size() + return 0, 0, w, h } func (p *imageParts) Dst(i int) (int, int, int, int) { - x := int(float64(i%10)*p.diff + 15) - y := int(float64(i/10)*p.diff + 20) - w, h := ebitenImage.Size() - return x, y, x + w, y + h + x := int(float64(i%10)*p.diff + 15) + y := int(float64(i/10)*p.diff + 20) + w, h := ebitenImage.Size() + return x, y, x + w, y + h } func (p *imageParts) Len() int { - return 10 * 10 + return 10 * 10 } func update(screen *ebiten.Image) error { - count++ - count %= ebiten.FPS * 10 - diff := float64(count) * 0.2 - switch { - case 480 < count: - diff = 0 - case 240 < count: - diff = float64(480-count) * 0.2 - } + count++ + count %= ebiten.FPS * 10 + diff := float64(count) * 0.2 + switch { + case 480 < count: + diff = 0 + case 240 < count: + diff = float64(480-count) * 0.2 + } - if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil { - return err - } - op := &ebiten.DrawImageOptions{} - op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) - op.ImageParts = &imageParts{ - diff: diff, - } - if err := screen.DrawImage(ebitenImage, op); err != nil { - return err - } - return nil + if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil { + return err + } + op := &ebiten.DrawImageOptions{} + op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) + op.ImageParts = &imageParts{ + diff: diff, + } + if err := screen.DrawImage(ebitenImage, op); err != nil { + return err + } + return nil } func main() { - var err error - ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/audio.html b/_docs/public/examples/audio.html index d0e13010f..2b8459f0e 100644 --- a/_docs/public/examples/audio.html +++ b/_docs/public/examples/audio.html @@ -27,287 +27,287 @@ package main import ( - "fmt" - "image/color" - "io/ioutil" - "log" - "time" + "fmt" + "image/color" + "io/ioutil" + "log" + "time" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/audio" - "github.com/hajimehoshi/ebiten/audio/vorbis" - "github.com/hajimehoshi/ebiten/audio/wav" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/audio" + "github.com/hajimehoshi/ebiten/audio/vorbis" + "github.com/hajimehoshi/ebiten/audio/wav" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - playerBarImage *ebiten.Image - playerCurrentImage *ebiten.Image + playerBarImage *ebiten.Image + playerCurrentImage *ebiten.Image ) func init() { - var err error - playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil { - log.Fatal(err) - } + var err error + playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil { + log.Fatal(err) + } - playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil { - log.Fatal(err) - } + playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil { + log.Fatal(err) + } } type Player struct { - audioPlayer *audio.Player - total time.Duration + audioPlayer *audio.Player + total time.Duration } var ( - audioContext *audio.Context - musicPlayer *Player - seBytes []byte - musicCh = make(chan *Player) - seCh = make(chan []byte) - mouseButtonState = map[ebiten.MouseButton]int{} - keyState = map[ebiten.Key]int{} - volume128 = 128 + audioContext *audio.Context + musicPlayer *Player + seBytes []byte + musicCh = make(chan *Player) + seCh = make(chan []byte) + mouseButtonState = map[ebiten.MouseButton]int{} + keyState = map[ebiten.Key]int{} + volume128 = 128 ) func playerBarRect() (x, y, w, h int) { - w, h = playerBarImage.Size() - x = (screenWidth - w) / 2 - y = screenHeight - h - 16 - return + w, h = playerBarImage.Size() + x = (screenWidth - w) / 2 + y = screenHeight - h - 16 + return } func (p *Player) updateSE() error { - if seBytes == nil { - return nil - } - if !ebiten.IsKeyPressed(ebiten.KeyP) { - keyState[ebiten.KeyP] = 0 - return nil - } - keyState[ebiten.KeyP]++ - if keyState[ebiten.KeyP] != 1 { - return nil - } - sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes) - if err != nil { - return err - } - return sePlayer.Play() + if seBytes == nil { + return nil + } + if !ebiten.IsKeyPressed(ebiten.KeyP) { + keyState[ebiten.KeyP] = 0 + return nil + } + keyState[ebiten.KeyP]++ + if keyState[ebiten.KeyP] != 1 { + return nil + } + sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes) + if err != nil { + return err + } + return sePlayer.Play() } func (p *Player) updateVolume() error { - if p.audioPlayer == nil { + if p.audioPlayer == nil { + return nil + } + if ebiten.IsKeyPressed(ebiten.KeyZ) { + volume128-- + } + if ebiten.IsKeyPressed(ebiten.KeyX) { + volume128++ + } + if volume128 < 0 { + volume128 = 0 + } + if 128 < volume128 { + volume128 = 128 + } + p.audioPlayer.SetVolume(float64(volume128) / 128) return nil - } - if ebiten.IsKeyPressed(ebiten.KeyZ) { - volume128-- - } - if ebiten.IsKeyPressed(ebiten.KeyX) { - volume128++ - } - if volume128 < 0 { - volume128 = 0 - } - if 128 < volume128 { - volume128 = 128 - } - p.audioPlayer.SetVolume(float64(volume128) / 128) - return nil } func (p *Player) updatePlayPause() error { - if p.audioPlayer == nil { - return nil - } - if !ebiten.IsKeyPressed(ebiten.KeyS) { - keyState[ebiten.KeyS] = 0 - return nil - } - keyState[ebiten.KeyS]++ - if keyState[ebiten.KeyS] != 1 { - return nil - } - if p.audioPlayer.IsPlaying() { - return p.audioPlayer.Pause() - } - return p.audioPlayer.Play() + if p.audioPlayer == nil { + return nil + } + if !ebiten.IsKeyPressed(ebiten.KeyS) { + keyState[ebiten.KeyS] = 0 + return nil + } + keyState[ebiten.KeyS]++ + if keyState[ebiten.KeyS] != 1 { + return nil + } + if p.audioPlayer.IsPlaying() { + return p.audioPlayer.Pause() + } + return p.audioPlayer.Play() } func (p *Player) updateBar() error { - if p.audioPlayer == nil { - return nil - } - if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { - mouseButtonState[ebiten.MouseButtonLeft] = 0 - return nil - } - mouseButtonState[ebiten.MouseButtonLeft]++ - if mouseButtonState[ebiten.MouseButtonLeft] != 1 { - return nil - } - x, y := ebiten.CursorPosition() - bx, by, bw, bh := playerBarRect() - const padding = 4 - if y < by-padding || by+bh+padding <= y { - return nil - } - if x < bx || bx+bw <= x { - return nil - } - pos := time.Duration(x-bx) * p.total / time.Duration(bw) - return p.audioPlayer.Seek(pos) + if p.audioPlayer == nil { + return nil + } + if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { + mouseButtonState[ebiten.MouseButtonLeft] = 0 + return nil + } + mouseButtonState[ebiten.MouseButtonLeft]++ + if mouseButtonState[ebiten.MouseButtonLeft] != 1 { + return nil + } + x, y := ebiten.CursorPosition() + bx, by, bw, bh := playerBarRect() + const padding = 4 + if y < by-padding || by+bh+padding <= y { + return nil + } + if x < bx || bx+bw <= x { + return nil + } + pos := time.Duration(x-bx) * p.total / time.Duration(bw) + return p.audioPlayer.Seek(pos) } func (p *Player) close() error { - return p.audioPlayer.Close() + return p.audioPlayer.Close() } func update(screen *ebiten.Image) error { - if musicPlayer == nil { - select { - case musicPlayer = <-musicCh: - default: + if musicPlayer == nil { + select { + case musicPlayer = <-musicCh: + default: + } } - } - if seBytes == nil { - select { - case seBytes = <-seCh: - default: + if seBytes == nil { + select { + case seBytes = <-seCh: + default: + } } - } - if musicPlayer != nil { - if err := musicPlayer.updateBar(); err != nil { - return err + if musicPlayer != nil { + if err := musicPlayer.updateBar(); err != nil { + return err + } + if err := musicPlayer.updatePlayPause(); err != nil { + return err + } + if err := musicPlayer.updateSE(); err != nil { + return err + } + if err := musicPlayer.updateVolume(); err != nil { + return err + } } - if err := musicPlayer.updatePlayPause(); err != nil { - return err - } - if err := musicPlayer.updateSE(); err != nil { - return err - } - if err := musicPlayer.updateVolume(); err != nil { - return err - } - } - op := &ebiten.DrawImageOptions{} - x, y, w, h := playerBarRect() - op.GeoM.Translate(float64(x), float64(y)) - if err := screen.DrawImage(playerBarImage, op); err != nil { - return err - } - currentTimeStr := "00:00" - if musicPlayer != nil { - c := musicPlayer.audioPlayer.Current() - - // Current Time - m := (c / time.Minute) % 100 - s := (c / time.Second) % 60 - currentTimeStr = fmt.Sprintf("%02d:%02d", m, s) - - // Bar - cw, ch := playerCurrentImage.Size() - cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2 - cy := y - (ch-h)/2 op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(cx), float64(cy)) - if err := screen.DrawImage(playerCurrentImage, op); err != nil { - return err + x, y, w, h := playerBarRect() + op.GeoM.Translate(float64(x), float64(y)) + if err := screen.DrawImage(playerBarImage, op); err != nil { + return err } - } + currentTimeStr := "00:00" + if musicPlayer != nil { + c := musicPlayer.audioPlayer.Current() - msg := fmt.Sprintf(`FPS: %0.2f + // Current Time + m := (c / time.Minute) % 100 + s := (c / time.Second) % 60 + currentTimeStr = fmt.Sprintf("%02d:%02d", m, s) + + // Bar + cw, ch := playerCurrentImage.Size() + cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2 + cy := y - (ch-h)/2 + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(cx), float64(cy)) + if err := screen.DrawImage(playerCurrentImage, op); err != nil { + return err + } + } + + msg := fmt.Sprintf(`FPS: %0.2f Press S to toggle Play/Pause Press P to play SE Press Z or X to change volume of the music %s`, ebiten.CurrentFPS(), currentTimeStr) - if musicPlayer == nil { - msg += "\nNow Loading..." - } - if err := ebitenutil.DebugPrint(screen, msg); err != nil { - return err - } - if err := audioContext.Update(); err != nil { - return err - } - return nil + if musicPlayer == nil { + msg += "\nNow Loading..." + } + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } + if err := audioContext.Update(); err != nil { + return err + } + return nil } func main() { - wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav") - if err != nil { - log.Fatal(err) - } - oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg") - if err != nil { - log.Fatal(err) - } - const sampleRate = 22050 - const bytesPerSample = 4 // TODO: This should be defined in audio package - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } - go func() { - s, err := wav.Decode(audioContext, wavF) + wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav") if err != nil { - log.Fatal(err) - return + log.Fatal(err) } - b, err := ioutil.ReadAll(s) + oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg") if err != nil { - log.Fatal(err) - return + log.Fatal(err) } - seCh <- b - close(seCh) - }() - go func() { - s, err := vorbis.Decode(audioContext, oggF) + const sampleRate = 22050 + const bytesPerSample = 4 // TODO: This should be defined in audio package + audioContext, err = audio.NewContext(sampleRate) if err != nil { - log.Fatal(err) - return + log.Fatal(err) } - p, err := audio.NewPlayer(audioContext, s) - if err != nil { - log.Fatal(err) - return + go func() { + s, err := wav.Decode(audioContext, wavF) + if err != nil { + log.Fatal(err) + return + } + b, err := ioutil.ReadAll(s) + if err != nil { + log.Fatal(err) + return + } + seCh <- b + close(seCh) + }() + go func() { + s, err := vorbis.Decode(audioContext, oggF) + if err != nil { + log.Fatal(err) + return + } + p, err := audio.NewPlayer(audioContext, s) + if err != nil { + log.Fatal(err) + return + } + musicCh <- &Player{ + audioPlayer: p, + total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, + } + close(musicCh) + // TODO: Is this goroutine-safe? + if err := p.Play(); err != nil { + log.Fatal(err) + return + } + }() + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { + log.Fatal(err) } - musicCh <- &Player{ - audioPlayer: p, - total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, + if musicPlayer != nil { + if err := musicPlayer.close(); err != nil { + log.Fatal(err) + } } - close(musicCh) - // TODO: Is this goroutine-safe? - if err := p.Play(); err != nil { - log.Fatal(err) - return - } - }() - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { - log.Fatal(err) - } - if musicPlayer != nil { - if err := musicPlayer.close(); err != nil { - log.Fatal(err) - } - } } diff --git a/_docs/public/examples/font.html b/_docs/public/examples/font.html index 11833d3e1..2d5ed62a1 100644 --- a/_docs/public/examples/font.html +++ b/_docs/public/examples/font.html @@ -27,95 +27,95 @@ package main import ( - "image" - "io/ioutil" - "log" + "image" + "io/ioutil" + "log" - "github.com/golang/freetype/truetype" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" - "golang.org/x/image/font" - "golang.org/x/image/math/fixed" + "github.com/golang/freetype/truetype" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" + "golang.org/x/image/font" + "golang.org/x/image/math/fixed" ) const ( - screenWidth = 640 - screenHeight = 480 + screenWidth = 640 + screenHeight = 480 ) var ( - textImage *ebiten.Image + textImage *ebiten.Image ) var text = []string{ - "The quick brown fox jumps over the lazy dog.", - "", - // A head part of a Japanese novel 山月記 (Sangetsuki) - // See http://www.aozora.gr.jp/cards/000119/files/624_14544.html. - "隴西の李徴は博学才穎、天宝の末年、", - "若くして名を虎榜に連ね、", - "ついで江南尉に補せられたが、", - "性、狷介、自ら恃むところ頗厚く、", - "賤吏に甘んずるを潔しとしなかった。", + "The quick brown fox jumps over the lazy dog.", + "", + // A head part of a Japanese novel 山月記 (Sangetsuki) + // See http://www.aozora.gr.jp/cards/000119/files/624_14544.html. + "隴西の李徴は博学才穎、天宝の末年、", + "若くして名を虎榜に連ね、", + "ついで江南尉に補せられたが、", + "性、狷介、自ら恃むところ頗厚く、", + "賤吏に甘んずるを潔しとしなかった。", } func parseFont() error { - f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf") - if err != nil { - return err - } - defer func() { - _ = f.Close() - }() - b, err := ioutil.ReadAll(f) - if err != nil { - return err - } - tt, err := truetype.Parse(b) - if err != nil { - return err - } - w, h := textImage.Size() - dst := image.NewRGBA(image.Rect(0, 0, w, h)) - const size = 24 - const dpi = 72 - d := &font.Drawer{ - Dst: dst, - Src: image.White, - Face: truetype.NewFace(tt, &truetype.Options{ - Size: size, - DPI: dpi, - Hinting: font.HintingFull, - }), - } - y := size - for _, s := range text { - d.Dot = fixed.P(0, y) - d.DrawString(s) - y += size - } - return textImage.ReplacePixels(dst.Pix) + f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf") + if err != nil { + return err + } + defer func() { + _ = f.Close() + }() + b, err := ioutil.ReadAll(f) + if err != nil { + return err + } + tt, err := truetype.Parse(b) + if err != nil { + return err + } + w, h := textImage.Size() + dst := image.NewRGBA(image.Rect(0, 0, w, h)) + const size = 24 + const dpi = 72 + d := &font.Drawer{ + Dst: dst, + Src: image.White, + Face: truetype.NewFace(tt, &truetype.Options{ + Size: size, + DPI: dpi, + Hinting: font.HintingFull, + }), + } + y := size + for _, s := range text { + d.Dot = fixed.P(0, y) + d.DrawString(s) + y += size + } + return textImage.ReplacePixels(dst.Pix) } func update(screen *ebiten.Image) error { - if err := screen.DrawImage(textImage, &ebiten.DrawImageOptions{}); err != nil { - return err - } - return nil + if err := screen.DrawImage(textImage, &ebiten.DrawImageOptions{}); err != nil { + return err + } + return nil } func main() { - var err error - textImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := parseFont(); err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + textImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := parseFont(); err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Font (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/gamepad.html b/_docs/public/examples/gamepad.html index b4b568594..d4b983159 100644 --- a/_docs/public/examples/gamepad.html +++ b/_docs/public/examples/gamepad.html @@ -27,55 +27,55 @@ package main import ( - "fmt" - "log" - "strconv" - "strings" + "fmt" + "log" + "strconv" + "strings" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) func update(screen *ebiten.Image) error { - // TODO: API to get the available, lowest ID - const gamepadID = 0 - axes := []string{} - pressedButtons := []string{} + // TODO: API to get the available, lowest ID + const gamepadID = 0 + axes := []string{} + pressedButtons := []string{} - maxAxis := ebiten.GamepadAxisNum(gamepadID) - for a := 0; a < maxAxis; a++ { - v := ebiten.GamepadAxis(gamepadID, a) - axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v)) - } - - maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID)) - for b := ebiten.GamepadButton(gamepadID); b < maxButton; b++ { - if ebiten.IsGamepadButtonPressed(gamepadID, b) { - pressedButtons = append(pressedButtons, strconv.Itoa(int(b))) + maxAxis := ebiten.GamepadAxisNum(gamepadID) + for a := 0; a < maxAxis; a++ { + v := ebiten.GamepadAxis(gamepadID, a) + axes = append(axes, fmt.Sprintf("%d: %0.6f", a, v)) } - } - str := `Gamepad + maxButton := ebiten.GamepadButton(ebiten.GamepadButtonNum(gamepadID)) + for b := ebiten.GamepadButton(gamepadID); b < maxButton; b++ { + if ebiten.IsGamepadButtonPressed(gamepadID, b) { + pressedButtons = append(pressedButtons, strconv.Itoa(int(b))) + } + } + + str := `Gamepad Axes: {{.Axes}} Pressed Buttons: {{.Buttons}}` - str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) - str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) - if err := ebitenutil.DebugPrint(screen, str); err != nil { - return err - } - return nil + str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) + str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) + if err := ebitenutil.DebugPrint(screen, str); err != nil { + return err + } + return nil } func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Gamepad (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/hsv.html b/_docs/public/examples/hsv.html index 4ebae89df..5bc310a2e 100644 --- a/_docs/public/examples/hsv.html +++ b/_docs/public/examples/hsv.html @@ -27,92 +27,92 @@ package main import ( - "fmt" - _ "image/jpeg" - "log" - "math" + "fmt" + _ "image/jpeg" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - hueInt = 0 - saturationInt = 128 - valueInt = 128 - gophersImage *ebiten.Image + hueInt = 0 + saturationInt = 128 + valueInt = 128 + gophersImage *ebiten.Image ) func clamp(v, min, max int) int { - if min > max { - panic("min must <= max") - } - if v < min { - return min - } - if max < v { - return max - } - return v + if min > max { + panic("min must <= max") + } + if v < min { + return min + } + if max < v { + return max + } + return v } func update(screen *ebiten.Image) error { - if ebiten.IsKeyPressed(ebiten.KeyQ) { - hueInt-- - } - if ebiten.IsKeyPressed(ebiten.KeyW) { - hueInt++ - } - if ebiten.IsKeyPressed(ebiten.KeyA) { - saturationInt-- - } - if ebiten.IsKeyPressed(ebiten.KeyS) { - saturationInt++ - } - if ebiten.IsKeyPressed(ebiten.KeyZ) { - valueInt-- - } - if ebiten.IsKeyPressed(ebiten.KeyX) { - valueInt++ - } - hueInt = clamp(hueInt, -256, 256) - saturationInt = clamp(saturationInt, 0, 256) - valueInt = clamp(valueInt, 0, 256) + if ebiten.IsKeyPressed(ebiten.KeyQ) { + hueInt-- + } + if ebiten.IsKeyPressed(ebiten.KeyW) { + hueInt++ + } + if ebiten.IsKeyPressed(ebiten.KeyA) { + saturationInt-- + } + if ebiten.IsKeyPressed(ebiten.KeyS) { + saturationInt++ + } + if ebiten.IsKeyPressed(ebiten.KeyZ) { + valueInt-- + } + if ebiten.IsKeyPressed(ebiten.KeyX) { + valueInt++ + } + hueInt = clamp(hueInt, -256, 256) + saturationInt = clamp(saturationInt, 0, 256) + valueInt = clamp(valueInt, 0, 256) - w, h := gophersImage.Size() - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) - hue := float64(hueInt) * 2 * math.Pi / 128 - saturation := float64(saturationInt) / 128 - value := float64(valueInt) / 128 - op.ColorM.ChangeHSV(hue, saturation, value) - if err := screen.DrawImage(gophersImage, op); err != nil { - return err - } + w, h := gophersImage.Size() + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) + hue := float64(hueInt) * 2 * math.Pi / 128 + saturation := float64(saturationInt) / 128 + value := float64(valueInt) / 128 + op.ColorM.ChangeHSV(hue, saturation, value) + if err := screen.DrawImage(gophersImage, op); err != nil { + return err + } - msg := fmt.Sprintf(`Hue: %0.2f [Q][W] + msg := fmt.Sprintf(`Hue: %0.2f [Q][W] Saturation: %0.2f [A][S] Value: %0.2f [Z][X]`, hue, saturation, value) - if err := ebitenutil.DebugPrint(screen, msg); err != nil { - return err - } - return nil + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } + return nil } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "HSV (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "HSV (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/hue.html b/_docs/public/examples/hue.html index 43c759dab..7799690ec 100644 --- a/_docs/public/examples/hue.html +++ b/_docs/public/examples/hue.html @@ -27,45 +27,45 @@ package main import ( - _ "image/jpeg" - "log" - "math" + _ "image/jpeg" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - count int - gophersImage *ebiten.Image + count int + gophersImage *ebiten.Image ) func update(screen *ebiten.Image) error { - count++ - w, h := gophersImage.Size() - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) - op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360) - if err := screen.DrawImage(gophersImage, op); err != nil { - return err - } - return nil + count++ + w, h := gophersImage.Size() + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2) + op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360) + if err := screen.DrawImage(gophersImage, op); err != nil { + return err + } + return nil } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Hue (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Hue (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/keyboard.html b/_docs/public/examples/keyboard.html index 8bc5334dd..78e4c310d 100644 --- a/_docs/public/examples/keyboard.html +++ b/_docs/public/examples/keyboard.html @@ -27,116 +27,116 @@ package main import ( - "log" - "strconv" + "log" + "strconv" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" - "github.com/hajimehoshi/ebiten/examples/keyboard/keyboard" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten/examples/keyboard/keyboard" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var keyboardImage *ebiten.Image func init() { - var err error - keyboardImage, _, err = ebitenutil.NewImageFromFile("_resources/images/keyboard/keyboard.png", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } + var err error + keyboardImage, _, err = ebitenutil.NewImageFromFile("_resources/images/keyboard/keyboard.png", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } } var keyNames = map[ebiten.Key]string{ - ebiten.KeyBackspace: "BS", - ebiten.KeyComma: ",", - ebiten.KeyDelete: "Del", - ebiten.KeyEnter: "Enter", - ebiten.KeyEscape: "Esc", - ebiten.KeyPeriod: ".", - ebiten.KeySpace: "Space", - ebiten.KeyTab: "Tab", + ebiten.KeyBackspace: "BS", + ebiten.KeyComma: ",", + ebiten.KeyDelete: "Del", + ebiten.KeyEnter: "Enter", + ebiten.KeyEscape: "Esc", + ebiten.KeyPeriod: ".", + ebiten.KeySpace: "Space", + ebiten.KeyTab: "Tab", - // Arrows - ebiten.KeyDown: "Down", - ebiten.KeyLeft: "Left", - ebiten.KeyRight: "Right", - ebiten.KeyUp: "Up", + // Arrows + ebiten.KeyDown: "Down", + ebiten.KeyLeft: "Left", + ebiten.KeyRight: "Right", + ebiten.KeyUp: "Up", - // Mods - ebiten.KeyShift: "Shift", - ebiten.KeyControl: "Ctrl", - ebiten.KeyAlt: "Alt", + // Mods + ebiten.KeyShift: "Shift", + ebiten.KeyControl: "Ctrl", + ebiten.KeyAlt: "Alt", } type pressedKeysParts []string func (p pressedKeysParts) Len() int { - return len(p) + return len(p) } func (p pressedKeysParts) Dst(i int) (x0, y0, x1, y1 int) { - k := p[i] - r, ok := keyboard.KeyRect(k) - if !ok { - return 0, 0, 0, 0 - } - return r.Min.X, r.Min.Y, r.Max.X, r.Max.Y + k := p[i] + r, ok := keyboard.KeyRect(k) + if !ok { + return 0, 0, 0, 0 + } + return r.Min.X, r.Min.Y, r.Max.X, r.Max.Y } func (p pressedKeysParts) Src(i int) (x0, y0, x1, y1 int) { - return p.Dst(i) + return p.Dst(i) } func update(screen *ebiten.Image) error { - const offsetX, offsetY = 24, 40 - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(offsetX, offsetY) - op.ColorM.Scale(0.5, 0.5, 0.5, 1) - if err := screen.DrawImage(keyboardImage, op); err != nil { - return err - } + const offsetX, offsetY = 24, 40 + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(offsetX, offsetY) + op.ColorM.Scale(0.5, 0.5, 0.5, 1) + if err := screen.DrawImage(keyboardImage, op); err != nil { + return err + } - pressed := []string{} - for i := 0; i <= 9; i++ { - if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) { - pressed = append(pressed, string(i+'0')) + pressed := []string{} + for i := 0; i <= 9; i++ { + if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.Key0) { + pressed = append(pressed, string(i+'0')) + } } - } - for c := 'A'; c <= 'Z'; c++ { - if ebiten.IsKeyPressed(ebiten.Key(c) - 'A' + ebiten.KeyA) { - pressed = append(pressed, string(c)) + for c := 'A'; c <= 'Z'; c++ { + if ebiten.IsKeyPressed(ebiten.Key(c) - 'A' + ebiten.KeyA) { + pressed = append(pressed, string(c)) + } } - } - for i := 1; i <= 12; i++ { - if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.KeyF1 - 1) { - pressed = append(pressed, "F"+strconv.Itoa(i)) + for i := 1; i <= 12; i++ { + if ebiten.IsKeyPressed(ebiten.Key(i) + ebiten.KeyF1 - 1) { + pressed = append(pressed, "F"+strconv.Itoa(i)) + } } - } - for key, name := range keyNames { - if ebiten.IsKeyPressed(key) { - pressed = append(pressed, name) + for key, name := range keyNames { + if ebiten.IsKeyPressed(key) { + pressed = append(pressed, name) + } } - } - op = &ebiten.DrawImageOptions{ - ImageParts: pressedKeysParts(pressed), - } - op.GeoM.Translate(offsetX, offsetY) - if err := screen.DrawImage(keyboardImage, op); err != nil { - return err - } + op = &ebiten.DrawImageOptions{ + ImageParts: pressedKeysParts(pressed), + } + op.GeoM.Translate(offsetX, offsetY) + if err := screen.DrawImage(keyboardImage, op); err != nil { + return err + } - return nil + return nil } func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Keyboard (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Keyboard (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/masking.html b/_docs/public/examples/masking.html index 70978b9b0..e0863afd4 100644 --- a/_docs/public/examples/masking.html +++ b/_docs/public/examples/masking.html @@ -27,129 +27,129 @@ package main import ( - "image" - "image/color" - _ "image/jpeg" - "log" - "math" + "image" + "image/color" + _ "image/jpeg" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - gophersImage *ebiten.Image - fiveyearsImage *ebiten.Image - maskImage *ebiten.Image - spotLightImage *ebiten.Image - spotLightX = 0 - spotLightY = 0 - spotLightVX = 1 - spotLightVY = 1 + gophersImage *ebiten.Image + fiveyearsImage *ebiten.Image + maskImage *ebiten.Image + spotLightImage *ebiten.Image + spotLightX = 0 + spotLightY = 0 + spotLightVX = 1 + spotLightVY = 1 ) func update(screen *ebiten.Image) error { - spotLightX += spotLightVX - spotLightY += spotLightVY - if spotLightX < 0 { - spotLightX = -spotLightX - spotLightVX = -spotLightVX - } - if spotLightY < 0 { - spotLightY = -spotLightY - spotLightVY = -spotLightVY - } - w, h := spotLightImage.Size() - maxX, maxY := screenWidth-w, screenHeight-h - if maxX < spotLightX { - spotLightX = -spotLightX + 2*maxX - spotLightVX = -spotLightVX - } - if maxY < spotLightY { - spotLightY = -spotLightY + 2*maxY - spotLightVY = -spotLightVY - } + spotLightX += spotLightVX + spotLightY += spotLightVY + if spotLightX < 0 { + spotLightX = -spotLightX + spotLightVX = -spotLightVX + } + if spotLightY < 0 { + spotLightY = -spotLightY + spotLightVY = -spotLightVY + } + w, h := spotLightImage.Size() + maxX, maxY := screenWidth-w, screenHeight-h + if maxX < spotLightX { + spotLightX = -spotLightX + 2*maxX + spotLightVX = -spotLightVX + } + if maxY < spotLightY { + spotLightY = -spotLightY + 2*maxY + spotLightVY = -spotLightVY + } - if err := maskImage.Clear(); err != nil { - return err - } + if err := maskImage.Clear(); err != nil { + return err + } - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(spotLightX), float64(spotLightY)) - if err := maskImage.DrawImage(spotLightImage, op); err != nil { - return err - } + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(spotLightX), float64(spotLightY)) + if err := maskImage.DrawImage(spotLightImage, op); err != nil { + return err + } - op = &ebiten.DrawImageOptions{} - op.CompositeMode = ebiten.CompositeModeSourceOut - if err := maskImage.DrawImage(fiveyearsImage, op); err != nil { - return err - } + op = &ebiten.DrawImageOptions{} + op.CompositeMode = ebiten.CompositeModeSourceOut + if err := maskImage.DrawImage(fiveyearsImage, op); err != nil { + return err + } - if err := screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff}); err != nil { - return err - } - if err := screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{}); err != nil { - return err - } - if err := screen.DrawImage(maskImage, &ebiten.DrawImageOptions{}); err != nil { - return err - } + if err := screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff}); err != nil { + return err + } + if err := screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{}); err != nil { + return err + } + if err := screen.DrawImage(maskImage, &ebiten.DrawImageOptions{}); err != nil { + return err + } - return nil + return nil } func max(a, b int) int { - if a < b { - return b - } - return a + if a < b { + return b + } + return a } func min(a, b int) int { - if a < b { - return a - } - return b + if a < b { + return a + } + return b } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - fiveyearsImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - - as := image.Point{128, 128} - a := image.NewAlpha(image.Rectangle{image.ZP, as}) - for j := 0; j < as.Y; j++ { - for i := 0; i < as.X; i++ { - r := as.X / 2 - d := math.Sqrt(float64((i-r)*(i-r) + (j-r)*(j-r))) - b := uint8(max(0, min(0xff, 3*0xff-int(d*3*0xff)/r))) - a.SetAlpha(i, j, color.Alpha{b}) + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + fiveyearsImage, _, err = ebitenutil.NewImageFromFile("_resources/images/fiveyears.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + + as := image.Point{128, 128} + a := image.NewAlpha(image.Rectangle{image.ZP, as}) + for j := 0; j < as.Y; j++ { + for i := 0; i < as.X; i++ { + r := as.X / 2 + d := math.Sqrt(float64((i-r)*(i-r) + (j-r)*(j-r))) + b := uint8(max(0, min(0xff, 3*0xff-int(d*3*0xff)/r))) + a.SetAlpha(i, j, color.Alpha{b}) + } + } + spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil { + log.Fatal(err) } - } - spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil { - log.Fatal(err) - } } diff --git a/_docs/public/examples/mosaic.html b/_docs/public/examples/mosaic.html index 69c5fec7a..8610ec4d1 100644 --- a/_docs/public/examples/mosaic.html +++ b/_docs/public/examples/mosaic.html @@ -27,53 +27,53 @@ package main import ( - _ "image/jpeg" - "log" + _ "image/jpeg" + "log" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) const mosaicRatio = 16 var ( - gophersImage *ebiten.Image - gophersRenderTarget *ebiten.Image + gophersImage *ebiten.Image + gophersRenderTarget *ebiten.Image ) func update(screen *ebiten.Image) error { - op := &ebiten.DrawImageOptions{} - op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) - if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil { - return err - } - op = &ebiten.DrawImageOptions{} - op.GeoM.Scale(mosaicRatio, mosaicRatio) - if err := screen.DrawImage(gophersRenderTarget, op); err != nil { - return err - } - return nil + op := &ebiten.DrawImageOptions{} + op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) + if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil { + return err + } + op = &ebiten.DrawImageOptions{} + op.GeoM.Scale(mosaicRatio, mosaicRatio) + if err := screen.DrawImage(gophersRenderTarget, op); err != nil { + return err + } + return nil } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - w, h := gophersImage.Size() - gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + w, h := gophersImage.Size() + gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/noise.html b/_docs/public/examples/noise.html index 8f2717720..abf908ae7 100644 --- a/_docs/public/examples/noise.html +++ b/_docs/public/examples/noise.html @@ -27,61 +27,61 @@ package main import ( - "fmt" - "image" - "log" + "fmt" + "image" + "log" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - noiseImage *image.RGBA + noiseImage *image.RGBA ) type rand struct { - x, y, z, w uint32 + x, y, z, w uint32 } func (r *rand) next() uint32 { - // math/rand is too slow to keep 60 FPS on web browsers. - // Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift - t := r.x ^ (r.x << 11) - r.x, r.y, r.z = r.y, r.z, r.w - r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8)) - return r.w + // math/rand is too slow to keep 60 FPS on web browsers. + // Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift + t := r.x ^ (r.x << 11) + r.x, r.y, r.z = r.y, r.z, r.w + r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8)) + return r.w } var randInstance = &rand{12345678, 4185243, 776511, 45411} func update(screen *ebiten.Image) error { - const l = screenWidth * screenHeight - for i := 0; i < l; i++ { - x := randInstance.next() - noiseImage.Pix[4*i] = uint8(x >> 24) - noiseImage.Pix[4*i+1] = uint8(x >> 16) - noiseImage.Pix[4*i+2] = uint8(x >> 8) - noiseImage.Pix[4*i+3] = 0xff - } - if err := screen.ReplacePixels(noiseImage.Pix); err != nil { - return err - } - if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())); err != nil { - return err - } - return nil + const l = screenWidth * screenHeight + for i := 0; i < l; i++ { + x := randInstance.next() + noiseImage.Pix[4*i] = uint8(x >> 24) + noiseImage.Pix[4*i+1] = uint8(x >> 16) + noiseImage.Pix[4*i+2] = uint8(x >> 8) + noiseImage.Pix[4*i+3] = 0xff + } + if err := screen.ReplacePixels(noiseImage.Pix); err != nil { + return err + } + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())); err != nil { + return err + } + return nil } func main() { - noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)) - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)) + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/paint.html b/_docs/public/examples/paint.html index ac87f9826..b6169e896 100644 --- a/_docs/public/examples/paint.html +++ b/_docs/public/examples/paint.html @@ -27,103 +27,103 @@ package main import ( - "fmt" - "image" - "image/color" - "log" - "math" + "fmt" + "image" + "image/color" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - count int - brushImage *ebiten.Image - canvasImage *ebiten.Image + count int + brushImage *ebiten.Image + canvasImage *ebiten.Image ) func paint(screen *ebiten.Image, x, y int) error { - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(x), float64(y)) - op.ColorM.Scale(1.0, 0.50, 0.125, 1.0) - theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS - op.ColorM.RotateHue(theta) - if err := canvasImage.DrawImage(brushImage, op); err != nil { - return err - } - return nil + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(float64(x), float64(y)) + op.ColorM.Scale(1.0, 0.50, 0.125, 1.0) + theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS + op.ColorM.RotateHue(theta) + if err := canvasImage.DrawImage(brushImage, op); err != nil { + return err + } + return nil } func update(screen *ebiten.Image) error { - drawn := false - mx, my := ebiten.CursorPosition() - if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { - if err := paint(screen, mx, my); err != nil { - return err + drawn := false + mx, my := ebiten.CursorPosition() + if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { + if err := paint(screen, mx, my); err != nil { + return err + } + drawn = true } - drawn = true - } - for _, t := range ebiten.Touches() { - x, y := t.Position() - if err := paint(screen, x, y); err != nil { - return err + for _, t := range ebiten.Touches() { + x, y := t.Position() + if err := paint(screen, x, y); err != nil { + return err + } + drawn = true + } + if drawn { + count++ } - drawn = true - } - if drawn { - count++ - } - if err := screen.DrawImage(canvasImage, nil); err != nil { - return err - } + if err := screen.DrawImage(canvasImage, nil); err != nil { + return err + } - msg := fmt.Sprintf("(%d, %d)", mx, my) - for _, t := range ebiten.Touches() { - x, y := t.Position() - msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID()) - } - if err := ebitenutil.DebugPrint(screen, msg); err != nil { - return err - } - return nil + msg := fmt.Sprintf("(%d, %d)", mx, my) + for _, t := range ebiten.Touches() { + x, y := t.Position() + msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID()) + } + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } + return nil } func main() { - var err error - const a0, a1, a2 = 0x40, 0xc0, 0xff - pixels := []uint8{ - a0, a1, a1, a0, - a1, a2, a2, a1, - a1, a2, a2, a1, - a0, a1, a1, a0, - } - brushImage, err = ebiten.NewImageFromImage(&image.Alpha{ - Pix: pixels, - Stride: 4, - Rect: image.Rect(0, 0, 4, 4), - }, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } + var err error + const a0, a1, a2 = 0x40, 0xc0, 0xff + pixels := []uint8{ + a0, a1, a1, a0, + a1, a2, a2, a1, + a1, a2, a2, a1, + a0, a1, a1, a0, + } + brushImage, err = ebiten.NewImageFromImage(&image.Alpha{ + Pix: pixels, + Stride: 4, + Rect: image.Rect(0, 0, 4, 4), + }, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } - canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := canvasImage.Fill(color.White); err != nil { - log.Fatal(err) - } + canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := canvasImage.Fill(color.White); err != nil { + log.Fatal(err) + } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/perspective.html b/_docs/public/examples/perspective.html index 6a130c977..cdc93a4db 100644 --- a/_docs/public/examples/perspective.html +++ b/_docs/public/examples/perspective.html @@ -27,66 +27,66 @@ package main import ( - _ "image/jpeg" - "log" + _ "image/jpeg" + "log" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - gophersImage *ebiten.Image + gophersImage *ebiten.Image ) type parts struct { - image *ebiten.Image + image *ebiten.Image } func (p parts) Len() int { - _, h := p.image.Size() - return h + _, h := p.image.Size() + return h } func (p parts) Dst(i int) (x0, y0, x1, y1 int) { - w, h := p.image.Size() - width := w + i*3/4 - x := ((h - i) * 3 / 4) / 2 - return x, i, x + width, i + 1 + w, h := p.image.Size() + width := w + i*3/4 + x := ((h - i) * 3 / 4) / 2 + return x, i, x + width, i + 1 } func (p parts) Src(i int) (x0, y0, x1, y1 int) { - w, _ := p.image.Size() - return 0, i, w, i + 1 + w, _ := p.image.Size() + return 0, i, w, i + 1 } func update(screen *ebiten.Image) error { - op := &ebiten.DrawImageOptions{ - ImageParts: &parts{gophersImage}, - } - w, h := gophersImage.Size() - maxWidth := float64(w) + float64(h)*0.75 - op.GeoM.Translate(-maxWidth/2, -float64(h)/2) - op.GeoM.Translate(screenWidth/2, screenHeight/2) - if err := screen.DrawImage(gophersImage, op); err != nil { - return err - } - return nil + op := &ebiten.DrawImageOptions{ + ImageParts: &parts{gophersImage}, + } + w, h := gophersImage.Size() + maxWidth := float64(w) + float64(h)*0.75 + op.GeoM.Translate(-maxWidth/2, -float64(h)/2) + op.GeoM.Translate(screenWidth/2, screenHeight/2) + if err := screen.DrawImage(gophersImage, op); err != nil { + return err + } + return nil } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/piano.html b/_docs/public/examples/piano.html index fa3615dc1..72b1fe0af 100644 --- a/_docs/public/examples/piano.html +++ b/_docs/public/examples/piano.html @@ -27,31 +27,31 @@ package main import ( - "fmt" - "image/color" - "log" - "math" + "fmt" + "image/color" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/audio" - "github.com/hajimehoshi/ebiten/ebitenutil" - "github.com/hajimehoshi/ebiten/examples/common" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/audio" + "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten/examples/common" ) const ( - screenWidth = 320 - screenHeight = 240 - sampleRate = 44100 + screenWidth = 320 + screenHeight = 240 + sampleRate = 44100 ) var audioContext *audio.Context func init() { - var err error - audioContext, err = audio.NewContext(sampleRate) - if err != nil { - log.Fatal(err) - } + var err error + audioContext, err = audio.NewContext(sampleRate) + if err != nil { + log.Fatal(err) + } } var pcm = make([]float64, 4*sampleRate) @@ -59,203 +59,203 @@ var pcm = make([]float64, 4*sampleRate) const baseFreq = 220 func init() { - s := float64(sampleRate) - amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2} - x := []float64{4.0, 2.0, 1.0, 0.5, 0.25} - for i := 0; i < len(pcm); i++ { - v := 0.0 - twoPiF := 2.0 * math.Pi * baseFreq - for j := 0; j < len(amp); j++ { - a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s)) - v += a * math.Sin(float64(i)*twoPiF*float64(j+1)/s) + s := float64(sampleRate) + amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2} + x := []float64{4.0, 2.0, 1.0, 0.5, 0.25} + for i := 0; i < len(pcm); i++ { + v := 0.0 + twoPiF := 2.0 * math.Pi * baseFreq + for j := 0; j < len(amp); j++ { + a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s)) + v += a * math.Sin(float64(i)*twoPiF*float64(j+1)/s) + } + pcm[i] = v / 5.0 } - pcm[i] = v / 5.0 - } } var ( - noteCache = map[int][]byte{} + noteCache = map[int][]byte{} ) func toBytes(l, r []int16) []byte { - if len(l) != len(r) { - panic("len(l) must equal to len(r)") - } - b := make([]byte, len(l)*4) - for i := range l { - b[4*i] = byte(l[i]) - b[4*i+1] = byte(l[i] >> 8) - b[4*i+2] = byte(r[i]) - b[4*i+3] = byte(r[i] >> 8) - } - return b + if len(l) != len(r) { + panic("len(l) must equal to len(r)") + } + b := make([]byte, len(l)*4) + for i := range l { + b[4*i] = byte(l[i]) + b[4*i+1] = byte(l[i] >> 8) + b[4*i+2] = byte(r[i]) + b[4*i+3] = byte(r[i] >> 8) + } + return b } func addNote(freq float64, vol float64) error { - // TODO: Call Close method of *audio.Player. - // However, this works without Close because Close is automatically called when GC - // collects a *audio.Player object. - f := int(freq) - if n, ok := noteCache[f]; ok { + // TODO: Call Close method of *audio.Player. + // However, this works without Close because Close is automatically called when GC + // collects a *audio.Player object. + f := int(freq) + if n, ok := noteCache[f]; ok { + p, err := audio.NewPlayerFromBytes(audioContext, n) + if err != nil { + return err + } + if err := p.Play(); err != nil { + return err + } + return nil + } + length := len(pcm) * baseFreq / f + l := make([]int16, length) + r := make([]int16, length) + j := 0 + jj := 0 + for i := 0; i < len(l); i++ { + p := pcm[j] + l[i] = int16(p * vol * math.MaxInt16) + r[i] = l[i] + jj += f + j = jj / baseFreq + } + n := toBytes(l, r) + noteCache[f] = n p, err := audio.NewPlayerFromBytes(audioContext, n) if err != nil { - return err + return err } if err := p.Play(); err != nil { - return err + return err } return nil - } - length := len(pcm) * baseFreq / f - l := make([]int16, length) - r := make([]int16, length) - j := 0 - jj := 0 - for i := 0; i < len(l); i++ { - p := pcm[j] - l[i] = int16(p * vol * math.MaxInt16) - r[i] = l[i] - jj += f - j = jj / baseFreq - } - n := toBytes(l, r) - noteCache[f] = n - p, err := audio.NewPlayerFromBytes(audioContext, n) - if err != nil { - return err - } - if err := p.Play(); err != nil { - return err - } - return nil } var keys = []ebiten.Key{ - ebiten.KeyQ, - ebiten.KeyA, - ebiten.KeyW, - ebiten.KeyS, - ebiten.KeyD, - ebiten.KeyR, - ebiten.KeyF, - ebiten.KeyT, - ebiten.KeyG, - ebiten.KeyH, - ebiten.KeyU, - ebiten.KeyJ, - ebiten.KeyI, - ebiten.KeyK, - ebiten.KeyO, - ebiten.KeyL, + ebiten.KeyQ, + ebiten.KeyA, + ebiten.KeyW, + ebiten.KeyS, + ebiten.KeyD, + ebiten.KeyR, + ebiten.KeyF, + ebiten.KeyT, + ebiten.KeyG, + ebiten.KeyH, + ebiten.KeyU, + ebiten.KeyJ, + ebiten.KeyI, + ebiten.KeyK, + ebiten.KeyO, + ebiten.KeyL, } var keyStates = map[ebiten.Key]int{} func init() { - for _, key := range keys { - keyStates[key] = 0 - } + for _, key := range keys { + keyStates[key] = 0 + } } func updateInput() { - for _, key := range keys { - if !ebiten.IsKeyPressed(key) { - keyStates[key] = 0 - continue + for _, key := range keys { + if !ebiten.IsKeyPressed(key) { + keyStates[key] = 0 + continue + } + keyStates[key]++ } - keyStates[key]++ - } } var ( - imagePiano *ebiten.Image + imagePiano *ebiten.Image ) func init() { - var err error - imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest) - if err != nil { - panic(err) - } - if err := imageEmpty.Fill(color.White); err != nil { - panic(err) - } - imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) - if err != nil { - panic(err) - } - whiteKeys := []string{"A", "S", "D", "F", "G", "H", "J", "K", "L"} - width := 24 - y := 48 - for i, k := range whiteKeys { - x := i*width + 36 - height := 112 - op := &ebiten.DrawImageOptions{} - w, h := imageEmpty.Size() - op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) - op.GeoM.Translate(float64(x), float64(y)) - op.ColorM.Scale(1, 1, 1, 1) - if err := imagePiano.DrawImage(imageEmpty, op); err != nil { - panic(err) + var err error + imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest) + if err != nil { + panic(err) } - if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black); err != nil { - panic(err) + if err := imageEmpty.Fill(color.White); err != nil { + panic(err) + } + imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) + if err != nil { + panic(err) + } + whiteKeys := []string{"A", "S", "D", "F", "G", "H", "J", "K", "L"} + width := 24 + y := 48 + for i, k := range whiteKeys { + x := i*width + 36 + height := 112 + op := &ebiten.DrawImageOptions{} + w, h := imageEmpty.Size() + op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) + op.GeoM.Translate(float64(x), float64(y)) + op.ColorM.Scale(1, 1, 1, 1) + if err := imagePiano.DrawImage(imageEmpty, op); err != nil { + panic(err) + } + if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black); err != nil { + panic(err) + } } - } - blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"} - for i, k := range blackKeys { - if k == "" { - continue + blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"} + for i, k := range blackKeys { + if k == "" { + continue + } + x := i*width + 24 + height := 64 + op := &ebiten.DrawImageOptions{} + w, h := imageEmpty.Size() + op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) + op.GeoM.Translate(float64(x), float64(y)) + op.ColorM.Scale(0, 0, 0, 1) + if err := imagePiano.DrawImage(imageEmpty, op); err != nil { + panic(err) + } + if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White); err != nil { + panic(err) + } } - x := i*width + 24 - height := 64 - op := &ebiten.DrawImageOptions{} - w, h := imageEmpty.Size() - op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) - op.GeoM.Translate(float64(x), float64(y)) - op.ColorM.Scale(0, 0, 0, 1) - if err := imagePiano.DrawImage(imageEmpty, op); err != nil { - panic(err) - } - if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White); err != nil { - panic(err) - } - } } func update(screen *ebiten.Image) error { - updateInput() - for i, key := range keys { - if keyStates[key] != 1 { - continue + updateInput() + for i, key := range keys { + if keyStates[key] != 1 { + continue + } + if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil { + return err + } } - if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil { - return err + + if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { + return err + } + if err := screen.DrawImage(imagePiano, nil); err != nil { + return err } - } - if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { - return err - } - if err := screen.DrawImage(imagePiano, nil); err != nil { - return err - } + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil { + return err + } - if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil { - return err - } - - if err := audioContext.Update(); err != nil { - return err - } - return nil + if err := audioContext.Update(); err != nil { + return err + } + return nil } func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/rotate.html b/_docs/public/examples/rotate.html index cd4d50266..d63ad338b 100644 --- a/_docs/public/examples/rotate.html +++ b/_docs/public/examples/rotate.html @@ -27,46 +27,46 @@ package main import ( - _ "image/jpeg" - "log" - "math" + _ "image/jpeg" + "log" + "math" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - count int - gophersImage *ebiten.Image + count int + gophersImage *ebiten.Image ) func update(screen *ebiten.Image) error { - count++ - w, h := gophersImage.Size() - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(-float64(w)/2, -float64(h)/2) - op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360) - op.GeoM.Translate(screenWidth/2, screenHeight/2) - if err := screen.DrawImage(gophersImage, op); err != nil { - return err - } - return nil + count++ + w, h := gophersImage.Size() + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(-float64(w)/2, -float64(h)/2) + op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360) + op.GeoM.Translate(screenWidth/2, screenHeight/2) + if err := screen.DrawImage(gophersImage, op); err != nil { + return err + } + return nil } func main() { - var err error - gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Rotate (Ebiten Demo)"); err != nil { - log.Fatal(err) - } + var err error + gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Rotate (Ebiten Demo)"); err != nil { + log.Fatal(err) + } } diff --git a/_docs/public/examples/sprites.html b/_docs/public/examples/sprites.html index fffd89776..b4cf24c83 100644 --- a/_docs/public/examples/sprites.html +++ b/_docs/public/examples/sprites.html @@ -27,149 +27,149 @@ package main import ( - "fmt" - _ "image/png" - "log" - "math/rand" + "fmt" + _ "image/png" + "log" + "math/rand" - "github.com/hajimehoshi/ebiten" - "github.com/hajimehoshi/ebiten/ebitenutil" + "github.com/hajimehoshi/ebiten" + "github.com/hajimehoshi/ebiten/ebitenutil" ) const ( - screenWidth = 320 - screenHeight = 240 + screenWidth = 320 + screenHeight = 240 ) var ( - ebitenImage *ebiten.Image - ebitenImageWidth = 0 - ebitenImageHeight = 0 + ebitenImage *ebiten.Image + ebitenImageWidth = 0 + ebitenImageHeight = 0 ) type Sprite struct { - image *ebiten.Image - x int - y int - vx int - vy int + image *ebiten.Image + x int + y int + vx int + vy int } func (s *Sprite) Update() { - s.x += s.vx - s.y += s.vy - if s.x < 0 { - s.x = -s.x - s.vx = -s.vx - } - if s.y < 0 { - s.y = -s.y - s.vy = -s.vy - } - w, h := s.image.Size() - if screenWidth <= s.x+w { - s.x = 2*(screenWidth-w) - s.x - s.vx = -s.vx - } - if screenHeight <= s.y+h { - s.y = 2*(screenHeight-h) - s.y - s.vy = -s.vy - } + s.x += s.vx + s.y += s.vy + if s.x < 0 { + s.x = -s.x + s.vx = -s.vx + } + if s.y < 0 { + s.y = -s.y + s.vy = -s.vy + } + w, h := s.image.Size() + if screenWidth <= s.x+w { + s.x = 2*(screenWidth-w) - s.x + s.vx = -s.vx + } + if screenHeight <= s.y+h { + s.y = 2*(screenHeight-h) - s.y + s.vy = -s.vy + } } type Sprites struct { - sprites []*Sprite - num int + sprites []*Sprite + num int } func (s Sprites) Update() { - for _, sprite := range s.sprites { - sprite.Update() - } + for _, sprite := range s.sprites { + sprite.Update() + } } func (s Sprites) Len() int { - return s.num + return s.num } func (s Sprites) Dst(i int) (x0, y0, x1, y1 int) { - if s.num <= i { - return 0, 0, 0, 0 - } - ss := s.sprites[i] - return ss.x, ss.y, ss.x + ebitenImageWidth, ss.y + ebitenImageHeight + if s.num <= i { + return 0, 0, 0, 0 + } + ss := s.sprites[i] + return ss.x, ss.y, ss.x + ebitenImageWidth, ss.y + ebitenImageHeight } func (s Sprites) Src(i int) (x0, y0, x1, y1 int) { - if s.num <= i { - return 0, 0, 0, 0 - } - return 0, 0, ebitenImageWidth, ebitenImageHeight + if s.num <= i { + return 0, 0, 0, 0 + } + return 0, 0, ebitenImageWidth, ebitenImageHeight } const ( - MinSprites = 0 - MaxSprites = 50000 + MinSprites = 0 + MaxSprites = 50000 ) var sprites = &Sprites{make([]*Sprite, MaxSprites), 500} func update(screen *ebiten.Image) error { - if ebiten.IsKeyPressed(ebiten.KeyLeft) { - sprites.num -= 20 - if sprites.num < MinSprites { - sprites.num = MinSprites + if ebiten.IsKeyPressed(ebiten.KeyLeft) { + sprites.num -= 20 + if sprites.num < MinSprites { + sprites.num = MinSprites + } } - } - if ebiten.IsKeyPressed(ebiten.KeyRight) { - sprites.num += 20 - if MaxSprites < sprites.num { - sprites.num = MaxSprites + if ebiten.IsKeyPressed(ebiten.KeyRight) { + sprites.num += 20 + if MaxSprites < sprites.num { + sprites.num = MaxSprites + } } - } - sprites.Update() + sprites.Update() - if ebiten.IsRunningSlowly() { - return nil - } - op := &ebiten.DrawImageOptions{ - ImageParts: sprites, - } - op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) - if err := screen.DrawImage(ebitenImage, op); err != nil { - return err - } - msg := fmt.Sprintf(`FPS: %0.2f + if ebiten.IsRunningSlowly() { + return nil + } + op := &ebiten.DrawImageOptions{ + ImageParts: sprites, + } + op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) + if err := screen.DrawImage(ebitenImage, op); err != nil { + return err + } + msg := fmt.Sprintf(`FPS: %0.2f Num of sprites: %d Press <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len()) - if err := ebitenutil.DebugPrint(screen, msg); err != nil { - return err - } - return nil + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } + return nil } func main() { - var err error - ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) - if err != nil { - log.Fatal(err) - } - ebitenImageWidth, ebitenImageHeight = ebitenImage.Size() - for i := range sprites.sprites { - w, h := ebitenImage.Size() - x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h) - vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1 - sprites.sprites[i] = &Sprite{ - image: ebitenImage, - x: x, - y: y, - vx: vx, - vy: vy, + var err error + ebitenImage, _, err = ebitenutil.NewImageFromFile("_resources/images/ebiten.png", ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + ebitenImageWidth, ebitenImageHeight = ebitenImage.Size() + for i := range sprites.sprites { + w, h := ebitenImage.Size() + x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h) + vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1 + sprites.sprites[i] = &Sprite{ + image: ebitenImage, + x: x, + y: y, + vx: vx, + vy: vy, + } + } + if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil { + log.Fatal(err) } - } - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sprites (Ebiten Demo)"); err != nil { - log.Fatal(err) - } } diff --git a/_docs/public/index.html b/_docs/public/index.html index 3efc99038..529cdb539 100644 --- a/_docs/public/index.html +++ b/_docs/public/index.html @@ -98,17 +98,17 @@ the main.go file:

package main
 
 import (
-    "github.com/hajimehoshi/ebiten"
-    "github.com/hajimehoshi/ebiten/ebitenutil"
+        "github.com/hajimehoshi/ebiten"
+        "github.com/hajimehoshi/ebiten/ebitenutil"
 )
 
 func update(screen *ebiten.Image) error {
-    ebitenutil.DebugPrint(screen, "Hello world!")
-    return nil
+        ebitenutil.DebugPrint(screen, "Hello world!")
+        return nil
 }
 
 func main() {
-    ebiten.Run(update, 320, 240, 2, "Hello world!")
+        ebiten.Run(update, 320, 240, 2, "Hello world!")
 }