113 lines
2.2 KiB
Go
113 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
"github.com/micosilent/go_chip8/emulator"
|
|
"image/color"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
screenWidth = 640
|
|
screenHeight = 320
|
|
gridWidth = 64
|
|
gridHeight = 32
|
|
)
|
|
|
|
type Game struct {
|
|
counter int
|
|
lastFrameTime time.Time
|
|
debugText string
|
|
emulator *emulator.CHIP8
|
|
screenImage *ebiten.Image
|
|
}
|
|
|
|
func (g *Game) Update() error {
|
|
|
|
// We need the emulator to process around 700 instructions per second
|
|
for i := 0; i < 1; i++ {
|
|
g.emulator.EmulateCycle()
|
|
if g.emulator.DrawFlag {
|
|
g.screenImage = g.DrawPixels()
|
|
g.emulator.DrawFlag = false
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *Game) DrawPixels() *ebiten.Image {
|
|
img := ebiten.NewImage(gridWidth, gridHeight)
|
|
white := color.White
|
|
black := color.Black
|
|
|
|
// Draw the boolean array to the image
|
|
for x := 0; x < gridWidth; x++ {
|
|
for y := 0; y < gridHeight; y++ {
|
|
if g.emulator.GetPixelAtCoordinate(x, y) {
|
|
img.Set(x, y, white)
|
|
} else {
|
|
img.Set(x, y, black)
|
|
}
|
|
}
|
|
}
|
|
|
|
return img
|
|
}
|
|
|
|
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()
|
|
|
|
scaleX := float64(screenWidth) / float64(gridWidth)
|
|
scaleY := float64(screenHeight) / float64(gridHeight)
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
op.GeoM.Scale(scaleX, scaleY)
|
|
screen.DrawImage(g.screenImage, op)
|
|
}
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
return screenWidth, screenHeight
|
|
}
|
|
|
|
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(screenWidth, screenHeight)
|
|
ebiten.SetWindowTitle("Go CHIP8 Emulator")
|
|
|
|
err := ebiten.RunGame(game)
|
|
check(err)
|
|
}
|