Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
various uncommitted optimisations (oops forgot to commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiedleroD committed Oct 8, 2020
1 parent 9e7a9c3 commit 5339da3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
6 changes: 3 additions & 3 deletions CONSTANTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
DTIME=0
TIMEC=0
BUCKLEN=2000
SCOLOR=(255,0,0)
ECOLOR=(0,255,255)
SCOLOR=(0,255,255)
ECOLOR=(255,0,0)

class Timer():
def __init__(self):
Expand All @@ -32,7 +32,7 @@ def get(self):
return self.t/self.c

profs=[Timer() for i in range(0)]#for speed debugging purposes
colorlamb=lambda perc:[int(SCOLOR[x]*(perc)+ECOLOR[x]*(1-perc)) for x in range(len(SCOLOR))]*2
colorlamb=lambda perc:[int(SCOLOR[x]*(1-perc)+ECOLOR[x]*perc) for x in range(len(SCOLOR))]*2
audiolamb=lambda perc:pyglet.media.StaticSource(synthesis.Square(1/80,440+440*perc,sample_size=8))#less than 1/60 because that reduces cpu load apparently, but doesn't sound too bad

COLORS=[color for i in range(BUCKLEN) for color in colorlamb(i/BUCKLEN)]
Expand Down
59 changes: 36 additions & 23 deletions Entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def draw(self):
self.next.release()

class Bucket(Entity):
def __init__(self,x,y,w,h,itemc,batch,anch=0):
def __init__(self,x,y,w,h,itemc,batch,anch=0,maxps=None):
super().__init__(x,y,w,h,batch,anch)
self.maxic=abs(itemc)
blanc=self.blanc=[0,0,0]*self.maxic*2
Expand All @@ -456,22 +456,27 @@ def __init__(self,x,y,w,h,itemc,batch,anch=0):
self.items=[i for i in range(self.itemc)]
self.racts=set()
self.wacts=set()
if (not maxps) or maxps>self.maxic:
maxps=self.maxic
self.setmaxps(maxps)#also sets ravl and wavl
self.colors=deque(colors)
self.ravl=batch.add(
self.maxic*2,GL_LINES,GRmp,
('v2f/dynamic',[pos for i in range(self.maxic) for pos in self._getract(i)]),
('c3B/stream',blanc.copy())
)
self.wavl=batch.add(
self.maxic*2,GL_LINES,GRmp,
('v2f/dynamic',[pos for i in range(self.maxic) for pos in self._getwact(i)]),
('c3B/stream',blanc.copy())
)
self.vl=batch.add(
self.maxic*2,GL_LINES,GRmp,
('v2f/dynamic',[pos for i in range(self.maxic) for pos in self._getline(i)]),
('c3B/stream',colors)
)
def setmaxps(self,maxps):
self.maxps=maxps
self.ravl=self.batch.add(
maxps*2,GL_LINES,GRmp,
('v2f/dynamic',[0,0,0,0]*maxps),
('c3B/stream',(0,255,0,0,255,0)*maxps)
)
self.wavl=self.batch.add(
maxps*2,GL_LINES,GRmp,
('v2f/dynamic',[0,0,0,0]*maxps),
('c3B/stream',(255,0,0,255,0,0)*maxps)
)
def _getyfromi(self,i):
return self.y+self.h*(i+1)/self.maxic
def _getline(self,i):
Expand Down Expand Up @@ -549,8 +554,12 @@ def insert_from(self,x,y,other):
return False
def render(self):
self.vl.vertices[:]=[pos for i in range(self.maxic) for pos in self._getline(i)]
self.wavl.vertices[:]=[pos for i in range(self.maxic) for pos in self._getwact(i)]
self.ravl.vertices[:]=[pos for i in range(self.maxic) for pos in self._getract(i)]
wvs=self._getwact(0)
wvs=[wvs[0],0,wvs[2],0]*self.maxps
self.wavl.vertices[:]=wvs
rvs=self._getract(0)
rvs=[rvs[0],0,rvs[2],0]*self.maxps
self.ravl.vertices[:]=rvs
self.rendered=True
def render_colors(self):
self.colors=[col for i in self.items for col in colorlamb(i/self.maxic)]
Expand All @@ -560,16 +569,20 @@ def draw(self):
if self.itemc>self.maxic:
raise ValueError("Bucket: itemc is larger than maxic")
self.vl.colors=self.colors
#clearing wavl and ravl colors
self.wavl.colors[:]=self.blanc.copy()
self.ravl.colors[:]=self.blanc.copy()
#actually faster than two separate for loops since looping over a set is pretty slow
#and checking if a value is in a set is lightning fast
#and looping over a range is not only memory efficient, but also pretty fast
for num in self.wacts:
self.wavl.colors[num*6:6+num*6]=(255,0,0,255,0,0)
for num in self.racts:
self.ravl.colors[num*6:6+num*6]=(0,255,0,0,255,0)
for i,num in enumerate(self.wacts):
if i>=self.maxps:
break
self.wavl.vertices[1+i*4]=self.wavl.vertices[3+i*4]=self._getyfromi(num)
wl=len(self.wacts)
for i in range(wl,self.maxps):
self.wavl.vertices[1+i*4]=self.wavl.vertices[3+i*4]=-1
for i,num in enumerate(self.racts):
if i>=self.maxps:
break
self.ravl.vertices[1+i*4]=self.ravl.vertices[3+i*4]=self._getyfromi(num)
rl=len(self.racts)
for i in range(rl,self.maxps):
self.ravl.vertices[1+i*4]=self.ravl.vertices[3+i*4]=-1
if self.wacts:
self.wacts.clear()
if self.racts:
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def act_new_buck(self,act):
for i,buck in enumerate(self.bucks):
buck.set_pos(chunksize*i,0)
buck.set_size(chunksize,HEIGHT)
self.bucks.append(Bucket(WIDTH2-chunksize,0,chunksize,HEIGHT,-BUCKLEN,self.batch))
self.bucks.append(Bucket(WIDTH2-chunksize,0,chunksize,HEIGHT,-BUCKLEN,self.batch,maxps=self.edits[0].getNum()))
self.stats[3]+=1
return True
def act_buckswap(self,act):
Expand Down Expand Up @@ -317,7 +317,7 @@ def on_key_press(self,symbol,modifiers):
window.rads=[ RadioListPaged(WIDTH,HEIGHT-BTNHEIGHT*5,BTNWIDTH*2,BTNHEIGHT*13,[alg.name for alg in algs],12,window.batch,8,selected=0)]#radiolists
window.edits=[ IntEdit(WIDTH-BTNWIDTH,HEIGHT-BTNHEIGHT*3,BTNWIDTH,BTNHEIGHT,"Speed","100",window.batch,8),#Edits
IntEdit(WIDTH,HEIGHT-BTNHEIGHT*3,BTNWIDTH,BTNHEIGHT,"FPS/UPS","60",window.batch,8)]
window.bucks=[ Bucket(0,0,WIDTH2,HEIGHT,BUCKLEN,window.batch)]#buckets
window.bucks=[ Bucket(0,0,WIDTH2,HEIGHT,BUCKLEN,window.batch,maxps=window.edits[0].getNum())]#buckets
try:
pyglet.app.run()
finally:
Expand Down

0 comments on commit 5339da3

Please sign in to comment.