Browse Source

Added a plugin system

Robin de Jong 1 year ago
parent
commit
00958eb2b3
7 changed files with 62 additions and 28 deletions
  1. 5 3
      game/plugin/plugin.go
  2. 30 6
      game/game.go
  3. 6 5
      main.go
  4. BIN
      plugins/out/test.so
  5. 3 0
      plugins/test_plugin/build
  6. 1 2
      game/plugin/mario/map.go
  7. 17 12
      game/plugin/mario/plugin.go

+ 5 - 3
game/plugin/plugin.go

@@ -1,4 +1,4 @@
-package plugin
+package data
 
 import (
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/nes"
@@ -7,7 +7,9 @@ import (
 )
 
 type Plugin interface {
-	Init(w, h int)
-	Tick(frame screen.Frame, memory nes.Memory)
+	Init(util.Size, util.Size)
+	Tick(screen.Frame, nes.Memory)
 	GetCamera() util.Camera
+
+	Info() string
 }

+ 30 - 6
game/game.go

@@ -9,7 +9,9 @@ import (
 	"time"
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/util"
 	"runtime"
-	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/plugin/mario"
+	"log"
+	"plugin"
+	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/data"
 )
 
 var EmuSize = util.Size{Width: 240, Height: 256}
@@ -19,18 +21,41 @@ type Game struct {
 	screen *screen.Screen
 	buffer chan []uint32
 	outBuffer chan []uint32
+
+	plugin data.Plugin
 }
 
-func NewGame(width, height uint, rom string) *Game {
+func NewGame(width, height uint, plugin string, rom string) *Game {
 	g := Game{}
 	g.screen = screen.NewScreen(width, height)
 	g.size = util.Size{Width: width, Height: height}
 
+	g.initPlugin(plugin)
 	g.initNes(rom)
 
 	return &g
 }
 
+func (g *Game) initPlugin(pluginDir string) {
+	plug, err := plugin.Open("plugins/out/test.so")
+	if err != nil {
+		log.Fatal("Error opening data: ", err)
+	}
+
+	ref, err := plug.Lookup("Plugin")
+	if err != nil {
+		log.Fatal("Error looking up the reference: ", err)
+	}
+
+	var ok bool
+	g.plugin, ok = ref.(data.Plugin)
+	if !ok {
+		log.Fatal("Error referencing the data")
+	}
+
+	log.Println("Loaded data: ", g.plugin.Info())
+}
+
 func (g *Game) initNes(rom string) {
 	parts := strings.Split(rom, "/")
 	nes.GameName = strings.Split(parts[len(parts) - 1], ".")[0]
@@ -73,18 +98,17 @@ func (g *Game) Run() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
 
 
-	plug := mario.Plugin{}
 	//plug.Init(150, 16)
-	plug.Init(EmuSize, g.size)
+	g.plugin.Init(EmuSize, g.size)
 
 	go func() {
 		for {
 			buf := <-g.buffer
 
 			frame := screen.NewFrame(buf, EmuSize)
-			plug.Tick(frame, nes.Ram)
+			g.plugin.Tick(frame, nes.Ram)
 
-			g.screen.Convert(frame, plug.GetCamera(), g.size)
+			g.screen.Convert(frame, g.plugin.GetCamera(), g.size)
 		}
 	}()
 

+ 6 - 5
main.go

@@ -1,11 +1,12 @@
 package main
 
 import (
-	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game"
+	"log"
+	"os"
+
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/screen/render"
+	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game"
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/server"
-	"os"
-	"log"
 )
 
 const Width = 150
@@ -29,10 +30,10 @@ func main() {
 	io := server.NewServer(9998)
 
 	if Ledbanner {
-		g = game.NewGame(Width, Height, os.Args[1])
+		g = game.NewGame(Width, Height, os.Args[1], os.Args[2])
 		renderer = render.StdOutRenderer{}
 	} else {
-		g = game.NewGame(Width, Height, "rom/mario_2.nes")
+		g = game.NewGame(Width, Height, "plugins/out/test.so", "rom/mario_2.nes")
 		ioRender := render.SocketIORenderer{}
 		ioRender.IO = io.GetSocketIOServer()
 		renderer = ioRender

BIN
plugins/out/test.so


+ 3 - 0
plugins/test_plugin/build

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+go build -buildmode=plugin -o ../out/test.so ./main.go

+ 1 - 2
game/plugin/mario/map.go

@@ -1,4 +1,4 @@
-package mario
+package mapper
 
 import (
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/nes"
@@ -30,7 +30,6 @@ const (
 type Map struct {
 	memory nes.Memory
 
-
 	// Prevent yPosition from lagging
 	lagFix []uint8
 	lagPos int // Can't bother to do it differently

+ 17 - 12
game/plugin/mario/plugin.go

@@ -1,20 +1,20 @@
-package mario
+package main
 
 import (
-	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/nes"
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/screen"
+	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/nes"
 	"glitter.jemoeders.website/biodiscus/ledbanner_nes/util"
-	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/plugin"
+	"glitter.jemoeders.website/biodiscus/ledbanner_nes/game/data"
+	"glitter.jemoeders.website/biodiscus/ledbanner_nes/plugins/test_plugin/data"
 )
 
-//const NormalHeight = 24
 const NormalHeight = 24
 const BigHeight = 35
 
-type Plugin struct {
-	plugin.Plugin
+type Mario struct {
+	data.Plugin
 
-	mario *Map
+	mario *mapper.Map
 
 	camera util.Camera
 	scale int
@@ -25,23 +25,23 @@ type Plugin struct {
 	yOffset int
 }
 
-func (m *Plugin) Init(originalSize util.Size, projectionSize util.Size) {
+func (m *Mario) Init(originalSize util.Size, projectionSize util.Size) {
 	m.original = originalSize
 	m.projection = projectionSize
 	m.yOffset = 0
 
 	m.camera = util.NewCamera(0, 0, int(m.original.Width), NormalHeight)
-	m.mario = NewMario(nes.Ram)
+	m.mario = mapper.NewMario(nes.Ram)
 }
 
-func (m *Plugin) Tick(frame screen.Frame, memory nes.Memory) {
+func (m *Mario) Tick(frame screen.Frame, memory nes.Memory) {
 	m.mario.SetMemory(memory)
 
 	y := m.mario.GetYPosition()
 
 	viewportState := m.mario.GetViewportState()
 
-	if viewportState == InView {
+	if viewportState == mapper.InView {
 		m.camera.SetPosition(0, int(y))
 
 		if m.mario.IsJumping() {
@@ -109,7 +109,12 @@ func (m *Plugin) Tick(frame screen.Frame, memory nes.Memory) {
 	}
 }
 
-func (m *Plugin) GetCamera() util.Camera {
+func (m *Mario) GetCamera() util.Camera {
 	return m.camera
 }
 
+func (m *Mario) Info() string {
+	return "Mario data V0.1"
+}
+
+var Plugin Mario