69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
"github.com/micosilent/go_chip8/emulator"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type Game struct {
|
|
counter int
|
|
lastFrameTime time.Time
|
|
debugText string
|
|
emulator *emulator.CHIP8
|
|
}
|
|
|
|
func (g *Game) Update() error {
|
|
return nil
|
|
}
|
|
|
|
func newGame() *Game {
|
|
return &Game{
|
|
counter: 1,
|
|
lastFrameTime: time.Now(),
|
|
debugText: "",
|
|
emulator: emulator.NewChip8(),
|
|
}
|
|
}
|
|
|
|
func check(err error) {
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
deltaTime := time.Since(g.lastFrameTime)
|
|
fps := int(1 / deltaTime.Seconds())
|
|
g.debugText = fmt.Sprintf("Counter: %d, fps: %d", g.counter, fps)
|
|
ebitenutil.DebugPrint(screen, g.debugText)
|
|
g.counter++
|
|
g.lastFrameTime = time.Now()
|
|
}
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
return 320, 240
|
|
}
|
|
|
|
func (g *Game) LoadROM(path string) {
|
|
dat, err := os.ReadFile(path)
|
|
check(err)
|
|
|
|
err = g.emulator.LoadROMIntoMemory(dat)
|
|
check(err)
|
|
}
|
|
|
|
func main() {
|
|
game := newGame()
|
|
game.LoadROM("roms/ibm_logo.ch8")
|
|
ebiten.SetWindowSize(480, 320)
|
|
ebiten.SetWindowTitle("Go CHIP8 Emulator")
|
|
|
|
err := ebiten.RunGame(game)
|
|
check(err)
|
|
}
|