Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Aug 6, 2019
1 parent d79d565 commit c15ca63
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitignore
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
5 changes: 4 additions & 1 deletion README.md
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/).
37 changes: 37 additions & 0 deletions example/example.collection
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
}
}
30 changes: 30 additions & 0 deletions example/example.script
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
16 changes: 16 additions & 0 deletions game.project
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

4 changes: 4 additions & 0 deletions input/game.input_binding
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mouse_trigger {
input: MOUSE_BUTTON_1
action: "touch"
}
2 changes: 2 additions & 0 deletions myextension/ext.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# C++ symbol in your extension
name: "MyExtension"
97 changes: 97 additions & 0 deletions myextension/src/myextension.cpp
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)

0 comments on commit c15ca63

Please sign in to comment.