Browse Source

Native Plugin Example

JoshEngebretson 10 years ago
parent
commit
1fb6363730

+ 2 - 0
NativePluginExample/.gitignore

@@ -0,0 +1,2 @@
+Native-build/*
+*.dll

+ 28 - 0
NativePluginExample/Native/CMakeLists.txt

@@ -0,0 +1,28 @@
+#set the native sdk folder, could also be an environment variable, stored locally, etc
+set (NATIVESDK "C:/Dev/atomic/AtomicGameEngine/Bin/NativeSDK")
+
+set (PLUGINNAME MyNativePlugin) 
+
+set (SOURCEFILES MyNativePlugin.cpp)
+
+if (MSVC)
+
+	# compile with static runtime, so our plugin doesn't depend on having some MSVC runtime installed
+	
+	set(CompilerFlags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE)
+
+	foreach(CompilerFlag ${CompilerFlags})
+    	string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
+	endforeach()
+
+endif()
+
+include_directories("${NATIVESDK}/Windows")
+
+add_library(${PLUGINNAME} SHARED ${SOURCEFILES})
+
+target_link_libraries(${PLUGINNAME} ${NATIVESDK}/Windows/x64/AtomicPlugin.lib)
+
+add_custom_command (TARGET ${PLUGINNAME} POST_BUILD
+	COMMAND ${CMAKE_COMMAND}
+	ARGS -E copy \"$<TARGET_FILE:${PLUGINNAME}>\" \"${CMAKE_SOURCE_DIR}/../Resources/Plugins/Windows/x64/$<TARGET_FILE_NAME:${PLUGINNAME}>\")

+ 37 - 0
NativePluginExample/Native/MyNativePlugin.cpp

@@ -0,0 +1,37 @@
+#include "AtomicPlugin.h"
+#ifdef _MSC_VER
+    #include <windows.h>
+    #define EXPORT_API __declspec(dllexport)
+#else
+    #define EXPORT_API
+#endif
+
+extern "C" 
+{	
+	static int js_getAnswer(duk_context* ctx)
+	{
+		duk_push_number(ctx, 42);
+		return 1;
+		
+	}
+
+	// plugin init is a duk cfunction itself, so we can pcall the import	
+	int EXPORT_API atomic_plugin_init(duk_context* ctx)
+	{
+		// exports object at 0
+        if (!duk_is_object(ctx, 0))
+		{
+			duk_push_boolean(ctx, 0);		
+			return 1;			
+		}
+
+		// export out native functions
+		duk_push_c_function(ctx, js_getAnswer, 0);
+		duk_put_prop_string(ctx, -2, "getAnswer");
+		
+		// and return success
+		duk_push_boolean(ctx, 1);		
+		return 1;
+	}
+	
+}

+ 1 - 1
NativePluginExample/Resources/Components/Star.js

@@ -10,7 +10,7 @@ function start() {
 }
 }
 
 
 function update(timeStep) {	
 function update(timeStep) {	
-
+	
 	node.roll(timeStep * 100);
 	node.roll(timeStep * 100);
 
 
 }
 }

+ 7 - 2
NativePluginExample/Resources/Scripts/main.js

@@ -1,13 +1,18 @@
-
 // This script is the main entry point of the game
 // This script is the main entry point of the game
+
 require("AtomicGame");
 require("AtomicGame");
 
 
-Atomic.game.init(start, update);
+// bring in our native plugin
+var NativePlugin = require("MyNativePlugin");
 
 
+Atomic.game.init(start, update);
 
 
 // called at the start of play
 // called at the start of play
 function start() {
 function start() {
 
 
+	// call our native method
+	print("The answer is: ", NativePlugin.getAnswer());
+
 	var game = Atomic.game;
 	var game = Atomic.game;
 
 
 	// create a 2D scene
 	// create a 2D scene