-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps_counter.py
117 lines (93 loc) · 3.29 KB
/
fps_counter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import time
import gamesense
from fps_inspector_sdk import fps_inspector
from win32gui import GetForegroundWindow
from win32process import GetWindowThreadProcessId
from PIL import Image
from pystray import Icon, Menu, MenuItem
import threading
import os
import sys
def start_counter():
gs = gamesense.GameSense("FPS_COUNTER", "FPS Counter")
register_resp = gs.register_game(icon_color_id=gamesense.GS_ICON_GOLD)
if not register_resp.success:
print(register_resp.data)
screen_handler = {
'device-type': 'screened',
'zone': 'one',
'mode': 'screen',
'datas': [
{
"icon-id": 28,
"lines": [
{
"has-text": True,
"context-frame-key": "title"
},
{
"has-text": True,
"context-frame-key": "fps",
"bold": True,
}
]
}
],
}
event_bind_resp = gs.bind_event("RECEIVE_FPS", 0, 999, 0, [screen_handler])
if not event_bind_resp.success:
print(event_bind_resp.data)
event_resp = gs.register_event("RECEIVE_FPS", 0, 999)
if not event_resp.success:
print(event_resp.data)
while not exit_event.is_set():
pid = int(GetWindowThreadProcessId(GetForegroundWindow())[1])
fps_inspector.start_fliprate_recording(pid)
time.sleep(0.5)
fps_inspector.stop_fliprate_recording()
data = fps_inspector.get_all_fliprates()
fps = int(data.get('FPS').to_list()[0]) if len(
data.get('FPS').to_list()) > 0 else None
if fps is not None:
data = {
"value": fps,
"frame": {
"title": "FPS",
"fps": fps
}
}
send_event_resp = gs.send_event("RECEIVE_FPS", data)
if not send_event_resp.success:
print(send_event_resp.data)
else:
send_hb_resp = gs.send_heartbeat()
if not send_hb_resp.success:
print(send_hb_resp.data)
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.join(os.path.dirname(__file__))
return os.path.join(base_path, relative_path)
def exit(icon: Icon) -> None:
icon.visible = False
exit_event.set()
icon.stop()
def setup(icon: Icon) -> None:
icon.visible = True # Required to make the systray icon show up
while not exit_event.is_set(): # This exits the loop if exit is ever set -> program was quit
start_counter()
# allows exiting while waiting. time.sleep would block
exit_event.wait(5)
def main():
# This event is used to stop the loop.
global exit_event
exit_event = threading.Event()
with Image.open(resource_path("meter.ico")) as im:
im.load()
icon = Icon('Steelseries Gamesense FPS Counter', im, 'Steelseries Gamesense FPS Counter', Menu(
MenuItem('Quit', exit)
))
icon.run(setup=setup)
if __name__ == "__main__":
main()