Get SFML cross-platform typedefs in SWIG
I'm writing an C++ game and I'm connecting it with Lua. The tool I chose for this task was SWIG, since I want to make my game available to be written in python or some other language. I'm also using SFML 1.6 as the API for multimedia access. This game is also intended to be cross-platform compilable. I'm working at the moment with Xubuntu 11.04 for the first try of the project.
I've already wrapped 90%+ of the SFML API in my game, but when I tried to create开发者_开发百科 an new sf::Color
object in my Lua script (so that I could call an sf::RenderWindow::Clear(sf::Color)
method), my Lua Script accused that this call ...
renderWindow:Clear( sf.Color( 200, 0, 0 ) ) --Fill the screen with the color.
... tried to invoke method sf:Color( Uint8, Uint8, Uint8 )
; This warning alerted me that SWIG could not identify the SFML special typedef integers for cross-platform development that are defined in the <SFML/Config.hpp>
header file.
Now, in my SWIG Config.i
file I could simply write ...
typedef unsigned char Uint8; //Always correct
typedef unsigned short int Uint16; //Not always true
typedef unsigned int Uint32; //Not always true
... and when compiling my project in another platform, I could just write these typedefs attending to this new platform, but I found out that c's limits.h
header file contains some preprocessor defines for the size of each type of integer variable.
My main purpose is to create these typedefs in my SWIG script without having to worry in which platform (or compiler) I'm compiling my project.
Right now, my Config.i
SWIG file looks like this:
%{
#include <limits.h>
#include <climits.h>
#include <SFML/Config.hpp>
%}
%include <SFML/Config.hpp>
And my SWIG command for generating the wrappers is:
swig -c++ -lua -I/PathToSFML -I/PathToLimits ./SFML.i
I was hoping that SWIG could find the preprocessor defined variables in the limits.h
file, which is used by <SFML/Config.hpp>
but I couldn't make this work...
Does anyone have some tips on how to accomplish my goal ( dynamic typedefing for each platform ) or know a way to make swig get the preprocessor variables defined in limits.h
?
Any types defined in limits.h
are not standard and probably shouldn't be relied on. If you want cross-platform fixed size integer typedefs, the C++11 standard library provides the cstdint
header. This header gives you typedefs for signed and unsigned 8, 16, 32, and 64-bit integers: int32_t
, uint32_t
, int8_t
, and so on.
Most standard library implementations provided cstdint
as an extension before C++11, but if your implementation doesn't have it, Boost also provides it.
Thanks to user dauphic's answer, I tried to use <stdint.h>
in my example, and then I got this error message when running SWIG in my module:
/usr/include/stdint.h:44: Error: Syntax error in input(1).
Searching on Google resulted to me these two web pages:
- [Perl] a simple typedef question #1
- [Perl] a simple typedef question #2
The second one gave me the answer to this.
My final code looks like this right now:
%module Config
%include <stdint.i>
//namespace sf // For some reason, not working when the namespace is here...
//{ // Turns out that I don't need the sf anyway...
typedef int8_t Int8;
typedef uint8_t Uint8;
typedef int16_t Int16;
typedef uint16_t Uint16;
typedef uint32_t Int32;
typedef uint32_t Uint32;
//}
As you can see it, %include <stdint.i>
is a pre-defined module in SWIG since version 1.34 (not really sure about the version... read it somewhere and forgot) which is ready to be used.
精彩评论