Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Flickering problem with panel  (Read 5249 times)

0 Members and 1 Guest are viewing this topic.

TheR89

  • Newbie
  • *
  • Posts: 3
    • View Profile
Flickering problem with panel
« on: May 07, 2010, 11:28:00 pm »
Hello SFML-Community!

I'm working on a small game with a friend of mine to learn C++ combined with SFML. (I've already experience with various programming languages, so I'm not a beginner! ;))

Now, we want to develop a small map editor for our game. Because I've advanced skills in C# and Windows Forms, I chose this language, esspecially to concentrate on the code besides the GUI-stuff.

Before I start with the design, I wanted to try the SFML.NET binding, so I can use it also in Windows Forms applications. I got the RenderWindow to draw it stuff into an User Control, derived from a panel. I added this panel to my form and it would draw the sprite correctly - but there's still a problem: the panel flickers a lot!

I've already searched the forum and changed the styles of my panel (UserPaint, OptimizedDoubleBuffer, AllPaintingInWmPaint); the control is still flickering.

Here's the code of my User Control:
Code: [Select]
using System;
using System.Windows.Forms;

namespace Editor.UserControls
{
    public partial class sfmlPanel : Panel
    {
        // Timer for refreshing the panel
        System.Windows.Forms.Timer timer = new Timer();

        // SFML-stuff
        SFML.Graphics.RenderWindow sfml_RWnd;
        SFML.Graphics.Image image;
        SFML.Graphics.Sprite sprite;

        // Constructor
        public sfmlPanel()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            UpdateStyles();
           
            // Raise the timer_tick-event after 10ms
            timer.Interval = 10;
            // Adds the timer_tick-event
            timer.Tick += new EventHandler(timer_Tick);
            // Activate the timer
            timer.Enabled = true;

            // Set the handle for the RenderWindow to the panel
            sfml_RWnd = new SFML.Graphics.RenderWindow(this.Handle);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            // Refresh the panel
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            sfml_RWnd.Clear(SFML.Graphics.Color.Blue);
            sfml_RWnd.Draw(sprite);
            sfml_RWnd.Display();
        }

        public void AddSprite(SFML.Graphics.Sprite srcSprite)
        {
            sprite = srcSprite;
            sprite.Position = new SFML.Graphics.Vector2(20, 20);
        }
    }
}


And here's the code to include the panel to my form:
Code: [Select]

...

UserControls.sfmlPanel panMap;
SFML.Graphics.Image sfml_Image;
SFML.Graphics.Sprite sfml_Sprite;
...

// Constructor
public frmMain()
{
...
  panMap = new UserControls.sfmlPanel();
  panMap.Top = 0;
  panMap.Left = 0;
  panMap.Width = 200;
  panMap.Height = 200;
  sfml_Image = new SFML.Graphics.Image("tile_01.png");
  sfml_Sprite = new SFML.Graphics.Sprite(sfml_Image);
  sfml_Sprite.Position = new SFML.Graphics.Vector2(20, 20);
  panMap.AddSprite(sfml_Sprite);
  this.Controls.Add(panMap);
...
}


Maybe I forgot just a small stupid detail and you can help me?

Thanks in advance! :)

Best regards

PS: I'm sorry for my English, but I'm naturally speaking German!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Flickering problem with panel
« Reply #1 on: May 08, 2010, 01:46:48 pm »
Hi

Similar issues have already been solved on this forum, have you looked at the corresponding discussions?
Laurent Gomila - SFML developer

TheR89

  • Newbie
  • *
  • Posts: 3
    • View Profile
Flickering problem with panel
« Reply #2 on: May 08, 2010, 02:49:16 pm »
Hi Laurent!

Thank you for your posting!

I've already searched the forums the last days for that problem and also found some solutions. After all, I didn't find any working one and the control is still flickering.

I've tried the way with SetStyle, as mentioned here, but it didn't work anyway.
Besides the above way, I tried to override the Paint-Event of the Panel (what is actually necessary). In detail, I tried it with overriding OnPaint() and adding a PaintEventHandler - still the same situation as before.

It seems like the panel gets erased shortly after painting the SFML-stuff.

I'll try to build up a whole new Windows Form project this day and try it again.

Best regards and thanks again!

EDIT: I tried a new project, but the problem is still the same. It must be something wrong with the code.

TheR89

  • Newbie
  • *
  • Posts: 3
    • View Profile
Flickering problem with panel
« Reply #3 on: May 09, 2010, 04:12:25 pm »
I've solved the problem!

The flackering is caused by double buffering of Windows Forms.

I just had to remove this line out of the constructor:
Code: [Select]
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

Maybe this will also help other people with the same problem.

Thanks again for your answer, Laurent!

Best regards!