- Joined
- Feb 18, 2010
- Messages
- 1,850 (0.33/day)
System Name | Eldritch |
---|---|
Processor | AMD Ryzen 5 5800X3D |
Motherboard | ASUS TUF X570 Pro Wifi |
Cooling | Satan's butthole after going to Taco Bell |
Memory | 64 GB G.Skill TridentZ |
Video Card(s) | Vega 56 |
Storage | 6*8TB Western Digital Blues in RAID 6, 2*512 GB Samsung 960 Pros |
Display(s) | Acer CB281HK |
Case | Phanteks Enthoo Pro PH-ES614P_BK |
Audio Device(s) | ASUS Xonar DX |
Power Supply | EVGA Supernova 750 G2 |
Mouse | Razer Viper 8K |
Software | Debian Bullseye |
I'm currently pretending to make a game, it uses a hex-based tile system and uses Python 3.2 and PyGame. Most everything is fine, but I decided I want to use really big maps - and therein lies the problem.
I am currently using a map that is ~22000 tiles big, and guess what? Things are really slow, and I've spent about 3 hours optimizing every thing that's related to it.
So here is my unoptimized code:
Which takes ~1.5 seconds (also, it gets called for every tile).
And here is my optimized code:
Which takes ~.08 seconds at default zoom and ~.72 seconds zoomed out as far as I can go. It only gets called the one time and does everything from there and tileset is currently a frozenset (I was thinking about sorting it into a tuple and culling from there).
Unfortunately I'm still not happy with that and the only way I can think to make it faster is to rewrite it in C - which I really do not want to do (at least not at the moment).
So do you have any tips (aside from the sorting, which I'll get on later today)? Anything small little thing should help (seriously, it loops 21,931 times, anything will do).
I am currently using a map that is ~22000 tiles big, and guess what? Things are really slow, and I've spent about 3 hours optimizing every thing that's related to it.
So here is my unoptimized code:
Code:
def drawhex(tile, layer=0, pos=(int(VIEW[x]+(MODE[x]/2)), int(VIEW[y]+(MODE[y]/2))), mul=STEP*VIEW[z]):
#This needs to be seriously sped up:
tile.polys = ((tile.verts[0][x]*mul+pos[x], tile.verts[0][y]*VIEW[z]+pos[y]),
(tile.verts[1][x]*mul+pos[x], tile.verts[1][y]*VIEW[z]+pos[y]),
(tile.verts[2][x]*mul+pos[x], tile.verts[2][y]*VIEW[z]+pos[y]),
(tile.verts[3][x]*mul+pos[x], tile.verts[3][y]*VIEW[z]+pos[y]),
(tile.verts[4][x]*mul+pos[x], tile.verts[4][y]*VIEW[z]+pos[y]),
(tile.verts[5][x]*mul+pos[x], tile.verts[5][y]*VIEW[z]+pos[y]))
#This is fine:
pygame.draw.polygon(LAYERSURFACES[layer], COLORS[tile.terrain], tile.polys)
if GRIDSPACING: pygame.draw.lines(OVERLAYSURFACES[0], (0, 0, 0, 255), True, tile.polys, GRIDSPACING)
And here is my optimized code:
Code:
def drawhex(tileset, layer=0):
#Sorry, I had to optimize the shit out of this
#This needs to be seriously sped up (still a significant slowdown while zoomed out):
#for tile in tileset.values():
#timmay = time.time()
pos = (int(VIEW[x]+(MODE[x]/2)), int(VIEW[y]+(MODE[y]/2)))
mul = STEP*VIEW[z]
posx = pos[x]
posy = pos[y]
zoom = VIEW[z]
#These seem help when zoomed out
drawlayer = LAYERSURFACES[layer]
overlay = OVERLAYSURFACES[0]
space = GRIDSPACING
color = COLORS
#Seems to help a little bit:
xA = 0.00
xB = 0.00
xC = 0.00
yA = 0.00
yB = 0.00
yC = 0.00
yD = 0.00
for tile in tileset:
verts = tile.verts
xB = verts[1][0]*mul+posx
if xB > 0:#Maybe check Y values first?
xC = verts[4][0]*mul+posx
if xC < MODE[0]:#Width
yA = verts[0][1]*zoom+posy
if yA > 0:
yD = verts[3][1]*zoom+posy
if yD < MODE[1]:#Height
polys = tile.polys
xA = verts[0][0]*mul+posx
yB = verts[1][1]*zoom+posy
yC = verts[2][1]*zoom+posy
polys = ((xA, yA),
(xB, yB),
(xB, yC),
(xA, yD),
(xC, yC),
(xC, yB))
pygame.draw.polygon(drawlayer, color[tile.terrain], polys)
if GRIDSPACING: pygame.draw.lines(overlay, (0, 0, 0, 255), True, polys, space)
#print(time.time()-timmay)
Unfortunately I'm still not happy with that and the only way I can think to make it faster is to rewrite it in C - which I really do not want to do (at least not at the moment).
So do you have any tips (aside from the sorting, which I'll get on later today)? Anything small little thing should help (seriously, it loops 21,931 times, anything will do).