Browse Source

Remove unused function in LuaScript. Add Lua property to Audio module.

Aster Jian 12 years ago
parent
commit
23d25cefa4

+ 22 - 21
Extras/LuaScript/LuaScript.cpp

@@ -54,6 +54,7 @@ extern int tolua_ResourceLuaAPI_open(lua_State*);
 extern int tolua_SceneLuaAPI_open(lua_State*);
 extern int tolua_UILuaAPI_open(lua_State*);
 extern int tolua_LuaScriptLuaAPI_open(lua_State*);
+
 namespace Urho3D
 {
 
@@ -68,7 +69,7 @@ LuaScript::LuaScript(Context* context) :
     luaState_ = luaL_newstate();
     if (!luaState_)
     {
-        LOGERROR("Could not create Lua state");
+        LOGERROR("Could not create Lua state.");
         return;
     }
 
@@ -155,6 +156,7 @@ bool LuaScript::ExecuteString(const char* string)
     PROFILE(ExecuteString);
 
     int top = lua_gettop(luaState_);
+
     if (luaL_dostring(luaState_, string) != 0)
     {
         const char* message = lua_tostring(luaState_, -1);
@@ -177,11 +179,12 @@ bool LuaScript::ExecuteFunction(const char* funcName)
     PROFILE(ExecuteFunction);
 
     int top = lua_gettop(luaState_);
+
     lua_getglobal(luaState_, funcName);
     if (!lua_isfunction(luaState_, -1))
     {
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to get lua global: '") + funcName + "'.");
+        LOGRAW(String("Lua: Unable to get Lua global: '") + funcName + "'.");
         return false;
     }
 
@@ -189,7 +192,7 @@ bool LuaScript::ExecuteFunction(const char* funcName)
     {
         const char* message = lua_tostring(luaState_, -1);
         lua_settop(luaState_, top);
-        LOGRAW(String("Lua: Unable to execute function '") + funcName + "'.");
+        LOGRAW(String("Lua: Unable to execute Lua function '") + funcName + "'.");
         LOGRAW(String("Lua: ") + message);
         return false;
     }
@@ -205,21 +208,23 @@ void LuaScript::ScriptSendEvent(const char* eventName, VariantMap& eventData)
 void LuaScript::ScriptSubscribeToEvent(const char* eventName, const char* functionName)
 {
     StringHash eventType(eventName);
-    eventTypeToFunctionNameMap_[eventType].Push(String(functionName));
 
-    SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
+	if (!eventTypeToFunctionNameMap_.Contains(eventType))
+		SubscribeToEvent(eventType, HANDLER(LuaScript, HandleEvent));
+
+    eventTypeToFunctionNameMap_[eventType].Push(String(functionName));
 }
 
 void LuaScript::ReplacePrintFunction()
 {
-    static const struct luaL_reg printlib[] =
+    static const struct luaL_reg reg[] =
     {
         {"print", &LuaScript::Print},
         { NULL, NULL}
     };
 
     lua_getglobal(luaState_, "_G");
-    luaL_register(luaState_, NULL, printlib);
+    luaL_register(luaState_, NULL, reg);
     lua_pop(luaState_, 1);
 }
 
@@ -250,39 +255,35 @@ int LuaScript::Print(lua_State *L)
     return 0;
 }
 
-int LuaScript::PCallCallback(lua_State* L)
-{
-    String message = lua_tostring(L, -1);
-    LOGRAW("Lua pcall error: " + message);
-    return 0;
-}
-
 void LuaScript::HandleEvent(StringHash eventType, VariantMap& eventData)
 {
     HashMap<StringHash, Vector<String> >::ConstIterator it = eventTypeToFunctionNameMap_.Find(eventType);
     if (it == eventTypeToFunctionNameMap_.End())
         return;
-
-    for (unsigned i = 0; i < it->second_.Size(); ++i)
+	
+	const Vector<String>& functionNames = it->second_;
+    for (unsigned i = 0; i < functionNames.Size(); ++i)
     {
-        const String& functionName = it->second_[i];
-        
+        const String& functionName = functionNames[i];
+
 		int top = lua_gettop(luaState_);
+
         lua_getglobal(luaState_, functionName.CString());
         if (!lua_isfunction(luaState_, -1))
         {
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to get lua global: '") + functionName + "'.");
+            LOGRAW(String("Lua: Unable to get Lua global: '") + functionName + "'.");
             return;
         }
 
         tolua_pushusertype(luaState_, (void*)&eventType, "StringHash");
         tolua_pushusertype(luaState_, (void*)&eventData, "VariantMap");
-        if (lua_pcall(luaState_, 2, 0, 0))
+
+        if (lua_pcall(luaState_, 2, 0, 0) != 0)
         {
             const char* message = lua_tostring(luaState_, -1);
             lua_settop(luaState_, top);
-            LOGRAW(String("Lua: Unable to execute function '") + functionName + "'.");
+            LOGRAW(String("Lua: Unable to execute Lua function '") + functionName + "'.");
             LOGRAW(String("Lua: ") + message);
             return;
         }

+ 3 - 9
Extras/LuaScript/LuaScript.h

@@ -43,13 +43,13 @@ public:
     /// Destruct. Release the AngelLuna engine.
     ~LuaScript();
 
-    /// Execute lua script file.
+    /// Execute script file.
     bool ExecuteFile(const char* fileName);
 
-    /// Execute lua string.
+    /// Execute script string.
     bool ExecuteString(const char* string);
 
-    /// Execute lua function.
+    /// Execute script function.
     bool ExecuteFunction(const char* funcName);
 
     /// Script send event.
@@ -58,9 +58,6 @@ public:
     /// Script subscribe event.
     void ScriptSubscribeToEvent(const char* eventName, const char* functionName);
 
-	/// Return the lua state.
-	lua_State* GetLuaState() const { return luaState_; }
-
 private:
     /// Replace print function.
     void ReplacePrintFunction();
@@ -68,9 +65,6 @@ private:
     /// Print function.
     static int Print(lua_State* L);
 
-    /// PCall callback.
-    static int PCallCallback(lua_State* L);
-
     /// Handle event.
     void HandleEvent(StringHash eventType, VariantMap& eventData);
 

+ 32 - 33
Extras/LuaScript/pkgs/Audio/Audio.pkg

@@ -1,46 +1,45 @@
 $#include "Audio.h"
 
-/// %Audio subsystem.
+enum SoundType
+{
+    SOUND_EFFECT,
+    SOUND_AMBIENT,
+    SOUND_VOICE,
+    SOUND_MUSIC,
+    SOUND_MASTER,
+    MAX_SOUND_TYPES
+};
+
 class Audio : public Object
 {
 public:
-    /// Initialize sound output with specified buffer length and output mode.
+    // Methods:
     bool SetMode(int bufferLengthMSec, int mixRate, bool stereo, bool interpolation = true);
-    /// Run update on sound sources. Not required for continued playback, but frees unused sound sources & sounds and updates 3D positions.
-    void Update(float timeStep);
-    /// Restart sound output.
+    bool SetMode(int bufferLengthMSec, int mixRate, bool stereo/*, bool interpolation = true*/);
     bool Play();
-    /// Suspend sound output.
     void Stop();
-    /// Set master gain on a specific sound type such as sound effects, music or voice.
-    void SetMasterGain(SoundType type, float gain);
-    /// Set active sound listener for 3D sounds.
-    void SetListener(SoundListener* listener);
-    /// Stop any sound source playing a certain sound clip.
-    void StopSound(Sound* sound);
 
-    /// Return byte size of one sample.
-    unsigned GetSampleSize() const { return sampleSize_; }
-    /// Return mixing rate.
-    int GetMixRate() const { return mixRate_; }
-    /// Return whether output is interpolated.
-    bool GetInterpolation() const { return interpolation_; }
-    /// Return whether output is stereo.
-    bool IsStereo() const { return stereo_; }
-    /// Return whether audio is being output.
-    bool IsPlaying() const { return playing_; }
-    /// Return whether an audio stream has been reserved.
-    bool IsInitialized() const { return deviceID_ != 0; }
-    /// Return master gain for a specific sound source type.
+    void SetMasterGain(SoundType type, float gain);
     float GetMasterGain(SoundType type) const;
-    /// Return active sound listener.
-    SoundListener* GetListener() const;
     
-    /// Add a sound source to keep track of. Called by SoundSource.
-    void AddSoundSource(SoundSource* soundSource);
-    /// Remove a sound source. Called by SoundSource.
-    void RemoveSoundSource(SoundSource* soundSource);
+    void SetListener(SoundListener* listener);
+    SoundListener* GetListener() const;
     
-    /// Return sound type specific gain multiplied by master gain.
-    float GetSoundSourceMasterGain(SoundType type) const { return masterGain_[SOUND_MASTER] * masterGain_[type]; }
+    unsigned GetSampleSize() const;
+    int GetMixRate() const;
+    bool GetInterpolation() const;
+
+    bool IsStereo() const;
+    bool IsPlaying() const;
+    bool IsInitialized() const;  
+
+    // Properties:
+    tolua_property__get_set SoundListener* listener;
+    tolua_readonly tolua_property__get_set unsigned sampleSize;
+    tolua_readonly tolua_property__get_set int mixRate;
+    tolua_readonly tolua_property__get_set bool interpolation;
+
+    tolua_readonly tolua_property__is_set bool stereo;    
+    tolua_readonly tolua_property__is_set bool playing;
+    tolua_readonly tolua_property__is_set bool initialized;
 };

+ 19 - 32
Extras/LuaScript/pkgs/Audio/Sound.pkg

@@ -1,40 +1,27 @@
 $#include "Sound.h"
 
-/// %Sound resource.
 class Sound : public Resource
 {
 public:
-    /// Set uncompressed sound data format.
-    void SetFormat(unsigned frequency, bool sixteenBit, bool stereo);
-    /// Set loop on/off. If loop is enabled, sets the full sound as loop range.
-    void SetLooped(bool enable);
-    /// Define loop.
-    void SetLoop(unsigned repeatOffset, unsigned endOffset);
-    /// Fix interpolation by copying data from loop start to loop end (looped), or adding silence (oneshot.)
-    void FixInterpolation();
-    
-    /// Return sound data start.
-    signed char* GetStart() const { return data_.Get(); }
-    /// Return loop start.
-    signed char* GetRepeat() const { return repeat_; }
-    /// Return sound data end.
-    signed char* GetEnd() const { return end_; }
-    /// Return length in seconds.
+    // Methods:
     float GetLength() const;
-    /// Return total sound data size.
-    unsigned GetDataSize() const { return dataSize_; }
-    /// Return sample size.
     unsigned GetSampleSize() const;
-    /// Return default frequency as a float.
-    float GetFrequency() { return (float)frequency_; }
-    /// Return default frequency as an integer.
-    unsigned GetIntFrequency() { return frequency_; }
-    /// Return whether is looped.
-    bool IsLooped() const { return looped_; }
-    /// Return whether data is sixteen bit.
-    bool IsSixteenBit() const { return sixteenBit_; }
-    /// Return whether data is stereo.
-    bool IsStereo() const { return stereo_; }
-    /// Return whether is compressed in Ogg Vorbis format.
-    bool IsCompressed() const { return compressed_; }
+    float GetFrequency();
+    
+    void SetLooped(bool enable);
+    bool IsLooped() const;
+    
+    bool IsSixteenBit() const;
+    bool IsStereo() const;
+    bool IsCompressed() const;
+
+    // Properties:
+    tolua_readonly tolua_property__get_set float length;
+    tolua_readonly tolua_property__get_set unsigned sampleSize;
+    tolua_readonly tolua_property__get_set float frequency;
+    tolua_property__is_set bool looped;
+    tolua_readonly tolua_property__is_set bool sixteenBit;
+    tolua_readonly tolua_property__is_set bool stereo;
+    tolua_readonly tolua_property__is_set bool compressed;
+
 };

+ 0 - 2
Extras/LuaScript/pkgs/Audio/SoundListener.pkg

@@ -1,7 +1,5 @@
 $#include "SoundListener.h"
 
-/// %Sound listener component
 class SoundListener : public Component
 {
-public:
 };

+ 25 - 37
Extras/LuaScript/pkgs/Audio/SoundSource.pkg

@@ -1,57 +1,45 @@
 $#include "SoundSource.h"
 
-/// %Sound source component with stereo position.
 class SoundSource : public Component
 {
 public:
-    /// Play a sound.
+    // Methods:
     void Play(Sound* sound);
-    /// Play a sound with specified frequency.
     void Play(Sound* sound, float frequency);
-    /// Play a sound with specified frequency and gain.
     void Play(Sound* sound, float frequency, float gain);
-    /// Play a sound with specified frequency, gain and panning.
     void Play(Sound* sound, float frequency, float gain, float panning);
-    /// Stop playback.
     void Stop();
-    /// Set sound type, determines the master gain group.
+
     void SetSoundType(SoundType type);
-    /// Set frequency.
+    SoundType GetSoundType() const;
+    
     void SetFrequency(float frequency);
-    /// Set gain. 0.0 is silence, 1.0 is full volume.
+    float GetFrequency() const;
+    
     void SetGain(float gain);
-    /// Set attenuation. 1.0 is unaltered. Used for distance attenuated playback.
+    float GetGain() const;
+    
     void SetAttenuation(float attenuation);
-    /// Set stereo panning. -1.0 is full left and 1.0 is full right.
+    float GetAttenuation() const;
+    
     void SetPanning(float panning);
-   /// Set whether sound source will be automatically removed from the scene node when playback stops.
+    float GetPanning() const;
+    
     void SetAutoRemove(bool enable);
-    /// Set new playback position.
-    void SetPlayPosition(signed char* pos);
+    bool GetAutoRemove() const;
     
-    /// Return sound.
-    Sound* GetSound() const { return sound_; }
-    /// Return playback position.
-    volatile signed char* GetPlayPosition() const { return position_; }
-    /// Return sound type, determines the master gain group.
-    SoundType GetSoundType() const { return soundType_; }
-    /// Return playback time position.
-    float GetTimePosition() const { return timePosition_; }
-    /// Return frequency.
-    float GetFrequency() const { return frequency_; }
-    /// Return gain.
-    float GetGain() const { return gain_; }
-    /// Return attenuation.
-    float GetAttenuation() const { return attenuation_; }
-    /// Return stereo panning.
-    float GetPanning() const { return panning_; }
-    /// Return autoremove mode.
-    bool GetAutoRemove() const { return autoRemove_; }
-    /// Return whether is playing.
+    Sound* GetSound() const;
+    float GetTimePosition() const;
     bool IsPlaying() const;
     
-    /// Play a sound without locking the audio mutex. Called internally.
-    void PlayLockless(Sound* sound);
-    /// Stop sound without locking the audio mutex. Called internally.
-    void StopLockless();
+    // Properties:
+    tolua_property__get_set SoundType soundType;
+    tolua_property__get_set float frequency;
+    tolua_property__get_set float gain;
+    tolua_property__get_set float attenuation;
+    tolua_property__get_set float panning;
+    tolua_property__get_set bool autoRemove;
+    tolua_readonly tolua_property__get_set Sound* sound;
+    tolua_readonly tolua_property__get_set float timePosition;
+    tolua_readonly tolua_property__is_set bool playing;
 };

+ 14 - 16
Extras/LuaScript/pkgs/Audio/SoundSource3D.pkg

@@ -1,26 +1,24 @@
 $#include "SoundSource3D.h"
 
-/// %Sound source component with three-dimensional position.
 class SoundSource3D : public SoundSource
 {
 public:
-    /// Register object factory.
-    static void RegisterObject(Context* context);
-    /// Set attenuation parameters.
+    // Methods:
     void SetDistanceAttenuation(float nearDistance, float farDistance, float rolloffFactor);
-    /// Set near distance. Distances closer than this do not have an effect.
+        
     void SetNearDistance(float distance);
-    /// Set far distance. Beyond this sound will be completely attenuated.
+    float GetNearDistance() const;
+    
     void SetFarDistance(float distance);
-    /// Set rolloff power factor, defines attenuation function shape.
+    float GetFarDistance() const;
+
     void SetRolloffFactor(float factor);
-    /// Calculate attenuation and panning based on current position and listener position.
-    void CalculateAttenuation();
-    
-    /// Return near distance.
-    float GetNearDistance() const { return nearDistance_; }
-    /// Return far distance.
-    float GetFarDistance() const { return farDistance_; }
-    /// Return rolloff power factor.
-    float RollAngleoffFactor() const { return rolloffFactor_; }
+    float RollAngleoffFactor() const;
+
+    // Properties:
+    tolua_property__get_set float nearDistance;
+    tolua_property__get_set float farDistance;
+    tolua_property__get_set float rolloffFactor;
 };
+
+$#define GetRolloffFactor RollAngleoffFactor

+ 8 - 15
Extras/LuaScript/pkgs/Core/Timer.pkg

@@ -1,32 +1,25 @@
 $#include "Timer.h"
 
-/// Low-resolution operating system timer.
 class Timer
 {
 public:
-    /// Construct. Get the starting clock value.
-    Timer();
-    
-    /// Return elapsed milliseconds and optionally reset.
     unsigned GetMSec(bool reset);
-    
-    /// Reset the timer.
     void Reset();
 };
 
-/// %Time and frame counter subsystem.
 class Time : public Object
 {
 public:
-    /// Return frame number, starting from 1 once BeginFrame() is called for the first time.
+    // Methods:
     unsigned GetFrameNumber() const;
-    
-    /// Return current frame timestep as seconds.
     float GetTimeStep() const;
+    float GetElapsedTime();
     
-    /// Return current low-resolution timer period in milliseconds.
-    unsigned GetTimerPeriod();
+    static unsigned GetSystemTime();
+    static String GetTimeStamp();
     
-    /// Return elapsed time from program start as seconds.
-    float GetElapsedTime();    
+    // Properties:
+    tolua_readonly tolua_property__get_set unsigned frameNumber;
+    tolua_readonly tolua_property__get_set float timeStep;
+    tolua_readonly tolua_property__get_set float elapsedTime;
 };

+ 1 - 1
Extras/LuaScript/pkgs/GraphicsLuaAPI.pkg

@@ -1,5 +1,6 @@
 $#define TOLUA_RELEASE
 
+$pfile "Graphics/GraphicsDefs.pkg"
 $pfile "Graphics/AnimatedModel.pkg"
 $pfile "Graphics/Animation.pkg"
 $pfile "Graphics/AnimationController.pkg"
@@ -10,7 +11,6 @@ $pfile "Graphics/DebugRenderer.pkg"
 $pfile "Graphics/DecalSet.pkg"
 $pfile "Graphics/Drawable.pkg"
 $pfile "Graphics/Graphics.pkg"
-$pfile "Graphics/GraphicsDefs.pkg"
 $pfile "Graphics/Light.pkg"
 $pfile "Graphics/Material.pkg"
 $pfile "Graphics/Model.pkg"

+ 0 - 20
Extras/LuaScript/pkgs/Math/AreaAllocator.pkg

@@ -1,20 +0,0 @@
-$#include "AreaAllocator.h"
-
-/// Rectangular area allocator.
-class AreaAllocator
-{
-public:
-    /// Construct with given width and height.
-    AreaAllocator(int width, int height);
-    /// Construct with given width and height, and set the maximum it allows to grow.
-    AreaAllocator(int width, int height, int maxWidth, int maxHeight);
-    
-    /// Reset to given width and height and remove all previous allocations.
-    void Reset(int width, int height);
-    /// Try to allocate an area. Return true if successful, with x & y coordinates filled.
-    bool Allocate(int width, int height, int& x, int& y);
-    /// Return the current width.
-    int GetWidth() const { return size_.x_; }
-    /// Return the current height.
-    int GetHeight() const { return size_.y_; }
-};

+ 46 - 225
Extras/LuaScript/pkgs/Math/BoundingBox.pkg

@@ -1,252 +1,73 @@
 $#include "BoundingBox.h"
 
-/// Three-dimensional axis-aligned bounding box.
 class BoundingBox
 {
 public:
-    /// Construct with zero size.
-    BoundingBox() :
-        min_(Vector3::ZERO),
-        max_(Vector3::ZERO),
-        defined_(false)
-    {
-    }
-    
-    /// Copy-construct from another bounding box.
-    BoundingBox(const BoundingBox& box) :
-        min_(box.min_),
-        max_(box.max_),
-        defined_(box.defined_)
-    {
-    }
-    
-    /// Construct from a rect, with the Z dimension left zero.
-    BoundingBox(const Rect& rect) :
-        min_(Vector3(rect.min_, 0.0f)),
-        max_(Vector3(rect.max_, 0.0f)),
-        defined_(true)
-    {
-    }
-    
-    /// Construct from minimum and maximum vectors.
-    BoundingBox(const Vector3& min, const Vector3& max) :
-        min_(min),
-        max_(max),
-        defined_(true)
-    {
-    }
-    
-    /// Construct from minimum and maximum floats (all dimensions same.)
-    BoundingBox(float min, float max) :
-        min_(Vector3(min, min, min)),
-        max_(Vector3(max, max, max)),
-        defined_(true)
-    {
-    }
-    
-    /// Construct from an array of vertices.
-    BoundingBox(const Vector3* vertices, unsigned count) :
-        defined_(false)
-    {
-        Define(vertices, count);
-    }
-    
-    /// Construct from a frustum.
-    BoundingBox(const Frustum& frustum) :
-        defined_(false)
-    {
-        Define(frustum);
-    }
-    
-    /// Construct from a polyhedron.
-    BoundingBox(const Polyhedron& poly) :
-        defined_(false)
-    {
-        Define(poly);
-    }
-    
-    /// Construct from a sphere.
-    BoundingBox(const Sphere& sphere) :
-        defined_(false)
-    {
-        Define(sphere);
-    }
-    
-    /// Test for equality with another bounding box.
-    bool operator == (const BoundingBox& rhs) const { return (min_ == rhs.min_ && max_ == rhs.max_); }
-    
-    /// Define from another bounding box.
-    void Define(const BoundingBox& box)
-    {
-        Define(box.min_, box.max_);
-    }
-    
-    /// Define from a Rect.
-    void Define(const Rect& rect)
-    {
-        Define(Vector3(rect.min_, 0.0f), Vector3(rect.max_, 0.0f));
-    }
-    
-    /// Define from minimum and maximum vectors.
-    void Define(const Vector3& min, const Vector3& max)
-    {
-        min_ = min;
-        max_ = max;
-        defined_ = true;
-    }
-    
-    /// Define from minimum and maximum floats (all dimensions same.)
-    void Define(float min, float max)
-    {
-        min_ = Vector3(min, min, min);
-        max_ = Vector3(max, max, max);
-        defined_ = true;
-    }
-    
-    /// Define from a point.
-    void Define(const Vector3& point)
-    {
-        min_ = max_ = point;
-        defined_ = true;
-    }
-    
-    /// Merge a point.
-    void Merge(const Vector3& point)
-    {
-        if (!defined_)
-        {
-            min_ = max_ = point;
-            defined_ = true;
-            return;
-        }
-        
-        if (point.x_ < min_.x_)
-            min_.x_ = point.x_;
-        if (point.y_ < min_.y_)
-            min_.y_ = point.y_;
-        if (point.z_ < min_.z_)
-            min_.z_ = point.z_;
-        if (point.x_ > max_.x_)
-            max_.x_ = point.x_;
-        if (point.y_ > max_.y_)
-            max_.y_ = point.y_;
-        if (point.z_ > max_.z_)
-            max_.z_ = point.z_;
-    }
-    
-    /// Merge another bounding box.
-    void Merge(const BoundingBox& box)
-    {
-        if (!defined_)
-        {
-            min_ = box.min_;
-            max_ = box.max_;
-            defined_ = true;
-            return;
-        }
-    
-        if (box.min_.x_ < min_.x_)
-            min_.x_ = box.min_.x_;
-        if (box.min_.y_ < min_.y_)
-            min_.y_ = box.min_.y_;
-        if (box.min_.z_ < min_.z_)
-            min_.z_ = box.min_.z_;
-        if (box.max_.x_ > max_.x_)
-            max_.x_ = box.max_.x_;
-        if (box.max_.y_ > max_.y_)
-            max_.y_ = box.max_.y_;
-        if (box.max_.z_ > max_.z_)
-            max_.z_ = box.max_.z_;
-    }
-    
-    /// Define from an array of vertices.
-    void Define(const Vector3* vertices, unsigned count);
-    /// Define from a frustum.
+    BoundingBox();
+    BoundingBox(const BoundingBox& box);
+    BoundingBox(const Rect& rect);
+    BoundingBox(const Vector3& min, const Vector3& max);
+    BoundingBox(float min, float max);
+    BoundingBox(const Frustum& frustum);
+    BoundingBox(const Polyhedron& poly);
+    BoundingBox(const Sphere& sphere);
+    
+    bool operator == (const BoundingBox& rhs) const;
+    
+    void Define(const BoundingBox& box);
+    void Define(const Rect& rect);
+    void Define(const Vector3& min, const Vector3& max);
+    void Define(float min, float max);
+    void Define(const Vector3& point);
     void Define(const Frustum& frustum);
-    /// Define from a polyhedron.
     void Define(const Polyhedron& poly);
-    /// Define from a sphere.
     void Define(const Sphere& sphere);
-    /// Merge an array of vertices.
-    void Merge(const Vector3* vertices, unsigned count);
-    /// Merge a frustum.
+    
+    void Merge(const Vector3& point);
+    void Merge(const BoundingBox& box);
     void Merge(const Frustum& frustum);
-    /// Merge a polyhedron.
     void Merge(const Polyhedron& poly);
-    /// Merge a sphere.
     void Merge(const Sphere& sphere);
-    /// Clip with another bounding box.
+    
     void Clip(const BoundingBox& box);
-    /// Transform with a 3x3 matrix.
     void Transform(const Matrix3& transform);
-    /// Transform with a 3x4 matrix.
     void Transform(const Matrix3x4& transform);
+    void Clear();
     
-    /// Clear to undefined state.
-    void Clear()
-    {
-        min_ = Vector3::ZERO;
-        max_ = Vector3::ZERO;
-        defined_ = false;
-    }
+    Vector3 Center() const;
+    Vector3 Size() const;
+    Vector3 HalfSize() const;
     
-    /// Return center.
-    Vector3 Center() const { return (max_ + min_) * 0.5f; }
-    /// Return size.
-    Vector3 Size() const { return max_ - min_; }
-    /// Return half-size.
-    Vector3 HalfSize() const { return (max_ - min_) * 0.5f; }
-    
-    /// Return transformed by a 3x3 matrix.
     BoundingBox Transformed(const Matrix3& transform) const;
-    /// Return transformed by a 3x4 matrix.
     BoundingBox Transformed(const Matrix3x4& transform) const;
-    /// Return projected by a 4x4 projection matrix.
     Rect Projected(const Matrix4& projection) const;
     
-    /// Test if a point is inside.
-    Intersection IsInside(const Vector3& point) const
-    {
-        if (point.x_ < min_.x_ || point.x_ > max_.x_ || point.y_ < min_.y_ || point.y_ > max_.y_ ||
-            point.z_ < min_.z_ || point.z_ > max_.z_)
-            return OUTSIDE;
-        else
-            return INSIDE;
-    }
-    
-    /// Test if another bounding box is inside, outside or intersects.
-    Intersection IsInside(const BoundingBox& box) const
-    {
-        if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
-            box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
-            return OUTSIDE;
-        else if (box.min_.x_ < min_.x_ || box.max_.x_ > max_.x_ || box.min_.y_ < min_.y_ || box.max_.y_ > max_.y_ ||
-            box.min_.z_ < min_.z_ || box.max_.z_ > max_.z_)
-            return INTERSECTS;
-        else
-            return INSIDE;
-    }
-    
-    /// Test if another bounding box is (partially) inside or outside.
-    Intersection IsInsideFast(const BoundingBox& box) const
-    {
-        if (box.max_.x_ < min_.x_ || box.min_.x_ > max_.x_ || box.max_.y_ < min_.y_ || box.min_.y_ > max_.y_ ||
-            box.max_.z_ < min_.z_ || box.min_.z_ > max_.z_)
-            return OUTSIDE;
-        else
-            return INSIDE;
-    }
-    
-    /// Test if a sphere is inside, outside or intersects.
+    Intersection IsInside(const Vector3& point) const;
+    Intersection IsInside(const BoundingBox& box) const;
+    Intersection IsInsideFast(const BoundingBox& box) const;
     Intersection IsInside(const Sphere& sphere) const;
-    /// Test if a sphere is (partially) inside or outside.
     Intersection IsInsideFast(const Sphere& sphere) const;
     
-    /// Return as string.
     String ToString() const;
-    /// Minimum vector.
+    
     Vector3 min_ @ min;
-	
-    /// Maximum vector.
     Vector3 max_ @ max;
+    bool defined_ @ defined;
+    
+    tolua_readonly tolua_property__get_set Vector3 center;
+    tolua_readonly tolua_property__get_set Vector3 size;
+    tolua_readonly tolua_property__get_set Vector3 halfSize;
 };
+
+${
+
+#define TOLUA_DISABLE_tolua_get_BoundingBox_center
+#define tolua_get_BoundingBox_center tolua_MathLuaAPI_BoundingBox_Center00
+
+#define TOLUA_DISABLE_tolua_get_BoundingBox_size
+#define tolua_get_BoundingBox_size tolua_MathLuaAPI_BoundingBox_Size00
+
+#define TOLUA_DISABLE_tolua_get_BoundingBox_halfSize
+#define tolua_get_BoundingBox_halfSize tolua_MathLuaAPI_BoundingBox_HalfSize00
+
+$}

+ 9 - 70
Extras/LuaScript/pkgs/Math/MathDefs.pkg

@@ -1,12 +1,10 @@
 $#include "MathDefs.h"
 
 static const float M_PI;
-
 static const int M_MIN_INT;
 static const int M_MAX_INT;
 static const unsigned M_MIN_UNSIGNED;
 static const unsigned M_MAX_UNSIGNED;
-
 static const float M_EPSILON;
 static const float M_LARGE_EPSILON;
 static const float M_MIN_NEARCLIP;
@@ -17,7 +15,6 @@ static const float M_DEGTORAD;
 static const float M_DEGTORAD_2;
 static const float M_RADTODEG;
 
-/// Intersection test result.
 enum Intersection
 {
     OUTSIDE,
@@ -25,71 +22,13 @@ enum Intersection
     INSIDE
 };
 
-/// Linear interpolation between two float values.
-inline float Lerp(float lhs, float rhs, float t) { return lhs * (1.0f - t) + rhs * t; }
-/// Return the smaller of two floats.
-inline float Min(float lhs, float rhs) { return lhs < rhs ? lhs : rhs; }
-/// Return the larger of two floats.
-inline float Max(float lhs, float rhs) { return lhs > rhs ? lhs : rhs; }
-/// Return absolute value of a float.
-inline float Abs(float value) { return value >= 0.0f ? value : -value; }
-
-/// Clamp a float to a range.
-inline float Clamp(float value, float min, float max)
-{
-    if (value < min)
-        return min;
-    else if (value > max)
-        return max;
-    else
-        return value;
-}
-
-/// Check whether two floating point values are equal within accuracy.
-inline bool Equals(float lhs, float rhs) { return lhs + M_EPSILON >= rhs && lhs - M_EPSILON <= rhs; }
-/// Return the smaller of two integers.
-inline int Min(int lhs, int rhs) { return lhs < rhs ? lhs : rhs; }
-/// Return the larger of two integers.
-inline int Max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; }
-/// Return absolute value of an integer
-inline int Abs(int value) { return value >= 0 ? value : -value; }
-
-/// Clamp an integer to a range.
-inline int Clamp(int value, int min, int max)
-{
-    if (value < min)
-        return min;
-    else if (value > max)
-        return max;
-    else
-        return value;
-}
-
-/// Check whether an unsigned integer is a power of two.
-inline bool IsPowerOfTwo(unsigned value)
-{
-    if (!value)
-        return true;
-    while (!(value & 1))
-        value >>= 1;
-    return value == 1;
-}
-
-/// Round up to next power of two.
-inline unsigned NextPowerOfTwo(unsigned value)
-{
-    unsigned ret = 1;
-    while (ret < value && ret < 0x80000000)
-        ret <<= 1;
-    return ret;
-}
-
-/// Update a hash with the given 8-bit value using the SDBM algorithm.
-inline unsigned SDBMHash(unsigned hash, unsigned char c) { return c + (hash << 6) + (hash << 16) - hash; }
-/// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
-inline float Random() { return Rand() / 32768.0f; }
-/// Return a random float between 0.0 and range, inclusive from both ends.
-inline float Random(float range) { return Rand() * range / 32767.0f; }
-/// Return a random integer between 0 and range - 1.
-inline int Random @ RandomInt(int range) { return (Rand() * (range - 1) + 16384) / 32767; }
+float Lerp(float lhs, float rhs, float t);
+float Min(float lhs, float rhs);
+float Max(float lhs, float rhs);
+float Abs(float value);
+float Clamp(float value, float min, float max);
+bool Equals(float lhs, float rhs);
 
+float Random();
+float Random(float range);
+int Random @ RandomInt(int range);

+ 8 - 46
Extras/LuaScript/pkgs/Math/Plane.pkg

@@ -1,58 +1,20 @@
 $#include "Plane.h"
 
-/// Surface in three-dimensional space.
+
 class Plane
 {
 public:
-    /// Construct undefined.
-    Plane()
-    {
-    }
-    
-    /// Copy-construct from another plane.
-    Plane(const Plane& plane) :
-        normal_(plane.normal_),
-        absNormal_(plane.absNormal_),
-        intercept_(plane.intercept_)
-    {
-    }
-    
-    /// Construct from 3 vertices.
-    Plane(const Vector3& v0, const Vector3& v1, const Vector3& v2)
-    {
-        Define(v0, v1, v2);
-    }
+    Plane();
+    Plane(const Plane& plane);
+    Plane(const Vector3& v0, const Vector3& v1, const Vector3& v2);
+    Plane(const Vector3& normal, const Vector3& point);
     
-    /// Construct from a normal vector and a point on the plane.
-    Plane(const Vector3& normal, const Vector3& point)
-    {
-        Define(normal, point);
-    }
-    
-    /// Define from 3 vertices.
-    void Define(const Vector3& v0, const Vector3& v1, const Vector3& v2)
-    {
-        Vector3 dist1 = v1 - v0;
-        Vector3 dist2 = v2 - v0;
-        
-        Define(dist1.CrossProduct(dist2).Normalized(), v0);
-    }
-
-    /// Define from a normal and a point.
-    void Define(const Vector3& normal, const Vector3& point)
-    {
-        normal_ = normal;
-        absNormal_ = normal.Abs();
-        intercept_ = normal.DotProduct(point);
-    }
+    void Define(const Vector3& v0, const Vector3& v1, const Vector3& v2);
+    void Define(const Vector3& normal, const Vector3& point);
     
-    /// Return signed distance to a point.
-    float Distance(const Vector3& point) const { return normal_.DotProduct(point) - intercept_; }
+    float Distance(const Vector3& point) const;
     
-    /// Plane normal.
     Vector3 normal_ @ normal;
-    /// Plane absolute normal.
     Vector3 absNormal_ @ absNormal;
-    /// Plane intercept parameter.
     float intercept_ @ intercept;
 };

+ 10 - 47
Extras/LuaScript/pkgs/Math/Ray.pkg

@@ -1,66 +1,29 @@
 $#include "Ray.h"
 
-/// Infinite straight line in three-dimensional space.
 class Ray
 {
 public:
-    /// Construct undefined.
-    Ray()
-    {
-    }
-    
-    /// Construct from origin and direction. The direction must be normalized.
-    Ray(const Vector3& origin, const Vector3& direction) :
-        origin_(origin),
-        direction_(direction)
-    {
-    }
-    
-    /// Copy-construct from another ray.
-    Ray(const Ray& ray) :
-        origin_(ray.origin_),
-        direction_(ray.direction_)
-    {
-    }
-    
-    /// Check for equality with another ray.
-    bool operator == (const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
+    Ray();
+    Ray(const Vector3& origin, const Vector3& direction);
+    Ray(const Ray& ray);
+
+    bool operator == (const Ray& rhs) const;
     
-    /// Define from origin and direction. The direction will be normalized.
-    void Define(const Vector3& origin, const Vector3& direction)
-    {
-        origin_ = origin;
-        direction_ = direction.Normalized();
-    }
+    void Define(const Vector3& origin, const Vector3& direction);
     
-    /// Project a point on the ray.
     Vector3 Project(const Vector3& point) const;
-    /// Return distance of a point from the ray
     float Distance(const Vector3& point) const;
-    /// Return closest point to another ray.
     Vector3 ClosestPoint(const Ray& ray) const;
-    /// Return hit distance to a plane, or infinity if no hit.
+    
     float HitDistance(const Plane& plane) const;
-    /// Return hit distance to a bounding box, or infinity if no hit.
     float HitDistance(const BoundingBox& box) const;
-    /// Return hit distance to a frustum, or infinity if no hit. If solidInside parameter is true (default) rays originating from inside return zero distance, otherwise the distance to the closest plane.
+    
+    float HitDistance(const Frustum& frustum) const;
     float HitDistance(const Frustum& frustum, bool solidInside = true) const;
-    /// Return hit distance to a sphere, or infinity if no hit.
+    
     float HitDistance(const Sphere& sphere) const;
-    /// Return hit distance to a triangle, or infinity if no hit.
     float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
-    /// Return hit distance to non-indexed geometry data, or infinity if no hit.
-    float HitDistance(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
-    /// Return hit distance to indexed geometry data, or infinity if no hit.
-    float HitDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
-    /// Return whether ray is inside non-indexed geometry.
-    bool InsideGeometry(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
-    /// Return whether ray is inside indexed geometry.
-    bool InsideGeometry(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
     
-    /// Ray origin.
     Vector3 origin_ @ origin;
-    
-    /// Ray direction.
     Vector3 direction_ @ direction;
 };

+ 16 - 149
Extras/LuaScript/pkgs/Math/Sphere.pkg

@@ -1,173 +1,40 @@
 $#include "Sphere.h"
 
-/// %Sphere in three-dimensional space.
 class Sphere
 {
 public:
-    /// Construct undefined.
-    Sphere() :
-        center_(Vector3::ZERO),
-        radius_(0.0f),
-        defined_(false)
-    {
-    }
+    Sphere();
+    Sphere(const Sphere& sphere);
+    Sphere(const Vector3& center, float radius);
+    Sphere(const BoundingBox& box);
+    Sphere(const Frustum& frustum);
+    Sphere(const Polyhedron& poly);
     
-    /// Copy-construct from another sphere.
-    Sphere(const Sphere& sphere) :
-        center_(sphere.center_),
-        radius_(sphere.radius_),
-        defined_(sphere.defined_)
-    {
-    }
+    bool operator == (const Sphere& rhs) const;
     
-    /// Construct from center and radius.
-    Sphere(const Vector3& center, float radius) :
-        center_(center),
-        radius_(radius),
-        defined_(true)
-    {
-    }
-    
-    /// Construct from an array of vertices.
-    Sphere(const Vector3* vertices, unsigned count) :
-        defined_(false)
-    {
-        Define(vertices, count);
-    }
-    
-    /// Construct from a bounding box.
-    Sphere(const BoundingBox& box) :
-        defined_(false)
-    {
-        Define(box);
-    }
-    
-    /// Construct from a frustum.
-    Sphere(const Frustum& frustum) :
-        defined_(false)
-    {
-        Define(frustum);
-    }
-    
-    /// Construct from a polyhedron.
-    Sphere(const Polyhedron& poly) :
-        defined_(false)
-    {
-        Define(poly);
-    }
-    
-    /// Test for equality with another sphere.
-    bool operator == (const Sphere& rhs) const { return center_ == rhs.center_ && radius_ == rhs.radius_; }
-    
-    /// Define from another sphere.
-    void Define(const Sphere& sphere)
-    {
-        Define(sphere.center_, sphere.radius_);
-    }
-    
-    /// Define from center and radius.
-    void Define(const Vector3& center, float radius)
-    {
-        center_ = center;
-        radius_ = radius;
-        defined_ = true;
-    }
-    
-    /// Define from an array of vertices.
-    void Define(const Vector3* vertices, unsigned count);
-    /// Define from a bounding box.
+    void Define(const Sphere& sphere);
+    void Define(const Vector3& center, float radius);
     void Define(const BoundingBox& box);
-    /// Define from a frustum.
     void Define(const Frustum& frustum);
-    /// Define from a polyhedron.
     void Define(const Polyhedron& poly);
     
-    /// Merge a point.
-    void Merge(const Vector3& point)
-    {
-        if (!defined_)
-        {
-            center_ = point;
-            radius_ = 0.0f;
-            defined_ = true;
-            return;
-        }
-        
-        Vector3 offset = point - center_;
-        float dist = offset.Length();
-        
-        if (dist > radius_)
-        {
-            float half = (dist - radius_) * 0.5f;
-            radius_ += half;
-            center_ += (half / dist) * offset;
-        }
-    }
-    
-    /// Merge an array of vertices.
-    void Merge(const Vector3* vertices, unsigned count);
-    /// Merge a bounding box.
+    void Merge(const Vector3& point);
     void Merge(const BoundingBox& box);
-    /// Merge a frustum.
     void Merge(const Frustum& frustum);
-    /// Merge a polyhedron.
     void Merge(const Polyhedron& poly);
-    /// Merge a sphere.
     void Merge(const Sphere& sphere);
     
-    /// Clear to undefined state.
-    void Clear()
-    {
-        center_ = Vector3::ZERO;
-        radius_ = 0.0f;
-        defined_ = false;
-    }
-    
-    /// Test if a point is inside.
-    Intersection IsInside(const Vector3& point) const
-    {
-        float distSquared = (point - center_).LengthSquared();
-        if (distSquared < radius_ * radius_)
-            return INSIDE;
-        else
-            return OUTSIDE;
-    }
+    void Clear();
     
-    /// Test if another sphere is inside, outside or intersects.
-    Intersection IsInside(const Sphere& sphere) const
-    {
-        float dist = (sphere.center_ - center_).Length();
-        if (dist >= sphere.radius_ + radius_)
-            return OUTSIDE;
-        else if (dist + sphere.radius_ < radius_)
-            return INSIDE;
-        else
-            return INTERSECTS;
-    }
-    
-    /// Test if another sphere is (partially) inside or outside.
-    Intersection IsInsideFast(const Sphere& sphere) const
-    {
-        float distSquared = (sphere.center_ - center_).LengthSquared();
-        float combined = sphere.radius_ + radius_;
-        
-        if (distSquared >= combined * combined)
-            return OUTSIDE;
-        else
-            return INSIDE;
-    }
-    
-    /// Test if a bounding box is inside, outside or intersects.
+    Intersection IsInside(const Vector3& point) const;
+    Intersection IsInside(const Sphere& sphere) const;
+    Intersection IsInsideFast(const Sphere& sphere) const;
     Intersection IsInside(const BoundingBox& box) const;
-    /// Test if a bounding box is (partially) inside or outside.
     Intersection IsInsideFast(const BoundingBox& box) const;
     
-    /// Return distance of a point to the surface, or 0 if inside.
-    float Distance(const Vector3& point) const { return Max((point - center_).Length() - radius_, 0.0f); }
+    float Distance(const Vector3& point) const;
     
-    /// Sphere center.
     Vector3 center_ @ center;
-
-    /// Sphere radius.
     float radius_ @ radius;
+    bool defined_ @ defined;
 };

+ 1 - 2
Extras/LuaScript/pkgs/MathLuaAPI.pkg

@@ -1,10 +1,9 @@
 $#define TOLUA_RELEASE
 
-$pfile "Math/AreaAllocator.pkg"
+$pfile "Math/MathDefs.pkg"
 $pfile "Math/BoundingBox.pkg"
 $pfile "Math/Color.pkg"
 $pfile "Math/Frustum.pkg"
-$pfile "Math/MathDefs.pkg"
 $pfile "Math/Matrix3.pkg"
 $pfile "Math/Matrix3x4.pkg"
 $pfile "Math/Matrix4.pkg"