CD - Canvas Draw


Getting Started

The CD library is a basic graphic library (GL). In a GL paradigm you use primitives, which have attributes, to draw on a canvas. All the library functions reflect this paradigm.

The canvas is the basic element. It can have several forms: a paper, a video monitor, a graphic file format, etc. The virtual drawing surface where the canvas exists is represented by a driver. Only the driver knows how to draw on its surface. The user does not use the driver directly, but only the canvas.

To make the library simple we use the concept of an active canvas, over which all the primitives are drawn. This also allows the use of an expansion mechanism using function tables. Unfortunately if a function is called without an active canvas a memory invasion will occur. On the other hand, the mechanism allows the library to be expanded with new drivers without limits.

The attributes are also separated from the primitives. They reside in the canvas in a state mechanism. If you change the attribute's state in the canvas all the primitives drawn after that canvas and that depend on the attribute will be drawn in a different way.

The set of primitives is very small but complete enough to compose a GL. Some primitives are system dependent for performance reasons. Some drivers (window and device based) use system functions to optimally implement the primitives. Sometimes this implies in a in small different behavior of some functions. Also some primitives do not make sense in some drivers, like server images in file-based drivers.

The set of available functions is such that it can be implemented in most drivers. Some drivers have sophisticated resources, which cannot be implemented in other drivers but can be made available using a generic attribute mecanism.

Building Applications

All the CD functions are declared in the cd.h header file; World Coordinate functions are declared in the wd.h header file; and each driver has a correspondent header file that must be included to create a canvas. It is important to include each driver header after the inclusion of the cd.h header file.

To link the application you must add the cd.lib/libcd.a/ library to the linker options. If you use an IUP Canvas then you must also link with the cdiup.lib/libcdiup.a/ library.

In UNIX, CD uses the Xlib (X11) libraries. To link an application in UNIX, add also the "-lX11" option in the linker call.

The download files list includes the Tecgraf/PUC-Rio Library Download Tips document, with a description of all the available binaries.

Building the Library

The easiest way to build the library is to install the Tecmake tool into your system. It is easy and helps a lot. The Tecmake configuration files (*.mak) available at the "src" folder are very easy to understand also.

Tecmake is a command line multi compiler build tool available at Tecmake is used by all the Tecgraf libraries and many applications.

In CD's main source directory there is a file named make_uname (make_uname.bat in Windows) that build the libraries using Tecmake. To build the CD libraries for Windows using Visual C 7.0 for example, just execute make_uname.bat vc7 in the source folder.

But we also provide a stand alone makefile for Linux systems and a Visual Studio workspace with the respective projects. The stand alone makefile is created using Premake and a configuration file in lua called "premake.lua".

The library does not impose any specific compiler directive. Therefore, using it with the default compiler options would be enough to make things work. The library always has a static linking module, but on some platforms a dynamic linking library is also available. To compile the library it is necessary to define the symbol "__CD__". Internaly we use the definitions: "WIN32" and "SunOS", that must be defined in the respective systems.

Environment Variables

CDDIR - This environment variable is used by some drivers to locate useful data files, such as font definition files. It contains the directory path without the final slash.
CD_QUIET - In UNIX, if this variable is defined, it does not show the library's version info on sdtout.
CD_XERROR - In UNIX, if this variable is defined, it will show the X-Windows error messages on sdterr.

Implementing a Driver

The best way to implement a new driver is based on an existing one. For this reason, we provide a code of the simplest driver possible, see CDXX.H and CDXX.C. But first you should read the Internal Architecture.

Intercepting Primitives

To fill data structures of library primitives during a cdPlay call you must implement a driver and activate it before calling cdPlay. Inside your driver primitives you can fill your data structure with the information interpreted by the cdPlay function.

IUP Compatibility

The IupCanvas element of the IUP interface toolkit can be used as a visualization surface for a CD canvas. There are two moments in which one must be careful when an application is using both libraries: when creating the CD canvas, and when changing the size of the IUP canvas.

Creating the CD Canvas

The creation of the CD canvas must be made always after the IupCanvas element has been mapped in the system's native control. This happens when the application calls function IupShow or when the function IupMap is explicitally called.

Since a call to IupShow generates a call to the ACTION callback of the IUP canvas, we have a peculiar situation. The CD canvas cannot be created before IupShow, but if it is created after it one cannot draw on the first time the redrawing callback of the IUP canvas is called.

We can address this problem in several ways:

  • We can force the mapping prior to IupShow by calling the IupMap function before creating the CD canvas.
  • We can create the CD canvas after IupShow, but associating the canvas' redrawing callback also after IupShow and forcing a call to this function.
  • We can create the CD canvas during the redrawing callback or during the size change callback, which is also called during a IupShow.
  • We can create the canvas during the MAP_CB callback, which is called after the IupCanvas element has been mapped in the native control.

Any of the above solutions works perfectly. The most elegant solution seems to be the one that uses the MAP_CB callback.

Creating the CD canvas also requires some parameters to be passed to the Native Window driver. These parameters are obtained from the IUP canvas by means of the CONID attribute. Therefore, the canvas creation is:

myCdCanvas = cdCreateCanvas(CD_NATIVEWINDOW, IupGetAttribute(myIupCanvas, 
IupSetAttribute(myIupCanvas, "_CD_CANVAS", myCdCanvas);

The CD_IUP driver can still be used, but it must be linked with the cdiup library.

Resizing the IUP Canvas

If the application always activates the canvas before drawing, even if it is already active, then it is not necessary to worry about this situation. If this is not so, then the CD canvas mut be activated in the IUP canvas resize callback.