C++ SDL Game integration with Python

Started by
5 comments, last by Xirion 11 years, 8 months ago
Greetings!

I already know how to make games with C++ and SDL (already done a few)
but i've been reading a lot about learning a scripting code would be helpful,
so i was thinking on learning python but my question is, how do i actually
use a python script in my game development with C++ and SDL?
are there some tutorials out there so i can take a look on them?

I hope you can help me with that.

Thank you.

PS: Sorry for my bad english

Advertisement
There is two way to use python with C++ but in both cases you will have to expose your c++ API to python by generating bindings (using swig for example).


1) Extending python: you install your API bindings as a standard python extension and use it from the python interpreter: your program will start from python where you will call your C++ code. This method allow you to use the standard python packages and use some powerful debuggers to debug your python code.

2) Embedding python; you will integrate python inside your application by embedding the python interpreter. You will have to link and use the python c api from your C++ code to call your python scripts. With this method you will have a more specific python interpreter without stuff you don't need but you will have more problems for debugging your python scripts (I have never seen any decent debugger for debugging embeded python).


I personnaly choose the first method: I write my high level code in python and low level performance critical code in c++. That way I can use standard python modules and debug easily my application. (For debugging the c++ code I compile my bindings with debug information and attach to the python process from my IDE ).

Here are a few links about embedding and extending:

http://docs.python.org/extending/

http://twistedmatrix...t/extendit.html

And here is an so discussion about the different way to create python bindings:

http://stackoverflow...-swig-or-cython

I choose swig because of the great documentation, the stl support (std::shared_ptr,...) and the good integration with cmake.
Hi, thank you for the quick response.
I got another question though. What can i actually do using python scripts
that help me in game develop? What is the most common use of this in
2D game development?
You have all the benefits from a scripting language

- no compile time: stop your app, change a variable value in a script (let say the position of quad) and restart it to see the change immediately. You may even reload module at runtime if you detect a change in the script (but that is up to you to detect changes)

- don't bother with memory management and other low level stuffs, just think about your high level gameplay code

- easy to learn: even artists could write scripts (but keep in mind that real programmers will probably do a better job at scripting than an artist)

Using a scripting language just improve your productivity!

Also, if you decide to go with extending python, you will find all the tools to create tools for your engine. Using PyQt is an effective way to make GUI tools such as an editor for your engine.
Hi! Thank you again!
Also do you know by any chance a tutorial or/and example on this topic?
I work with my girlfriend (Graphic Designer) and i really want her to enter the
world of programming (so i help her in illustration and she helps me on programming).
I really like the part you say that artist can script.

Greetings.
What kind of tutorial are you interested in? About c++ to python or just about python?

I'm afraid there are not a lots of tutorial about how to expose your engine to python (except for simple things), you will most likely have to choose a bindings generator, learn how it works and what you can do with it. Also read the python documentation about extending and embedding and check the Abwood's Coding Notes to have a look at how things could be done.

For learning python, there is a lots of good tutorials on the web, just google "learn python" to find them.

If you speak french, I recommend reading this book. It is released under the creative commons license ( free ) and explains the fundementals very well.
Yes i was actually hoping for something on C++ plus Python.
I'll take a look on the links you provided. I'm reading a book on Python fundamentals wich seems very simple and easy to understand, sadly it doesn't cover what i'm asking for. And nope, i don't speak french, i'm currently improving my english and struggling with my japanese.

I really appreciate your time and your advices.

Thank you very much.

This topic is closed to new replies.

Advertisement