[gnome-games] retro-runner: Construct lazily



commit 70872b0670570268545dbfa4e23f4c341a17e78f
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue Mar 22 22:00:12 2016 +0100

    retro-runner: Construct lazily
    
    Move the initialization of the runner from its construction to when we
    use it.
    
    Allows to create the runner way before we use it which is needed to
    implement a generic game storing its runner.

 src/retro/retro-runner.vala |   66 +++++++++++++++++++++++++++---------------
 1 files changed, 42 insertions(+), 24 deletions(-)
---
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index d26d7e4..5d38173 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -33,6 +33,8 @@ public class Games.RetroRunner : Object, Runner {
        private string snapshot_path;
        private string screenshot_path;
 
+       private string module_basename;
+       private string uri;
        private Uid uid;
 
        private bool _running;
@@ -45,37 +47,18 @@ public class Games.RetroRunner : Object, Runner {
                get { return _running; }
        }
 
-       private bool construction_succeeded;
+       private bool is_initialized;
 
        public RetroRunner (string module_basename, string uri, Uid uid) throws Error {
-               construction_succeeded = false;
+               is_initialized = false;
 
+               this.module_basename = module_basename;
+               this.uri = uri;
                this.uid = uid;
-
-               video = new RetroGtk.CairoDisplay ();
-
-               widget = new Gtk.EventBox ();
-               widget.add (video);
-               video.visible = true;
-
-               gamepad = new RetroGtk.VirtualGamepad (widget);
-               keyboard = new RetroGtk.Keyboard (widget);
-
-               prepare_core (module_basename, uri);
-               core.shutdown.connect (on_shutdown);
-
-               core.run (); // Needed to finish preparing some cores.
-
-               loop = new Retro.MainLoop (core);
-               running = false;
-
-               load_screenshot ();
-
-               construction_succeeded = true;
        }
 
        ~RetroRunner () {
-               if (!construction_succeeded)
+               if (!is_initialized)
                        return;
 
                loop.stop ();
@@ -94,6 +77,9 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        public void start () throws Error {
+               if (!is_initialized)
+                       init();
+
                loop.stop ();
 
                load_ram ();
@@ -104,6 +90,9 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        public void resume () throws Error {
+               if (!is_initialized)
+                       init();
+
                loop.stop ();
 
                load_ram ();
@@ -114,6 +103,32 @@ public class Games.RetroRunner : Object, Runner {
                running = true;
        }
 
+       private void init () throws Error {
+               if (is_initialized)
+                       return;
+
+               video = new RetroGtk.CairoDisplay ();
+
+               widget = new Gtk.EventBox ();
+               widget.add (video);
+               video.visible = true;
+
+               gamepad = new RetroGtk.VirtualGamepad (widget);
+               keyboard = new RetroGtk.Keyboard (widget);
+
+               prepare_core (module_basename, uri);
+               core.shutdown.connect (on_shutdown);
+
+               core.run (); // Needed to finish preparing some cores.
+
+               loop = new Retro.MainLoop (core);
+               running = false;
+
+               load_screenshot ();
+
+               is_initialized = true;
+       }
+
        private void prepare_core (string module_basename, string uri) throws Error {
                var module_path = Retro.search_module (module_basename);
                var module = File.new_for_path (module_path);
@@ -180,6 +195,9 @@ public class Games.RetroRunner : Object, Runner {
        }
 
        public void pause () {
+               if (!is_initialized)
+                       return;
+
                loop.stop ();
                running = false;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]