diff options
author | davidovski <david@davidovski.xyz> | 2024-04-22 12:42:38 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2024-04-22 12:42:38 +0100 |
commit | ec04c06ffad66ec95560aba08e70134d3ba2d5e5 (patch) | |
tree | 3ba968099cc113d166a6abbc6e9d40363aae5f14 | |
parent | 63253bbeb5c6a6a346443483bddc75276625d48a (diff) |
add audio
-rw-r--r-- | assets/start.wav | bin | 0 -> 49192 bytes | |||
-rw-r--r-- | assets/stop.wav | bin | 0 -> 76708 bytes | |||
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | main.go | 108 | ||||
-rw-r--r-- | objects.go | 10 |
5 files changed, 91 insertions, 28 deletions
diff --git a/assets/start.wav b/assets/start.wav Binary files differnew file mode 100644 index 0000000..a46f444 --- /dev/null +++ b/assets/start.wav diff --git a/assets/stop.wav b/assets/stop.wav Binary files differnew file mode 100644 index 0000000..3e0c8de --- /dev/null +++ b/assets/stop.wav @@ -7,6 +7,7 @@ require github.com/hajimehoshi/ebiten/v2 v2.7.2 require ( github.com/ebitengine/gomobile v0.0.0-20240329170434-1771503ff0a8 // indirect github.com/ebitengine/hideconsole v1.0.0 // indirect + github.com/ebitengine/oto/v3 v3.2.0 // indirect github.com/ebitengine/purego v0.7.0 // indirect github.com/jezek/xgb v1.1.1 // indirect golang.org/x/sync v0.6.0 // indirect @@ -1,8 +1,10 @@ package main import ( + "bytes" _ "embed" "fmt" + "image" "image/color" _ "image/png" "log" @@ -11,7 +13,8 @@ import ( "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/inpututil" - + "github.com/hajimehoshi/ebiten/v2/audio" + "github.com/hajimehoshi/ebiten/v2/audio/wav" ) @@ -30,6 +33,8 @@ const ( exitTransitionWeight = 0.9 ghostAlpha = 0.5 hightlightBorder = 2 + + sampleRate = 44100 ) var ( @@ -37,11 +42,27 @@ var ( noneShader_src []byte //go:embed shaders/vcr.kage vcrShader_src []byte + + //go:embed assets/tiles.png + tilesPng_src []byte + + //go:embed assets/character.png + characterPng_src []byte + + //go:embed assets/rewind.wav + rewindWav_src []byte + + //go:embed assets/stop.wav + stopWav_src []byte + + //go:embed assets/start.wav + startWav_src []byte ) var ( shaders map[string]*ebiten.Shader tilesImage *ebiten.Image + characterImage *ebiten.Image ) type State int @@ -61,6 +82,12 @@ type RecPoint struct { alpha float32 } +type AudioPlayer struct { + audioContext *audio.Context + rewindAudio *audio.Player + stopAudio *audio.Player + startAudio *audio.Player +} type Game struct { surface *ebiten.Image @@ -81,6 +108,8 @@ type Game struct { playerAi [][3]bool playerAiIdx int + + audioPlayer *AudioPlayer } func (g * Game)RecordPoint() { @@ -193,18 +222,6 @@ func (g *Game) Init() { func (g *Game) Update() error { g.time += 1 - //if ebiten.IsKeyJustPressed(ebiten.KeyR) { - // if g.state == IN_GAME { - // g.state = REVERSING - // g.shaderName = "vcr" - // } - //} else { - // if g.state == REVERSING { - // g.state = IN_GAME - // g.shaderName = "none" - // } - //} - if g.state == IN_GAME { if inpututil.IsKeyJustPressed(ebiten.KeyR) { g.SetReversing() @@ -254,7 +271,6 @@ func (g *Game) Update() error { if len(g.recording) == 0 { g.TransitionState() - fmt.Printf("end of recording state transition\n") } } @@ -443,7 +459,7 @@ func (g *Game)RemoveObject(obj *GameObject) { i++ } } - // Prevent memory leak by erasing truncated values + // Prevent memory leak by erasing truncated values // (not needed if values don't contain pointers, directly or indirectly) for j := i; j < len(g.objects); j++ { g.objects[j] = nil @@ -455,31 +471,83 @@ func (g *Game) SetReversing() { g.state = REVERSING g.shaderName = "vcr" g.player.alpha = 1.0 + g.audioPlayer.rewindAudio.Rewind() + g.audioPlayer.rewindAudio.Play() +} + +func (g *Game) StopRewinding() { + g.shaderName = "none" + if g.audioPlayer.rewindAudio.IsPlaying() { + g.audioPlayer.rewindAudio.Pause() + g.audioPlayer.startAudio.Rewind() + g.audioPlayer.startAudio.Play() + } + } func (g *Game) SetInGame() { g.state = IN_GAME - g.shaderName = "none" g.player.alpha = 1.0 + g.StopRewinding() } func (g *Game) SetPlacing() { g.state = PLACING - g.shaderName = "none" g.player.alpha = ghostAlpha + g.StopRewinding() } -func main() { - LoadShaders() +func loadAudio(wavFile []byte, audioContext *audio.Context) *audio.Player { var err error - tilesImage, _, err = ebitenutil.NewImageFromFile("assets/tiles.png") + sound, err := wav.DecodeWithoutResampling(bytes.NewReader(wavFile)) + if err != nil { + return nil + } + + p, err := audioContext.NewPlayer(sound) + + if err != nil { + return nil + } + return p +} + +func (g *Game) LoadAudio() { + g.audioPlayer = &AudioPlayer{} + + g.audioPlayer.audioContext = audio.NewContext(sampleRate) + g.audioPlayer.rewindAudio = loadAudio(rewindWav_src, g.audioPlayer.audioContext) + g.audioPlayer.stopAudio = loadAudio(stopWav_src, g.audioPlayer.audioContext) + g.audioPlayer.startAudio = loadAudio(startWav_src, g.audioPlayer.audioContext) +} + +func (g *Game) LoadImages() { + + img, _, err := image.Decode(bytes.NewReader(characterPng_src)) if err != nil { log.Fatal(err) } + characterImage = ebiten.NewImageFromImage(img) + + img, _, err = image.Decode(bytes.NewReader(tilesPng_src)) + if err != nil { + log.Fatal(err) + } + + tilesImage = ebiten.NewImageFromImage(img) + +} + +func main() { + LoadShaders() + + ebiten.SetWindowTitle("Hello, World!") game := &Game{} + game.LoadAudio() + game.LoadImages() game.Init() if err := ebiten.RunGame(game); err != nil { @@ -3,10 +3,8 @@ package main import ( "image" "image/color" - "log" "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/ebitenutil" "github.com/hajimehoshi/ebiten/v2/vector" ) @@ -130,6 +128,7 @@ func (o * GameObject) HasCollision(tilemap Tilemap, others []*GameObject, dir Di return f(obj, o) } } + return false } @@ -142,8 +141,6 @@ func (o * GameObject) Draw(screen *ebiten.Image, tilemap Tilemap) { if o.highlight { vector.StrokeRect(screen, o.x, o.y, float32(o.image.Bounds().Dx()), float32(o.image.Bounds().Dy()), hightlightBorder, color.RGBA{255, 100, 100, 255}, false) } - - } func (object * GameObject) Collide(other *GameObject) bool { @@ -208,10 +205,7 @@ func NewObject(game *Game, x, y float32) *GameObject{ func NewPlayer(game *Game, x, y float32) *GameObject{ player := NewObject(game, x, y) - playerImage, _, err := ebitenutil.NewImageFromFile("assets/character.png") - if err != nil { - log.Fatal(err) - } + playerImage := ebiten.NewImageFromImage(characterImage) player.image = playerImage.SubImage(image.Rect(4, 8, 27, 32)).(*ebiten.Image) |