Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added profiler and try to optimize some code #41

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions gamescript/battle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions gamescript/battleui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pygame
import pygame.freetype
import cProfile

from gamescript.common import utility

Expand Down Expand Up @@ -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))
8 changes: 4 additions & 4 deletions gamescript/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
6 changes: 6 additions & 0 deletions gamescript/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down