www.xbdev.net xbdev - software development
Wednesday November 20, 2019
home | about | contact | Donations
>>
     
 

Programming..

With C# and .Net...

 


Using a screen buffer...

 

 

Writing directly to the screen can look nasty at times...watching all those bits and pieces...and text being drawn bit by bit....instead its a lot better to write to a back buffer.  Then when you've finished all your drawing/rendering...you just zoooOOoomm...flip it to the screen.  This is how most games are done as well.

 

DownloadSourceCode

 

Simple examples are always good...  As you can see by a screenshot on the right, the demo doesn't do much..and the graphics are just a bit of text and a few coloured squares..  But the secret to success, is to understand the fundamentals - this way you can see the code clearly and get an understanding of it from the start.  You should be able to build from it and add loads of functionality... working towards a GUI game like PacMan :)

 

 

Well things to note in the code, is that the drawing is done in the "Render()" function...so this is where we draw to a Bitmap object, then copy it to our screen.  Although I've not done so in this case, you would probably have a timer object which would call the render function repeatedly for a game demo.... every 30milliseconds or something... where it would update the characters position.

 

Alternatively you would only need to repaint your surface when its been made dirty...or you've made some changes to your graphics etc...  Its good to experiment and try new things...give it a go...see how simple C# really is.

 

Code: screen.cs

 

using System;

using System.Drawing;

using System.Windows.Forms;

 

class SimpleWindow : System.Windows.Forms.Form

{

      private PictureBox Screen;

 

      // This is where we render to a Bitmap img, then at the end, we just

      // copy it to our screen!

      void Render()

      {

            Bitmap img = new Bitmap(300, 300);

            Graphics g = Graphics.FromImage(img);

           

            g.FillRectangle(Brushes.Blue, 10, 10, 260, 250);

            g.FillRectangle(Brushes.Red, 100, 100, 50, 50);

 

            g.DrawString("xbdev - simple rendering", this.Font, Brushes.White, 15, 15);

            g.DrawString("(Boxes are easy to draw)", this.Font, Brushes.Black, 15, 35);

 

            // Apply the graphics buffer - so we put it to our screen.

            Screen.Image = img;

 

      } // End of Render()

 

      // Window constructor

      SimpleWindow()

      {

            this.Screen = new System.Windows.Forms.PictureBox();

            this.Screen.Name = "Screen";

            this.Screen.Size = new System.Drawing.Size(300, 300);

 

            // We need to add our screen buffer to our window

            this.Controls.AddRange(new System.Windows.Forms.Control[] {this.Screen});

 

            // Render to our backbuffer/screen.

            Render();

      }

 

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}// End of SimpleWindow Class

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.