Click here to Skip to main content
15,886,724 members
Articles / Programming Languages / C#

"Paint.exe" in CsGL

Rate me:
Please Sign up or sign in to vote.
3.91/5 (11 votes)
16 Dec 20037 min read 136.7K   4.3K   37  
Imitate Windows Paint.exe using C# and CsGL
using System;
using System.Drawing;
using System.Diagnostics;
using CsGL.OpenGL;

namespace _2D_draw
{
	/// <summary>
	/// Base class for the free draw (points) , the line draw object, and others as we add them
	/// </summary>
	public  class DrawingObject
	{
		protected int color;
		protected int Height;		// height of the view where we are plotting
		protected int TopWidth;	// thickness of the top of the form (menu bar, etc)
		protected int CursorMid;	// off set to the crosshair cursor mid point
		protected string strType;	// hold the "Pencil", "Line" type
		public uint ID;			// for hit testing

		public  DrawingObject()
		{
			Height = 100;		// just so it will never be undefined
			TopWidth = 20;
			CursorMid = 4;			
			ID = 0;
		}

		/// <summary>
		/// we need the width of the top bar on the form, and we need the height of
		/// the cursor
		/// </summary>
		/// <param name="h">height</param>
		/// <param name="t">top width</param>
		/// <param name="c">cursor height</param>
		public   void SetUp( int h , int t , int c , uint id)
		{
			Height = h;		// height of the view where we are plotting
			TopWidth = t;	// thickness of the top of the form (menu bar, etc)
			CursorMid = c;	// off set to the crosshair cursor mid point
			ID = id;		// for hit testing
		}

		// put the coord transform we need to relate the cursor position to the dot position
		protected Point transform(int iX , int iY)
		{
			//Trace.WriteLine("DrawingObjectBase.transform: " + iX.ToString() +"  "+ iY.ToString()); 

			Point p = new Point();
			p.X = iX;
			p.Y = (Height - iY) - TopWidth + CursorMid ; // transform window coor to open gl coord
			return p;
		}

		public virtual void Draw( uint mode , uint highlight)
		{
		}

		public virtual void HandleMouseDown(System.Windows.Forms.MouseEventArgs e){}

		public virtual void HandleMouseUp(System.Windows.Forms.MouseEventArgs e){}

		public virtual void HandleMouseMove(System.Windows.Forms.MouseEventArgs e){}

		protected void DrawLine( Point pStart , Point pEnd )
		{
			if ( pStart == pEnd ) return;
			GL.glBegin( GL.GL_LINES );
			GL.glVertex2i( pStart.X , pStart.Y );
			GL.glVertex2i( pEnd.X , pEnd.Y );
			GL.glEnd();
		}


		protected void DrawDot( Point p )
		{
			DrawDot( p.X , p.Y );
		}

		protected void DrawDot( int ix , int iy )
		{
			GL.glBegin( GL.GL_POINTS );
			GL.glVertex2i( ix , iy );
			GL.glEnd();
		}


		#region PrintError
		// **************************************************
		// pretty print the error
		// http://www.opengl.org/documentation/blue_book_1.0/book/ch05.html#id5488181
		public void PrintGLError( uint err, String from)
		{
			if ( err == GL.GL_NO_ERROR ) return;	// don't print
			if ( from != "" ) System.Console.WriteLine("GL Error: " + from);
			PrintGLError( err);
		}

		void PrintGLError( uint err)
		{
			String pretty = "";

			switch ( err )
			{
				case GL.GL_NO_ERROR:
					pretty = "GL_NO_ERROR ";
					pretty = pretty + "No error has been recorded. The value of this symbolic constant is guaranteed to be zero. ";
					break;
				case GL.GL_INVALID_ENUM:
					pretty = "GL_INVALID_ENUM ";
					pretty = pretty + "An unacceptable value is specified for an enumerated argument. The offending command is ignored, having no side effect other than to set the error flag ";
					break;
				case GL.GL_INVALID_VALUE:
					pretty = "GL_INVALID_VALUE ";
					pretty = pretty + "A numeric argument is out of range. The offending command is ignored, having no side effect other than to set the error flag.  ";
					break;
				case GL.GL_INVALID_OPERATION:
					pretty = "GL_INVALID_OPERATION ";
					pretty = pretty + "The specified operation is not allowed in the current state. The offending command is ignored, having no side effect other than to set the error flag.  ";
					break;
				case GL.GL_STACK_OVERFLOW:
					pretty = "GL_STACK_OVERFLOW ";
					pretty = pretty + "This command would cause a stack overflow. The offending command is ignored, having no side effect other than to set the error flag.  ";
					break;
				case GL.GL_STACK_UNDERFLOW:
					pretty = "GL_STACK_UNDERFLOW ";
					pretty = pretty + "This command would cause a stack underflow. The offending command is ignored, having no side effect other than to set the error flag.  ";
					break;
				case GL.GL_OUT_OF_MEMORY:
					pretty = "GL_OUT_OF_MEMORY ";
					pretty = pretty + "There is not enough memory left to execute the command. The state of the GL is undefined, except for the state of the error flags, after this error is recorded.  ";
					break;
			}
			System.Console.WriteLine("GL Error: " + pretty);
		}
		#endregion PrintError


		public string type
		{
			get
			{
				return strType;
			}
		}

		public string comment
		{
			get
			{
				return null;
			}
			set
			{
			}
		}


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions