Skip to content

Commit bbd70da

Browse files
committed
Added frame rate limiting code
1 parent a68771c commit bbd70da

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

examples/pysdl2.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
further down. Pull requests for the missing functionality are welcome.
66
77
Requires PySDL2 and SDL2 libraries.
8-
8+
99
Tested configurations:
1010
- Windows 7: SDL 2.0.7 and PySDL2 0.9.6
11-
- Fedora 25: SDL2 2.0.5 with PySDL2 0.9.3
11+
- Fedora 26: SDL2 2.0.7 with PySDL2 0.9.6
1212
- Ubuntu 14.04: SDL2 with PySDL2 0.9.6
1313
1414
Install instructions:
@@ -19,7 +19,7 @@
1919
- Ubuntu: sudo apt-get install libsdl2-dev
2020
2. Install PySDL2 using pip package manager:
2121
pip install PySDL2
22-
22+
2323
Missing functionality:
2424
- Performance is still not perfect, see Issue #324 for further details
2525
- Keyboard modifiers that are not yet handled in this example:
@@ -74,6 +74,8 @@ def main():
7474
browserWidth = width
7575
# Mouse wheel fudge to enhance scrolling
7676
scrollEnhance = 40
77+
# desired frame rate
78+
frameRate = 60
7779
# Initialise CEF for offscreen rendering
7880
sys.excepthook = cef.ExceptHook
7981
switches = {
@@ -124,7 +126,12 @@ def main():
124126
browser.WasResized()
125127
# Begin the main rendering loop
126128
running = True
129+
# FPS debug variables
130+
#frames = 0
131+
#lastFrameTick = sdl2.timer.SDL_GetTicks()
127132
while running:
133+
# record when we started drawing this frame
134+
startTime = sdl2.timer.SDL_GetTicks()
128135
# Convert SDL2 events into CEF events (where appropriate)
129136
events = sdl2.ext.get_events()
130137
for event in events:
@@ -269,6 +276,17 @@ def main():
269276
sdl2.SDL_Rect(0, headerHeight, browserWidth, browserHeight)
270277
)
271278
sdl2.SDL_RenderPresent(renderer)
279+
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)))
286+
287+
# regulate frame rate
288+
if sdl2.timer.SDL_GetTicks() - startTime < 1000.0 / frameRate:
289+
sdl2.timer.SDL_Delay((1000 / frameRate) - (sdl2.timer.SDL_GetTicks() - startTime))
272290
# User exited
273291
exit_app()
274292

@@ -298,11 +316,11 @@ def get_key_code(key):
298316

299317
class LoadHandler(object):
300318
"""Simple handler for loading URLs."""
301-
319+
302320
def OnLoadingStateChange(self, is_loading, **_):
303321
if not is_loading:
304322
print("[pysdl2.py] Page loading complete")
305-
323+
306324
def OnLoadError(self, frame, failed_url, **_):
307325
if not frame.IsMain():
308326
return
@@ -313,7 +331,7 @@ class RenderHandler(object):
313331
"""
314332
Handler for rendering web pages to the
315333
screen via SDL2.
316-
334+
317335
The object's texture property is exposed
318336
to allow the main rendering loop to access
319337
the SDL2 texture.
@@ -324,11 +342,11 @@ def __init__(self, renderer, width, height):
324342
self.__height = height
325343
self.__renderer = renderer
326344
self.texture = None
327-
345+
328346
def GetViewRect(self, rect_out, **_):
329347
rect_out.extend([0, 0, self.__width, self.__height])
330348
return True
331-
349+
332350
def OnPaint(self, element_type, paint_buffer, **_):
333351
"""
334352
Using the pixel data from CEF's offscreen rendering
@@ -380,7 +398,7 @@ def OnPaint(self, element_type, paint_buffer, **_):
380398
else:
381399
print("[pysdl2.py] ERROR: Unsupported mode: %s" % mode)
382400
exit_app()
383-
401+
384402
pxbuf = image.tobytes()
385403
# Create surface
386404
surface = sdl2.SDL_CreateRGBSurfaceFrom(

0 commit comments

Comments
 (0)