| | |
HGE and Python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Good day.
I've been trying to port HGE (http://hge.relishgames.com) to Python for more than 4 months now...
HGE is a hardware accelerated 2D game engine.
It comes with the source and examples. In the folder "include", you can find "hge.h", the file that i am talking about in all the post.
#
I tried to load the DLL functions with Python Ctypes like this :
But i get this error : "Procedure called with not enough arguments (4 bytes missing) or wrong calling convention".
The call should be done with hgeCreate(HGE_VERSION) and the constant is defined as "#define HGE_VERSION 0x180"...
Number 0x180 means 384 in Python. I don't mean what it means in C.
So i am stuck.
I also tried to modify the "hge.h" file on line 408, and export the rest of the classes...
But after compilation, i am not able to access them from Python "ctypes". They seem to remain invisible. Perhaps i am not doing it right?...
#
I tried Cython.
I tried to load "hge.h" in Cython and use the structures.
The first error i get is at line 173: "typedef bool (*hgeCallback)();". I am not that good in C/C++ to understand what it means. So i commented it...
The second big error is at line 408: "extern "C" { EXPORT HGE * CALL hgeCreate(int ver); }". This should be the DLL export. I comented that too...
I used this code in Cython, loading from "hge.h":
And i get this errors:
Then i tried to define hgeVertex in Cython like:
But i get the exact same errors.
If i comment all the structures hgeVertex and Triple and Quad and the HGE class, the Cython file compiles !... But it's not useful at all.
My Cython is okay, i compiled a few programs before, so that's not a problem...
#
Then i tried Swig (and it worked with the examples )... But the problems with "hge,h" seem to be somewhat similar.
I call swig like this : "swig -c++ -python hge.i"
And the file "hge.i" contains:
And the error i get after commenting the HGE callback and DLL export is this : "hge.h(276): Error: Syntax error in input(3)." Line 276 is exactly the first call of HGE class : "virtual void CALL Release() = 0;".
#
I tried Boost.Python (and it works with embedding and extending examples), but i could't compile "hge.h" even after commenting the structures and classes. I have to write some wrapper code in "hge.h" and i am probably doing it completely wrong.
So i am stuck again...
I am not that good in neither C or Python.
Can anyone suggest any ideas? Please?
I really really really want to port HGE in Python. It's the greatest game engine i have ever seen. The particle engine is EXCELLENT and i need it.
Thank you in advance.
I've been trying to port HGE (http://hge.relishgames.com) to Python for more than 4 months now...
HGE is a hardware accelerated 2D game engine.
It comes with the source and examples. In the folder "include", you can find "hge.h", the file that i am talking about in all the post.
#
I tried to load the DLL functions with Python Ctypes like this :
Python Syntax (Toggle Plain Text)
>>> from ctypes import * >>> HGE = cdll.LoadLibrary("C:/hge181/hge") >>> HGE.hgeCreate(0x180)
The call should be done with hgeCreate(HGE_VERSION) and the constant is defined as "#define HGE_VERSION 0x180"...
Number 0x180 means 384 in Python. I don't mean what it means in C.
So i am stuck.
I also tried to modify the "hge.h" file on line 408, and export the rest of the classes...
Python Syntax (Toggle Plain Text)
__declspec (dllexport) hgeVertex; __declspec (dllexport) hgeTriple; __declspec (dllexport) hgeQuad; __declspec (dllexport) hgeInputEvent;
#
I tried Cython.
I tried to load "hge.h" in Cython and use the structures.
The first error i get is at line 173: "typedef bool (*hgeCallback)();". I am not that good in C/C++ to understand what it means. So i commented it...
The second big error is at line 408: "extern "C" { EXPORT HGE * CALL hgeCreate(int ver); }". This should be the DLL export. I comented that too...
I used this code in Cython, loading from "hge.h":
Python Syntax (Toggle Plain Text)
# file HGE.pyx cdef extern from "windows.h": pass cdef extern from "hge.h": pass
Python Syntax (Toggle Plain Text)
..\hge.h(215) : error C2061: syntax error : identifier 'hgeVertex' ..\hge.h(218) : error C2059: syntax error : '}' ..\hge.h(226) : error C2061: syntax error : identifier 'hgeVertex' ..\hge.h(229) : error C2059: syntax error : '}' ..\hge.h(274) : error C2061: syntax error : identifier 'HGE' ..\hge.h(274) : error C2059: syntax error : ';' ..\hge.h(275) : error C2449: found '{' at file scope (missing function header?) ..\hge.h(407) : error C2059: syntax error : '}'
Python Syntax (Toggle Plain Text)
struct hgeVertex: float x, y # screen position float z # Z-buffer depth 0..1 DWORD col # color float tx, ty # texture coordinates
If i comment all the structures hgeVertex and Triple and Quad and the HGE class, the Cython file compiles !... But it's not useful at all.
My Cython is okay, i compiled a few programs before, so that's not a problem...
#
Then i tried Swig (and it worked with the examples )... But the problems with "hge,h" seem to be somewhat similar.
I call swig like this : "swig -c++ -python hge.i"
And the file "hge.i" contains:
Python Syntax (Toggle Plain Text)
/* File : hge.i */ %module hge %{ #include "hge.h" %} /* Let's just grab the original header file here */ %include "hge.h"
#
I tried Boost.Python (and it works with embedding and extending examples), but i could't compile "hge.h" even after commenting the structures and classes. I have to write some wrapper code in "hge.h" and i am probably doing it completely wrong.
Python Syntax (Toggle Plain Text)
BOOST_PYTHON_MODULE(hge) { using namespace boost::python; class_<hgeVertex> base("hgeVertex"); }
I am not that good in neither C or Python.
Can anyone suggest any ideas? Please?
I really really really want to port HGE in Python. It's the greatest game engine i have ever seen. The particle engine is EXCELLENT and i need it.
Thank you in advance.
Last edited by Prahaai; Dec 11th, 2008 at 8:11 am. Reason: spelling
•
•
Join Date: Feb 2008
Posts: 37
Reputation:
Solved Threads: 5
Im not into ctypes yet but I do know that in "#define HGE_VERSION 0x180" 0x180 is not just for python. It is a hex number so it should be represented in most if not all programming languages. All else fails you could do the conversion your self i guess...
"There are only two industries that refer to their customers as 'users'." (Edward Tufte)
"There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."(C.A.R. Hoare)
"There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."(C.A.R. Hoare)
•
•
Join Date: May 2008
Posts: 640
Reputation:
Solved Threads: 104
I did some initial review, and I was able to load the library and call hgeCreate using ctypes:
Run this from a directory with the hge.dll in it, I was in the hge181 directory.
But from there, it would seem ctypes does not support making calls into c++ classes, and that appears to be how you interface to the rest of hge. (hgeCreate returns a pointer to an instance of the class)
Run this from a directory with the hge.dll in it, I was in the hge181 directory.
python Syntax (Toggle Plain Text)
from ctypes import * hge = windll.hge hgeCreate = hge.hgeCreate hgeCreate.restype = c_void_p hge = hgeCreate(0x180)
But from there, it would seem ctypes does not support making calls into c++ classes, and that appears to be how you interface to the rest of hge. (hgeCreate returns a pointer to an instance of the class)
I have problems too in learning ctypes. They are powerful way to port C++ power into python, but seems to lack begginers tutorial which is very sad. For man like me with little knowledge in c++, it is big burded.
Anyway, so far what I know is that, in order to rightly call any function in Python using ctypes are
1. define result type using restype
2. define arguments types using argtype
3. call the function with right arguments
But so far I haven't been successful to do anything useful with ctypes. I believe sometimes I will do!
Anyway, so far what I know is that, in order to rightly call any function in Python using ctypes are
1. define result type using restype
2. define arguments types using argtype
3. call the function with right arguments
But so far I haven't been successful to do anything useful with ctypes. I believe sometimes I will do!
Good day.
Thank you so much for your answers!
I managed to call HGE dll the way you told:
>>> HGE = windll.LoadLibrary("C:/hge181/hge")
>>> HGE.hgeCreate(0x180)
But it's not helpful. It would mean that i have to re-create all the header files from "include" directory in python, and all helper functions from the "libs" to be able to use hge in the same way as the original.
So ctypes is not what i need. Anyway, it's a big step!
I also tried swig, a lot.
A lot, a lot, a lot of examples. Hacking examples and stuff.
And i managed to compile "hge.h" header and call the structs and HGE main class from python. I don't seem to be able to acces the "enums", but that's not a big problem.
How i did that, if anyone wants to know: There are 4 files.
- There is "example.cxx", that includes "hge.h" in the proper C++ way and defines a HGE* hge=0 just like in the tutorials.
- Then is "hge.h", the original file, with ONE line commented: line 408 with the DLL EXPORT. Ah, and one more thing. I had to delete ALL "CALL" keywords from HGE class, because if one CALL appears, the swig compiler flags the line as error. So, for now, no stdcall.
- Then is "example.i", with "#include "hge.h"" and "%include "hge.h"". Simple as that.
- And then, there is setup.py. "example_module = Extension('_example', sources=['example_wrap.cxx', 'example.cxx'],)"
That's it. I compile it all like this:
swig -c++ -python example.i
setup build_ext --inplace
So it's a huge step for me. The compiler i have in my system is mingw-3.4.5. I don't know if it's good for this. Maybe i should try a MS compiler instead?
Next things i want to do:
- try to compile the project with a MS compiles. Visual studio 7.1 or somethin.
- learn if i can do stdcalls from swig.
- try to compile one normal hge tutorial WITHOUT including "hge.lib" and "hgehelp.lib". All functions defined there can be included just like normal headers, the source is there and i compiled the libs myself. I need this step because i don't think i can include lib files in a swig project.
- try to do all i did in swig, with Cython. It's pretty easy to use and faster from what i know.
That's all for now.
It would be of great help if anyone could tell how to deal with the __stdcall thingy...
Thank you very much.
Thank you so much for your answers!
I managed to call HGE dll the way you told:
>>> HGE = windll.LoadLibrary("C:/hge181/hge")
>>> HGE.hgeCreate(0x180)
But it's not helpful. It would mean that i have to re-create all the header files from "include" directory in python, and all helper functions from the "libs" to be able to use hge in the same way as the original.
So ctypes is not what i need. Anyway, it's a big step!
I also tried swig, a lot.
A lot, a lot, a lot of examples. Hacking examples and stuff.
And i managed to compile "hge.h" header and call the structs and HGE main class from python. I don't seem to be able to acces the "enums", but that's not a big problem.
How i did that, if anyone wants to know: There are 4 files.
- There is "example.cxx", that includes "hge.h" in the proper C++ way and defines a HGE* hge=0 just like in the tutorials.
- Then is "hge.h", the original file, with ONE line commented: line 408 with the DLL EXPORT. Ah, and one more thing. I had to delete ALL "CALL" keywords from HGE class, because if one CALL appears, the swig compiler flags the line as error. So, for now, no stdcall.
- Then is "example.i", with "#include "hge.h"" and "%include "hge.h"". Simple as that.
- And then, there is setup.py. "example_module = Extension('_example', sources=['example_wrap.cxx', 'example.cxx'],)"
That's it. I compile it all like this:
swig -c++ -python example.i
setup build_ext --inplace
So it's a huge step for me. The compiler i have in my system is mingw-3.4.5. I don't know if it's good for this. Maybe i should try a MS compiler instead?
Next things i want to do:
- try to compile the project with a MS compiles. Visual studio 7.1 or somethin.
- learn if i can do stdcalls from swig.
- try to compile one normal hge tutorial WITHOUT including "hge.lib" and "hgehelp.lib". All functions defined there can be included just like normal headers, the source is there and i compiled the libs myself. I need this step because i don't think i can include lib files in a swig project.
- try to do all i did in swig, with Cython. It's pretty easy to use and faster from what i know.
That's all for now.
It would be of great help if anyone could tell how to deal with the __stdcall thingy...
Thank you very much.
There is an evil pixie scratching my neck.
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: simple wxpython app
- Next Thread: Call function like normal built-in.
Views: 739 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






