Pre-scale backgrounds

This commit is contained in:
theo@manjaro 2021-11-21 18:08:58 +01:00
parent e3ec185f01
commit a0e74238a3

View File

@ -23,6 +23,17 @@ class TilesetRenderer(BaseObject):
self.bgoffset = self.layers[0]["surface"].get_height()/2-self.game.globals["camerah"] self.bgoffset = self.layers[0]["surface"].get_height()/2-self.game.globals["camerah"]
# Prescale backgrounds
self.sep = self.game.pygame.transform.scale(self.game.sprite_lib["backgroundseparator.png"],[self.game.globals["cameraw"],self.game.globals["camerah"]])
self.scaledbgs = []
wratio = self.game.globals["cameraw"]/self.bgs[0]["sprite"].get_width()
hratio = self.game.globals["camerah"]/self.bgs[0]["sprite"].get_height()
for bg in self.bgs:
width, height = round(bg["sprite"].get_width()*wratio),round(bg["sprite"].get_height()*hratio)
sprite = self.game.pygame.transform.scale(bg["sprite"],(width,height))
self.scaledbgs.append(sprite)
def step(self): def step(self):
# Spawning ennemies # Spawning ennemies
while len(self.queue)>0: while len(self.queue)>0:
@ -31,24 +42,21 @@ class TilesetRenderer(BaseObject):
self.game.gameloop.summon(e) self.game.gameloop.summon(e)
def draw(self): def draw(self):
wratio = self.game.globals["cameraw"]/self.bgs[0]["sprite"].get_width()
hratio = self.game.globals["camerah"]/self.bgs[0]["sprite"].get_height()
lastoffset = 0 lastoffset = 0
for i,bg in enumerate(self.bgs): for i,bg in enumerate(self.scaledbgs):
ratio = 1-(len(self.bgs)-i)/len(self.bgs) ratio = 1-(len(self.bgs)-i)/len(self.bgs)
width, height = round(bg["sprite"].get_width()*wratio),round(bg["sprite"].get_height()*hratio) width = bg.get_width()
sprite = self.game.pygame.transform.scale(bg["sprite"],(width,height)) height = bg.get_height()
nbs = self.game.globals["cameraw"]/width nbs = self.game.globals["cameraw"]/width
lastoffset = self.rect[1]+(self.bgoffset-self.game.globals["cameray"])*ratio lastoffset = self.rect[1]+(self.bgoffset-self.game.globals["cameray"])*ratio
for i in range(round(nbs+0.5)): for i in range(round(nbs+0.5)):
self.game.window.blit(sprite,[(-self.game.globals["camerax"]*ratio)%width+(i-1)*width,lastoffset]) self.game.window.blit(bg,[(-self.game.globals["camerax"]*ratio)%width+(i-1)*width,lastoffset])
# Draw filler # Draw filler
if self.level["filler"]: if self.level["filler"]:
filleroffset = lastoffset+height filleroffset = lastoffset+height
self.game.window.blit(self.level["filler"],[0,filleroffset]) self.game.window.blit(self.level["filler"],[0,filleroffset])
# Separator # Separator
sep = self.game.pygame.transform.scale(self.game.sprite_lib["backgroundseparator.png"],[self.game.globals["cameraw"],self.game.globals["camerah"]]) self.game.window.blit(self.sep,[0,0])
self.game.window.blit(sep,[0,0])
# Layers # Layers
for layer in self.layers: for layer in self.layers:
self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"])) self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"]))