Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Commit

Permalink
Add pure Python usage example for issue #3
Browse files Browse the repository at this point in the history
q3py_hello.py uses ctypes to implement a part of the Quake 3
`game` module by printing hello world.
  • Loading branch information
robo9k committed Jun 7, 2014
1 parent 74ad2f6 commit bc62c3a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ EXCLUDE_SYMBOLS =
# directories that contain example code fragments that are included (see
# the \include command).

EXAMPLE_PATH = $(SRCDIR)/src
EXAMPLE_PATH = $(SRCDIR)/doc/examples

# If the value of the EXAMPLE_PATH tag contains directories, you can use the
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
Expand Down
45 changes: 45 additions & 0 deletions doc/examples/q3py_hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import q3py
import ctypes


# Game module initialization function.
# defined in ioq3/code/game/g_public.h
GAME_INIT = 0

# Engine system trap to print errors from game module.
# Defined in ioq3/code/game/g_public.h
G_ERROR = 1


# Compare with trap_Error() from ioq3/code/game/g_syscalls.c
def qerror(msg):
c_msg = ctypes.create_string_buffer(msg)

return q3py.syscall(G_ERROR, ctypes.addressof(c_msg))


# Compare with G_InitGame() from ioq3/code/game/g_main.c
def init_game(level_time, random_seed, restart):
print("Python init_game(level_time={level_time}, "
"random_seed={random_seed}, "
"restart={restart})".format(level_time=level_time,
random_seed=random_seed,
restart=restart))
qerror(b"Hello, Quake 3!")


# Compare with vmMain() from ioq3/code/game/g_main.c
def vm_main(cmd, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8,
arg9, arg10, arg11):

if (cmd == GAME_INIT):
init_game(arg0, arg1, bool(arg2))

return -1


# Related to dllEntry() in ioq3/code/game/g_syscalls.c
def dll_entry():
print("Python dll_entry() called")

return vm_main
11 changes: 11 additions & 0 deletions doc/mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ TODO: Describe how quake3.exe <-> q3py.so <-> libpython.so <-> my.py works

For instructions on how to download, compile, install, configure
and run q3py take a look at the \ref manual "user manual".

\example q3py_hello.py
To run this example;
\code
Q3PY_ENTRYPOINT='q3py_hello:dll_entry' PYTHONPATH=. ioq3ded +set vm_game 0 +map q3dm6
\endcode

Which sets this Python module and its `dll_entry()` method as q3py's
entry point, instructs Python to look for modules in the current
directory, starts the ioquake3 dedicated server with native game module
and loads an arbitrary map.
*/

0 comments on commit bc62c3a

Please sign in to comment.