-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/.internal | ||
/build | ||
.externalToolBuilders | ||
.DS_Store | ||
Thumbs.db | ||
.lock-wscript | ||
*.pyc | ||
.project | ||
.cproject | ||
builtins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# template-native-extension | ||
# Native extension template | ||
This template contains the basic setup for creation of a Defold native extension. | ||
|
||
You can learn more about native extensions in the [official manual](https://www.defold.com/manuals/extensions/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function init(self) | ||
local s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
local rot_s = myextension.rot13(s) | ||
print(rot_s) --> nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[bootstrap] | ||
main_collection = /example/example.collectionc | ||
|
||
[script] | ||
shared_state = 1 | ||
|
||
[display] | ||
width = 960 | ||
height = 640 | ||
|
||
[project] | ||
title = myextension | ||
|
||
[library] | ||
include_dirs = myextension | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mouse_trigger { | ||
input: MOUSE_BUTTON_1 | ||
action: "touch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# C++ symbol in your extension | ||
name: "MyExtension" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// myextension.cpp | ||
// Extension lib defines | ||
#define LIB_NAME "MyExtension" | ||
#define MODULE_NAME "myextension" | ||
|
||
// include the Defold SDK | ||
#include <dmsdk/sdk.h> | ||
#include <stdlib.h> | ||
|
||
static int Rot13(lua_State* L) | ||
{ | ||
int top = lua_gettop(L); | ||
|
||
// Check and get parameter string from stack | ||
const char* str = luaL_checkstring(L, 1); | ||
|
||
// Allocate new string | ||
int len = strlen(str); | ||
char *rot = (char *) malloc(len + 1); | ||
|
||
// Iterate over the parameter string and create rot13 string | ||
for(int i = 0; i <= len; i++) { | ||
const char c = str[i]; | ||
if((c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'm')) { | ||
// Between A-M just add 13 to the char. | ||
rot[i] = c + 13; | ||
} else if((c >= 'N' && c <= 'Z') || (c >= 'n' && c <= 'z')) { | ||
// If rolling past 'Z' which happens below 'M', wrap back (subtract 13) | ||
rot[i] = c - 13; | ||
} else { | ||
// Leave character intact | ||
rot[i] = c; | ||
} | ||
} | ||
|
||
// Put the rotated string on the stack | ||
lua_pushstring(L, rot); | ||
|
||
// Free string memory. Lua has a copy by now. | ||
free(rot); | ||
|
||
// Assert that there is one item on the stack. | ||
assert(top + 1 == lua_gettop(L)); | ||
|
||
// Return 1 item | ||
return 1; | ||
} | ||
|
||
// Functions exposed to Lua | ||
static const luaL_reg Module_methods[] = | ||
{ | ||
{"rot13", Rot13}, | ||
{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) | ||
{ | ||
return dmExtension::RESULT_OK; | ||
} | ||
|
||
dmExtension::Result InitializeMyExtension(dmExtension::Params* params) | ||
{ | ||
// Init Lua | ||
LuaInit(params->m_L); | ||
printf("Registered %s Extension\n", MODULE_NAME); | ||
return dmExtension::RESULT_OK; | ||
} | ||
|
||
dmExtension::Result AppFinalizeMyExtension(dmExtension::AppParams* params) | ||
{ | ||
return dmExtension::RESULT_OK; | ||
} | ||
|
||
dmExtension::Result FinalizeMyExtension(dmExtension::Params* params) | ||
{ | ||
return dmExtension::RESULT_OK; | ||
} | ||
|
||
|
||
// 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, 0, 0, FinalizeMyExtension) |