# Flickering DX

**URL:** <https://discourse.libsdl.org/t/flickering-dx/14947>\
**Category:** SDL Development\
**Created:** [November 5, 2007, 6:49pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947 "2007-11-05T18:49:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Pragier](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@Miguel\_Pragier](https://discourse.libsdl.org/u/Miguel_Pragier)\
**Post date:** [November 5, 2007, 6:49pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/1 "2007-11-05T18:49:33Z")

</div>

When I use DirectX, my screen is flickering.  
Is it common?

I know my videoBoard support the DX I’m using.

---

<div class="post-metadata">

**Author:** ![Torsten\_Giebl](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/torsten_giebl/32/2804_2.png) [@Torsten\_Giebl](https://discourse.libsdl.org/u/Torsten_Giebl)\
**Post date:** [November 5, 2007, 7:01pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/2 "2007-11-05T19:01:44Z")

</div>

Hello !

> When I use DirectX, my screen is flickering.  
> Is it common?

What do you mean with “flickering” ? Do you mean the same  
thing that happens when playing games with VSYNC turned off ?

CU

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [November 5, 2007, 7:10pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/3 "2007-11-05T19:10:42Z")

</div>

> When I use DirectX, my screen is flickering.

How? Directly or via SDL? If the latter, are you using SDL 1.2  
(stable) or 1.3 (development)? (These have rather different video  
subsystems.)

> Is it common?

Yes, it’s perfectly normal if you’re?trying (and failing) to  
perform “smart updates” (ie redraw only what’s changed) over a double  
buffered hardware surface.

The reason is that a double buffered hardware surface normally uses  
two actual VRAM surfaces that are _flipped_ by SDL\_FlipSurface(). No  
copying is done, so the new “drawing page” you get after a flip is  
two frames old, rather than identical to the last drawing page.

Simple solution: Always redraw the whole screen. Basically, assume  
that the drawing page is filled with random garbage after a flip.

If you want to do it the hard (but in some cases, very fast) way, you  
can have a look at my Fixed Rate Pig example (pig-1.0), which (among  
other things) demonstrates one way of performing smart updates, even  
on true page flipped displays:  
[Mixed Downloads](http://olofson.net/mixed.html)

For starters, just verify that “Pig” actually works in with a double  
buffered hardware surface. (Retrace sync’ed frame rate probably; no  
flickering, no tearing.)

> I know my videoBoard support the DX I’m using.

I don’t think you would be able to set up a display at all if it  
wasn’t.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- [http://olofson.net](http://olofson.net) - Games, SDL examples -------.  
| [http://zeespace.net](http://zeespace.net) - 2.5D rendering engine |  
| [http://audiality.org](http://audiality.org) - Music/audio engine |  
| [http://eel.olofson.net](http://eel.olofson.net) - Real time scripting |  
'-- [http://www.reologica.se](http://www.reologica.se) - Rheology instrumentation --'On Monday 05 November 2007, Miguel Pragier wrote:

---

<div class="post-metadata">

**Author:** ![Miguel\_Pragier](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@Miguel\_Pragier](https://discourse.libsdl.org/u/Miguel_Pragier)\
**Post date:** [November 5, 2007, 7:14pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/4 "2007-11-05T19:14:06Z")

</div>

Torsten Giebl \<wizard [syntheticsw.com](http://syntheticsw.com)\> writes:

> Hello !
> 
> > When I use DirectX, my screen is flickering.  
> > Is it common?
> 
> What do you mean with “flickering” ? Do you mean the same  
> thing that happens when playing games with VSYNC turned off ?
> 
> CU

Hi, Mr. Giebl.

The screen keeps blinking.  
And blinking hard!

---

<div class="post-metadata">

**Author:** ![Miguel\_Pragier](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@Miguel\_Pragier](https://discourse.libsdl.org/u/Miguel_Pragier)\
**Post date:** [November 5, 2007, 7:20pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/5 "2007-11-05T19:20:45Z")

</div>

David Olofson \<david [olofson.net](http://olofson.net)\> writes:

> > When I use DirectX, my screen is flickering.
> 
> How? Directly or via SDL? If the latter, are you using SDL 1.2  
> (stable) or 1.3 (development)? (These have rather different video  
> subsystems.)

Thru SDL 1.2.

> I don’t think you would be able to set up a display at all if it  
> wasn’t.

Yes, now I know 😉

Every modern videoboard supports DoubleBuffering?  
Or is this technique obsolete?

Thank you, mr. Olofson.\> On Monday 05 November 2007, Miguel Pragier wrote:

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [November 5, 2007, 7:27pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/6 "2007-11-05T19:27:29Z")

</div>

[…]

> The screen keeps blinking.  
> And blinking hard!

This sounds like the typical result of setting up a double buffered  
display, drawing something and then going into a loop, flipping.

Basically, you need to draw stuff twice, or it’ll only be visible  
every other frame, creating massive flickering.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- [http://olofson.net](http://olofson.net) - Games, SDL examples -------.  
| [http://zeespace.net](http://zeespace.net) - 2.5D rendering engine |  
| [http://audiality.org](http://audiality.org) - Music/audio engine |  
| [http://eel.olofson.net](http://eel.olofson.net) - Real time scripting |  
'-- [http://www.reologica.se](http://www.reologica.se) - Rheology instrumentation --'On Monday 05 November 2007, Miguel Pragier wrote:

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [November 5, 2007, 7:44pm UTC](https://discourse.libsdl.org/t/flickering-dx/14947/7 "2007-11-05T19:44:26Z")

</div>

> David Olofson \<david [olofson.net](http://olofson.net)\> writes:
> 
> > > When I use DirectX, my screen is flickering.
> > 
> > How? Directly or via SDL? If the latter, are you using SDL 1.2  
> > (stable) or 1.3 (development)? (These have rather different video  
> > subsystems.)
> 
> Thru SDL 1.2.
> 
> > I don’t think you would be able to set up a display at all if it  
> > wasn’t.
> 
> Yes, now I know 😉
> 
> Every modern videoboard supports DoubleBuffering?

Yes - and triple buffering, and other configurations as well. It’s  
mostly limited by drivers and/or APIs.

> Or is this technique obsolete?

It’s the only practical way of animating graphics without tearing or  
flickering, so I don’t think it’s going away any time soon. 🙂

However, there are many different ways of implementing double  
buffering! There are basically two scenarios that you have to be  
aware of when using SDL:

1. The “real”, efficient method, which is what most serious  
drivers use when possible, is to have two separate buffers  
that are directly available to the video card’s RAMDAC.  
This way, a “flip” is only a matter of changing a pointer -  
no copying of graphics data needed.  
Flips _may_ be synchronized with the display refresh to  
avoid pumping out frames that no one will see, and to avoid  
tearing. No guarantee though, as some platforms and drivers  
don’t support it, and many drivers come with it disabled  
in the driver settings by default.

2. Another rather common method, used in most cases on Linux  
(OpenGL and X11 alike), SDL in windowed mode on most  
platforms etc, is to have only one actual display buffer,  
and perform flipping by physically copying the “drawing page”  
into the display buffer when “flipping”.

The second approach is slower and makes retrace sync very hard to do  
properly (which causes tearing) - but it has the advantage that the  
application is always rendering into the very same buffer, which  
makes smart updates simpler to get right.

You can force SDL to use the second method by requesting a software  
display surface, but then you definitely lose the retrace sync, and  
have to rely on manual “throttling” to avoid a constant 100% CPU  
load. Well, you should actually have that throttling logic either  
way, as you don’t know if you’ll get retrace sync even if you get a  
hardware display surface.

Now, if you redraw the whole screen every frame (like a full screen  
scrolling game would do anyway), the difference between the modes is  
irrelevant an far as flickering is concerned, as you never depend on  
old data in the buffers. Obviously though, you don’t want to do this  
in applications with mostly still screens, as it’s just be a waste of  
CPU or GPU cycles and/or substantially raises the minimum system  
requirements.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- [http://olofson.net](http://olofson.net) - Games, SDL examples -------.  
| [http://zeespace.net](http://zeespace.net) - 2.5D rendering engine |  
| [http://audiality.org](http://audiality.org) - Music/audio engine |  
| [http://eel.olofson.net](http://eel.olofson.net) - Real time scripting |  
'-- [http://www.reologica.se](http://www.reologica.se) - Rheology instrumentation --'On Monday 05 November 2007, Miguel Pragier wrote:

> > On Monday 05 November 2007, Miguel Pragier wrote:
