summaryrefslogtreecommitdiff
path: root/engine/CoreObjects
diff options
context:
space:
mode:
Diffstat (limited to 'engine/CoreObjects')
-rw-r--r--engine/CoreObjects/Balancer.go75
-rw-r--r--engine/CoreObjects/OverlayScene.go6
-rw-r--r--engine/CoreObjects/Scene.go44
-rw-r--r--engine/CoreObjects/SceneManager.go54
4 files changed, 137 insertions, 42 deletions
diff --git a/engine/CoreObjects/Balancer.go b/engine/CoreObjects/Balancer.go
new file mode 100644
index 0000000..0e80b10
--- /dev/null
+++ b/engine/CoreObjects/Balancer.go
@@ -0,0 +1,75 @@
+package coreobjects
+
+import (
+ "sync"
+
+ settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
+)
+
+type runner struct {
+ ExecChan chan UpdateFunction
+ CompleteChan chan any
+}
+
+func makeRunner(execChan chan UpdateFunction, completeChan chan any) *runner {
+ var outp = &runner {
+ ExecChan: execChan,
+ CompleteChan: completeChan,
+ }
+ outp.Init()
+ return outp
+}
+
+func (base *runner) Init() {
+ go func() {
+ for {
+ var task = <- base.ExecChan
+ task()
+ base.CompleteChan <- nil
+ }
+ }()
+}
+
+// Balances the updates so that the execution of WASM doesn't clog the system
+type balancer struct {
+ MaxRoutines int
+ // So that we don't use invloke balancer twice
+ mut *sync.Mutex
+ Runners []*runner
+ // balancer ->
+ ExecChan chan UpdateFunction
+ CompleteChan chan any
+}
+
+func makeBalancer() *balancer {
+ var maxRoutines = settings.Current.MaxUpdateRoutines
+ var outp = &balancer{
+ MaxRoutines: maxRoutines,
+ mut: &sync.Mutex{},
+ Runners: make([]*runner, maxRoutines),
+ ExecChan: make(chan UpdateFunction, maxRoutines),
+ CompleteChan: make(chan any, maxRoutines),
+ }
+ for i := range maxRoutines {
+ outp.Runners[i] = makeRunner(outp.ExecChan, outp.CompleteChan)
+ }
+ return outp
+}
+
+func (base *balancer) Execute(tasks []UpdateFunction) {
+ base.mut.Lock()
+ defer base.mut.Unlock()
+ for _, v := range tasks {
+ base.ExecChan <- v
+ }
+
+ // Sync
+ var results = 0
+ for {
+ <- base.CompleteChan
+ results++
+ if results == len(tasks) {
+ break
+ }
+ }
+}
diff --git a/engine/CoreObjects/OverlayScene.go b/engine/CoreObjects/OverlayScene.go
index 73aa987..3689262 100644
--- a/engine/CoreObjects/OverlayScene.go
+++ b/engine/CoreObjects/OverlayScene.go
@@ -9,9 +9,15 @@ import (
type OveralyScene struct {
FPS FpsMeter
+ updateFunctions []UpdateFunction
}
func (base *OveralyScene) Create(_ *SceneManager) {
+ base.updateFunctions = []UpdateFunction { base.Update }
+}
+
+func (base *OveralyScene) GetUpdateFunctions() []UpdateFunction {
+ return base.updateFunctions
}
func (base *OveralyScene) Destroy() {
diff --git a/engine/CoreObjects/Scene.go b/engine/CoreObjects/Scene.go
index b6b886c..8a11e29 100644
--- a/engine/CoreObjects/Scene.go
+++ b/engine/CoreObjects/Scene.go
@@ -2,54 +2,14 @@ package coreobjects
import rl "github.com/gen2brain/raylib-go/raylib"
-type SceneManager struct {
- OverlayScene Scene
- SelectedScene Scene
-}
+type UpdateFunction func()
type Scene interface {
Create(*SceneManager)
Destroy()
Update()
+ GetUpdateFunctions() []UpdateFunction
Draw()
GetMousePosition() rl.Vector2
}
-func InitSceneManager() SceneManager {
- return SceneManager{
- OverlayScene: &OveralyScene{},
- SelectedScene: nil,
- }
-}
-
-func (base *SceneManager) ChangeScene(newScene Scene) {
- if base.SelectedScene != nil {
- base.SelectedScene.Destroy()
- }
- base.SelectedScene = newScene
- base.SelectedScene.Create(base)
-}
-
-func (base *SceneManager) Update() {
- if (base.SelectedScene != nil) {
- base.SelectedScene.Update()
- }
-
- if (base.OverlayScene != nil) {
- base.OverlayScene.Update()
- }
-
- if rl.IsKeyDown(rl.KeyF11) {
- rl.ToggleFullscreen()
- }
-}
-
-func (base *SceneManager) Draw() {
- if (base.SelectedScene != nil) {
- base.SelectedScene.Draw()
- }
-
- if (base.OverlayScene != nil) {
- base.OverlayScene.Draw()
- }
-}
diff --git a/engine/CoreObjects/SceneManager.go b/engine/CoreObjects/SceneManager.go
new file mode 100644
index 0000000..dc44dd9
--- /dev/null
+++ b/engine/CoreObjects/SceneManager.go
@@ -0,0 +1,54 @@
+package coreobjects
+
+import (
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+type SceneManager struct {
+ OverlayScene Scene
+ SelectedScene Scene
+
+ balancer *balancer
+}
+
+func InitSceneManager() SceneManager {
+ return SceneManager{
+ OverlayScene: &OveralyScene{},
+ SelectedScene: nil,
+ balancer: makeBalancer(),
+ }
+}
+
+func (base *SceneManager) ChangeScene(newScene Scene) {
+ if base.SelectedScene != nil {
+ base.SelectedScene.Destroy()
+ }
+ base.SelectedScene = newScene
+ base.SelectedScene.Create(base)
+}
+
+func (base *SceneManager) Update() {
+ if (base.SelectedScene != nil) {
+ base.SelectedScene.Update()
+ }
+
+ if (base.OverlayScene != nil) {
+ base.OverlayScene.Update()
+ }
+
+ base.balancer.Execute(base.SelectedScene.GetUpdateFunctions())
+
+ if rl.IsKeyDown(rl.KeyF11) {
+ rl.ToggleFullscreen()
+ }
+}
+
+func (base *SceneManager) Draw() {
+ if (base.SelectedScene != nil) {
+ base.SelectedScene.Draw()
+ }
+
+ if (base.OverlayScene != nil) {
+ base.OverlayScene.Draw()
+ }
+}