X Tutup
Skip to content

Commit c46055d

Browse files
committed
Fixed: PEP8 conformance, FPS counter and added PySDL2 and SDL2 version info.
1 parent f0d7816 commit c46055d

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

examples/pysdl2.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@
4747
import logging
4848
import sys
4949

50+
5051
def die(msg):
5152
"""
5253
Helper function to exit application on failed imports etc.
5354
"""
5455
sys.stderr.write("%s\n" % msg)
5556
sys.exit(1)
5657

58+
5759
try:
5860
# noinspection PyUnresolvedReferences
5961
from cefpython3 import cefpython as cef
6062
except ImportError:
61-
die("ERROR: cefpython3 package not found\nTo install type: `pip install cefpython3`")
63+
die("""ERROR: cefpython3 package not found\n
64+
To install type: `pip install cefpython3`""")
6265
try:
6366
# noinspection PyUnresolvedReferences
6467
import sdl2
@@ -72,14 +75,33 @@ def die(msg):
7275
except ImportError:
7376
die("ERROR: PIL package not found\nTo install type: pip install Pillow")
7477

78+
7579
def main():
76-
parser = argparse.ArgumentParser(description='PySDL2 / cefpython example', add_help=True)
77-
parser.add_argument('-v', '--verbose', help='Turn on debug info', dest='verbose', action='store_true')
80+
parser = argparse.ArgumentParser(
81+
description='PySDL2 / cefpython example',
82+
add_help=True
83+
)
84+
parser.add_argument(
85+
'-v',
86+
'--verbose',
87+
help='Turn on debug info',
88+
dest='verbose',
89+
action='store_true'
90+
)
7891
args = parser.parse_args()
7992
logLevel = logging.INFO
8093
if args.verbose:
8194
logLevel = logging.DEBUG
82-
logging.basicConfig(format='[%(filename)s %(levelname)s]: %(message)s', level=logLevel)
95+
logging.basicConfig(
96+
format='[%(filename)s %(levelname)s]: %(message)s',
97+
level=logLevel
98+
)
99+
logging.info("Using PySDL2 %s" % sdl2.__version__)
100+
version = sdl2.SDL_version()
101+
sdl2.SDL_GetVersion(version)
102+
logging.info(
103+
"Using SDL2 %s.%s.%s" % (version.major, version.minor, version.patch)
104+
)
83105
# The following variables control the dimensions of the window
84106
# and browser display area
85107
width = 800
@@ -146,13 +168,16 @@ def main():
146168
# Begin the main rendering loop
147169
running = True
148170
# FPS debug variables
149-
if logLevel == logging.DEBUG:
150-
frames = 0
151-
lastFrameTick = sdl2.timer.SDL_GetTicks()
171+
frames = 0
152172
logging.debug("beginning rendering loop")
173+
resetFpsTime = True
174+
fpsTime = 0
153175
while running:
154176
# record when we started drawing this frame
155177
startTime = sdl2.timer.SDL_GetTicks()
178+
if resetFpsTime:
179+
fpsTime = sdl2.timer.SDL_GetTicks()
180+
resetFpsTime = False
156181
# Convert SDL2 events into CEF events (where appropriate)
157182
events = sdl2.ext.get_events()
158183
for event in events:
@@ -297,18 +322,17 @@ def main():
297322
sdl2.SDL_Rect(0, headerHeight, browserWidth, browserHeight)
298323
)
299324
sdl2.SDL_RenderPresent(renderer)
300-
301325
# FPS debug code
302-
if logLevel == logging.DEBUG:
303-
frames += 1
304-
currentTick = sdl2.timer.SDL_GetTicks()
305-
if currentTick - lastFrameTick > 1000:
306-
lastFrameTick = sdl2.timer.SDL_GetTicks()
307-
logging.debug("FPS %d" % (frames / (currentTick / 1000.0)))
308-
326+
frames += 1
327+
if sdl2.timer.SDL_GetTicks() - fpsTime > 1000:
328+
logging.debug("FPS: %d" % frames)
329+
frames = 0
330+
resetFpsTime = True
309331
# regulate frame rate
310332
if sdl2.timer.SDL_GetTicks() - startTime < 1000.0 / frameRate:
311-
sdl2.timer.SDL_Delay((1000 / frameRate) - (sdl2.timer.SDL_GetTicks() - startTime))
333+
sdl2.timer.SDL_Delay(
334+
(1000 / frameRate) - (sdl2.timer.SDL_GetTicks() - startTime)
335+
)
312336
# User exited
313337
exit_app()
314338

@@ -329,10 +353,12 @@ def get_key_code(key):
329353
if key in key_map:
330354
return key_map[key]
331355
# Key not mapped, raise exception
332-
logging.error("Keyboard mapping incomplete:"
333-
" unsupported SDL key %d."
334-
" See https://wiki.libsdl.org/SDLKeycodeLookup for mapping."
335-
% key)
356+
logging.error(
357+
"""
358+
Keyboard mapping incomplete: unsupported SDL key %d.
359+
See https://wiki.libsdl.org/SDLKeycodeLookup for mapping.
360+
""" % key
361+
)
336362
return None
337363

338364

0 commit comments

Comments
 (0)
X Tutup