Skip to content

Instantly share code, notes, and snippets.

@jasonjohnson
Last active December 29, 2015 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonjohnson/7635835 to your computer and use it in GitHub Desktop.
Save jasonjohnson/7635835 to your computer and use it in GitHub Desktop.
import os
import sys
os.environ["PYSDL2_DLL_PATH"] = "lib"
import sdl2
import sdl2.ext as ext
def run():
ext.init()
window = ext.Window("Demo", size=(800, 600))
window.show()
running = True
while running:
events = ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
window.refresh()
return 0
if __name__ == "__main__":
sys.exit(run())
# -*- mode: python -*-
def data_toc(d):
import os
return [(f, f, 'DATA') for f in ["%s/%s" % (d,f) for f in os.listdir(d)]]
a = Analysis(['demo.py'], pathex=['.'])
pyz = PYZ(a.pure)
exe = EXE(pyz, a.scripts, exclude_binaries=True, name='demo.exe',
debug=False, console=False)
coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, data_toc('lib'),
name='demo')

Quick Game/Simulation Bootstrap

Downloads

Python 2.7

http://python.org/download/

Install a 32-bit version of Python 2.7. At the time of writing, this was 2.7.6. The default installation location is sufficient. Make sure the "python" command is usable from the command line.

pywin32

http://sourceforge.net/projects/pywin32/

pywin32 provides a graphical installer which will detect your Python installation directory. No options should be presented, and it should install without issue. This library is required to properly bundle our application for distrubition.

PyInstaller

http://www.pyinstaller.org/

PyInstaller will be used later to bundle our application. If Python has been added to your system path correctly, this should work fine.

python setup.py install

py-sdl2

https://bitbucket.org/marcusva/py-sdl2/downloads

py-sdl2 provides ctypes bindings to SDL2. This should install fine without having the actual SDL2 libraries present on your system.

python setup.py install

SDL2

http://www.libsdl.org/download-2.0.php

http://www.libsdl.org/projects/SDL_image/

http://www.libsdl.org/projects/SDL_mixer/

http://www.libsdl.org/projects/SDL_ttf/

Download all Windows 32-bit runtime libraries. Once unzipped, copy all of the DLL's into a folder named lib under your project root - as a sibling to demo.py.

Development

During development, there's nothing abnormal about this setup. The SDL2 and Python documentation should be very nearly sufficient to build your demonstration. To run your program, simply use:

python demo.py

Packaging

Now that our demo is running, we can package everything up by running the PyInstaller script.

pyinstaller demo.spec

The spec file will locate all DLL's for us and scan our demo.py file for other included libraries. It will also detect and bundle the required Microsoft runtime manifest and DLL's. Once built, you can reach into the dist directory, create an archive of the demo folder and hand it to anyone running a newish version of Windows.

Directory Structure

The final directory structure will look like this:

/my_demo
    demo.py
    demo.spec
    /build
        /demo
            demo.exe.manifest
            ...
    /dist
        /demo
            /include
            /lib
                SDL2.dll
                SDL2_image.dll
                ...
            _ctypes.pyd
            _hashlib.pyd
            ...
            demo.exe
    /lib
        SDL2.dll
        SDL2_image.dll
        ...    

The important directory here is: /my_demo/dist/demo, which you will zip and distribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment