From c69912268272867325882408b7ecdcbfc68f143b Mon Sep 17 00:00:00 2001 From: Soumitra Shewale Date: Wed, 7 Apr 2021 18:00:40 +0530 Subject: [PATCH] Add argument handling --- README.md | 18 +++++++++++++++++- go.mod | 1 + go.sum | 6 ++++++ main.go | 35 +++++++++++++++++++++++++++++------ screen.go | 6 +++--- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e27d60a..13b8c79 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,23 @@ My first attempt at emulation. This Go project aims to emulate a fully working C Everything works as far as I have tested, except for audio. +## Usage +``` +Usage: chip8 [--scaling SCALING] [--fps FPS] [--cpuspeed CPUSPEED] ROMPATH + +Positional arguments: + ROMPATH Path to CHIP8 ROM file. + +Options: + --scaling SCALING, -s SCALING + Pixel scaling. Adjusts size of display. [default: 15] + --fps FPS, -f FPS FPS to run display at. [default: 60] + --cpuspeed CPUSPEED, -c CPUSPEED + Speed of CPU relative to FPS. [default: 10] + --help, -h display this help and exit + --version display version and exit +``` + ## Controls Keys @@ -42,7 +59,6 @@ respectively ## TODO (in no particular order): -- Create a way to customize controls and settings (maybe using config files, maybe some GUI) - Add more invasive features (e.g. Soft reset, Hard reset, memory manipulation, etc.) - Implement VIP/Super chip8 - Implement audio (needs a better understanding of goroutines and channels) diff --git a/go.mod b/go.mod index 5052a14..5e2ae46 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/soumitradev/chip8 go 1.16 require ( + github.com/alexflint/go-arg v1.3.0 // indirect github.com/faiface/pixel v0.10.0 github.com/go-gl/gl v0.0.0-20210315015930-ae072cafe09d // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210311203641-62640a716d48 // indirect diff --git a/go.sum b/go.sum index dd39cf7..35e7fbd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,10 @@ +github.com/alexflint/go-arg v1.3.0 h1:UfldqSdFWeLtoOuVRosqofU4nmhI1pYEbT4ZFS34Bdo= +github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= +github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= +github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0= github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g= github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q= @@ -22,6 +27,7 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= diff --git a/main.go b/main.go index b2f2363..04fe91a 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,26 @@ package main import ( "time" + + "github.com/alexflint/go-arg" ) -// Adjust these according to your game/preferences -const SCALING = 15 -const FPS = 60 -const speed = 10 +// Adjust these according to your game preferences +var SCALING = 15 +var FPS = 60 +var speed = 10 + +type args struct { + ROMPATH string `arg:"positional" help:"Path to CHIP8 ROM file."` + SCALING int `default:"15" arg:"-s" help:"Pixel scaling. Adjusts size of display."` + FPS int `default:"60" arg:"-f" help:"FPS to run display at."` + CPUspeed int `default:"10" arg:"-c" help:"Speed of CPU relative to FPS."` +} + +// Return version info +func (args) Version() string { + return "chip8 v0.1.0" +} // Timing vars var startTime time.Time @@ -17,14 +31,23 @@ var elapsed time.Duration var fpsInterval time.Duration func main() { + // Use prefs in args + var args args + + arg.MustParse(&args) + var ROMPATH = args.ROMPATH + SCALING = args.SCALING + FPS = args.FPS + speed = args.CPUspeed + // Decide how often we render a frame - fpsInterval = time.Duration(time.Millisecond * 1000.0 / FPS) + fpsInterval = time.Duration(time.Millisecond * 1000.0 / time.Duration(FPS)) then = time.Now() startTime = then // Load the chip8 font and ROM into memory loadSprites() - loadROM("ROMs/SPACEINVADERS.ch8") + loadROM(ROMPATH) // Start display and take one step forward (render one frame) startDisplay() diff --git a/screen.go b/screen.go index 3b1462a..50461b2 100644 --- a/screen.go +++ b/screen.go @@ -83,7 +83,7 @@ func run() { // Configure window cfg := pixelgl.WindowConfig{ Title: "Chip8", - Bounds: pixel.R(0, 0, 64*SCALING, 32*SCALING), + Bounds: pixel.R(0, 0, float64(64*SCALING), float64(32*SCALING)), VSync: true, } window, err := pixelgl.NewWindow(cfg) @@ -134,10 +134,10 @@ func screenRender() { imd.Clear() for i := 0; i < 64*32; i++ { x := float64((i % 64) * SCALING) - y := 31*SCALING - float64((i/64)*SCALING) + y := float64(31*SCALING - (i/64)*SCALING) if display[i] { imd.Push(pixel.V(x, y)) - imd.Push(pixel.V(x+SCALING, y+SCALING)) + imd.Push(pixel.V(x+float64(SCALING), y+float64(SCALING))) imd.Rectangle(0) } }