CD - Canvas Draw


Simple Draw

This is an example of a simple drawing program using a IUP canvas:

cdCanvas* canvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(IupCanvas,"CONID")); 
cdCanvasLineStyle(canvas, CD_DASHED);
cdCanvasLine(canvas, 0, 0, 100, 100);

If you want to use World Coordinates:

cdCanvas* canvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(IupCanvas,"CONID")); 
wdCanvasViewport(canvas, 0, 100, 0, 100);
wdCanvasWindow(canvas, -1.5, 1.5, -3000, 3000);
cdCanvasLineStyle(canvas, CD_DASHED);
wdCanvasLine(canvas, -0.5, -500, 1.0, 1000);

Off Screen Drawing (Double Buffering)

To draw in the background and later on transfer the drawing to the screen, use:

cdCanvas* canvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(IupCanvas,"CONID")); 
cdCanvas* db_canvas = cdCreateCanvas(CD_DBUFFER, canvas); cdCanvasActivate(db_canvas); // update canvas size, window could be resized
cdCanvasLineStyle(db_canvas, CD_DASHED);
cdCanvasLine(db_canvas, 10, 10, 50, 50);
cdCanvasFlush(db_canvas); // swap to the window canvas

To draw in a RGB image, use:

cdCanvas* canvas = cdCreateCanvasf(CD_IMAGERGB, "%dx%d", width, height);
cdCanvasLineStyle(canvas, CD_DASHED); 
cdCanvasLine(canvas, 10, 10, 50, 50); 
unsigned char* red = cdCanvasGetAttribute(canvas, "REDIMAGE");
// do something with the raw image data

Lua Samples

To draw in a RGB image in CDLua for Lua 5:

imagergb = cd.CreateImageRGB(200,200)
canvas = cd.CreateCanvas(cd.IMAGERGB, imagergb)
canvas:Font("Times", cd.BOLD, 12)
canvas:Text(10, 10, "Test")

Check the file for several samples in Lua and samples in C to test them. You will need the CD, IUP and Lua libraries to compile and link the applications.

Screen Capture in Windows

Using a NULL parameter to the NATIVEWINDOW driver you can get access to the entire screen:

cdCanvas *canvas = cdCreateCanvas(CD_NATIVEWINDOW, NULL);
cdCanvasGetCanvasSize(canvas, &width, &height, NULL, NULL);
// allocate red, green and blue pointers
cdCanvasGetImageRGB(canvas, red, green, blue, 0, 0, width, height);

Complete Example

We have created an application called Simple Draw that illustrates the use of all functions in the CD library (including WD). You can see the source code in the simple.c file, or take the file for a complete set of files including makefiles for all platforms. Extract the files creating subfolders, using parameter "-d".

Example for Tests

The CDTEST example is actually one of the applications used to test virtually all functions of the CD library. Its interface uses the IUP library, and it can run in several platforms. You can take either the .EXE files or the source code. Extract the files creating subfolders, using parameter "-d". Warning: This application is not didactic.