Pyglet's batch draw different sprites without sfml fast
But pyglet's batch draw the same sprite (each sprite has the same texture) much faster than sfml's draw(sprite)... such as batch draw 10000 sprites.
Can you add a batch draw of the same texture sprite in sfml later? This is faster, on some occasions
#pysmfl example: not full,but you can use c++ sfml to test the speed of draw 10000 same sprite
windowStyle=sf.Style.DEFAULT
if 1:
window = sf.RenderWindow(sf.VideoMode(640, 480),
'Drawing an image with SFML',windowStyle)
#sf.Style.NONE
window.vertical_synchronization = True
window.framerate_limit = 60
window.key_repeat_enabled=False
running = True
#texture = sf.Texture.from_file('zblow3.png')
ls=list()
png=pngList[0]
texture = sf.Texture.from_file(png)
texture.smooth=True
for i in xrange(10000):
#png=choice(pngList)
#png=pngList[0]
#texture = sf.Texture.from_file(png)
#texture.smooth=True
sprite = sf.Sprite(texture)
x=randint(0,640)
y=randint(0,480)
sprite.position=x,y
sprite.rotation=randint(1,360)
ls.append(sprite)
... ...
t1=time.clock()
for s in ls:
#x=randint(0,640)
#y=randint(0,480)
#s.position=x,y
#s.rotation=x
window.draw(s)
pass
#window.draw(sprite)
t2=time.clock()
print 'cost is',t2-t1
###test result:
cost is 0.0336539525734
cost is 0.0295262057547
cost is 0.0373839061182
cost is 0.0333649108478
cost is 0.0326527325671
cost is 0.0181461101111
cost is 0.0336869950792
cost is 0.0303814612936
cost is 0.0413666508726
cost is 0.0317208055827
cost is 0.0334431862985
cost is 0.0297379344328
cost is 0.0289368942675
cost is 0.0311719150249
#pyglet(pure python code) example result:
cost time is 0.0121541885226
cost time is 0.01268735983
cost time is 0.0138351859058
cost time is 0.0119851263812
cost time is 0.0135422945678
cost time is 0.012218027927
cost time is 0.011419874971
cost time is 0.0114622207066
cost time is 0.0113592435768
cost time is 0.0118051570048
cost time is 0.0114689575282
cost time is 0.0115831626939
cost time is 0.0117217487377
cost time is 0.0115045664422
cost time is 0.0115616690251
cost time is 0.0116351324603
cost time is 0.011666570961
cost time is 0.0113877948683
cost time is 0.0114237245833
cost time is 0.0116152427967
full pyglet example code follow (need a same png with pysfml example)
# -*- coding: cp936 -*-
import pyglet
pyglet.options['debug_gl'] = False
from pyglet.gl import *
import os
import sys
import time
from random import *
from pyglet.window import key
from pyglet.window import mouse
window = pyglet.window.Window(width=640,height=480)
pyglet.gl.glEnable(GL_BLEND)
pyglet.gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
fps_display = pyglet.clock.ClockDisplay()
fps_display.label.color=(0.5,1,1,1)
fps_display.label.x=10
fps_display.label.y=300
Layer1_Batch = pyglet.graphics.Batch()
pic=pyglet.image.load("shot5.png").get_texture()
sprite = pyglet.sprite.Sprite(pic, x=100, y=100, batch=None)
def getallpng():
ls=list()
walk=os.walk("images")
for i in walk:
dir1,subdirlist,filelist=i
for filename in filelist:
if filename.endswith("png"):
pngPath=dir1+"\\"+filename
ls.append(pngPath)
return ls
pngList=getallpng()
ls=list()
def makeSprite():
pic=pyglet.image.load(pngList[0]).get_texture()
for i in xrange(10000):
#png=choice(pngList)
#pic=pyglet.image.load(png).get_texture()
x=randint(0,640)
y=randint(0,480)
sprite = pyglet.sprite.Sprite(pic, x, y, batch=Layer1_Batch)
sprite.rotation=randint(1,360)
ls.append(sprite)
makeSprite()
@window.event
def on_draw():
global camx,camy
window.clear()
t1=time.clock()
Layer1_Batch.draw()
t2=time.clock()
print 'cost time is',t2-t1
fps_display.draw()
def update(dt):
#print 'dt is',dt
dt = 1.0/60. #override dt to keep physics simulation stable
#whenKeyPressed()
pass
#no window,the result is fake
def costTime():
t1=time.clock()
Layer1_Batch.draw()
t2=time.clock()
print 'cost time222 is',t2-t1
pyglet.clock.set_fps_limit(60)
pyglet.clock.schedule_interval(update, 1/60.0)
#pyglet.clock.schedule_once(spawn_logo, .1)
#pyglet.clock.schedule_interval(spawn_logo, 10/6.)
pyglet.app.run()