Skip to content

Commit

Permalink
feat(core): Add basic emulation and gui
Browse files Browse the repository at this point in the history
  • Loading branch information
flga committed Feb 28, 2019
0 parents commit 8a601fc
Show file tree
Hide file tree
Showing 365 changed files with 58,254 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Editor bloat
.vscode

sdlldev
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=vnes
BINARY_WIN=vnes.exe

all: test build
build:
$(GOBUILD) -o $(BINARY_NAME) ./cmd/nes/*.go
test:
$(GOTEST) -v ./nes
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
rm -f $(BINARY_WIN)
run:
$(GOBUILD) -o $(BINARY_NAME) ./cmd/nes/*.go
./$(BINARY_NAME)

build-windows:
CGO_ENABLED="1" CC="/usr/bin/x86_64-w64-mingw32-gcc" GOOS="windows" \
CGO_LDFLAGS="-lmingw32 -lSDL2" CGO_CFLAGS="-D_REENTRANT" \
$(GOBUILD) -x -o $(BINARY_WIN) ./cmd/nes/*.go
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nes
Binary file added assets/runescape_uf.ttf
Binary file not shown.
265 changes: 265 additions & 0 deletions cmd/nes/game.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package main

import (
"fmt"
"time"

"github.com/veandco/go-sdl2/ttf"

"github.com/flga/nes/nes"
"github.com/veandco/go-sdl2/sdl"
)

type gameWindow struct {
baseWidth int32
baseHeight int32
visible bool
showGrid bool
window *sdl.Window
renderer *sdl.Renderer
tex *sdl.Texture
rect *sdl.Rect
font *ttf.Font
}

func newGameWindow(scale int32, title string) (*gameWindow, uint32, error) {
var baseWidth, baseHeight int32 = 256, 240

window, renderer, err := sdl.CreateWindowAndRenderer(baseWidth*scale, baseHeight*scale, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
if err != nil {
return nil, 0, fmt.Errorf("unable to create game window: %s", err)
}
renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND)

id, err := window.GetID()
if err != nil {
return nil, 0, fmt.Errorf("unable to get game window id: %s", err)
}

font, err := ttf.OpenFont("assets/runescape_uf.ttf", 6*int(scale))
if err != nil {
return nil, 0, fmt.Errorf("unable to open font: %s", err)
}

tex, err := renderer.CreateTexture(sdl.PIXELFORMAT_ABGR8888, sdl.TEXTUREACCESS_STREAMING, baseWidth, baseHeight)
if err != nil {
return nil, id, fmt.Errorf("unable to create game texture: %s", err)
}

rect := &sdl.Rect{X: 0, Y: 0, W: baseWidth * scale, H: baseHeight * scale}

window.SetTitle(title)

return &gameWindow{
baseWidth: baseWidth,
baseHeight: baseHeight,
visible: true,
window: window,
renderer: renderer,
tex: tex,
font: font,
rect: rect,
}, id, nil
}

func (w *gameWindow) Render(console *nes.Console, fps time.Duration) error {
buf := console.Buffer()

pixels, _, err := w.tex.Lock(nil)
if err != nil {
return fmt.Errorf("unable to lock game texture: %s", err)
}
copy(pixels, buf.Pix)
w.tex.Unlock()

if err := w.renderer.Clear(); err != nil {
return fmt.Errorf("unable to clear game renderer: %s", err)
}

if err := w.renderer.Copy(w.tex, nil, w.rect); err != nil {
return fmt.Errorf("unable to copy game: %s", err)
}

fpsSur, err := w.font.RenderUTF8Solid(fmt.Sprintf("%.fms/%.fms", fps.Seconds()*1000, console.FrameTime().Seconds()*1000), sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil {
return fmt.Errorf("unable to create fps surface: %s", err)
}
defer fpsSur.Free()

fpsTex, err := w.renderer.CreateTextureFromSurface(fpsSur)
if err != nil {
return fmt.Errorf("unable to create fps texture: %s", err)
}
defer fpsTex.Destroy()

if err := w.renderer.Copy(fpsTex, nil, &sdl.Rect{W: fpsSur.W, H: fpsSur.H, X: w.rect.X + w.rect.W - fpsSur.W - 10, Y: w.rect.Y + 10}); err != nil {
return fmt.Errorf("unable to copy game: %s", err)
}

if w.showGrid {
w.renderer.SetDrawColor(255, 255, 255, 255/2)
for i := int32(0); i < 30; i++ {
cellHeight := i * w.rect.H / 30
w.renderer.DrawLine(
w.rect.X, w.rect.Y+cellHeight,
w.rect.X+w.rect.W, w.rect.Y+cellHeight,
)
}
for i := int32(0); i < 32; i++ {
cellWidth := i * w.rect.W / 32
w.renderer.DrawLine(
w.rect.X+cellWidth, w.rect.Y,
w.rect.X+cellWidth, w.rect.Y+w.rect.H,
)
}
w.renderer.SetDrawColor(0x13, 0xE0, 0xD7, 255)
for i := int32(0); i < 8; i++ {
cellHeight := i * w.rect.W / 8
w.renderer.DrawLine(
w.rect.X, w.rect.Y+cellHeight,
w.rect.X+w.rect.W, w.rect.Y+cellHeight,
)
}
for i := int32(0); i < 8; i++ {
cellWidth := i * w.rect.W / 8
w.renderer.DrawLine(
w.rect.X+cellWidth, w.rect.Y,
w.rect.X+cellWidth, w.rect.Y+w.rect.H,
)
}
}
w.renderer.SetDrawColor(0, 0, 0, 255)
w.renderer.Present()

return nil
}

func (w *gameWindow) Handle(event sdl.Event, console *nes.Console) error {
// fmt.Printf("%T, handle %T\n", w, event)
press := func(b nes.Button, pressed bool) {
if pressed {
console.Controller1.Press(b)
} else {
console.Controller1.Release(b)
}
}

switch evt := event.(type) {
case *sdl.WindowEvent:
if evt.Event == sdl.WINDOWEVENT_CLOSE {
w.hide()
}

if evt.Event == sdl.WINDOWEVENT_RESIZED {
resize(w.window, float64(w.baseWidth), float64(w.baseHeight), w.rect)
}
case *sdl.ControllerButtonEvent:
if evt.Button == sdl.CONTROLLER_BUTTON_GUIDE {
console.Reset()
}
if evt.Button == sdl.CONTROLLER_BUTTON_A {
press(nes.A, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_B {
press(nes.B, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_START {
press(nes.Start, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_BACK {
press(nes.Select, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_DPAD_UP {
press(nes.Up, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_DPAD_DOWN {
press(nes.Down, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_DPAD_LEFT {
press(nes.Left, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
if evt.Button == sdl.CONTROLLER_BUTTON_DPAD_RIGHT {
press(nes.Right, evt.Type == sdl.CONTROLLERBUTTONDOWN)
}
case *sdl.KeyboardEvent:
if evt.Type == sdl.KEYUP && evt.Keysym.Sym == sdl.K_g {
w.showGrid = !w.showGrid
}

if evt.Type == sdl.KEYUP && evt.Keysym.Sym == sdl.K_r {
console.Reset()
}

if evt.Keysym.Sym == sdl.K_a {
press(nes.A, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_z {
press(nes.B, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_s {
press(nes.Start, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_x {
press(nes.Select, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_UP {
press(nes.Up, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_DOWN {
press(nes.Down, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_LEFT {
press(nes.Left, evt.Type == sdl.KEYDOWN)
}
if evt.Keysym.Sym == sdl.K_RIGHT {
press(nes.Right, evt.Type == sdl.KEYDOWN)
}
}

return nil
}

func (w *gameWindow) Visible() bool {
return w.visible
}

func (w *gameWindow) show() {
w.visible = true
w.window.Show()
}

func (w *gameWindow) hide() {
w.visible = false
w.window.Hide()
}

func (w *gameWindow) Toggle() {
if w.visible {
w.hide()
} else {
w.show()
}
}

func (w *gameWindow) Free() error {
if w.font != nil {
w.font.Close()
}
if w.tex != nil {
if err := w.tex.Destroy(); err != nil {
return err
}
}
if w.renderer != nil {
if err := w.renderer.Destroy(); err != nil {
return err
}
}
if w.window != nil {
if err := w.window.Destroy(); err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 8a601fc

Please sign in to comment.