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"]
# 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):
# Spawning ennemies
while len(self.queue)>0:
@ -31,24 +42,21 @@ class TilesetRenderer(BaseObject):
self.game.gameloop.summon(e)
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
for i,bg in enumerate(self.bgs):
for i,bg in enumerate(self.scaledbgs):
ratio = 1-(len(self.bgs)-i)/len(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))
width = bg.get_width()
height = bg.get_height()
nbs = self.game.globals["cameraw"]/width
lastoffset = self.rect[1]+(self.bgoffset-self.game.globals["cameray"])*ratio
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
if self.level["filler"]:
filleroffset = lastoffset+height
self.game.window.blit(self.level["filler"],[0,filleroffset])
# 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(sep,[0,0])
self.game.window.blit(self.sep,[0,0])
# 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"]))