snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 61ef22bcafe91ff7273872af295693e09438e178
parent 16895fcf7ae17457d754e3dd2e9abd22afb7b056
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Mon,  8 Jun 2020 01:24:53 -0400

Merge pull request #90 from mdoege/fps_counter

Add FPS counter
Diffstat:
Asnakewm/apps/tools/fps/README.md | 18++++++++++++++++++
Asnakewm/apps/tools/fps/__init__.py | 14++++++++++++++
Asnakewm/apps/tools/fps/fps.py | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/tools/fps/README.md b/snakewm/apps/tools/fps/README.md @@ -0,0 +1,18 @@ +# FPS +FPS displays the current frame rate. + +# Authors + +Martin C. Doege + ++ github: https://github.com/mdoege + ++ date: 7 Jun 2020 + +Based on Clock by Abdur-Rahmaan Janhangeer + ++ github: https://github.com/Abdur-rahmaanJ + ++ mail: arj.python@gmail.com + ++ date: 30 May 2020 diff --git a/snakewm/apps/tools/fps/__init__.py b/snakewm/apps/tools/fps/__init__.py @@ -0,0 +1,14 @@ +from .fps import SnakeFPS + + +def load(manager, params): + """ + Create and launch a new instance of SnakeFPS. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + SnakeFPS(pos, manager) diff --git a/snakewm/apps/tools/fps/fps.py b/snakewm/apps/tools/fps/fps.py @@ -0,0 +1,54 @@ +# FPS counter for Snakeware + +import pygame +import pygame_gui + +import time, statistics + +MAXSAMP = 300 + + +class SnakeFPS(pygame_gui.elements.UIWindow): + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (395, 100)), + manager=manager, + window_display_title="fps", + object_id="#fps", + resizable=True, + ) + + self.textbox = pygame_gui.elements.UITextBox( + "", + relative_rect=pygame.Rect(0, 1, 363, 40), + manager=manager, + container=self, + anchors={ + "left": "left", + "right": "right", + "top": "top", + "bottom": "bottom", + }, + ) + self.last = 0 + self.samp = [] + + def process_event(self, event): + super().process_event(event) + + def update(self, time_delta): + super().update(time_delta) + + fps = 1 / (time.time() - self.last) + self.samp.append(fps) + if len(self.samp) > MAXSAMP: + self.samp = self.samp[-MAXSAMP:] + self.last = time.time() + self.set_text( + "FPS: %3u AVG: %3u MIN: %3u MAX: %3u" + % (fps, statistics.mean(self.samp), min(self.samp), max(self.samp)) + ) + + def set_text(self, text): + self.textbox.html_text = text + self.textbox.rebuild()