Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
fix: Fixed some specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Aug 12, 2019
1 parent 4c7adc3 commit 06a34b3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 28 deletions.
11 changes: 1 addition & 10 deletions 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"
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 15 additions & 7 deletions pkg/gb/gb_test.go
Expand Up @@ -2,6 +2,7 @@ package gb

import (
"image"
"image/color"
"image/png"
"os"
"testing"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions 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)
Expand Down
15 changes: 11 additions & 4 deletions pkg/window/native.go
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/window/wasm.go
Expand Up @@ -4,7 +4,6 @@ package window

import (
"github.com/bokuweb/gopher-boy/pkg/pad"
"github.com/bokuweb/gopher-boy/pkg/types"
)

// Window is
Expand All @@ -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 */
}

Expand Down
6 changes: 6 additions & 0 deletions public/index.html
Expand Up @@ -74,12 +74,18 @@
};
</script>
<style>
html {
padding: 0;
margin: 0;
}
body {
background: #1abc9c;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 0;
margin: 0;
}
.gameboy {
height: 480px;
Expand Down
Binary file added test/expect/cpu_instr.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06a34b3

Please sign in to comment.