Skip to content

Instantly share code, notes, and snippets.

@sinistersnare
Last active December 26, 2015 01:59
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 sinistersnare/7074791 to your computer and use it in GitHub Desktop.
Save sinistersnare/7074791 to your computer and use it in GitHub Desktop.
Python libgdx wrapper ideas

Ideas about PyGdx, a pythonic wrapper for LibGDX Java library using Jython.

SpriteBatch

Sprite batches in java are used like so:

SpriteBatch batch = new SpriteBatch(); //init'd in create()

//...

//in render()

batch.begin();

batch.draw(texture,x,y,width,height);

batch.end();

maybe this could be better implemented in python using a context manager:

//in __init__(self)

batch = SpriteBatch()

//...
// in render(self)

with drawing(batch):
    batch.draw(texture,x,y,width,height)

the batch.begin()/end() will be handled autoamtically. The reason i chose not to use with SpriteBatch() as batch: is because it is hazardous to use new (A.K.A. constructing a new object) ever in a render loop. (Calling new 60 times a second will bring your monster computer to a halt.)

We can probably just call begin on the given parameter during enter and end on exit, so it'll be good.

heres a simple implementation of this idea:

from contextlib import contextmanager

@contextmanager
def drawing(batch):
    batch.begin()
    yield
    batch.end()

This will reduce boilerplate and greatly increase readability.

//more ideas to come...

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