Procházet zdrojové kódy

Refactored all gameplay begin() member functions to start() (to match with the API change of end() to finish()).
Removed ScriptController::getInstance() and added Game::getScriptController().

Chris Culy před 13 roky
rodič
revize
648dd5474d
38 změnil soubory, kde provedl 440 přidání a 428 odebrání
  1. 1 11
      gameplay-luagen/README.md
  2. 2 2
      gameplay/src/Container.cpp
  3. 1 1
      gameplay/src/Control.cpp
  4. 2 2
      gameplay/src/Font.cpp
  5. 2 2
      gameplay/src/Font.h
  6. 1 1
      gameplay/src/Form.cpp
  7. 7 23
      gameplay/src/Game.cpp
  8. 11 11
      gameplay/src/Game.h
  9. 6 5
      gameplay/src/Game.inl
  10. 1 1
      gameplay/src/Joystick.cpp
  11. 1 1
      gameplay/src/Label.cpp
  12. 1 1
      gameplay/src/MeshBatch.cpp
  13. 2 2
      gameplay/src/MeshBatch.h
  14. 1 1
      gameplay/src/ParticleEmitter.cpp
  15. 1 1
      gameplay/src/PhysicsController.cpp
  16. 4 3
      gameplay/src/PlatformAndroid.cpp
  17. 4 3
      gameplay/src/PlatformMacOSX.mm
  18. 4 3
      gameplay/src/PlatformQNX.cpp
  19. 4 3
      gameplay/src/PlatformWin32.cpp
  20. 4 3
      gameplay/src/PlatformiOS.mm
  21. 1 1
      gameplay/src/Scene.cpp
  22. 2 2
      gameplay/src/Scene.h
  23. 106 83
      gameplay/src/ScriptController.cpp
  24. 76 74
      gameplay/src/ScriptController.h
  25. 33 32
      gameplay/src/ScriptController.inl
  26. 1 1
      gameplay/src/ScriptListener.cpp
  27. 2 2
      gameplay/src/SpriteBatch.cpp
  28. 2 2
      gameplay/src/SpriteBatch.h
  29. 35 35
      gameplay/src/lua/lua_Font.cpp
  30. 1 1
      gameplay/src/lua/lua_Font.h
  31. 47 0
      gameplay/src/lua/lua_Game.cpp
  32. 1 0
      gameplay/src/lua/lua_Game.h
  33. 35 35
      gameplay/src/lua/lua_MeshBatch.cpp
  34. 1 1
      gameplay/src/lua/lua_MeshBatch.h
  35. 1 42
      gameplay/src/lua/lua_ScriptController.cpp
  36. 0 1
      gameplay/src/lua/lua_ScriptController.h
  37. 35 35
      gameplay/src/lua/lua_SpriteBatch.cpp
  38. 1 1
      gameplay/src/lua/lua_SpriteBatch.h

+ 1 - 11
gameplay-luagen/README.md

@@ -31,13 +31,6 @@ There are also prebuilt binaries in the gameplay/bin folder.
     end
 - Note: you can't pass an enum to a function that doesn't explicitly take an enum (i.e. Control::setTextColor, which takes an unsigned char). In these cases, you need to go look up the enum values and pass them directly.
 
-
-
-Refactored ScriptController's interface to be more clean (moved utility functions that are only used by the generated bindings outside ScriptController into the ScriptUtil namespace).
-
-
-
-
 ## Unsupported Features
 - operators
 - templates
@@ -45,11 +38,8 @@ Refactored ScriptController's interface to be more clean (moved utility function
 - Lua doesn't support as many types as C++ so if there are functions that overload on parameters with types that overlap in Lua, the overloading won't work properly (i.e. char, short, int, long, float, double and all corresponding unsigned variants overlap in Lua).
 
 ### To Do List
-- Change begin() functions to start()
+- Get Mac OS X and iOS working.
 - Fix hierarchy inheritance
-- Remove getInstance
-- Make callback names same casing as actual functions (i.e. gamepadEvent).
-- Get all platforms working.
 - Fix memory leaks in gameplay-luagen and in generated code.
     * Add "@script{create}" to the appropriate gameplay functions.
     * Add "@script{own}" to array parameters that are owned by the function or class they are passed to?

+ 2 - 2
gameplay/src/Container.cpp

@@ -397,7 +397,7 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
         needsClear = true;
     }
 
-    spriteBatch->begin();
+    spriteBatch->start();
     Control::drawBorder(spriteBatch, clip);
     spriteBatch->finish();
 
@@ -419,7 +419,7 @@ void Container::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needs
         // Draw scroll bars.
         Rectangle clipRegion(_viewportClipBounds);
 
-        spriteBatch->begin();
+        spriteBatch->start();
 
         if (_scrollBarBounds.height > 0 &&
             ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL))

+ 1 - 1
gameplay/src/Control.cpp

@@ -991,7 +991,7 @@ void Control::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsCl
         GL_ASSERT( glDisable(GL_SCISSOR_TEST) );
     }
 
-    spriteBatch->begin();
+    spriteBatch->start();
     drawBorder(spriteBatch, clip);
     drawImages(spriteBatch, clip);
     spriteBatch->finish();

+ 2 - 2
gameplay/src/Font.cpp

@@ -172,10 +172,10 @@ unsigned int Font::getSize()
     return _size;
 }
 
-void Font::begin()
+void Font::start()
 {
     GP_ASSERT(_batch);
-    _batch->begin();
+    _batch->start();
 }
 
 Font::Text* Font::createText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size, Justify justify,

+ 2 - 2
gameplay/src/Font.h

@@ -155,9 +155,9 @@ public:
     unsigned int getSize();
 
     /**
-     * Begins text drawing for this font.
+     * Starts text drawing for this font.
      */
-    void begin();
+    void start();
 
     /**
      * Finishes text batching for this font and renders all drawn text.

+ 1 - 1
gameplay/src/Form.cpp

@@ -564,7 +564,7 @@ void Form::draw()
             _spriteBatch = SpriteBatch::create(_frameBuffer->getRenderTarget()->getTexture());
             GP_ASSERT(_spriteBatch);
         }
-        _spriteBatch->begin();
+        _spriteBatch->start();
         _spriteBatch->draw(_bounds.x, _bounds.y, 0, _bounds.width, _bounds.height, 0, _v1, _u2, 0, Vector4::one());
         _spriteBatch->finish();
     }

+ 7 - 23
gameplay/src/Game.cpp

@@ -134,7 +134,7 @@ bool Game::startup()
             const char* name;
             while ((name = scripts->getNextProperty()) != NULL)
             {
-                ScriptController::ScriptCallback callback = toCallback(name);
+                ScriptController::ScriptCallback callback = ScriptController::toCallback(name);
                 if (callback != ScriptController::INVALID_CALLBACK)
                 {
                     std::string url = scripts->getString();
@@ -317,6 +317,12 @@ void Game::frame()
     }
 }
 
+void Game::renderOnce(const char* function)
+{
+    _scriptController->executeFunction<void>(function, NULL);
+    Platform::swapBuffers();
+}
+
 void Game::updateOnce()
 {
     GP_ASSERT(_animationController);
@@ -514,26 +520,4 @@ Gamepad* Game::createGamepad(const char* gamepadId, const char* gamepadFormPath)
     return gamepad;
 }
 
-ScriptController::ScriptCallback Game::toCallback(const char* name)
-{
-    if (strcmp(name, "INITIALIZE") == 0)
-        return ScriptController::INITIALIZE;
-    else if (strcmp(name, "UPDATE") == 0)
-        return ScriptController::UPDATE;
-    else if (strcmp(name, "RENDER") == 0)
-        return ScriptController::RENDER;
-    else if (strcmp(name, "FINALIZE") == 0)
-        return ScriptController::FINALIZE;
-    else if (strcmp(name, "KEY_EVENT") == 0)
-        return ScriptController::KEY_EVENT;
-    else if (strcmp(name, "TOUCH_EVENT") == 0)
-        return ScriptController::TOUCH_EVENT;
-    else if (strcmp(name, "MOUSE_EVENT") == 0)
-        return ScriptController::MOUSE_EVENT;
-    else if (strcmp(name, "GAMEPAD_EVENT") == 0)
-        return ScriptController::GAMEPAD_EVENT;
-    else
-        return ScriptController::INVALID_CALLBACK;
-}
-
 }

+ 11 - 11
gameplay/src/Game.h

@@ -9,7 +9,6 @@
 #include "AudioController.h"
 #include "AnimationController.h"
 #include "PhysicsController.h"
-#include "ScriptController.h"
 #include "AudioListener.h"
 #include "Rectangle.h"
 #include "Vector4.h"
@@ -19,6 +18,8 @@
 namespace gameplay
 {
 
+class ScriptController;
+
 /**
  * Defines the basic game initialization, logic and platform delegates.
  */
@@ -218,6 +219,14 @@ public:
      */
     inline PhysicsController* getPhysicsController() const;
 
+    /**
+     * Gets the script controller for managing control of Lua scripts
+     * associated with the game.
+     * 
+     * @return The script controller for this game.
+     */
+    inline ScriptController* getScriptController() const;
+
     /**
      * Gets the audio listener for 3D audio.
      * 
@@ -435,7 +444,7 @@ protected:
      *
      * This is useful for rendering splash screens.
      */
-    inline void renderOnce(const char* function);
+    void renderOnce(const char* function);
 
     /**
      * Updates the game's internal systems (audio, animation, physics) once.
@@ -504,15 +513,6 @@ private:
      */
     Gamepad* createGamepad(const char* gamepadId, const char* gamepadFormPath);
 
-    /**
-     * Converts the given string to a valid script callback enumeration value
-     * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
-     * 
-     * @param name The name of the callback.
-     * @return The corresponding callback enumeration value.
-     */
-    static ScriptController::ScriptCallback toCallback(const char* name);
-
     bool _initialized;                          // If game has initialized yet.
     State _state;                               // The game state.
     static double _pausedTimeLast;              // The last time paused.

+ 6 - 5
gameplay/src/Game.inl

@@ -49,6 +49,11 @@ inline PhysicsController* Game::getPhysicsController() const
     return _physicsController;
 }
 
+inline ScriptController* Game::getScriptController() const
+{
+    return _scriptController;
+}
+
 template <class T>
 void Game::renderOnce(T* instance, void (T::*method)(void*), void* cookie)
 {
@@ -57,11 +62,7 @@ void Game::renderOnce(T* instance, void (T::*method)(void*), void* cookie)
     Platform::swapBuffers();
 }
 
-void Game::renderOnce(const char* function)
-{
-    ScriptController::getInstance()->executeFunction<void>(function, NULL);
-    Platform::swapBuffers();
-}
+
 
 inline bool Game::hasMouse()
 {

+ 1 - 1
gameplay/src/Joystick.cpp

@@ -195,7 +195,7 @@ void Joystick::update(const Control* container, const Vector2& offset)
 void Joystick::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
 {
     GP_ASSERT(spriteBatch);
-    spriteBatch->begin();
+    spriteBatch->start();
 
     // If the joystick is not absolute, then only draw if it is active.
     if (_absolute || (!_absolute && _state == ACTIVE))

+ 1 - 1
gameplay/src/Label.cpp

@@ -100,7 +100,7 @@ void Label::drawText(const Rectangle& clip)
     // Draw the text.
     if (_font)
     {
-        _font->begin();
+        _font->start();
         _font->drawText(_text.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
         _font->finish();
     }

+ 1 - 1
gameplay/src/MeshBatch.cpp

@@ -156,7 +156,7 @@ bool MeshBatch::resize(unsigned int capacity)
     return true;
 }
     
-void MeshBatch::begin()
+void MeshBatch::start()
 {
     _vertexCount = 0;
     _indexCount = 0;

+ 2 - 2
gameplay/src/MeshBatch.h

@@ -91,7 +91,7 @@ public:
     void add(T* vertices, unsigned int vertexCount, unsigned short* indices = NULL, unsigned int indexCount = 0);
 
     /**
-     * Begins batching.
+     * Starts batching.
      *
      * This method should be called before calling add() to add primitives to the batch.
      * After all primitives have been added to the batch, call the finish() method to
@@ -100,7 +100,7 @@ public:
      * Calling this method will clear any primitives currently in the batch and set the
      * position of the batch back to the beginning.
      */
-    void begin();
+    void start();
 
     /**
      * Indicates that batching is complete and prepares the batch for drawing.

+ 1 - 1
gameplay/src/ParticleEmitter.cpp

@@ -934,7 +934,7 @@ void ParticleEmitter::draw()
         }
 
         // Begin sprite batch drawing
-        _spriteBatch->begin();
+        _spriteBatch->start();
 
         // 2D Rotation.
         static const Vector2 pivot(0.5f, 0.5f);

+ 1 - 1
gameplay/src/PhysicsController.cpp

@@ -1458,7 +1458,7 @@ void PhysicsController::DebugDrawer::begin(const Matrix& viewProjection)
 {
     GP_ASSERT(_meshBatch);
     _viewProjection = &viewProjection;
-    _meshBatch->begin();
+    _meshBatch->start();
 }
 
 void PhysicsController::DebugDrawer::end()

+ 4 - 3
gameplay/src/PlatformAndroid.cpp

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "Game.h"
 #include "Form.h"
+#include "ScriptController.h"
 #include <unistd.h>
 
 #include <android/sensor.h>
@@ -965,7 +966,7 @@ void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned
     if (!Form::touchEventInternal(evt, x, y, contactIndex))
     {
         Game::getInstance()->touchEvent(evt, x, y, contactIndex);
-        ScriptController::getInstance()->touchEvent(evt, x, y, contactIndex);
+        Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
     }
 }
 
@@ -974,7 +975,7 @@ void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
     if (!Form::keyEventInternal(evt, key))
     {
         Game::getInstance()->keyEvent(evt, key);
-        ScriptController::getInstance()->keyEvent(evt, key);
+        Game::getInstance()->getScriptController()->keyEvent(evt, key);
     }
 }
 
@@ -990,7 +991,7 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
     else
     {
-        return ScriptController::getInstance()->mouseEvent(evt, x, y, wheelDelta);
+        return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }
 

+ 4 - 3
gameplay/src/PlatformMacOSX.mm

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "Game.h"
 #include "Form.h"
+#include "ScriptController.h"
 #include <unistd.h>
 #import <Cocoa/Cocoa.h>
 #import <QuartzCore/CVDisplayLink.h>
@@ -844,7 +845,7 @@ void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned
     if (!Form::touchEventInternal(evt, x, y, contactIndex))
     {
         Game::getInstance()->touchEvent(evt, x, y, contactIndex);
-        ScriptController::getInstance()->touchEvent(evt, x, y, contactIndex);
+        Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
     }
 }
     
@@ -853,7 +854,7 @@ void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
     if (!Form::keyEventInternal(evt, key))
     {
         Game::getInstance()->keyEvent(evt, key);
-        ScriptController::getInstance()->keyEvent(evt, key);
+        Game::getInstance()->getScriptController()->keyEvent(evt, key);
     }
 }
 
@@ -869,7 +870,7 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
     else
     {
-        return ScriptController::getInstance()->mouseEvent(evt, x, y, wheelDelta);
+        return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }
 

+ 4 - 3
gameplay/src/PlatformQNX.cpp

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "Game.h"
 #include "Form.h"
+#include "ScriptController.h"
 #include <unistd.h>
 #include <sys/keycodes.h>
 #include <screen/screen.h>
@@ -1161,7 +1162,7 @@ void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned
     if (!Form::touchEventInternal(evt, x, y, contactIndex))
     {
         Game::getInstance()->touchEvent(evt, x, y, contactIndex);
-        ScriptController::getInstance()->touchEvent(evt, x, y, contactIndex);
+        Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
     }
 }
 
@@ -1170,7 +1171,7 @@ void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
     if (!Form::keyEventInternal(evt, key))
     {
         Game::getInstance()->keyEvent(evt, key);
-        ScriptController::getInstance()->keyEvent(evt, key);
+        Game::getInstance()->getScriptController()->keyEvent(evt, key);
     }
 }
 
@@ -1186,7 +1187,7 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
     else
     {
-        return ScriptController::getInstance()->mouseEvent(evt, x, y, wheelDelta);
+        return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }
 

+ 4 - 3
gameplay/src/PlatformWin32.cpp

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "Game.h"
 #include "Form.h"
+#include "ScriptController.h"
 #include <GL/wglew.h>
 #include <windowsx.h>
 
@@ -846,7 +847,7 @@ void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned
     if (!Form::touchEventInternal(evt, x, y, contactIndex))
     {
         Game::getInstance()->touchEvent(evt, x, y, contactIndex);
-        ScriptController::getInstance()->touchEvent(evt, x, y, contactIndex);
+        Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
     }
 }
 
@@ -855,7 +856,7 @@ void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
     if (!Form::keyEventInternal(evt, key))
     {
         Game::getInstance()->keyEvent(evt, key);
-        ScriptController::getInstance()->keyEvent(evt, key);
+        Game::getInstance()->getScriptController()->keyEvent(evt, key);
     }
 }
 
@@ -871,7 +872,7 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
     else
     {
-        return ScriptController::getInstance()->mouseEvent(evt, x, y, wheelDelta);
+        return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }
 

+ 4 - 3
gameplay/src/PlatformiOS.mm

@@ -5,6 +5,7 @@
 #include "FileSystem.h"
 #include "Game.h"
 #include "Form.h"
+#include "ScriptController.h"
 #include <unistd.h>
 #import <UIKit/UIKit.h>
 #import <QuartzCore/QuartzCore.h>
@@ -954,7 +955,7 @@ void Platform::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned
     if (!Form::touchEventInternal(evt, x, y, contactIndex))
     {
         Game::getInstance()->touchEvent(evt, x, y, contactIndex);
-        ScriptController::getInstance()->touchEvent(evt, x, y, contactIndex);
+        Game::getInstance()->getScriptController()->touchEvent(evt, x, y, contactIndex);
     }
 }
     
@@ -963,7 +964,7 @@ void Platform::keyEventInternal(Keyboard::KeyEvent evt, int key)
     if (!Form::keyEventInternal(evt, key))
     {
         Game::getInstance()->keyEvent(evt, key);
-        ScriptController::getInstance()->keyEvent(evt, key);
+        Game::getInstance()->getScriptController()->keyEvent(evt, key);
     }
 }
 
@@ -979,7 +980,7 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
     else
     {
-        return ScriptController::getInstance()->mouseEvent(evt, x, y, wheelDelta);
+        return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }    
 

+ 1 - 1
gameplay/src/Scene.cpp

@@ -508,7 +508,7 @@ void Scene::drawDebug(unsigned int debugFlags)
         SAFE_RELEASE(material);
     }
 
-    _debugBatch->begin();
+    _debugBatch->start();
 
     for (Node* node = _firstNode; node != NULL; node = node->_nextSibling)
     {

+ 2 - 2
gameplay/src/Scene.h

@@ -331,13 +331,13 @@ bool Scene::visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*,C),
 inline bool Scene::visitNode(Node* node, const char* visitMethod)
 {
     // Invoke the visit method for this node.
-    if (!ScriptController::getInstance()->executeFunction<bool>(visitMethod, "<Node>", node))
+    if (!Game::getInstance()->getScriptController()->executeFunction<bool>(visitMethod, "<Node>", node))
         return false;
 
     // Recurse for all children.
     for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
     {
-        if (!ScriptController::getInstance()->executeFunction<bool>(visitMethod, "<Node>", child))
+        if (!Game::getInstance()->getScriptController()->executeFunction<bool>(visitMethod, "<Node>", child))
             return false;
     }
 

+ 106 - 83
gameplay/src/ScriptController.cpp

@@ -4,20 +4,21 @@
 #include "lua/lua_all_bindings.h"
 
 #define GENERATE_LUA_GET_POINTER(type, checkFunc) \
+    ScriptController* sc = Game::getInstance()->getScriptController(); \
     /* Check that the parameter is the correct type. */ \
-    if (!lua_istable(ScriptController::__instance->_lua, index)) \
+    if (!lua_istable(sc->_lua, index)) \
     { \
-        if (lua_islightuserdata(ScriptController::__instance->_lua, index)) \
-            return (type*)lua_touserdata(ScriptController::__instance->_lua, index); \
-        lua_pushfstring(ScriptController::__instance->_lua, "Expected a " #type " pointer (an array represented as a Lua table), got '%s' instead.", \
-            luaL_typename(ScriptController::__instance->_lua, index)); \
-        lua_error(ScriptController::__instance->_lua); \
+        if (lua_islightuserdata(sc->_lua, index)) \
+            return (type*)lua_touserdata(sc->_lua, index); \
+        lua_pushfstring(sc->_lua, "Expected a " #type " pointer (an array represented as a Lua table), got '%s' instead.", \
+            luaL_typename(sc->_lua, index)); \
+        lua_error(sc->_lua); \
         return NULL; \
     } \
     \
     /* Get the size of the array. */ \
-    lua_len(ScriptController::__instance->_lua, index); \
-    int size = luaL_checkint(ScriptController::__instance->_lua, -1); \
+    lua_len(sc->_lua, index); \
+    int size = luaL_checkint(sc->_lua, -1); \
     if (size <= 0) \
         return NULL; \
     \
@@ -25,14 +26,14 @@
     type* values = (type*)malloc(sizeof(type)*size); \
     \
     /* Push the first key. */ \
-    lua_pushnil(ScriptController::__instance->_lua); \
+    lua_pushnil(sc->_lua); \
     int i = 0; \
-    for (; lua_next(ScriptController::__instance->_lua, index) != 0 && i < size; i++) \
+    for (; lua_next(sc->_lua, index) != 0 && i < size; i++) \
     { \
-        values[i] = (checkFunc(ScriptController::__instance->_lua, -1)); \
+        values[i] = (checkFunc(sc->_lua, -1)); \
         \
         /* Remove the value we just retrieved, but leave the key for the next iteration. */ \
-        lua_pop(ScriptController::__instance->_lua, 1); \
+        lua_pop(sc->_lua, 1); \
     } \
     \
     return values
@@ -42,115 +43,124 @@ namespace gameplay
 
 void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions)
 {
-    lua_newtable(ScriptController::__instance->_lua);
+    ScriptController* sc = Game::getInstance()->getScriptController();
+    lua_newtable(sc->_lua);
 
     // Go through the list of functions and add them to the table.
     const luaL_Reg* iter = functions;
     for (; iter && iter->name; iter++)
     {
-        lua_pushcfunction(ScriptController::__instance->_lua, iter->func);
-        lua_setfield(ScriptController::__instance->_lua, -2, iter->name);
+        lua_pushcfunction(sc->_lua, iter->func);
+        lua_setfield(sc->_lua, -2, iter->name);
     }
 
-    lua_setglobal(ScriptController::__instance->_lua, name);
+    lua_setglobal(sc->_lua, name);
 }
 
 void ScriptUtil::registerConstantBool(std::string name, bool value, std::vector<std::string> scopePath)
 {
+    ScriptController* sc = Game::getInstance()->getScriptController();
+
     // If the constant is within a scope, get the correct parent 
     // table on the stack before setting its value.
     if (scopePath.size() > 0)
     {
-        lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
+        lua_getglobal(sc->_lua, scopePath[0].c_str());
         for (unsigned int i = 1; i < scopePath.size(); i++)
         {
-            lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
-            lua_gettable(ScriptController::__instance->_lua, -2);
+            lua_pushstring(sc->_lua, scopePath[i].c_str());
+            lua_gettable(sc->_lua, -2);
         }
         
         // Add the constant to the parent table.
-        lua_pushboolean(ScriptController::__instance->_lua, value);
-        lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
+        lua_pushboolean(sc->_lua, value);
+        lua_setfield(sc->_lua, -2, name.c_str());
 
         // Pop all the parent tables off the stack.
         int size = scopePath.size();
-        lua_pop(ScriptController::__instance->_lua, size);
+        lua_pop(sc->_lua, size);
     }
     else
     {
         // TODO: Currently unsupported (we don't parse for this yet).
         // If the constant is global, add it to the global table.
-        lua_pushboolean(ScriptController::__instance->_lua, value);
-        lua_pushvalue(ScriptController::__instance->_lua, -1);
-        lua_setglobal(ScriptController::__instance->_lua, name.c_str());
+        lua_pushboolean(sc->_lua, value);
+        lua_pushvalue(sc->_lua, -1);
+        lua_setglobal(sc->_lua, name.c_str());
     }
 }
 
 void ScriptUtil::registerConstantNumber(std::string name, double value, std::vector<std::string> scopePath)
 {
+    ScriptController* sc = Game::getInstance()->getScriptController();
+
     // If the constant is within a scope, get the correct parent 
     // table on the stack before setting its value.
     if (scopePath.size() > 0)
     {
-        lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
+        lua_getglobal(sc->_lua, scopePath[0].c_str());
         for (unsigned int i = 1; i < scopePath.size(); i++)
         {
-            lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
-            lua_gettable(ScriptController::__instance->_lua, -2);
+            lua_pushstring(sc->_lua, scopePath[i].c_str());
+            lua_gettable(sc->_lua, -2);
         }
         
         // Add the constant to the parent table.
-        lua_pushnumber(ScriptController::__instance->_lua, value);
-        lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
+        lua_pushnumber(sc->_lua, value);
+        lua_setfield(sc->_lua, -2, name.c_str());
 
         // Pop all the parent tables off the stack.
         int size = scopePath.size();
-        lua_pop(ScriptController::__instance->_lua, size);
+        lua_pop(sc->_lua, size);
     }
     else
     {
         // TODO: Currently unsupported (we don't parse for this yet).
         // If the constant is global, add it to the global table.
-        lua_pushnumber(ScriptController::__instance->_lua, value);
-        lua_pushvalue(ScriptController::__instance->_lua, -1);
-        lua_setglobal(ScriptController::__instance->_lua, name.c_str());
+        lua_pushnumber(sc->_lua, value);
+        lua_pushvalue(sc->_lua, -1);
+        lua_setglobal(sc->_lua, name.c_str());
     }
 }
 
 void ScriptUtil::registerConstantString(std::string name, std::string value, std::vector<std::string> scopePath)
 {
+    ScriptController* sc = Game::getInstance()->getScriptController();
+
     // If the constant is within a scope, get the correct parent 
     // table on the stack before setting its value.
     if (scopePath.size() > 0)
     {
-        lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
+        lua_getglobal(sc->_lua, scopePath[0].c_str());
         for (unsigned int i = 1; i < scopePath.size(); i++)
         {
-            lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
-            lua_gettable(ScriptController::__instance->_lua, -2);
+            lua_pushstring(sc->_lua, scopePath[i].c_str());
+            lua_gettable(sc->_lua, -2);
         }
         
         // Add the constant to the parent table.
-        lua_pushstring(ScriptController::__instance->_lua, value.c_str());
-        lua_setfield(ScriptController::__instance->_lua, -2, name.c_str());
+        lua_pushstring(sc->_lua, value.c_str());
+        lua_setfield(sc->_lua, -2, name.c_str());
 
         // Pop all the parent tables off the stack.
         int size = scopePath.size();
-        lua_pop(ScriptController::__instance->_lua, size);
+        lua_pop(sc->_lua, size);
     }
     else
     {
         // TODO: Currently unsupported (we don't parse for this yet).
         // If the constant is global, add it to the global table.
-        lua_pushstring(ScriptController::__instance->_lua, value.c_str());
-        lua_pushvalue(ScriptController::__instance->_lua, -1);
-        lua_setglobal(ScriptController::__instance->_lua, name.c_str());
+        lua_pushstring(sc->_lua, value.c_str());
+        lua_pushvalue(sc->_lua, -1);
+        lua_setglobal(sc->_lua, name.c_str());
     }
 }
 
 void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, 
     lua_CFunction deleteFunction, const luaL_Reg* statics,  std::vector<std::string> scopePath)
 {
+    ScriptController* sc = Game::getInstance()->getScriptController();
+
     // If the type is an inner type, get the correct parent 
     // table on the stack before creating the table for the class.
     if (scopePath.size() > 0)
@@ -158,89 +168,89 @@ void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CF
         std::string tablename = name;
 
         // Strip off the scope path part of the name.
-        lua_getglobal(ScriptController::__instance->_lua, scopePath[0].c_str());
+        lua_getglobal(sc->_lua, scopePath[0].c_str());
         std::size_t index = tablename.find(scopePath[0]);
         if (index != tablename.npos)
             tablename = tablename.substr(index + scopePath[0].size());
         
         for (unsigned int i = 1; i < scopePath.size(); i++)
         {
-            lua_pushstring(ScriptController::__instance->_lua, scopePath[i].c_str());
-            lua_gettable(ScriptController::__instance->_lua, -2);
+            lua_pushstring(sc->_lua, scopePath[i].c_str());
+            lua_gettable(sc->_lua, -2);
 
             index = tablename.find(scopePath[i]);
             if (index != tablename.npos)
                 tablename = tablename.substr(index + scopePath[i].size());
         }
 
-        lua_pushstring(ScriptController::__instance->_lua, tablename.c_str());
-        lua_newtable(ScriptController::__instance->_lua);
+        lua_pushstring(sc->_lua, tablename.c_str());
+        lua_newtable(sc->_lua);
     }
     else
     {
         // If the type is not an inner type, set it as a global table.
-        lua_newtable(ScriptController::__instance->_lua);
-        lua_pushvalue(ScriptController::__instance->_lua, -1);
-        lua_setglobal(ScriptController::__instance->_lua, name);
+        lua_newtable(sc->_lua);
+        lua_pushvalue(sc->_lua, -1);
+        lua_setglobal(sc->_lua, name);
     }
     
     // Create the metatable and populate it with the member functions.
-    lua_pushliteral(ScriptController::__instance->_lua, "__metatable");
-    luaL_newmetatable(ScriptController::__instance->_lua, name);
+    lua_pushliteral(sc->_lua, "__metatable");
+    luaL_newmetatable(sc->_lua, name);
     if (members)
-        luaL_setfuncs(ScriptController::__instance->_lua, members, 0);
-    lua_pushstring(ScriptController::__instance->_lua, "__index");
-    lua_pushvalue(ScriptController::__instance->_lua, -2);
-    lua_settable(ScriptController::__instance->_lua, -3);
+        luaL_setfuncs(sc->_lua, members, 0);
+    lua_pushstring(sc->_lua, "__index");
+    lua_pushvalue(sc->_lua, -2);
+    lua_settable(sc->_lua, -3);
 
     // Add the delete function if it was specified.
     if (deleteFunction)
     {
-        lua_pushstring(ScriptController::__instance->_lua, "__gc");
-        lua_pushcfunction(ScriptController::__instance->_lua, deleteFunction);
-        lua_settable(ScriptController::__instance->_lua, -3);
+        lua_pushstring(sc->_lua, "__gc");
+        lua_pushcfunction(sc->_lua, deleteFunction);
+        lua_settable(sc->_lua, -3);
     }
 
     // Set the metatable on the main table.
-    lua_settable(ScriptController::__instance->_lua, -3);
+    lua_settable(sc->_lua, -3);
     
     // Populate the main table with the static functions.
     if (statics)
-        luaL_setfuncs(ScriptController::__instance->_lua, statics, 0);
+        luaL_setfuncs(sc->_lua, statics, 0);
 
     // Set the new function(s) for the class.
     if (newFunction)
     {
-        lua_pushliteral(ScriptController::__instance->_lua, "new");
-        lua_pushcfunction(ScriptController::__instance->_lua, newFunction);
-        lua_settable(ScriptController::__instance->_lua, -3);
+        lua_pushliteral(sc->_lua, "new");
+        lua_pushcfunction(sc->_lua, newFunction);
+        lua_settable(sc->_lua, -3);
     }
 
     // Set the table we just created within the correct parent table.
     if (scopePath.size() > 0)
     {
-        lua_settable(ScriptController::__instance->_lua, -3);
+        lua_settable(sc->_lua, -3);
 
         // Pop all the parent tables off the stack.
         int size = scopePath.size();
-        lua_pop(ScriptController::__instance->_lua, size);
+        lua_pop(sc->_lua, size);
     }
     else
     {
         // Pop the main table off the stack.
-        lua_pop(ScriptController::__instance->_lua, 1);
+        lua_pop(sc->_lua, 1);
     }
 }
 
 void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction)
 {
-    lua_pushcfunction(ScriptController::__instance->_lua, cppFunction);
-    lua_setglobal(ScriptController::__instance->_lua, luaFunction);
+    lua_pushcfunction(Game::getInstance()->getScriptController()->_lua, cppFunction);
+    lua_setglobal(Game::getInstance()->getScriptController()->_lua, luaFunction);
 }
 
 void ScriptUtil::setGlobalHierarchy(std::map<std::string, std::vector<std::string> > hierarchy)
 {
-    ScriptController::__instance->_hierarchy = hierarchy;
+    Game::getInstance()->getScriptController()->_hierarchy = hierarchy;
 }
 
 bool* ScriptUtil::getBoolPointer(int index)
@@ -295,9 +305,9 @@ double* ScriptUtil::getDoublePointer(int index)
 
 const char* ScriptUtil::getString(int index, bool isStdString)
 {
-    if (lua_type(ScriptController::__instance->_lua, index) == LUA_TSTRING)
-        return luaL_checkstring(ScriptController::__instance->_lua, index);
-    else if (lua_type(ScriptController::__instance->_lua, index) == LUA_TNIL && !isStdString)
+    if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TSTRING)
+        return luaL_checkstring(Game::getInstance()->getScriptController()->_lua, index);
+    else if (lua_type(Game::getInstance()->getScriptController()->_lua, index) == LUA_TNIL && !isStdString)
         return NULL;
     else
     {
@@ -318,13 +328,6 @@ bool ScriptUtil::luaCheckBool(lua_State* state, int n)
 }
 
 
-ScriptController* ScriptController::__instance = NULL;
-
-ScriptController* ScriptController::getInstance()
-{
-    return __instance;
-}
-
 void ScriptController::loadScript(const char* path)
 {
     const char* scriptContents = FileSystem::readAll(path);
@@ -481,12 +484,10 @@ void ScriptController::setString(const char* name, const char* v)
 ScriptController::ScriptController() : _lua(NULL)
 {
     memset(_callbacks, 0, sizeof(std::string*) * CALLBACK_COUNT);
-    __instance = this;
 }
 
 ScriptController::~ScriptController()
 {
-    __instance = NULL;
 }
 
 void ScriptController::initialize()
@@ -688,6 +689,28 @@ void ScriptController::registerCallback(ScriptCallback callback, std::string fun
     _callbacks[callback] = new std::string(function);
 }
 
+ScriptController::ScriptCallback ScriptController::toCallback(const char* name)
+{
+    if (strcmp(name, "initialize") == 0)
+        return ScriptController::INITIALIZE;
+    else if (strcmp(name, "update") == 0)
+        return ScriptController::UPDATE;
+    else if (strcmp(name, "render") == 0)
+        return ScriptController::RENDER;
+    else if (strcmp(name, "finalize") == 0)
+        return ScriptController::FINALIZE;
+    else if (strcmp(name, "keyEvent") == 0)
+        return ScriptController::KEY_EVENT;
+    else if (strcmp(name, "touchEvent") == 0)
+        return ScriptController::TOUCH_EVENT;
+    else if (strcmp(name, "mouseEvent") == 0)
+        return ScriptController::MOUSE_EVENT;
+    else if (strcmp(name, "gamepadEvent") == 0)
+        return ScriptController::GAMEPAD_EVENT;
+    else
+        return ScriptController::INVALID_CALLBACK;
+}
+
 
 // Helper macros.
 #define SCRIPT_EXECUTE_FUNCTION_NO_PARAM(type, checkfunc) \

+ 76 - 74
gameplay/src/ScriptController.h

@@ -2,6 +2,7 @@
 #define SCRIPTCONTROLLER_H
 
 #include "Base.h"
+#include "Game.h"
 #include "Gamepad.h"
 
 namespace gameplay
@@ -230,14 +231,7 @@ class ScriptController
     friend class Game;
     friend class Platform;
 
-public:    
-    /**
-     * Gets the global instance of the script controller.
-     * 
-     * @return The global instance of the script controller (NULL if it hasn't yet been created).
-     */
-    static ScriptController* getInstance();
-
+public:
     /**
      * Loads the given script file and executes its global code.
      * 
@@ -278,237 +272,237 @@ public:
     template<typename T> T executeFunction(const char* func, const char* args, ...);
 
     /**
-     * Gets the global boolean variable with the given name.
+     * Gets the global boolean script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global boolean variable.
+     * @return The global boolean script variable.
      * @script{ignore}
      */
     bool getBool(const char* name);
 
     /**
-     * Gets the global char variable with the given name.
+     * Gets the global char script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global char variable.
+     * @return The global char script variable.
      * @script{ignore}
      */
     char getChar(const char* name);
 
     /**
-     * Gets the global short variable with the given name.
+     * Gets the global short script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global short variable.
+     * @return The global short script variable.
      * @script{ignore}
      */
     short getShort(const char* name);
 
     /**
-     * Gets the global int variable with the given name.
+     * Gets the global int script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global int variable.
+     * @return The global int script variable.
      * @script{ignore}
      */
     int getInt(const char* name);
 
     /**
-     * Gets the global long variable with the given name.
+     * Gets the global long script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global long variable.
+     * @return The global long script variable.
      * @script{ignore}
      */
     long getLong(const char* name);
 
     /**
-     * Gets the global unsigned char variable with the given name.
+     * Gets the global unsigned char script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global unsigned char variable.
+     * @return The global unsigned char script variable.
      * @script{ignore}
      */
     unsigned char getUnsignedChar(const char* name);
 
     /**
-     * Gets the global unsigned short variable with the given name.
+     * Gets the global unsigned short script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global unsigned short variable.
+     * @return The global unsigned short script variable.
      * @script{ignore}
      */
     unsigned short getUnsignedShort(const char* name);
 
     /**
-     * Gets the global unsigned int variable with the given name.
+     * Gets the global unsigned int script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global unsigned int variable.
+     * @return The global unsigned int script variable.
      * @script{ignore}
      */
     unsigned int getUnsignedInt(const char* name);
 
     /**
-     * Gets the global unsigned long variable with the given name.
+     * Gets the global unsigned long script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global unsigned long variable.
+     * @return The global unsigned long script variable.
      * @script{ignore}
      */
     unsigned long getUnsignedLong(const char* name);
 
     /**
-     * Gets the global float variable with the given name.
+     * Gets the global float script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global float variable.
+     * @return The global float script variable.
      * @script{ignore}
      */
     float getFloat(const char* name);
 
     /**
-     * Gets the global double variable with the given name.
+     * Gets the global double script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global double variable.
+     * @return The global double script variable.
      * @script{ignore}
      */
     double getDouble(const char* name);
 
     /**
-     * Gets the global string variable with the given name.
+     * Gets the global string script variable with the given name.
      * 
      * @param name The name of the variable.
-     * @return The global string variable.
+     * @return The global string script variable.
      * @script{ignore}
      */
     const char* getString(const char* name);
 
     /**
-     * Gets the global pointer variable of the given type with the given name.
+     * Gets the global pointer script variable of the given type with the given name.
      * 
      * @param type The type of the variable in Lua.
      * @param name The name of the variable.
-     * @return The global pointer variable.
+     * @return The global pointer script variable.
      * @script{ignore}
      */
     template<typename T>T* getObjectPointer(const char* type, const char* name);
 
     /**
-     * Sets the global boolean variable with the given name.
+     * Sets the global boolean script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The boolean variable.
+     * @param name The name of the script variable.
+     * @param v The boolean value.
      * @script{ignore}
      */
     void setBool(const char* name, bool v);
 
     /**
-     * Sets the global char variable with the given name.
+     * Sets the global char script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The char variable.
+     * @param name The name of the script variable.
+     * @param v The char value.
      * @script{ignore}
      */
     void setChar(const char* name, char v);
 
     /**
-     * Sets the global short variable with the given name.
+     * Sets the global short script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The short variable.
+     * @param name The name of the script variable.
+     * @param v The short value.
      * @script{ignore}
      */
     void setShort(const char* name, short v);
 
     /**
-     * Sets the global int variable with the given name.
+     * Sets the global int script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The int variable.
+     * @param name The name of the script variable.
+     * @param v The int value.
      * @script{ignore}
      */
     void setInt(const char* name, int v);
 
     /**
-     * Sets the global long variable with the given name.
+     * Sets the global long script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The long variable.
+     * @param name The name of the script variable.
+     * @param v The long value.
      * @script{ignore}
      */
     void setLong(const char* name, long v);
 
     /**
-     * Gets the global unsigned char variable with the given name.
+     * Gets the global unsigned char script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The unsigned char variable.
+     * @param name The name of the script variable.
+     * @param v The unsigned char value.
      * @script{ignore}
      */
     void setUnsignedChar(const char* name, unsigned char v);
 
     /**
-     * Sets the global unsigned short variable with the given name.
+     * Sets the global unsigned short script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The unsigned short variable.
+     * @param name The name of the script variable.
+     * @param v The unsigned short value.
      * @script{ignore}
      */
     void setUnsignedShort(const char* name, unsigned short v);
 
     /**
-     * Sets the global unsigned int variable with the given name.
+     * Sets the global unsigned int script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The unsigned int variable.
+     * @param name The name of the script variable.
+     * @param v The unsigned int value.
      * @script{ignore}
      */
     void setUnsignedInt(const char* name, unsigned int v);
 
     /**
-     * Sets the global unsigned long variable with the given name.
+     * Sets the global unsigned long script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The unsigned long variable.
+     * @param name The name of the script variable.
+     * @param v The unsigned long value.
      * @script{ignore}
      */
     void setUnsignedLong(const char* name, unsigned long v);
 
     /**
-     * Sets the global float variable with the given name.
+     * Sets the global float script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The float variable.
+     * @param name The name of the script variable.
+     * @param v The float value.
      * @script{ignore}
      */
     void setFloat(const char* name, float v);
 
     /**
-     * Sets the global double variable with the given name.
+     * Sets the global double script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The double variable.
+     * @param name The name of the script variable.
+     * @param v The double value.
      * @script{ignore}
      */
     void setDouble(const char* name, double v);
 
     /**
-     * Sets the global string variable with the given name.
+     * Sets the global string script variable with the given name to the given value.
      * 
-     * @param name The name of the variable.
-     * @param v The string variable.
+     * @param name The name of the script variable.
+     * @param v The string value.
      * @script{ignore}
      */
     void setString(const char* name, const char* v);
 
     /**
-     * Sets the global pointer variable of the given type with the given name.
+     * Sets the global pointer script variable of the given type with the given name to the given value.
      * 
-     * @param type The type of the variable in Lua.
+     * @param type The type of the script variable.
      * @param name The name of the variable.
-     * @param v The pointer variable.
+     * @param v The pointer value.
      * @script{ignore}
      */
     template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
@@ -658,6 +652,15 @@ private:
      */
     void registerCallback(ScriptCallback callback, std::string function);
 
+    /**
+     * Converts the given string to a valid script callback enumeration value
+     * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
+     * 
+     * @param name The name of the callback.
+     * @return The corresponding callback enumeration value.
+     */
+    static ScriptController::ScriptCallback toCallback(const char* name);
+
     // Friend functions (used by Lua script bindings).
     friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
     friend void ScriptUtil::registerConstantBool(std::string name, bool value, std::vector<std::string> scopePath);
@@ -683,7 +686,6 @@ private:
     lua_State* _lua;
     unsigned int _returnCount;
     std::map<std::string, std::vector<std::string> > _hierarchy;
-    static ScriptController* __instance;
     std::string* _callbacks[CALLBACK_COUNT];
 };
 

+ 33 - 32
gameplay/src/ScriptController.inl

@@ -5,7 +5,8 @@ namespace gameplay
 
 template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull)
 {
-    if (lua_type(ScriptController::__instance->_lua, index) == LUA_TNIL)
+    ScriptController* sc = Game::getInstance()->getScriptController();
+    if (lua_type(sc->_lua, index) == LUA_TNIL)
     {
         if (nonNull)
         {
@@ -14,11 +15,11 @@ template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type,
 
         return NULL;
     }
-    else if (lua_type(ScriptController::__instance->_lua, index) == LUA_TTABLE)
+    else if (lua_type(sc->_lua, index) == LUA_TTABLE)
     {
         // Get the size of the array.
-        lua_len(ScriptController::__instance->_lua, index);
-        int size = luaL_checkint(ScriptController::__instance->_lua, -1);
+        lua_len(sc->_lua, index);
+        int size = luaL_checkint(sc->_lua, -1);
 
         if (size <= 0)
             return NULL;
@@ -27,51 +28,51 @@ template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type,
         T* values = (T*)malloc(sizeof(T)*size);
         
         // Push the first key.
-        lua_pushnil(ScriptController::__instance->_lua);
+        lua_pushnil(sc->_lua);
         int i = 0;
-        for (; lua_next(ScriptController::__instance->_lua, index) != 0 && i < size; i++)
+        for (; lua_next(sc->_lua, index) != 0 && i < size; i++)
         {
-            void* p = lua_touserdata(ScriptController::__instance->_lua, -1);
+            void* p = lua_touserdata(sc->_lua, -1);
             if (p != NULL)
             {
-                if (lua_getmetatable(ScriptController::__instance->_lua, -1))
+                if (lua_getmetatable(sc->_lua, -1))
                 {
                     // Check if it matches the type's metatable.
-                    luaL_getmetatable(ScriptController::__instance->_lua, type);
-                    if (lua_rawequal(ScriptController::__instance->_lua, -1, -2))
+                    luaL_getmetatable(sc->_lua, type);
+                    if (lua_rawequal(sc->_lua, -1, -2))
                     {
-                        lua_pop(ScriptController::__instance->_lua, 2);
+                        lua_pop(sc->_lua, 2);
                         T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
                         if (ptr)
                             memcpy(&values[i], ptr, sizeof(T));
                         else
                             memset(&values[i], 0, sizeof(T));
 
-                        lua_pop(ScriptController::__instance->_lua, 1);
+                        lua_pop(sc->_lua, 1);
                         continue;
                     }
-                    lua_pop(ScriptController::__instance->_lua, 1);
+                    lua_pop(sc->_lua, 1);
 
                     // Check if it matches any of the derived types' metatables.
-                    const std::vector<std::string>& types = ScriptController::__instance->_hierarchy[type];
+                    const std::vector<std::string>& types = sc->_hierarchy[type];
                     for (unsigned int k = 0, count = types.size(); k < count; k++)
                     {
-                        luaL_getmetatable(ScriptController::__instance->_lua, types[k].c_str());
-                        if (lua_rawequal(ScriptController::__instance->_lua, -1, -2))
+                        luaL_getmetatable(sc->_lua, types[k].c_str());
+                        if (lua_rawequal(sc->_lua, -1, -2))
                         {
-                            lua_pop(ScriptController::__instance->_lua, 2);
+                            lua_pop(sc->_lua, 2);
                             T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
                             if (ptr)
                                 memcpy(&values[i], ptr, sizeof(T));
                             else
                                 memset(&values[i], 0, sizeof(T));
-                            lua_pop(ScriptController::__instance->_lua, 1);
+                            lua_pop(sc->_lua, 1);
                             continue;
                         }
-                        lua_pop(ScriptController::__instance->_lua, 1);
+                        lua_pop(sc->_lua, 1);
                     }
             
-                    lua_pop(ScriptController::__instance->_lua, 1);
+                    lua_pop(sc->_lua, 1);
                 }
             }
         }
@@ -80,16 +81,16 @@ template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type,
     }
     else
     {
-        void* p = lua_touserdata(ScriptController::__instance->_lua, index);
+        void* p = lua_touserdata(sc->_lua, index);
         if (p != NULL)
         {
-            if (lua_getmetatable(ScriptController::__instance->_lua, index))
+            if (lua_getmetatable(sc->_lua, index))
             {
                 // Check if it matches the type's metatable.
-                luaL_getmetatable(ScriptController::__instance->_lua, type);
-                if (lua_rawequal(ScriptController::__instance->_lua, -1, -2))
+                luaL_getmetatable(sc->_lua, type);
+                if (lua_rawequal(sc->_lua, -1, -2))
                 {
-                    lua_pop(ScriptController::__instance->_lua, 2);
+                    lua_pop(sc->_lua, 2);
                     T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
                     if (ptr == NULL && nonNull)
                     {
@@ -97,16 +98,16 @@ template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type,
                     }
                     return ptr;
                 }
-                lua_pop(ScriptController::__instance->_lua, 1);
+                lua_pop(sc->_lua, 1);
 
                 // Check if it matches any of the derived types' metatables.
-                const std::vector<std::string>& types = ScriptController::__instance->_hierarchy[type];
+                const std::vector<std::string>& types = sc->_hierarchy[type];
                 for (unsigned int i = 0, count = types.size(); i < count; i++)
                 {
-                    luaL_getmetatable(ScriptController::__instance->_lua, types[i].c_str());
-                    if (lua_rawequal(ScriptController::__instance->_lua, -1, -2))
+                    luaL_getmetatable(sc->_lua, types[i].c_str());
+                    if (lua_rawequal(sc->_lua, -1, -2))
                     {
-                        lua_pop(ScriptController::__instance->_lua, 2);
+                        lua_pop(sc->_lua, 2);
                         T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
                         if (ptr == NULL && nonNull)
                         {
@@ -114,10 +115,10 @@ template<typename T>T* ScriptUtil::getObjectPointer(int index, const char* type,
                         }
                         return ptr;
                     }
-                    lua_pop(ScriptController::__instance->_lua, 1);
+                    lua_pop(sc->_lua, 1);
                 }
             
-                lua_pop(ScriptController::__instance->_lua, 1);
+                lua_pop(sc->_lua, 1);
             }
         }
 

+ 1 - 1
gameplay/src/ScriptListener.cpp

@@ -46,7 +46,7 @@ void ScriptListener::timeEvent(long timeDiff, void* cookie)
 ScriptListener::ScriptListener(const char* function)
 {
     _function = function ? function : "";
-    _sc = ScriptController::getInstance();
+    _sc = Game::getInstance()->getScriptController();
 
     GP_ASSERT(_function.size() > 0);
     GP_ASSERT(_sc);

+ 2 - 2
gameplay/src/SpriteBatch.cpp

@@ -160,9 +160,9 @@ SpriteBatch* SpriteBatch::create(Texture* texture, Effect* effect, unsigned int
     return batch;
 }
 
-void SpriteBatch::begin()
+void SpriteBatch::start()
 {
-    _batch->begin();
+    _batch->start();
 }
 
 void SpriteBatch::draw(const Rectangle& dst, const Rectangle& src, const Vector4& color)

+ 2 - 2
gameplay/src/SpriteBatch.h

@@ -87,12 +87,12 @@ public:
     virtual ~SpriteBatch();
 
     /**
-     * Begins drawing sprites.
+     * Starts drawing sprites.
      *
      * This method must be called before drawing any sprites and it must eventually be
      * followed by a call to finish().
      */
-    void begin();
+    void start();
 
     /**
      * Draws a single sprite.

+ 35 - 35
gameplay/src/lua/lua_Font.cpp

@@ -18,7 +18,6 @@ void luaRegister_Font()
     const luaL_Reg lua_members[] = 
     {
         {"addRef", lua_Font_addRef},
-        {"begin", lua_Font_begin},
         {"createText", lua_Font_createText},
         {"drawText", lua_Font_drawText},
         {"finish", lua_Font_finish},
@@ -29,6 +28,7 @@ void luaRegister_Font()
         {"getSpriteBatch", lua_Font_getSpriteBatch},
         {"measureText", lua_Font_measureText},
         {"release", lua_Font_release},
+        {"start", lua_Font_start},
         {NULL, NULL}
     };
     const luaL_Reg lua_statics[] = 
@@ -123,40 +123,6 @@ int lua_Font_addRef(lua_State* state)
     return 0;
 }
 
-int lua_Font_begin(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 1:
-        {
-            if ((lua_type(state, 1) == LUA_TUSERDATA))
-            {
-                Font* instance = getInstance(state);
-                instance->begin();
-                
-                return 0;
-            }
-            else
-            {
-                lua_pushstring(state, "lua_Font_begin - Failed to match the given parameters to a valid function signature.");
-                lua_error(state);
-            }
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 1).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Font_createText(lua_State* state)
 {
     // Get the number of parameters.
@@ -1587,6 +1553,40 @@ int lua_Font_release(lua_State* state)
     return 0;
 }
 
+int lua_Font_start(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                Font* instance = getInstance(state);
+                instance->start();
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_Font_start - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_Font_static_create(lua_State* state)
 {
     // Get the number of parameters.

+ 1 - 1
gameplay/src/lua/lua_Font.h

@@ -7,7 +7,6 @@ namespace gameplay
 // Lua bindings for Font.
 int lua_Font__gc(lua_State* state);
 int lua_Font_addRef(lua_State* state);
-int lua_Font_begin(lua_State* state);
 int lua_Font_createText(lua_State* state);
 int lua_Font_drawText(lua_State* state);
 int lua_Font_finish(lua_State* state);
@@ -18,6 +17,7 @@ int lua_Font_getSize(lua_State* state);
 int lua_Font_getSpriteBatch(lua_State* state);
 int lua_Font_measureText(lua_State* state);
 int lua_Font_release(lua_State* state);
+int lua_Font_start(lua_State* state);
 int lua_Font_static_create(lua_State* state);
 int lua_Font_static_getJustify(lua_State* state);
 

+ 47 - 0
gameplay/src/lua/lua_Game.cpp

@@ -38,6 +38,7 @@ void luaRegister_Game()
         {"getGamepadCount", lua_Game_getGamepadCount},
         {"getHeight", lua_Game_getHeight},
         {"getPhysicsController", lua_Game_getPhysicsController},
+        {"getScriptController", lua_Game_getScriptController},
         {"getState", lua_Game_getState},
         {"getViewport", lua_Game_getViewport},
         {"getWidth", lua_Game_getWidth},
@@ -752,6 +753,52 @@ int lua_Game_getPhysicsController(lua_State* state)
     return 0;
 }
 
+int lua_Game_getScriptController(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                Game* instance = getInstance(state);
+                void* returnPtr = (void*)instance->getScriptController();
+                if (returnPtr)
+                {
+                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
+                    object->instance = returnPtr;
+                    object->owns = false;
+                    luaL_getmetatable(state, "ScriptController");
+                    lua_setmetatable(state, -2);
+                }
+                else
+                {
+                    lua_pushnil(state);
+                }
+
+                return 1;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_Game_getScriptController - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_Game_getState(lua_State* state)
 {
     // Get the number of parameters.

+ 1 - 0
gameplay/src/lua/lua_Game.h

@@ -21,6 +21,7 @@ int lua_Game_getGamepad(lua_State* state);
 int lua_Game_getGamepadCount(lua_State* state);
 int lua_Game_getHeight(lua_State* state);
 int lua_Game_getPhysicsController(lua_State* state);
+int lua_Game_getScriptController(lua_State* state);
 int lua_Game_getState(lua_State* state);
 int lua_Game_getViewport(lua_State* state);
 int lua_Game_getWidth(lua_State* state);

+ 35 - 35
gameplay/src/lua/lua_MeshBatch.cpp

@@ -12,12 +12,12 @@ void luaRegister_MeshBatch()
 {
     const luaL_Reg lua_members[] = 
     {
-        {"begin", lua_MeshBatch_begin},
         {"draw", lua_MeshBatch_draw},
         {"finish", lua_MeshBatch_finish},
         {"getCapacity", lua_MeshBatch_getCapacity},
         {"getMaterial", lua_MeshBatch_getMaterial},
         {"setCapacity", lua_MeshBatch_setCapacity},
+        {"start", lua_MeshBatch_start},
         {NULL, NULL}
     };
     const luaL_Reg lua_statics[] = 
@@ -77,40 +77,6 @@ int lua_MeshBatch__gc(lua_State* state)
     return 0;
 }
 
-int lua_MeshBatch_begin(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 1:
-        {
-            if ((lua_type(state, 1) == LUA_TUSERDATA))
-            {
-                MeshBatch* instance = getInstance(state);
-                instance->begin();
-                
-                return 0;
-            }
-            else
-            {
-                lua_pushstring(state, "lua_MeshBatch_begin - Failed to match the given parameters to a valid function signature.");
-                lua_error(state);
-            }
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 1).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_MeshBatch_draw(lua_State* state)
 {
     // Get the number of parameters.
@@ -300,6 +266,40 @@ int lua_MeshBatch_setCapacity(lua_State* state)
     return 0;
 }
 
+int lua_MeshBatch_start(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                MeshBatch* instance = getInstance(state);
+                instance->start();
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_MeshBatch_start - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_MeshBatch_static_create(lua_State* state)
 {
     // Get the number of parameters.

+ 1 - 1
gameplay/src/lua/lua_MeshBatch.h

@@ -6,12 +6,12 @@ namespace gameplay
 
 // Lua bindings for MeshBatch.
 int lua_MeshBatch__gc(lua_State* state);
-int lua_MeshBatch_begin(lua_State* state);
 int lua_MeshBatch_draw(lua_State* state);
 int lua_MeshBatch_finish(lua_State* state);
 int lua_MeshBatch_getCapacity(lua_State* state);
 int lua_MeshBatch_getMaterial(lua_State* state);
 int lua_MeshBatch_setCapacity(lua_State* state);
+int lua_MeshBatch_start(lua_State* state);
 int lua_MeshBatch_static_create(lua_State* state);
 
 void luaRegister_MeshBatch();

+ 1 - 42
gameplay/src/lua/lua_ScriptController.cpp

@@ -15,11 +15,7 @@ void luaRegister_ScriptController()
         {"loadScript", lua_ScriptController_loadScript},
         {NULL, NULL}
     };
-    const luaL_Reg lua_statics[] = 
-    {
-        {"getInstance", lua_ScriptController_static_getInstance},
-        {NULL, NULL}
-    };
+    const luaL_Reg* lua_statics = NULL;
     std::vector<std::string> scopePath;
 
     ScriptUtil::registerClass("ScriptController", lua_members, NULL, NULL, lua_statics, scopePath);
@@ -70,41 +66,4 @@ int lua_ScriptController_loadScript(lua_State* state)
     return 0;
 }
 
-int lua_ScriptController_static_getInstance(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 0:
-        {
-            void* returnPtr = (void*)ScriptController::getInstance();
-            if (returnPtr)
-            {
-                ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
-                object->instance = returnPtr;
-                object->owns = false;
-                luaL_getmetatable(state, "ScriptController");
-                lua_setmetatable(state, -2);
-            }
-            else
-            {
-                lua_pushnil(state);
-            }
-
-            return 1;
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 0).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 }

+ 0 - 1
gameplay/src/lua/lua_ScriptController.h

@@ -6,7 +6,6 @@ namespace gameplay
 
 // Lua bindings for ScriptController.
 int lua_ScriptController_loadScript(lua_State* state);
-int lua_ScriptController_static_getInstance(lua_State* state);
 
 void luaRegister_ScriptController();
 

+ 35 - 35
gameplay/src/lua/lua_SpriteBatch.cpp

@@ -12,13 +12,13 @@ void luaRegister_SpriteBatch()
 {
     const luaL_Reg lua_members[] = 
     {
-        {"begin", lua_SpriteBatch_begin},
         {"draw", lua_SpriteBatch_draw},
         {"finish", lua_SpriteBatch_finish},
         {"getMaterial", lua_SpriteBatch_getMaterial},
         {"getProjectionMatrix", lua_SpriteBatch_getProjectionMatrix},
         {"getStateBlock", lua_SpriteBatch_getStateBlock},
         {"setProjectionMatrix", lua_SpriteBatch_setProjectionMatrix},
+        {"start", lua_SpriteBatch_start},
         {NULL, NULL}
     };
     const luaL_Reg lua_statics[] = 
@@ -78,40 +78,6 @@ int lua_SpriteBatch__gc(lua_State* state)
     return 0;
 }
 
-int lua_SpriteBatch_begin(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 1:
-        {
-            if ((lua_type(state, 1) == LUA_TUSERDATA))
-            {
-                SpriteBatch* instance = getInstance(state);
-                instance->begin();
-                
-                return 0;
-            }
-            else
-            {
-                lua_pushstring(state, "lua_SpriteBatch_begin - Failed to match the given parameters to a valid function signature.");
-                lua_error(state);
-            }
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 1).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_SpriteBatch_draw(lua_State* state)
 {
     // Get the number of parameters.
@@ -983,6 +949,40 @@ int lua_SpriteBatch_setProjectionMatrix(lua_State* state)
     return 0;
 }
 
+int lua_SpriteBatch_start(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                SpriteBatch* instance = getInstance(state);
+                instance->start();
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_SpriteBatch_start - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_SpriteBatch_static_create(lua_State* state)
 {
     // Get the number of parameters.

+ 1 - 1
gameplay/src/lua/lua_SpriteBatch.h

@@ -6,13 +6,13 @@ namespace gameplay
 
 // Lua bindings for SpriteBatch.
 int lua_SpriteBatch__gc(lua_State* state);
-int lua_SpriteBatch_begin(lua_State* state);
 int lua_SpriteBatch_draw(lua_State* state);
 int lua_SpriteBatch_finish(lua_State* state);
 int lua_SpriteBatch_getMaterial(lua_State* state);
 int lua_SpriteBatch_getProjectionMatrix(lua_State* state);
 int lua_SpriteBatch_getStateBlock(lua_State* state);
 int lua_SpriteBatch_setProjectionMatrix(lua_State* state);
+int lua_SpriteBatch_start(lua_State* state);
 int lua_SpriteBatch_static_create(lua_State* state);
 
 void luaRegister_SpriteBatch();