This project is read-only.

2. Drawing Primitives - Tutorial

Drawing primitives is the easiest part of Vortex2D Canvas.API. Each primitive can be drawn with one method call. Sad, but Vortex2D still has poor support of primitive drawing without antialiasing support. There are available line, rectangle, ellipse, polyline, polygon, filled rect, filled ellipse. That is all for now.

NB. All drawing stuff should be placed into Render() method of main game application class.

Before drawing new frame it is better to clean up last frame content. Canvas2D.Clear method do this filling target surface (or part of surface) with specified color.

canvas.Clear(new ColorU(40, 40, 40));
Drawing lines is very simple operation. Canvas2D.DrawLine requires start, end points and line color.

canvas.DrawLine(20, 50, 200, 100, ColorU.White);
canvas.DrawLine(new Vector(20, 50), new Vector(200, 100), ColorU.White);
Canvas2D.DrawRect requires rectangle attributes: top-left point and size or Rect structure + color.

canvas.DrawRect(20, 130, 150, 80, ColorU.Blue);
canvas.DrawRect(new Rect(20, 130, 170, 210), ColorU.Blue);
Canvas2D.DrawEllipse requires the same aguments as DrawRect nethod. It draws ellipse inside of rect specified in arguments.

canvas.DrawEllipse(20, 250, 200, 80, ColorU.LimeGreen);

Source information for drawing polylines and ploygons is array of vectors (or its part). Only one difference between Canvas2D.DrawPolyline and Canvas2D.DrawPolygon is last one adds extra line between first and last point of array.

Vector[] polylinePoints = new Vector[] {
    new Vector(10, 25),
    new Vector(new System.Drawing.SizeF(630, 25)),
    new Vector(630, 470)

canvas.DrawPolyline(polylinePoints, ColorU.Yellow);

Vector[] polygonPoints = new Vector[] {
    new Vector(400),
    new Vector(45, 41) * 10,
    new Vector(160, 170) + new Vector(300, 300),
    new Vector(-420, -420).Negate(),
    new Vector(new System.Drawing.PointF(340, 440))

canvas.DrawPolygon(polygonPoints, ColorU.White);

canvas.DrawPolygon(polygonPoints, 1, 3, ColorU.Red);

Canvas2D.DrawFilledRect and Canvas2D.DrawFilledEllipse has absolutely the same signature as methods without "filled". Difference only in shading method.
canvas.DrawFilledRect(320, 130, 150, 80, ColorU.Blue);

canvas.DrawFilledEllipse(320, 180, 200, 180, ColorU.LimeGreen.MultiplyAlpha(0.4f));


Vortex2D.NET has a set of primitive drawing functions. It is not to much yet but it could be useful for drawing some diagnostics or simple geometry stuff. It is still planned to add ability to draw arcs, filled polygons, bezier curves etc in next releases.

Last edited Dec 20, 2010 at 6:22 PM by AlexKhomich, version 6


No comments yet.