From fd007c569cf64352233cf0886473a98a5b2da90e Mon Sep 17 00:00:00 2001 From: Ahmed Maawy Date: Sat, 3 Oct 2020 23:55:57 +0300 Subject: [PATCH] Add a new module known as Sample onto Love2D --- src/common/Module.h | 3 +- src/modules/love/love.cpp | 7 ++- src/modules/sample/Sample.h | 46 +++++++++++++++++ src/modules/sample/wrap_Sample.cpp | 79 ++++++++++++++++++++++++++++++ src/modules/sample/wrap_Sample.h | 38 ++++++++++++++ src/scripts/boot.lua | 3 ++ src/scripts/boot.lua.h | 4 ++ 7 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 src/modules/sample/Sample.h create mode 100644 src/modules/sample/wrap_Sample.cpp create mode 100644 src/modules/sample/wrap_Sample.h diff --git a/src/common/Module.h b/src/common/Module.h index f988673..6001ebe 100644 --- a/src/common/Module.h +++ b/src/common/Module.h @@ -57,7 +57,8 @@ class Module : public Object M_TOUCH, M_VIDEO, M_WINDOW, - M_MAX_ENUM + M_SAMPLE, + M_MAX_ENUM, }; Module(); diff --git a/src/modules/love/love.cpp b/src/modules/love/love.cpp index ac895f0..ac4caae 100644 --- a/src/modules/love/love.cpp +++ b/src/modules/love/love.cpp @@ -29,6 +29,7 @@ // C++ #include #include +#include #ifdef LOVE_WINDOWS #include @@ -83,6 +84,7 @@ extern "C" // of addressing implementations directly. extern "C" { + extern int luaopen_love_sample(lua_State *); #if defined(LOVE_ENABLE_AUDIO) extern int luaopen_love_audio(lua_State*); #endif @@ -145,6 +147,7 @@ extern "C" } static const luaL_Reg modules[] = { + { "love.sample", luaopen_love_sample }, #if defined(LOVE_ENABLE_AUDIO) { "love.audio", luaopen_love_audio }, #endif @@ -462,8 +465,10 @@ int luaopen_love(lua_State *L) } // Preload module loaders. - for (int i = 0; modules[i].name != nullptr; i++) + for (int i = 0; modules[i].name != nullptr; i++) { + std::cout << "Loading module: " << modules[i].name << " with: " << modules[i].func << "\n"; love::luax_preload(L, modules[i].func, modules[i].name); + } // Necessary for Data-creating methods to work properly in Data subclasses. love::luax_require(L, "love.data"); diff --git a/src/modules/sample/Sample.h b/src/modules/sample/Sample.h new file mode 100644 index 0000000..9ff0818 --- /dev/null +++ b/src/modules/sample/Sample.h @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2006-2019 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#ifndef LOVE_SAMPLE_SAMPLE_H +#define LOVE_SAMPLE_SAMPLE_H + +#include "common/Module.h" + +namespace love +{ +namespace sample +{ + +class Sample : public Module +{ +public: + Sample() {} + virtual ~Sample() {} + + // Implements Module. + virtual ModuleType getModuleType() const { return M_SAMPLE; } + const char *getName() const override { return "love.sample"; } + +}; // Sample + +} // sample +} // love + +#endif // LOVE_SAMPLE_SAMPLE_H diff --git a/src/modules/sample/wrap_Sample.cpp b/src/modules/sample/wrap_Sample.cpp new file mode 100644 index 0000000..c3c7115 --- /dev/null +++ b/src/modules/sample/wrap_Sample.cpp @@ -0,0 +1,79 @@ +/** + * Copyright (c) 2006-2019 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +// LOVE +#include "Sample.h" +#include "wrap_Sample.h" +#include "common/config.h" + +#include + +namespace love +{ +namespace sample +{ + +#define instance() (Module::getInstance(Module::M_SAMPLE)) + +int w_doSum(lua_State *L) { + int num1 = (int) luaL_optinteger(L, 1, 0); + int num2 = (int) luaL_optinteger(L, 2, 0); + lua_pushnumber(L, num1 + num2); + return 1; +} + +// List of functions to wrap. +static const luaL_Reg functions[] = +{ + { "doSum", w_doSum }, + { 0, 0 } +}; + +extern "C" int luaopen_love_sample(lua_State *L) +{ + Sample *instance = instance(); + + if (instance == nullptr) + { + std::cout << "Sample Module was null\n"; + luax_catchexcept(L, [&](){ instance = new love::sample::Sample(); }); + } + else + { + std::cout << "Sample Module was not null\n"; + instance->retain(); + } + + std::cout << "Wrapping sample module\n"; + + WrappedModule w; + w.module = instance; + w.name = "sample"; + w.type = &Module::type; + w.functions = functions; + w.types = 0; + + std::cout << "Registering 'sample' onto Lua\n"; + + return luax_register_module(L, w); +} + +} // sample +} // love diff --git a/src/modules/sample/wrap_Sample.h b/src/modules/sample/wrap_Sample.h new file mode 100644 index 0000000..3bdd361 --- /dev/null +++ b/src/modules/sample/wrap_Sample.h @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2006-2019 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#ifndef LOVE_MOUSE_WRAP_SAMPLE_H +#define LOVE_MOUSE_WRAP_SAMPLE_H + +// LOVE +#include "common/runtime.h" +#include "common/config.h" + +namespace love +{ +namespace sample +{ + +extern "C" LOVE_EXPORT int luaopen_love_sample(lua_State *L); + +} // sample +} // love + +#endif // LOVE_MOUSE_WRAP_SAMPLE_H \ No newline at end of file diff --git a/src/scripts/boot.lua b/src/scripts/boot.lua index e402ef3..a0378e3 100644 --- a/src/scripts/boot.lua +++ b/src/scripts/boot.lua @@ -422,6 +422,7 @@ function love.init() thread = true, window = true, video = true, + sample = true, }, audio = { mixwithsystem = true, -- Only relevant for Android / iOS. @@ -500,8 +501,10 @@ function love.init() "graphics", "math", "physics", + "sample", } do if c.modules[v] then + print("loading: " .. v) require("love." .. v) end end diff --git a/src/scripts/boot.lua.h b/src/scripts/boot.lua.h index 0b61d2b..31f108d 100644 --- a/src/scripts/boot.lua.h +++ b/src/scripts/boot.lua.h @@ -835,6 +835,7 @@ const unsigned char boot_lua[] = 0x09, 0x09, 0x09, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x09, 0x77, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x09, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, + 0x09, 0x09, 0x09, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x0a, 0x09, 0x09, 0x7d, 0x2c, 0x0a, 0x09, 0x09, 0x61, 0x75, 0x64, 0x69, 0x6f, 0x20, 0x3d, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x09, 0x6d, 0x69, 0x78, 0x77, 0x69, 0x74, 0x68, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x3d, @@ -988,9 +989,12 @@ const unsigned char boot_lua[] = 0x09, 0x09, 0x22, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x22, 0x2c, 0x0a, 0x09, 0x09, 0x22, 0x6d, 0x61, 0x74, 0x68, 0x22, 0x2c, 0x0a, 0x09, 0x09, 0x22, 0x70, 0x68, 0x79, 0x73, 0x69, 0x63, 0x73, 0x22, 0x2c, 0x0a, + 0x09, 0x09, 0x22, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x22, 0x2c, 0x0a, 0x09, 0x7d, 0x20, 0x64, 0x6f, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x63, 0x2e, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x5b, 0x76, 0x5d, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, + 0x09, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x28, 0x22, 0x6c, 0x6f, 0x61, 0x64, 0x69, 0x6e, 0x67, 0x3a, + 0x20, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x76, 0x29, 0x0a, 0x09, 0x09, 0x09, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x28, 0x22, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x22, 0x20, 0x2e, 0x2e, 0x20, 0x76, 0x29, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,