Scrolling tile map example (C extension)

306 views
Skip to first unread message

Gary Daniels

unread,
Dec 9, 2011, 3:52:26 AM12/9/11
to pyglet-users

After experimenting with scrolling tile maps and deciding that I
couldn't meet my requirements with straight Python, I wrote a C
extension to create a vertex array based on numpy tileMap and texCoord
arrays.

it supports flipping, rotating and tinting (with alpha) individual
tiles and I get about 600fps while scrolling under Linux (8600 GT
video).

The source code is available at:

http://www.koltois.com/2011/12/09/tilemap-test-source-code/

My requirements included unlimited map size, animated tiles, on-the-
fly tinting, etc. and I couldn't get it to work at a decent speed with
Python or even Cython. The rest of the project I am working on is all
Python with Pyglet.

The way it's done also easily supports animated tiles (change the
tileMap ID in the array), but the above code does not implement it.

This code was hacked out of a larger project and is a bit messy and
probably not optimal, but I'm definitely open to suggestions for
improvements.

anatoly techtonik

unread,
Dec 12, 2011, 3:56:56 AM12/12/11
to pyglet...@googlegroups.com
It will be useful as an drop-in speedup extensions for pyglet. I don't know if it will require inventing a new API in pyglet, though.

Gary Daniels

unread,
Dec 12, 2011, 9:12:56 AM12/12/11
to pyglet...@googlegroups.com
On Mon, Dec 12, 2011 at 3:56 AM, anatoly techtonik <tech...@gmail.com> wrote:
It will be useful as an drop-in speedup extensions for pyglet. I don't know if it will require inventing a new API in pyglet, though.

I haven't really tested it enough to submit it for anything official. I posted it mainly as a working starting point for those who might need fast scrolling. As a complete OpenGL newb, I had considerable trouble piecing together the theories and suggestions on the mailing list and from Google searches to get working code.


Alejandro Castellanos

unread,
Dec 12, 2011, 10:35:42 AM12/12/11
to pyglet-users
I keep trying to build tileC with setup tools but it gives me an
error:


tileC.c:4:31: fatal error: numpy/arrayobject.h: No such file or
directory
compilation terminated


I do have numpy on my system, so I other than that I dunno what I'm
doing wrong; maybe the wrong command in setup tools?

On 12 dic, 07:12, Gary Daniels <wif...@gmail.com> wrote:

Anonymouse

unread,
Dec 12, 2011, 10:39:52 AM12/12/11
to pyglet...@googlegroups.com
Do you have the numpy source code/header files? Because it looks like that's what's missing.

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.


Gary Daniels

unread,
Dec 12, 2011, 11:08:57 AM12/12/11
to pyglet...@googlegroups.com

You probably already have the files and just need to change the include to point to them. For me, they were in:

C:\Python26\Lib\site-packages\numpy\core\include\numpy

For temporary testing, you could just copy that whole "numpy" folder to to "tileC" folder.

A previous post on that blog has an already compiled example packed in to a single exe that you can download. It also implements animation and multiple layers.

Christoph Gohlke

unread,
Dec 12, 2011, 11:18:26 AM12/12/11
to pyglet...@googlegroups.com

In setup.py, try `import numpy` and use

library_dirs = ['/usr/local/lib', numpy.get_include()],

Using Visual C compiler, the OpenGL library is 'opengl32' and
'windows.h' needs to be included before 'GL/gl.h' (in tileC.c). MSVC
doesn't know `0b00000001` constants; change them to decimal values.

Christoph

Gary Daniels

unread,
Dec 14, 2011, 6:46:19 PM12/14/11
to pyglet...@googlegroups.com

Did you get it to compile and work under Windows with vc?

I tried to build it with vc but get error messages about files missing and I ran out of patience tracking them down.

It works fine for me using MinGW and gcc.

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users+unsubscribe@googlegroups.com.

Ryexander

unread,
Dec 15, 2011, 4:03:05 PM12/15/11
to pyglet-users
the setup.py file uses paths specifically for lynix systems. I can set
the paths to find my local files properly but it's a pain. you may
want to fix that.

On Dec 14, 4:46 pm, Gary Daniels <wif...@gmail.com> wrote:
> Did you get it to compile and work under Windows with vc?
>
> I tried to build it with vc but get error messages about files missing and
> I ran out of patience tracking them down.
>
> It works fine for me using MinGW and gcc.
>

> On Mon, Dec 12, 2011 at 11:18 AM, Christoph Gohlke <cjgoh...@gmail.com>wrote:
>
>
>
>
>
>
>
> > On 12/12/2011 7:35 AM, Alejandro Castellanos wrote:
>
> >> I keep trying to build tileC with setup tools but it gives me an
> >> error:
>
> >> tileC.c:4:31: fatal error: numpy/arrayobject.h: No such file or
> >> directory
> >> compilation terminated
>
> >> I do have numpy on my system, so I other than that I dunno what I'm
> >> doing wrong; maybe the wrong command in setup tools?
>
> >> On 12 dic, 07:12, Gary Daniels<wif...@gmail.com>  wrote:
>

> >>> On Mon, Dec 12, 2011 at 3:56 AM, anatoly techtonik<techto...@gmail.com>*
> >>> *wrote:


>
> >>>  It will be useful as an drop-in speedup extensions for pyglet. I don't
> >>>> know if it will require inventing a new API in pyglet, though.
>
> >>> I haven't really tested it enough to submit it for anything official. I
> >>> posted it mainly as a working starting point for those who might need
> >>> fast
> >>> scrolling. As a complete OpenGL newb, I had considerable trouble piecing
> >>> together the theories and suggestions on the mailing list and from Google
> >>> searches to get working code.
>
> > In setup.py, try `import numpy` and use
>
> >    library_dirs = ['/usr/local/lib', numpy.get_include()],
>
> > Using Visual C compiler, the OpenGL library is 'opengl32' and 'windows.h'
> > needs to be included before 'GL/gl.h' (in tileC.c).  MSVC doesn't know
> > `0b00000001` constants; change them to decimal values.
>
> > Christoph
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "pyglet-users" group.
> > To post to this group, send email to pyglet...@googlegroups.com.

> > To unsubscribe from this group, send email to pyglet-users+unsubscribe@**
> > googlegroups.com <pyglet-users%2Bunsu...@googlegroups.com>.
> > For more options, visit this group athttp://groups.google.com/**
> > group/pyglet-users?hl=en<http://groups.google.com/group/pyglet-users?hl=en>
> > .

Gary Daniels

unread,
Dec 16, 2011, 10:37:36 AM12/16/11
to pyglet...@googlegroups.com

I was hoping someone that got it to compile with VC would post the changes needed to make it work. I get error messages about compiler-specific files missing, like "vcvarsall" I think it was. I don't use VC and gave up trying to figure it out since it works fine with gcc.

To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.


Christoph Gohlke

unread,
Dec 16, 2011, 12:23:27 PM12/16/11
to pyglet...@googlegroups.com
It builds and seems to work (at ~1000 fps) using msvc9 with the code changes I mentioned before.

The "vcvarsall" error indicates that you don't have the correct version of Visual Studio installed. Instead of Visual Studio 2008 Pro, the default compiler for Python 2.6-3.2, you can try to use the "Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1" as described at <http://wiki.cython.org/64BitCythonExtensionsOnWindows>

Christoph

Gary Daniels

unread,
Dec 17, 2011, 9:23:49 AM12/17/11
to pyglet...@googlegroups.com

Ok, I didn't know if those were general tips or you actually got it to compile.

I still only have Windows XP and bandwidth-limited Internet, so I guess I'll leave it to the user to read your tips and figure it out for their system for now.

Thanks.
Reply all
Reply to author
Forward
0 new messages