From 0746a2a88ef463f07f4946996bb416cff48d9a81 Mon Sep 17 00:00:00 2001 From: Nick Leeman Date: Tue, 23 Feb 2021 15:45:55 +0100 Subject: [PATCH] Initialized Extension --- .gitignore | 10 ++++ LICENSE.md | 21 +++++++ SIGW/ext.manifest | 2 + SIGW/src/myextension.cpp | 112 +++++++++++++++++++++++++++++++++++++ example/example.collection | 37 ++++++++++++ example/example.script | 30 ++++++++++ game.project | 16 ++++++ input/game.input_binding | 4 ++ 8 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 SIGW/ext.manifest create mode 100644 SIGW/src/myextension.cpp create mode 100644 example/example.collection create mode 100644 example/example.script create mode 100644 game.project create mode 100644 input/game.input_binding diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a32d29f --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/.internal +/build +.externalToolBuilders +.DS_Store +Thumbs.db +.lock-wscript +*.pyc +.project +.cproject +builtins \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..3e328f1 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Defold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/SIGW/ext.manifest b/SIGW/ext.manifest new file mode 100644 index 0000000..23bb57f --- /dev/null +++ b/SIGW/ext.manifest @@ -0,0 +1,2 @@ +# C++ symbol in your extension +name: "MyExtension" diff --git a/SIGW/src/myextension.cpp b/SIGW/src/myextension.cpp new file mode 100644 index 0000000..3cb4010 --- /dev/null +++ b/SIGW/src/myextension.cpp @@ -0,0 +1,112 @@ +// myextension.cpp +// Extension lib defines +#define LIB_NAME "MyExtension" +#define MODULE_NAME "myextension" + +// include the Defold SDK +#include + +static int Reverse(lua_State* L) +{ + // The number of expected items to be on the Lua stack + // once this struct goes out of scope + DM_LUA_STACK_CHECK(L, 1); + + // Check and get parameter string from stack + char* str = (char*)luaL_checkstring(L, 1); + + // Reverse the string + int len = strlen(str); + for(int i = 0; i < len / 2; i++) { + const char a = str[i]; + const char b = str[len - i - 1]; + str[i] = b; + str[len - i - 1] = a; + } + + // Put the reverse string on the stack + lua_pushstring(L, str); + + // Return 1 item + return 1; +} + +// Functions exposed to Lua +static const luaL_reg Module_methods[] = +{ + {"reverse", Reverse}, + {0, 0} +}; + +static void LuaInit(lua_State* L) +{ + int top = lua_gettop(L); + + // Register lua names + luaL_register(L, MODULE_NAME, Module_methods); + + lua_pop(L, 1); + assert(top == lua_gettop(L)); +} + +dmExtension::Result AppInitializeMyExtension(dmExtension::AppParams* params) +{ + dmLogInfo("AppInitializeMyExtension\n"); + return dmExtension::RESULT_OK; +} + +dmExtension::Result InitializeMyExtension(dmExtension::Params* params) +{ + // Init Lua + LuaInit(params->m_L); + dmLogInfo("Registered %s Extension\n", MODULE_NAME); + return dmExtension::RESULT_OK; +} + +dmExtension::Result AppFinalizeMyExtension(dmExtension::AppParams* params) +{ + dmLogInfo("AppFinalizeMyExtension\n"); + return dmExtension::RESULT_OK; +} + +dmExtension::Result FinalizeMyExtension(dmExtension::Params* params) +{ + dmLogInfo("FinalizeMyExtension\n"); + return dmExtension::RESULT_OK; +} + +dmExtension::Result OnUpdateMyExtension(dmExtension::Params* params) +{ + dmLogInfo("OnUpdateMyExtension\n"); + return dmExtension::RESULT_OK; +} + +void OnEventMyExtension(dmExtension::Params* params, const dmExtension::Event* event) +{ + switch(event->m_Event) + { + case dmExtension::EVENT_ID_ACTIVATEAPP: + dmLogInfo("OnEventMyExtension - EVENT_ID_ACTIVATEAPP\n"); + break; + case dmExtension::EVENT_ID_DEACTIVATEAPP: + dmLogInfo("OnEventMyExtension - EVENT_ID_DEACTIVATEAPP\n"); + break; + case dmExtension::EVENT_ID_ICONIFYAPP: + dmLogInfo("OnEventMyExtension - EVENT_ID_ICONIFYAPP\n"); + break; + case dmExtension::EVENT_ID_DEICONIFYAPP: + dmLogInfo("OnEventMyExtension - EVENT_ID_DEICONIFYAPP\n"); + break; + default: + dmLogWarning("OnEventMyExtension - Unknown event id\n"); + break; + } +} + +// Defold SDK uses a macro for setting up extension entry points: +// +// DM_DECLARE_EXTENSION(symbol, name, app_init, app_final, init, update, on_event, final) + +// MyExtension is the C++ symbol that holds all relevant extension data. +// It must match the name field in the `ext.manifest` +DM_DECLARE_EXTENSION(MyExtension, LIB_NAME, AppInitializeMyExtension, AppFinalizeMyExtension, InitializeMyExtension, OnUpdateMyExtension, OnEventMyExtension, FinalizeMyExtension) diff --git a/example/example.collection b/example/example.collection new file mode 100644 index 0000000..ff3608f --- /dev/null +++ b/example/example.collection @@ -0,0 +1,37 @@ +name: "main" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"example\"\n" + " component: \"/example/example.script\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + position { + x: 0.0 + y: 0.0 + z: 0.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale3 { + x: 1.0 + y: 1.0 + z: 1.0 + } +} diff --git a/example/example.script b/example/example.script new file mode 100644 index 0000000..53b4483 --- /dev/null +++ b/example/example.script @@ -0,0 +1,30 @@ +function init(self) + local s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + local reverse_s = myextension.reverse(s) + print(reverse_s) --> ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba +end + +function final(self) + -- Add finalization code here + -- Remove this function if not needed +end + +function update(self, dt) + -- Add update code here + -- Remove this function if not needed +end + +function on_message(self, message_id, message, sender) + -- Add message-handling code here + -- Remove this function if not needed +end + +function on_input(self, action_id, action) + -- Add input-handling code here + -- Remove this function if not needed +end + +function on_reload(self) + -- Add reload-handling code here + -- Remove this function if not needed +end diff --git a/game.project b/game.project new file mode 100644 index 0000000..d28c00d --- /dev/null +++ b/game.project @@ -0,0 +1,16 @@ +[bootstrap] +main_collection = /example/example.collectionc + +[script] +shared_state = 1 + +[display] +width = 960 +height = 640 + +[project] +title = SIGW-Extension + +[library] +include_dirs = SIGW + diff --git a/input/game.input_binding b/input/game.input_binding new file mode 100644 index 0000000..8ed1d4e --- /dev/null +++ b/input/game.input_binding @@ -0,0 +1,4 @@ +mouse_trigger { + input: MOUSE_BUTTON_1 + action: "touch" +}