diff --git a/pkg/gb/gb.go b/pkg/gb/gb.go index 9416978..b768e0e 100644 --- a/pkg/gb/gb.go +++ b/pkg/gb/gb.go @@ -1,10 +1,8 @@ package gb import ( - "image/color" "time" - "github.com/bokuweb/gopher-boy/pkg/constants" "github.com/bokuweb/gopher-boy/pkg/cpu" "github.com/bokuweb/gopher-boy/pkg/gpu" "github.com/bokuweb/gopher-boy/pkg/interfaces/window" @@ -44,14 +42,7 @@ func (g *GB) Start() { select { case <-t.C: buf := g.Next() - imgData := make([]color.RGBA, constants.ScreenWidth*constants.ScreenHeight) - i := 0 - for i*4 < len(buf) { - y := constants.ScreenHeight - (i / constants.ScreenWidth) - 1 - imgData[y*constants.ScreenWidth+i%constants.ScreenWidth] = color.RGBA{buf[i*4], buf[i*4+1], buf[i*4+2], buf[i*4+3]} - i++ - } - g.win.Render(imgData) + g.win.Render(buf) } } t.Stop() diff --git a/pkg/gb/gb_test.go b/pkg/gb/gb_test.go index 53897c7..095770b 100644 --- a/pkg/gb/gb_test.go +++ b/pkg/gb/gb_test.go @@ -2,6 +2,7 @@ package gb import ( "image" + "image/color" "image/png" "os" "testing" @@ -18,7 +19,6 @@ import ( "github.com/bokuweb/gopher-boy/pkg/interfaces/window" "github.com/bokuweb/gopher-boy/pkg/logger" "github.com/bokuweb/gopher-boy/pkg/ram" - "github.com/bokuweb/gopher-boy/pkg/types" "github.com/bokuweb/gopher-boy/pkg/utils" ) @@ -61,17 +61,25 @@ func setup(file string) *GB { return emu } -func set(img *image.RGBA, imageData types.ImageData) { +func set(img *image.RGBA, buf []byte) { + imgData := make([]color.RGBA, constants.ScreenWidth*constants.ScreenHeight) + i := 0 + for i*4 < len(buf) { + y := constants.ScreenHeight - (i / constants.ScreenWidth) - 1 + imgData[y*constants.ScreenWidth+i%constants.ScreenWidth] = color.RGBA{buf[i*4], buf[i*4+1], buf[i*4+2], buf[i*4+3]} + i++ + } + rect := img.Rect for y := rect.Min.Y; y < rect.Max.Y; y++ { for x := rect.Min.X; x < rect.Max.X; x++ { - img.Set(x, rect.Max.Y-y, imageData[y*rect.Max.X+x]) + img.Set(x, rect.Max.Y-y, imgData[y*rect.Max.X+x]) } } } -func skipFrame(emu *GB, n int) types.ImageData { - var image types.ImageData +func skipFrame(emu *GB, n int) []byte { + var image []byte for i := 0; i < n; i++ { image = emu.Next() } @@ -158,14 +166,14 @@ func TestROMs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { emu := setup(tt.path) - imageData := skipFrame(emu, tt.frame) + buf := skipFrame(emu, tt.frame) file, err := os.Create(ImagePathPrefix + tt.name + ".png") defer file.Close() if err != nil { panic(err) } img := image.NewRGBA(image.Rect(0, 0, constants.ScreenWidth, constants.ScreenHeight)) - set(img, imageData) + set(img, buf) if err := png.Encode(file, img); err != nil { panic(err) } diff --git a/pkg/interfaces/window/window.go b/pkg/interfaces/window/window.go index 78797e5..f92aaef 100644 --- a/pkg/interfaces/window/window.go +++ b/pkg/interfaces/window/window.go @@ -1,12 +1,8 @@ package window -import ( - "github.com/bokuweb/gopher-boy/pkg/types" -) - // Window is type Window interface { - Render(imageData types.ImageData) + Render(imageData []byte) Run(run func()) PollKey() KeyDown(button byte) diff --git a/pkg/window/native.go b/pkg/window/native.go index fd465a7..cfd71b6 100644 --- a/pkg/window/native.go +++ b/pkg/window/native.go @@ -8,7 +8,6 @@ import ( "github.com/bokuweb/gopher-boy/pkg/constants" "github.com/bokuweb/gopher-boy/pkg/pad" - "github.com/bokuweb/gopher-boy/pkg/types" "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" "golang.org/x/image/colornames" @@ -22,8 +21,17 @@ type Window struct { } // Render renders the pixels on the window. -func (w *Window) Render(imageData types.ImageData) { - w.image.Pix = imageData +func (w *Window) Render(buf []byte) { + + imgData := make([]color.RGBA, constants.ScreenWidth*constants.ScreenHeight) + i := 0 + for i*4 < len(buf) { + y := constants.ScreenHeight - (i / constants.ScreenWidth) - 1 + imgData[y*constants.ScreenWidth+i%constants.ScreenWidth] = color.RGBA{buf[i*4], buf[i*4+1], buf[i*4+2], buf[i*4+3]} + i++ + } + + w.image.Pix = imgData bg := color.RGBA{R: 0x0F, G: 0x38, B: 0x0F, A: 0xFF} w.win.Clear(bg) @@ -52,7 +60,6 @@ func (w *Window) Init() { cfg := pixelgl.WindowConfig{ Title: "gopher-boy", Bounds: pixel.R(0, 0, constants.ScreenWidth, constants.ScreenHeight), - // VSync: true, } win, err := pixelgl.NewWindow(cfg) if err != nil { diff --git a/pkg/window/wasm.go b/pkg/window/wasm.go index 5fa6735..18d2432 100644 --- a/pkg/window/wasm.go +++ b/pkg/window/wasm.go @@ -4,7 +4,6 @@ package window import ( "github.com/bokuweb/gopher-boy/pkg/pad" - "github.com/bokuweb/gopher-boy/pkg/types" ) // Window is @@ -14,7 +13,7 @@ type Window struct { } // Render renders the pixels on the window. -func (w *Window) Render(imageData types.ImageData) { +func (w *Window) Render(imageData []byte) { /* NOP */ } diff --git a/public/index.html b/public/index.html index 5005370..76b3546 100644 --- a/public/index.html +++ b/public/index.html @@ -74,12 +74,18 @@ };