py-sdl / py-sdl2

Python ctypes wrapper around SDL2
Other
297 stars 50 forks source link

gfxdrawing example presents context in the wrong place (?) #22

Closed marcusva closed 4 years ago

marcusva commented 10 years ago

Originally reported by: Brett Calcott (Bitbucket: bcalcott, GitHub: Unknown)


The gfxdrawing example calls context.present() every pass through the loop. This cause flashing on OSX. I changed it calls it only calls it when the drawing changes:

#!diff

diff --git a/examples/gfxdrawing.py b/examples/gfxdrawing.py
--- a/examples/gfxdrawing.py
+++ b/examples/gfxdrawing.py
@@ -304,6 +304,7 @@
     # which function to execute next.
     curindex = 0
     draw_lines(context, 800, 600)
+    context.present()

     # The event loop is nearly the same as we used in colorpalettes.py. If you
     # do not know, what happens here, take a look at colorpalettes.py for a
@@ -325,8 +326,8 @@
                 # function with the arguments.
                 func, args = functions[curindex]
                 func(*args)
+                context.present()
                 break
-        context.present()
     sdl2ext.quit()
     return 0

marcusva commented 10 years ago

Original comment by Brett Calcott (Bitbucket: bcalcott, GitHub: Unknown):


Apologies for taking so long to respond -- work overwhelm. I'm only using python for sdl2, so haven't had the chance to see if it happens elsewhere. Is there any easily installable test suites that are explicitly multi-platform?

I've know I've seen this kind of flickering before when double-buffering was turned off. ie it looks as though I'm seeing a refresh of the image on-screen BEFORE the drawing is done. But I have no ideas double buffering works in sdl2.

This was all I could find: http://www.dreamincode.net/forums/topic/327673-sdl2-rendering/

marcusva commented 10 years ago

Original comment by Marcus von Appen (Bitbucket: marcusva, GitHub: marcusva):


Were you able verify, if this also happens in a similar non-Python example?

marcusva commented 10 years ago

Original comment by Marcus von Appen (Bitbucket: marcusva, GitHub: marcusva):


Changing the context as provided in your example snippet will cause artifacts to appear on other platforms, such as Windows and X11, especially, when the window is moved or another window overlaps the example window.

This could be fixed with adding several checks for events (raising the window, movements, resize, etc.), but would defeat the purpose of the examples of being simple.

Does the problem occur in similar C examples, too? Do you have any chance to check this?

a-hurst commented 4 years ago

I've sort of fixed this in the latest commit, by tweaking the demo to behave the same as the gui.py demo does: that is, default to software rendering (which doesn't have the issue) but allow hardware rendering by passing the -hardware flag to the script. I also enabled vsync for hardware rendering, which seemed to improve (but not fix) the issue on my Mac.

During my searches I found a lot of people talking about flicker issues using SDL2 Renderers with the OpenGL back-end, most of which were using SDL2 with C directly, so I'm going to assume this is a low-level SDL2 problem rather than a py-sdl2 problem. Oddly enough, the example renders perfectly fine with the OpenGL backend in a Mojave VM on my desktop, so it might have to do with differences in the quality of macOS OpenGL drivers for different graphics cards. Also, only certain SDL2_gfx drawing functions seem to set it off: for example, I don't get any flicker when drawing a scene using only sdlgfx.lineColor(), but if I add sdlgfx.thickLineColor() or sdlgfx.aalineColor() the flicker problem appears.

Anyway, since this doesn't seem to be py-sdl2's fault and the new defaults for the example eliminate the issue, I think it's fair to close this.