diff options
Diffstat (limited to 'Source/Samples')
-rw-r--r-- | Source/Samples/CursorTest/main.cpp | 10 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/header.h | 60 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/main.cpp | 255 | ||||
-rw-r--r-- | Source/Samples/LuaxTest/script.lua | 138 |
4 files changed, 0 insertions, 463 deletions
diff --git a/Source/Samples/CursorTest/main.cpp b/Source/Samples/CursorTest/main.cpp deleted file mode 100644 index f8946d9..0000000 --- a/Source/Samples/CursorTest/main.cpp +++ /dev/null @@ -1,10 +0,0 @@ -/// -/// ʽ -/// - -#include "SDL2/SDL.h" - -int main(int argc, char* args[]) -{ - return 0; -}
\ No newline at end of file diff --git a/Source/Samples/LuaxTest/header.h b/Source/Samples/LuaxTest/header.h deleted file mode 100644 index 386ae71..0000000 --- a/Source/Samples/LuaxTest/header.h +++ /dev/null @@ -1,60 +0,0 @@ - -#ifndef ASSERT -#ifdef NDEBUG -#define ASSERT(x) { false ? (void)(x) : (void)0; } -#else -#ifdef _WIN32 -#define ASURA_DEBUG_BREAK() __debugbreak() -#else -#define ASURA_DEBUG_BREAK() raise(SIGTRAP) -#endif -#define ASSERT(x) do { const volatile bool asura_assert_b____ = !(x); if(asura_assert_b____) ASURA_DEBUG_BREAK(); } while (false) -#endif -#endif - -/// -/// ̳Singletonڵһʵʱʵ֮ٴʵᱨ -/// -template<class T> -class Singleton -{ -public: - - static T* Get() - { - // ֮ǰûдһ - if (!instance) instance = new T; - // ʵ - return instance; - } - - static void Destroy() - { - delete instance; - instance = nullptr; - } - -protected: - - Singleton() - { - // instanceζִһʵǴġ - ASSERT(!instance); - // ʵΪʵ - instance = static_cast<T*>(this); - }; - - virtual ~Singleton() {}; - - static T* instance; - -private: - - Singleton(const Singleton& singleton); - - Singleton& operator = (const Singleton& singleton); - -}; - -template<class T> -T* Singleton<T>::instance = nullptr; diff --git a/Source/Samples/LuaxTest/main.cpp b/Source/Samples/LuaxTest/main.cpp deleted file mode 100644 index f87226e..0000000 --- a/Source/Samples/LuaxTest/main.cpp +++ /dev/null @@ -1,255 +0,0 @@ -/// -/// Scripting with Lua. -/// -extern "C"{ -#include "Lua51/lua.h" -#include "Lua51/lauxlib.h" -} -#include "Luax/luax.h" -#include "header.h" - -#include <iostream> -#include <string> - -using namespace std; -using namespace Luax; - -//---------------------------------------------------------------------------------------------------------------- - -class School - : public Singleton<School> - , public LuaxNativeClass<School> -{ -public: - - School() : mName("Koko") {} - -private: - - const char* mName; - -public: - - LUAX_DECL_SINGLETON(School); - - LUAX_DECL_METHOD(l_GetName); - -}; - -int School::l_GetName(lua_State* L) -{ - LUAX_STATE(L); - - School* school = Get(); - - state.Push(school->mName); - return 1; -} - -void School::RegisterLuaxClass(LuaxState& state) -{ - state.SetField(-1, "Region", "Block 1"); // 101 - - luaL_Reg regTable[] = { - { "GetName", l_GetName }, - { NULL, NULL } - }; - - state.RegisterMethods(regTable); - -} - -void School::RegisterLuaxPostprocess(Luax::LuaxState&) -{ -} - -//---------------------------------------------------------------------------------------------------------------- - -class Boy : public LuaxNativeClass<Boy> -{ -public: - - Boy(int age, const char* name) : mAge(age), mName(name){} - - int mAge; - - const char* mName; - -private: - -public: - - LUAX_DECL_FACTORY(SimBoy); - - // member methods - LUAX_DECL_METHOD(l_GetAge); - LUAX_DECL_METHOD(l_GetName); - - // class method - LUAX_DECL_METHOD(l_New); - LUAX_DECL_METHOD(l_GetGender); - - LUAX_DECL_METHOD(l_Write); - LUAX_DECL_METHOD(l_Speak); - -private: - - LuaxMemberRef mCallbak; - -}; - -int Boy::l_New(lua_State* L) -{ - LUAX_STATE(L); - - int age = state.GetValue(1, 0); - const char* name = state.GetValue(2, ""); - - Boy* boy = new Boy(age, name); - boy->PushLuaxUserdata(state); - - return 1; -} - -int Boy::l_GetAge(lua_State* L) -{ - LUAX_SETUP(L, "U"); - Boy* self = state.GetLuaUserdata<Boy>(1); - state.Push(self->mAge); - return 1; -} - -int Boy::l_GetName(lua_State* L) -{ - LUAX_SETUP(L, "U"); - Boy* self = state.GetLuaUserdata<Boy>(1); - state.Push(self->mName); - return 1; -} - -int Boy::l_GetGender(lua_State* L) -{ - LUAX_SETUP(L, "*"); - state.Push("male student!"); - return 1; -} - -int Boy::l_Speak(lua_State* L) -{ - LUAX_STATE(L); - - Boy* self = state.GetLuaUserdata<Boy>(1); - self->PushMemberRef(state, self->mCallbak); - state.Call(0, 1); - return 1; -} - -int Boy::l_Write(lua_State* L) -{ - LUAX_STATE(L); - // self, func - Boy* self = state.GetLuaUserdata<Boy>(1); - self->SetMemberRef(state, self->mCallbak, 2); - return 0; -} - -void Boy::RegisterLuaxClass(LuaxState& state) -{ - luaL_Reg regTable[] = { - // class functions - { "New", l_New }, - { "GetGender", l_GetGender }, - // members - { "GetAge", l_GetAge }, - { "GetName", l_GetName }, - { "Write", l_Write }, - { "Speak", l_Speak }, - { 0, 0} - }; - - state.RegisterMethods(regTable); - - // boyİ - LuaxEnum EHabits[] = { - { "Computer", 1}, - { "Buscketball", 2}, - { "Baseball", 3}, - { "Girls", 4}, - {0, 0} - }; - - state.RegisterEnum("EHabits", EHabits); -} - -void Boy::RegisterLuaxPostprocess(LuaxState& state) -{ - // boyİ - LuaxEnum EHabits[] = { - { "Computer", 1}, - { "Buscketball", 2}, - { "Baseball", 3}, - { "Girls", 4}, - {0, 0} - }; - - state.RegisterEnum("EHabits", EHabits); -} - -//---------------------------------------------------------------------------------------------------------------- - -class Girl : public LuaxNativeClass<Girl> -{ -}; - -//---------------------------------------------------------------------------------------------------------------- - -#include "script.lua" - -//---------------------------------------------------------------------------------------------------------------- - -int func(lua_State* L) -{ - LUAX_SETUP(L, "*"); - state.Push("func ok"); - return 1; -} - -int main() -{ - LuaxRuntime& runtime = LuaxRuntime::Get(); - lua_State* L = runtime.Open(); - - Luax::LuaxState& state = runtime[L].state; - state.OpenLibs(); - state.PushGlobalNamespace(); - state.PushNamespace("Asura"); - state.RegisterPlainClassRegistry("Class"); // Asura.Class("Foo") - state.RegisterPlainEnumRegistry("Enum"); // Asura.Enum("EFoo", { ... }); - //עenum - LuaxEnum EGender[] = { - { "BOY", 7 }, - { "GIRL", 8 }, - { "MID", 9 }, - { 0, 0 }, - }; - - state.RegisterEnum("EGender", EGender); - - state.PushNamespace("author"); - state.SetField(-1, "name", "chai"); - state.SetField(-1, "func", func); - state.PopNamespace(); // Asura.author - state.SetField(-1, "version", 100); - cout << state.GetField<float>(-1, "version", 0); - - state.RegisterSingleton<School>(); - state.RegisterFactory<Boy>(); - - state.PopNamespace(); // Asura - state.PopNamespace(); // Global - state.DoString(script); - - runtime.Close(L); - - getchar(); -}
\ No newline at end of file diff --git a/Source/Samples/LuaxTest/script.lua b/Source/Samples/LuaxTest/script.lua deleted file mode 100644 index 7bf9337..0000000 --- a/Source/Samples/LuaxTest/script.lua +++ /dev/null @@ -1,138 +0,0 @@ -string script = R"scriptcode( --- start script - -function main() - local a = 19 - print(Asura.version) - print(Asura.author.name) - print("ok") - print(Asura.author.func()) --- local boy = Asura.SimBoy.New("I am peter!", 19) --- boy:Say() --- print(Asura.SimSchool.GetName()) - print(Asura.SimBoy.Class) - print(Asura.SimBoy.Gender) - print(Asura.SimBoy.GetClassName()) - print(Asura.School.GetName()) - print(Asura.School.Region) ---[[ - local Kid = Asura.SimBoy.Extend() - Asura.Kid = Kid - Kid.New = function(self, height, age) - self.base(age) - self.height = height - end - Kid.GetHeight = function(self) - print(self.height) - end - local kid = Kid.New(110, 12) - kid:GetHeight() - ]] - local kid = Asura.SimBoy.New(23, "Chai") - print(kid:GetAge()) - print(kid:GetName()) - kid.fruit = function() - return "apple" - end - print(kid.fruit()) - print(Asura.SimBoy.GetGender()) - Asura.SimBoy.Havefun = function() - return "Boys have some fun!" - end - print(Asura.SimBoy.Havefun()) - --- - Asura.SimBoy.Foo = function() - return "SimBoy.Foo" - end - print(Asura.SimBoy.Foo()) - - print(Asura.EGender.BOY) - --Asura.EGender.BOY = 2 - print(Asura.EGender.BOY) - print(Asura.SimBoy.EHabits.Girls) - print(Asura.EHabits.Girls) - print(kid) - - kid:Write(function() - return "kid:Write()" - end ) - print(kid:Speak()) - - kid:Write(function() - return "kid:Write() 2" - end ) - print(kid:Speak()) - -------------------- plain class test - local Foo = Asura.Class("Foo") - Foo.Ctor = function(self, age, name, boy) - self.age = age - self.name = name - self.boy = boy - end - Foo.GetAge = function(self) - return self.age - end - Foo.GetName = function(self) - return self.name - end - local foo = Foo.New(10, "lee", kid) - print(foo:GetName()) - print(Foo.GetClassName()) - print(foo.GetClassName()) - print(foo.boy:GetAge()) ---------------------inherits test - local Coo = Foo.Extend("Coo") - print(Coo.GetClassName()) - local coo = Coo.New(20, "Wang", kid) - print(coo:GetAge()) - Coo.Ctor = function(self, age, name, boy) - self.age = age - 1 - self.name = boy:GetName() - self.boy = boy - self.__base.Ctor(self, age, "Wangba", boy) - end - Coo.GetName = function(self) - local name = self.__base.GetName(self) - return "his name is " .. name - end - local coo2 = Coo.New(20, "Wang", kid) - print(coo2:GetAge()) - print(coo2.GetClassName()) - print(coo2:GetName()) - print(coo2) - print(coo) ----------------------plain enum test - local ERace = Asura.Enum("ERace", { - ["White"] = 1, - ["Asian"] = 2, - ["Black"] = 3, - }) - print(ERace.White) - print(ERace.Asian) -----------------------native class inherit test - local Boy2 = Asura.SimBoy.Extend("Boy2") - Boy2.Speak = function(self) - return self.__base.GetAge(self) - end - Boy2.Ctor = function(self, age, name) - print("ctor " .. age) - print("ctor " .. name) - end - local boy22 = Boy2.New(12, "Liu") - print(boy22) - print(boy22:Speak()) - -------------------------gc test - local boy = Asura.SimBoy.New(11, "chaichai") - boy = nil -end -function err(msg) - print(msg) -end -xpcall(main, err) - - --- end script -)scriptcode";
\ No newline at end of file |