CD - Canvas Draw

Sample Codes

Simple Draw

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

cdCanvas* Canvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(IupCanvas,"CONID"));
cdActivate(Canvas);
cdLineStyle(CD_DASHED);
cdLine(0, 0, 100, 100);
cdKillCanvas(Canvas);

If you want to use World Coordinates:

cdCanvas* Canvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(IupCanvas,"CONID"));
wdActivate(Canvas);
wdViewport(0, 100, 0, 100);
wdWindow(-1.5, 1.5, -3000, 3000);
cdLineStyle(CD_DASHED);
wdLine(-0.5, -500, 1.0, 1000);
cdKillCanvas(Canvas);

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"));
cdActivate(Canvas);
void* Image = cdCreateImage(100, 100);
cdCanvas* ImageCanvas = cdCreateCanvas(CD_IMAGE, Image);
cdActivate(ImageCanvas);
cdLineStyle(CD_DASHED);
cdLine(10, 10, 50, 50);
cdActivate(Canvas);
cdPutImage(Image, 0, 0);
cdKillImage(Image);
cdKillCanvas(ImageCanvas);
cdKillCanvas(Canvas);

For a more easier use of double buffering see the driver CD_DBUFFER.

To draw in a RGB image, use:

unsigned char* red = malloc(width * height);
unsigned char* gree = malloc(width * height);
unsigned char* blue = malloc(width * height);

cdCanvas* canvas = cdCreateCanvasf(CD_IMAGERGB, "%dx%d %p %p %p", width, height, red, green, blue);
cdActivate(canvas);
....
cdLineStyle(CD_DASHED); 
cdLine(10, 10, 50, 50); 
...
cdKillCanvas(canvas); 
...
free(red);
free(green);
free(blue);

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 simple.zip 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.