×
Namespaces

Variants
Actions
(Difference between revisions)

Avoid flickering with double buffering on Symbian

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
Forum Nokia KB (Talk | contribs)
Line 11: Line 11:
 
|-
 
|-
 
|'''Category''' || Symbian C++
 
|'''Category''' || Symbian C++
|'''Subcategory''' || Code Examples
+
|'''Subcategory''' || Graphics
 
|-
 
|-
 
|}
 
|}
Line 22: Line 22:
  
 
==Overview==
 
==Overview==
When drawing images directly on the screen and there exists some animation
+
When images are drawn directly on the screen and there is an animation, the screen may flicker. A common technique to solve this is to draw images on an off-screen buffer,
can screen flicker.
+
A common technique to solve this is to draw images on an off-screen buffer,
+
 
and them copy its contents to the screen when the drawing operations are finished.
 
and them copy its contents to the screen when the drawing operations are finished.
  
Following example shows how to implement this technique. There is neede two buffers,
+
The following example shows how to implement this technique. Two buffers are needed,
hence the term "double buffering". This off-screen buffer has the same dimensions of the screen than
+
hence the term "double buffering". This off-screen buffer has the same screen dimensions as the original one.
the original one.
+
  
 
==MMP file==
 
==MMP file==
Line 40: Line 37:
  
 
==Header==
 
==Header==
Here is example header of CCoeControl componet where double buffering is enabled
+
Here is an example header of the CCoeControl component where double buffering is enabled.
 
<code cpp>
 
<code cpp>
 
#include <coecntrl.h>
 
#include <coecntrl.h>
Line 81: Line 78:
  
 
==Source==
 
==Source==
There is source code only double buffering-related data
+
This source code is only for data related to double buffering:
 
<code cpp>
 
<code cpp>
 
void CMyContainer::SizeChanged()
 
void CMyContainer::SizeChanged()
Line 164: Line 161:
  
 
==Using==
 
==Using==
Old way was
+
Previously this was done as follows:
 
<code cpp>
 
<code cpp>
 
iAppContainer->DrawNow();
 
iAppContainer->DrawNow();
 
</code>
 
</code>
  
and now with double buffering
+
and now with double buffering:
 
<code cpp>
 
<code cpp>
 
iAppContainer->DrawToBackBuffer();
 
iAppContainer->DrawToBackBuffer();
Line 176: Line 173:
  
 
==Postconditions==  
 
==Postconditions==  
No anymore flickering
+
The screen does not flicker any more.
  
 
==See also==
 
==See also==
Line 183: Line 180:
 
[http://en.wikipedia.org/wiki/Double_buffering Double buffering in Wikipedia]
 
[http://en.wikipedia.org/wiki/Double_buffering Double buffering in Wikipedia]
  
[[Category:Symbian C++]][[Category:Code Examples]]
+
[[Category:Symbian C++]][[Category:Code Examples]][[Category:Graphics]]

Revision as of 10:39, 1 July 2008


Template:KBCS

ID CS001049 Creation date July 1, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95
Category Symbian C++ Subcategory Graphics


Keywords (APIs, classes, methods, functions): double buffering, CFbsBitmapDevice, CFbsBitGc, CFbsBitmap

Overview

When images are drawn directly on the screen and there is an animation, the screen may flicker. A common technique to solve this is to draw images on an off-screen buffer, and them copy its contents to the screen when the drawing operations are finished.

The following example shows how to implement this technique. Two buffers are needed, hence the term "double buffering". This off-screen buffer has the same screen dimensions as the original one.

MMP file

The following capabilities and libraries are required:

CAPABILITY        NONE
LIBRARY fbscli.lib
LIBRARY bitgdi.lib

Header

Here is an example header of the CCoeControl component where double buffering is enabled.

#include <coecntrl.h>
#include <bitstd.h>
#include <bitdev.h>
 
class CMyContainer : public CCoeControl
{
public:
void NewL(const TRect& aRect)
void ConstructL(const TRect& aRect);
virtual ~CImageConverterContainer();
private:
void CMyContainer();
void SizeChanged();
void HandleResourceChange(TInt aType);
TInt CountComponentControls() const;
CCoeControl* ComponentControl(TInt aIndex) const;
void Draw(const TRect& aRect) const;
 
// for Double buffering
public:
void DrawToBackBuffer();
// for Double buffering
private:
void CreateBackBufferL();
void ReleaseBackBuffer();
 
// for Double buffering
private:
CFbsBitmap* iBackBuffer;
CFbsBitmapDevice* iBackBufferDevice;
CFbsBitGc* iBackBufferContext;
TSize iBackBufferSize;
 
// class other member variables if needed
private:
};

Source

This source code is only for data related to double buffering:

void CMyContainer::SizeChanged()
{
// Delete back buffer and create a new one with new size
ReleaseBackBuffer();
CreateBackBufferL();
DrawToBackBuffer();
}
 
void CMyContainer::CreateBackBufferL()
{
// Create back buffer bitmap
iBackBuffer = new (ELeave) CFbsBitmap;
 
User::LeaveIfError( iBackBuffer->Create(Size(),
iEikonEnv->DefaultDisplayMode()));
 
// Create back buffer graphics context
iBackBufferDevice = CFbsBitmapDevice::NewL(iBackBuffer);
User::LeaveIfError(iBackBufferDevice->CreateContext(iBackBufferContext));
iBackBufferContext->SetPenStyle(CGraphicsContext::ESolidPen);
 
iBackBufferSize = iBackBuffer->SizeInPixels();
}
 
void CMyContainer::ReleaseBackBuffer()
{
// Release double buffering classes
if (iBackBufferContext)
{
delete iBackBufferContext;
iBackBufferContext = NULL;
}
if (iBackBufferDevice)
{
delete iBackBufferDevice;
iBackBufferDevice = NULL;
}
if (iBackBuffer)
{
delete iBackBuffer;
iBackBuffer = NULL;
}
iBackBufferSize = TSize(0, 0);
}
 
void CMyContainer::DrawToBackBuffer()
{
if (!iBackBufferContext)
{
return;
}
 
iBackBufferContext->Clear();
 
// TODO: do your drawing there.
// Remember to draw to iBackBufferContext buffered graphic contex
 
// Example of drawing bitmap into iBackBufferContext
//if(iBitmap)
// {
// iBackBufferContext->BitBlt( iPicturePoint, iBitmap );
// }
}
 
void CMyContainer::Draw(const TRect& aRect) const
{
CWindowGc& gc = SystemGc();
 
// Copy backbuffer into the screen
gc.BitBlt(TPoint(0, 0), iBackBuffer);
 
// TODO: Remove your all drawing from there to DrawToBackBuffer
}
 
CMyContainer::~CMyContainer()
{
ReleaseBackBuffer();
}

Using

Previously this was done as follows:

iAppContainer->DrawNow();

and now with double buffering:

iAppContainer->DrawToBackBuffer();
iAppContainer->DrawNow();

Postconditions

The screen does not flicker any more.

See also

Anti-tearing with CDirectScreenBitmap

Double buffering in Wikipedia

116 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×