• Advertisement
Sign in to follow this  

C# GDI - Screen is choppy

This topic is 3903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am new to games programming and am working on something simple but haven't gotten very far. I have the following C# code that creates a black 800x600 window that displays the FPS in the bottom right corner. Problem is that the FPS is displayed choppy and has lines running through it. It is getting 600 - 700 FPS but looks choppy. I ran it on a better machine with the same results just an increses FPS (1200 -1400). If you could point out the problem to me or any other suggestions for improving my code would be greatly appreciated.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace gdi
{
	public class MainForm : Form
	{
		public static DateTime startTime;
		private int frames;
		private float fps;
		private TimeSpan elapsed;

		public MainForm()
		{
			this.Initialize();
		}

		static void Main()
		{
			Application.Run(new MainForm());
		}

		private void Initialize()
		{
			startTime = DateTime.Now;
			frames = 0;
			this.Size = new Size(800,600);
			this.Text = "GDI+";
			this.StartPosition = FormStartPosition.CenterScreen;
			this.FormBorderstyle = FormBorderstyle.None;
			this.Cursor.Dispose();

			// IMPORTANT: Tell Windows that we will do all
			// drawing in the Paint method
			this.Setstyle(Controlstyles.AllPaintingInWmPaint |
				Controlstyles.Opaque, true);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			e.Graphics.FillRectangle(new SolidBrush(Color.Black), this.ClientRectangle);

			//Calculate Frames Per Second (FPS)
			fps = 0;
			elapsed = DateTime.Now - MainForm.startTime;
	
			if(elapsed.Seconds > 0)
				fps = frames/(elapsed.Seconds);

			e.Graphics.DrawString("FPS:" + fps,new Font("Verdana",10),new SolidBrush(Color.Red),new Point(700,500));
			frames++;

			// IMPORTANT: Ensure that we are called again as
			// soon as we are drawn
			this.Invalidate();
		}

		protected override void OnKeyDown(KeyEventArgs e)
		{
			base.OnKeyDown(e);

			if ((int)(byte)e.KeyCode == (int)Keys.Escape)
				this.Close();

			this.Invalidate();
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Using GDI isn't a fast way to do your rendering, and from what I've tried, continually repainting a scene and C# forms don't mix too well. I think you'll have better luck with SDL, as it also has a .NET binding available.

EDIT: Next time, surround your code with [ source ] [ /source ] tags. Makes posts that contain a lot of code much easier to read. ;)

Share this post


Link to post
Share on other sites
justinbezanson,

Your text is choppy not because your screen isn't rendering quickly enough, it appears choppy because by default WinForms/GDI+ render to a single buffer.

You need to enable double buffering. Once you do, you'll be fine. To enable Double Buffering, go to your set style line and change it to:


// IMPORTANT: Tell Windows that we will do all drawing in the Paint method
this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque | Controlstyles.DoubleBuffer , true);


Cheers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement