diff --git a/gamescript/battle.py b/gamescript/battle.py index 3a73bc518..4c2c8a30e 100644 --- a/gamescript/battle.py +++ b/gamescript/battle.py @@ -533,7 +533,13 @@ def run_game(self): # self.mapunitarray = [[x[random.randint(0, 1)] if i != j else 0 for i in range(1000)] for j in range(1000)] pygame.mixer.music.set_endevent(self.SONG_END) # End current music before battle start + frame = 0 while True: # self running + frame += 1 + + if frame % 30 == 0 and hasattr(self.main, "profiler"): + self.main.profiler.refresh() + self.fps_count.fps_show(self.clock) event_key_press = None mouse_left_up = False # left click @@ -577,6 +583,10 @@ def run_game(self): elif event.type == pygame.KEYDOWN and event.key == K_ESCAPE: # open/close menu esc_press = True + elif event.type == pygame.KEYDOWN and event.key == K_F8: # show/hide profiler + if not hasattr(self.main, "profiler"): + self.main.setup_profiler() + elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: # left click mouse_left_up = True diff --git a/gamescript/battleui.py b/gamescript/battleui.py index a943988cd..532436c4c 100644 --- a/gamescript/battleui.py +++ b/gamescript/battleui.py @@ -3,6 +3,7 @@ import pygame import pygame.freetype +import cProfile from gamescript.common import utility @@ -1263,3 +1264,29 @@ def __init__(self, image, who, battle, layer=1): self.battle = battle self.image = image self.rect = self.image.get_rect(center=self.who.pos) + + +class Profiler(cProfile.Profile, pygame.sprite.Sprite): + + def __init__(self): + pygame.sprite.Sprite.__init__(self) + size = (900, 550) + self.image = pygame.Surface(size) + self.rect = pygame.Rect((0, 0, *size)) + font = "ubuntumono" # TODO: feel free to change this, the most important thing is that it + # is monospace and works good for every developer + self.font = pygame.font.SysFont(font, 16) + self.image.fill(0x112233) + self._layer = 12 + + def refresh(self): + import io + from pstats import Stats + s_io = io.StringIO() + stats = Stats(self, stream=s_io) + stats.sort_stats('tottime').print_stats(20) + info_str = s_io.getvalue() + self.enable() # profiler must be re-enabled after get stats + self.image.fill(0x112233) + for e, line in enumerate(info_str.split("\n")): + self.image.blit(self.font.render(line, True, pygame.Color("white")), (0, e*20)) diff --git a/gamescript/camera.py b/gamescript/camera.py index df5c97428..5aaf21f5f 100644 --- a/gamescript/camera.py +++ b/gamescript/camera.py @@ -17,8 +17,8 @@ def update(self, pos, surfaces): camera_y = self.pos[1] - camera_h / 2 # Current camera center y for surface in surfaces: # Blit sprite to camara image surface_x, surface_y = surface.rect.left, surface.rect.top - surface_w, surface_h = surface.image.get_rect().size - rect = Rect(surface_x - camera_x, surface_y - camera_y, surface_w, - surface_h) # get rect that shown inside camera + # surface_w, surface_h = surface.image.get_rect().size + # rect = Rect(surface_x - camera_x, surface_y - camera_y, surface_w, + # surface_h) # get rect that shown inside camera # if rect.x + surface_w > 0 and rect.y + surface_h > 0: - self.image.blit(surface.image, rect) + self.image.blit(surface.image, (surface_x - camera_x, surface_y - camera_y)) diff --git a/gamescript/game.py b/gamescript/game.py index 5c93b4637..78037345c 100644 --- a/gamescript/game.py +++ b/gamescript/game.py @@ -793,6 +793,12 @@ def game_intro(self, screen, clock, intro): pygame.display.set_caption(version_name) # set the self name on program border/tab + def setup_profiler(self): + from gamescript.battleui import Profiler + self.profiler = Profiler() + self.profiler.enable() + self.battle_ui_updater.add(self.profiler) + def run(self): while True: # Get user input