In this case, something like that would work. Note that the boolean can't be passed as a an argument if you want the thread to be able to modify it, because bools are immutable. That's the main reason why I created the ThreadData class.
Normally, you would probably need a mutex or a thread-safe list, but because of the GIL, I decided to make it simple for this example.
import glob
import threading
import sf
SPRITES = glob.iglob('/media/winxp/d/images/Textures/2D/TEXTURES/*.BMP')
class ThreadData(object):
def __init__(self, sprites):
self.sprites = sprites
self.join_thread = False
class LoadingThread(threading.Thread):
def __init__(self, data):
threading.Thread.__init__(self)
self.data = data
def run(self):
for i, filename in enumerate(SPRITES):
sprite = sf.Sprite(sf.Texture.load_from_file(filename))
self.data.sprites.append(sprite)
self.data.join_thread = True
print 'Finished loading'
def main():
window = sf.RenderWindow(sf.VideoMode(1280, 1024), 'Title')
window.framerate_limit = 60
sprites = []
thread_data = ThreadData(sprites)
thread = LoadingThread(thread_data)
thread.start()
running = True
while running:
for event in window.iter_events():
if event.type == sf.Event.CLOSED:
running = False
if thread_data.join_thread:
thread.join()
thread_data.join_thread = False
print 'Joined'
window.clear(sf.Color.WHITE)
for sprite in sprites:
window.draw(sprite)
window.display()
if __name__ == '__main__':
main()
Did you try to write a sample in C++, to see if it would speed things up? I'm interested to see if the binding needs to be optimized.