Skip to content

Commit

Permalink
weird...
Browse files Browse the repository at this point in the history
  • Loading branch information
temidaradev committed Jun 12, 2024
1 parent a832e03 commit 6c5d933
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
22 changes: 0 additions & 22 deletions assets/assets.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package assets

import (
"bytes"
"embed"
"image"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
)

//go:embed *
Expand All @@ -32,11 +29,6 @@ var Font_ttf []byte
//go:embed OpenSans-Medium.ttf
var Sans_ttf []byte

//go:embed lofi.ogg
var audioBGM []byte

const SampleRate = 44100

func GetSingleImage(name string) *ebiten.Image {
file, err := assets.Open(name)
if err != nil {
Expand All @@ -50,17 +42,3 @@ func GetSingleImage(name string) *ebiten.Image {

return ebiten.NewImageFromImage(img)
}

type Sound struct {
PlayerSound *audio.Player
}

func SoundFunc() {
_ = audio.NewContext(SampleRate)
stream, err := vorbis.DecodeWithSampleRate(SampleRate, bytes.NewReader(audioBGM))
if err != nil {
panic(err)
}
audioPlayer := audio.CurrentContext().NewPlayer(stream)

}
45 changes: 45 additions & 0 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,66 @@ package game

import (
"bytes"
_ "embed"
"fmt"
"image/color"
"log"
"main/assets"
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)

//go:embed lofi.ogg
var audioBGM []byte

type Sound struct {
player *audio.Player
audioContext *audio.Context
}

const (
screenWidth = 640
screenHeight = 480
sampleRate = 48000
bytesPerSample = 4

introLengthInSecond = 5
loopLengthInSecond = 4
)

func (s *Sound) SoundFunc() {
if s.audioContext == nil {
s.audioContext = audio.NewContext(sampleRate)
}

// Decode an Ogg file.
// oggS is a decoded io.ReadCloser and io.Seeker.
oggS, err := vorbis.DecodeWithoutResampling(bytes.NewReader(audioBGM))
if err != nil {
log.Fatal(err)
}

sound := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*bytesPerSample*sampleRate, loopLengthInSecond*bytesPerSample*sampleRate)

s.player, err = s.audioContext.NewPlayer(sound)
if err != nil {
log.Fatal(err)
}

s.player.Play()
}

type Game struct {
camera camera
player Player
npc NPC
s Sound

background Background
isDebugModeOn bool
Expand Down Expand Up @@ -125,6 +169,7 @@ func (g *Game) isDebugMode(enabled bool) {
}

func (g *Game) Update() error {
g.s.SoundFunc()
if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
g.menuOff = true
}
Expand Down
File renamed without changes.

0 comments on commit 6c5d933

Please sign in to comment.