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"));
cdActivate(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* green = 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);

Lua Samples

To draw in a RGB image in CDLua for Lua 3 (old names):

canvas_buf=cdCreateImageRGB(200,200)
canvas=cdCreateCanvas(CD_IMAGERGB,canvas_buf)
cdActivate(canvas)
cdFont(CD_TIMES_ROMAN, CD_BOLD, 8)
cdTextSize(10, 10, "Test")
cdKillCanvas(canvas)

and in CDLua for Lua 3 (new names) or Lua 5:

canvas_buf=cd.CreateImageRGB(200,200)
canvas=cd.CreateCanvas(cd.IMAGERGB,canvas_buf)
cd.Activate(canvas)
cd.Font(cd.TIMES_ROMAN, cd.BOLD, 8)
cd.TextSize(10, 10, "Test")
cd.KillCanvas(canvas)

Check the file samples_cdlua3.zip or samples_cdlua5.zip 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 *cd_canvas = cdCreateCanvas(CD_NATIVEWINDOW, NULL);
cdActivate(cd_canvas);

cdGetCanvasSize(&width, &height, NULL, NULL);
// allocate red, green and blue pointers

cdGetImageRGB(red, green, blue, 0, 0, width, height);

cdKillCanvas(cd_canvas);

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.