3636 https://github.com/neilmunday/pes/blob/master/lib/pes/ui.py
3737"""
3838
39+ import argparse
40+ import logging
3941import sys
42+
43+ def die (msg ):
44+ """
45+ Helper function to exit application on failed imports etc.
46+ """
47+ sys .stderr .write ("%s\n " % msg )
48+ sys .exit (1 )
49+
4050try :
4151 # noinspection PyUnresolvedReferences
4252 from cefpython3 import cefpython as cef
4353except ImportError :
44- print ("ERROR: cefpython3 package not found" )
45- print ("To install type: `pip install cefpython3`" )
46- sys .exit (1 )
54+ die ("ERROR: cefpython3 package not found\n To install type: `pip install cefpython3`" )
4755try :
4856 # noinspection PyUnresolvedReferences
4957 import sdl2
5058 # noinspection PyUnresolvedReferences
5159 import sdl2 .ext
5260except ImportError :
53- print ("ERROR: SDL2 package not found" )
54- print ("To install type: `pip install PySDL2`" )
55- sys .exit (1 )
61+ die ("ERROR: SDL2 package not found\n To install type: `pip install PySDL2`" )
5662try :
5763 # noinspection PyUnresolvedReferences
5864 from PIL import Image
5965except ImportError :
60- print ("ERROR: PIL package not found" )
61- print ("To install type: pip install Pillow" )
62- sys .exit (1 )
63-
66+ die ("ERROR: PIL package not found\n To install type: pip install Pillow" )
6467
6568def main ():
69+ parser = argparse .ArgumentParser (description = 'PySDL2 / cefpython example' , add_help = True )
70+ parser .add_argument ('-v' , '--verbose' , help = 'Turn on debug info' , dest = 'verbose' , action = 'store_true' )
71+ args = parser .parse_args ()
72+ logLevel = logging .INFO
73+ if args .verbose :
74+ logLevel = logging .DEBUG
75+ logging .basicConfig (format = '[%(filename)s %(levelname)s]: %(message)s' , level = logLevel )
6676 # The following variables control the dimensions of the window
6777 # and browser display area
6878 width = 800
@@ -75,7 +85,7 @@ def main():
7585 # Mouse wheel fudge to enhance scrolling
7686 scrollEnhance = 40
7787 # desired frame rate
78- frameRate = 60
88+ frameRate = 100
7989 # Initialise CEF for offscreen rendering
8090 sys .excepthook = cef .ExceptHook
8191 switches = {
@@ -88,16 +98,18 @@ def main():
8898 }
8999 browser_settings = {
90100 # Tweaking OSR performance (Issue #240)
91- "windowless_frame_rate" : 100
101+ "windowless_frame_rate" : frameRate
92102 }
93103 cef .Initialize (settings = {"windowless_rendering_enabled" : True },
94104 switches = switches )
105+ logging .debug ("cef initialised" )
95106 window_info = cef .WindowInfo ()
96107 window_info .SetAsOffscreen (0 )
97108 # Initialise SDL2 for video (add other init constants if you
98109 # require other SDL2 functionality e.g. mixer,
99110 # TTF, joystick etc.
100111 sdl2 .SDL_Init (sdl2 .SDL_INIT_VIDEO )
112+ logging .debug ("SDL2 initialised" )
101113 # Create the window
102114 window = sdl2 .video .SDL_CreateWindow (
103115 'cefpython3 SDL2 Demo' ,
@@ -127,8 +139,10 @@ def main():
127139 # Begin the main rendering loop
128140 running = True
129141 # FPS debug variables
130- #frames = 0
131- #lastFrameTick = sdl2.timer.SDL_GetTicks()
142+ if logLevel == logging .DEBUG :
143+ frames = 0
144+ lastFrameTick = sdl2 .timer .SDL_GetTicks ()
145+ logging .debug ("beginning rendering loop" )
132146 while running :
133147 # record when we started drawing this frame
134148 startTime = sdl2 .timer .SDL_GetTicks ()
@@ -277,12 +291,13 @@ def main():
277291 )
278292 sdl2 .SDL_RenderPresent (renderer )
279293
280- # FPS debug code - left here for reference
281- #frames += 1
282- #currentTick = sdl2.timer.SDL_GetTicks()
283- #if currentTick - lastFrameTick > 1000:
284- # lastFrameTick = sdl2.timer.SDL_GetTicks()
285- # print("FPS %d" % (frames / (currentTick / 1000.0)))
294+ # FPS debug code
295+ if logLevel == logging .DEBUG :
296+ frames += 1
297+ currentTick = sdl2 .timer .SDL_GetTicks ()
298+ if currentTick - lastFrameTick > 1000 :
299+ lastFrameTick = sdl2 .timer .SDL_GetTicks ()
300+ logging .debug ("FPS %d" % (frames / (currentTick / 1000.0 )))
286301
287302 # regulate frame rate
288303 if sdl2 .timer .SDL_GetTicks () - startTime < 1000.0 / frameRate :
@@ -307,7 +322,7 @@ def get_key_code(key):
307322 if key in key_map :
308323 return key_map [key ]
309324 # Key not mapped, raise exception
310- print ( "[pysdl2.py] Keyboard mapping incomplete:"
325+ logging . error ( " Keyboard mapping incomplete:"
311326 " unsupported SDL key %d."
312327 " See https://wiki.libsdl.org/SDLKeycodeLookup for mapping."
313328 % key )
@@ -319,12 +334,12 @@ class LoadHandler(object):
319334
320335 def OnLoadingStateChange (self , is_loading , ** _ ):
321336 if not is_loading :
322- print ( "[pysdl2.py] Page loading complete" )
337+ logging . info ( " Page loading complete" )
323338
324339 def OnLoadError (self , frame , failed_url , ** _ ):
325340 if not frame .IsMain ():
326341 return
327- print ( "[pysdl2.py] Failed to load %s" % failed_url )
342+ logging . error ( " Failed to load %s" % failed_url )
328343
329344
330345class RenderHandler (object ):
@@ -396,7 +411,7 @@ def OnPaint(self, element_type, paint_buffer, **_):
396411 depth = 32
397412 pitch = self .__width * 4
398413 else :
399- print ( "[pysdl2.py] ERROR: Unsupported mode: %s" % mode )
414+ logging . error ( " ERROR: Unsupported mode: %s" % mode )
400415 exit_app ()
401416
402417 pxbuf = image .tobytes ()
@@ -421,14 +436,14 @@ def OnPaint(self, element_type, paint_buffer, **_):
421436 # Free the surface
422437 sdl2 .SDL_FreeSurface (surface )
423438 else :
424- print ( "[pysdl2.py] WARNING: Unsupport element_type in OnPaint" )
439+ logging . warning ( " Unsupport element_type in OnPaint" )
425440
426441
427442def exit_app ():
428443 """Tidy up SDL2 and CEF before exiting."""
429444 sdl2 .SDL_Quit ()
430445 cef .Shutdown ()
431- print ( "[pysdl2.py] Exited gracefully" )
446+ logging . info ( " Exited gracefully" )
432447
433448
434449if __name__ == "__main__" :
0 commit comments