Skip to content

Commit

Permalink
SWIG: add C# bindings component
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed Dec 13, 2018
1 parent eaee2d2 commit f03b99b
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CMake/FeatureSummary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ endif ()
if (OGRE_BUILD_COMPONENT_JAVA)
set(_components "${_components} + Java/ JNI [BETA]\n")
endif ()
if (OGRE_BUILD_COMPONENT_CSHARP)
set(_components "${_components} + C#/ .NET [BETA]\n")
endif ()
if (OGRE_BUILD_COMPONENT_RTSHADERSYSTEM)
set(_components "${_components} + RTShader System\n")

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ option(OGRE_BUILD_COMPONENT_HLMS "Build HLMS component" TRUE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_BITES "Build OgreBites component" TRUE "OGRE_BUILD_COMPONENT_OVERLAY" FALSE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_PYTHON "Build Python bindings" TRUE "NOT OGRE_STATIC" FALSE)
option(OGRE_BUILD_COMPONENT_JAVA "Build Java (JNI) bindings" TRUE)
cmake_dependent_option(OGRE_BUILD_COMPONENT_CSHARP "Build Csharp bindings" TRUE "NOT ANDROID" FALSE)
option(OGRE_BUILD_COMPONENT_RTSHADERSYSTEM "Build RTShader System component" TRUE)
cmake_dependent_option(OGRE_BUILD_RTSHADERSYSTEM_CORE_SHADERS "Build RTShader System FFP core shaders" TRUE "OGRE_BUILD_COMPONENT_RTSHADERSYSTEM" FALSE)
cmake_dependent_option(OGRE_BUILD_RTSHADERSYSTEM_EXT_SHADERS "Build RTShader System extensions shaders" TRUE "OGRE_BUILD_COMPONENT_RTSHADERSYSTEM" FALSE)
Expand Down
4 changes: 3 additions & 1 deletion Components/Bites/include/OgreBites.i
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ JNIEnv* OgreJNIGetEnv();
%ignore OgreBites::TrayManager::getNumWidgets(TrayLocation);
%ignore OgreBites::TrayManager::getWidgetIterator;
%ignore OgreBites::SelectMenu::getItemsCount;
#ifndef SWIGCSHARP
%include "OgreTrays.h"
%include "OgreAdvancedRenderControls.h"
%include "OgreAdvancedRenderControls.h"
#endif
4 changes: 4 additions & 0 deletions Components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ if (OGRE_BUILD_COMPONENT_PYTHON)
add_subdirectory(Python)
endif()

if (OGRE_BUILD_COMPONENT_CSHARP)
add_subdirectory(Csharp)
endif()

if (OGRE_BUILD_COMPONENT_JAVA)
add_subdirectory(Java)
endif()
48 changes: 48 additions & 0 deletions Components/Csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
find_package(SWIG 3.0.8)

if(NOT SWIG_FOUND)
set(OGRE_BUILD_COMPONENT_CSHARP OFF CACHE BOOL "" FORCE)
message(WARNING "Csharp Component disabled because SWIG or Csharp was not found")
return()
endif()

include_directories("${PROJECT_BINARY_DIR}/include" "${PROJECT_SOURCE_DIR}/OgreMain/include")
include(${SWIG_USE_FILE})

if(MSVC)
# TODO: Set MSVC flags
add_definitions(-DMS_NO_COREDLL)
else()
add_definitions(-Wno-strict-aliasing -Wno-cast-qual -Wno-shadow -Wno-missing-declarations)
endif()

set(CMAKE_SWIG_FLAGS -w401,314 -namespace org.ogre)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/csharp/")
set(CMAKE_SWIG_OUTDIR "${PROJECT_BINARY_DIR}/csharp/")

set_source_files_properties(../../OgreMain/include/Ogre.i PROPERTIES CPLUSPLUS ON)
set(SWIG_INPUT_MODULES ../../OgreMain/include/Ogre.i)

if(OGRE_BUILD_COMPONENT_RTSHADERSYSTEM)
set_source_files_properties(../RTShaderSystem/include/OgreRTShader.i PROPERTIES CPLUSPLUS ON)
list(APPEND SWIG_INPUT_MODULES ../RTShaderSystem/include/OgreRTShader.i)
endif()

if(OGRE_BUILD_COMPONENT_OVERLAY)
set_source_files_properties(../Overlay/include/OgreOverlay.i PROPERTIES CPLUSPLUS ON)
list(APPEND SWIG_INPUT_MODULES ../Overlay/include/OgreOverlay.i)
endif()

if(OGRE_BUILD_COMPONENT_BITES)
if(SDL2_FOUND)
include_directories(${SDL2_INCLUDE_DIR})
endif()

set_source_files_properties(../Bites/include/OgreBites.i PROPERTIES CPLUSPLUS ON)
list(APPEND SWIG_INPUT_MODULES ../Bites/include/OgreBites.i)
endif()

swig_add_module(libOgre csharp ${SWIG_INPUT_MODULES})
SWIG_LINK_LIBRARIES(libOgre OgreBites OgreOverlay OgreRTShaderSystem OgreMain)
install(TARGETS ${SWIG_MODULE_libOgre_REAL_NAME} LIBRARY DESTINATION lib/)
4 changes: 4 additions & 0 deletions Components/Overlay/include/OgreOverlay.i
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

#define _OgreOverlayExport

#ifdef SWIGCSHARP
%csmethodmodifiers Ogre::OverlaySystem::eventOccurred "public";
#endif

%include "OgreOverlayPrerequisites.h"
%shared_ptr(Ogre::Font);
%include "OgreFont.h"
Expand Down
40 changes: 39 additions & 1 deletion OgreMain/include/Ogre.i
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ JNIEnv* OgreJNIGetEnv() {

#ifdef SWIGJAVA
#define REPRFUNC toString
#elif defined(SWIGCSHARP)
#define REPRFUNC ToString
#else
#define REPRFUNC __repr__
#endif
Expand All @@ -134,6 +136,42 @@ JNIEnv* OgreJNIGetEnv() {
%ignore *::getType;
#endif

#ifdef SWIGCSHARP
%ignore Ogre::TextureUsage;
%ignore Ogre::GpuConstantType;
%ignore Ogre::Capabilities;
%csmethodmodifiers *::ToString "public override";
// wrong "override" because of multiple inheritance
%csmethodmodifiers *::getMaterial "public";
%csmethodmodifiers *::getSquaredViewDepth "public";
%csmethodmodifiers *::getWorldTransforms "public";
%csmethodmodifiers *::getRenderOperation "public";
%csmethodmodifiers *::getLights "public";
%csmethodmodifiers *::queryResult "public";
%csmethodmodifiers *::createInstance "public";
%csmethodmodifiers *::loadingComplete "public";
%csmethodmodifiers *::getBoundingBox "public";
%csmethodmodifiers *::getBoundingRadius "public";
%csmethodmodifiers *::getMovableType "public";
%csmethodmodifiers *::visitRenderables "public";
%csmethodmodifiers *::loadResource "public";
%csmethodmodifiers *::createAnimation "public";
%csmethodmodifiers *::getAnimation "public";
%csmethodmodifiers *::hasAnimation "public";
%csmethodmodifiers *::removeAnimation "public";
%csmethodmodifiers *::getNumAnimations "public";
%csmethodmodifiers *::getAnimation "public";
%csmethodmodifiers *::_notifyCurrentCamera "public";
%csmethodmodifiers *::_updateRenderQueue "public";
%csmethodmodifiers *::_notifyAttached "public";
%csmethodmodifiers *::setRenderQueueGroup "public";
%csmethodmodifiers *::setRenderQueueGroupAndPriority "public";
%csmethodmodifiers *::getTypeFlags "public";
%csmethodmodifiers *::viewportDestroyed "public";
%csmethodmodifiers *::viewportDimensionsChanged "public";
%csmethodmodifiers *::viewportCameraChanged "public";
#endif

// connect operator[] to __getitem__
%feature("python:slot", "sq_item", functype="ssizeargfunc") *::operator[];
%rename(__getitem__) *::operator[];
Expand Down Expand Up @@ -286,7 +324,7 @@ SHARED_PTR(HardwareBuffer);
%include "OgreHardwareBuffer.h"
%include "OgreParticleIterator.h"

#ifndef SWIGJAVA
#ifdef SWIGPYTHON
%ignore std::vector<Ogre::ParameterDef>::resize; // non default constructible
%ignore std::vector<Ogre::ParameterDef>::vector;
%template(ParameterList) std::vector<Ogre::ParameterDef>;
Expand Down
16 changes: 16 additions & 0 deletions Samples/Csharp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Build instructions for Mono on Ubuntu

1. inside `build/csharp/`
```
mcs -target:library -out:Ogre.dll *.cs
```
2. copy `Ogre.dll` and `libOgre.so` to current directory
3. compile the sample
```
mcs example.cs -r:Ogre.dll
```
4. copy `build/bin/resources.cfg` to current directory
5. to start the sample run
```
mono example.exe
```
73 changes: 73 additions & 0 deletions Samples/Csharp/example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using org.ogre;

public class KeyListener : InputListener
{
ApplicationContext ctx;

public KeyListener(ApplicationContext ctx)
{
this.ctx = ctx;
}

public override bool keyPressed(KeyboardEvent evt)
{
if (evt.keysym.sym == 27)
ctx.getRoot().queueEndRendering();
return true;
}
}

public class Example : ApplicationContext
{
InputListener listener;

public Example()
{
listener = new KeyListener(this);
}

public override void setup()
{
base.setup();
addInputListener(listener);

var root = getRoot();
var scnMgr = root.createSceneManager();

var shadergen = ShaderGenerator.getSingleton();
shadergen.addSceneManager(scnMgr); // must be done before we do anything with the scene

scnMgr.setAmbientLight(new ColourValue(.1f, .1f, .1f));

var light = scnMgr.createLight("MainLight");
var lightnode = scnMgr.getRootSceneNode().createChildSceneNode();
lightnode.setPosition(0f, 10f, 15f);
lightnode.attachObject(light);

var cam = scnMgr.createCamera("myCam");
cam.setAutoAspectRatio(true);
cam.setNearClipDistance(5);
var camnode = scnMgr.getRootSceneNode().createChildSceneNode();
camnode.attachObject(cam);

var camman = new CameraMan(camnode);
camman.setStyle(CameraStyle.CS_ORBIT);
camman.setYawPitchDist(new Radian(0), new Radian(0.3f), 15f);
addInputListener(camman);

var vp = getRenderWindow().addViewport(cam);
vp.setBackgroundColour(new ColourValue(.3f, .3f, .3f));

var ent = scnMgr.createEntity("Sinbad.mesh");
var node = scnMgr.getRootSceneNode().createChildSceneNode();
node.attachObject(ent);
}

static void Main()
{
var app = new Example();
app.initApp();
app.getRoot().startRendering();
app.closeApp();
}
}

0 comments on commit f03b99b

Please sign in to comment.