diff mde/setup/init2.d @ 63:66d555da083e

Moved many modules/packages to better reflect usage.
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 27 Jun 2008 18:35:33 +0100
parents mde/scheduler/init2.d@bca7e2342d77
children 3a737e06dc50
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/setup/init2.d	Fri Jun 27 18:35:33 2008 +0100
@@ -0,0 +1,101 @@
+/* LICENSE BLOCK
+Part of mde: a Modular D game-oriented Engine
+Copyright © 2007-2008 Diggory Hardy
+
+This program is free software: you can redistribute it and/or modify it under the terms
+of the GNU General Public License as published by the Free Software Foundation, either
+version 2 of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>. */
+
+/** This module is the start of implementing the following:
+*
+* Idea: change import direction so this module adds all init functions. All init functions are
+* wrapped in another function before being run in a thread (i.e. run indirectly). Functions fail
+* either by throwing an exception or by returning a boolean. Functions may take parameters, e.g.
+* "out cleanupFunc[]".
+*
+* This should make it much easier to tell what actually happens during init and to order init such
+* that dependencies are honoured.
+*
+* Currently some external modules depend on InitFunctions, while some are set up from here. Once
+* all are set up from here, the Init* modules can be rearranged. */
+/* Idea: go back to init being controlled from elsewhere. Add a function to wait for another init
+ * function to complete (threaded; might need to be done differently for non-threaded). */
+module mde.setup.init2;
+
+import mde.setup.initFunctions;
+
+import tango.util.log.Log : Log, Logger;
+
+// Modules requiring init code running:
+import imde = mde.imde;
+import mde.gui.Gui;
+import mde.input.Input;
+import font = mde.font.font;
+
+// NOTE: error reporting needs a revision
+
+private Logger logger;
+static this () {
+    logger = Log.getLogger ("mde.setup.init2");
+    
+    init.addFunc (&initInput, "initInput");
+    init.addFunc (&guiLoad, "guiLoad");
+}
+
+void guiLoad () {   // init func
+    try {
+        font.FontStyle.initialize;
+        gui.load (GUI);
+        cleanup.addFunc (&guiSave, "guiSave");
+    } catch (Exception e) {
+        logger.fatal ("guiLoad failed: " ~ e.msg);
+        setInitFailure;
+    }
+}
+void guiSave () {   // cleanup func
+    try {
+        gui.save (GUI);
+    } catch (Exception e) {
+        logger.fatal ("guiSave failed: " ~ e.msg);
+        setInitFailure;
+    }
+}
+private const GUI = "gui";
+
+void initInput () { // init func
+    try {
+        imde.input.loadConfig ();         // (may also create instance)
+        
+        // Quit on escape. NOTE: quit via SDL_QUIT event is handled completely independently!
+        imde.input.addButtonCallback (cast(Input.inputID) 0x0u, delegate void(Input.inputID i, bool b) {
+            if (b) {
+                logger.info ("Quiting...");
+                imde.run = false;
+            }
+        } );
+    } catch (Exception e) {
+        logger.fatal ("initInput failed: " ~ e.msg);
+        setInitFailure;
+    }
+}
+
+/+ Potential wrapper function:
+// Template to call function, catching exceptions:
+void wrap(alias Func) () {
+    try {
+        Func();
+    } catch (Exception e) {
+        logger.fatal (FAIL_MSG);
+        logger.fatal (e.msg);
+        setInitFailure;
+    }
+}
+private const FAIL_MSG = "Unexpected exception caught:";
++/