#include "peer/components/core/TdkException.h" #include "peer/components/graphics/CD/TdkCDCanvas.h" #include "peer/components/core/services/TdkAbstractGraphicalService.h" #include "peer/components/core/TdkSetup.h" #include "peer/components/core/TdkDefaultGraphicalInitializer.h" void DrawText(TdkCanvas& canvas); void DrawPolygon(TdkCanvas& canvas); void DrawLine (TdkCanvas& canvas); void DrawPoint (TdkCanvas& canvas); void exportarImagem(TdkCanvas& canvas, char path[]); /* keywords ------------------- DrawCanvas Canvas DrawText DrawPolygon DrawLine DrawPoint GraphicalService Graphic Graphical LineStyleCustomDashes LineStylePeriodicSymbol PointStyleChar PolygonStyleBasic LineStyleBasic PointStyleBasic ------------------- description: Tutorial of the Graphical Module that shows how to Draw a point, line, polygon and text with and without styles on canvas. */ void main(int argc, char* argv[]) { TdkDefaultGraphicalInitializer graphicalInitializer; try { /* Starting setup - start tdk´s services (in this case we only need the Graphical Service) */ TdkSetup::getInstance()->initializeGraphics(graphicalInitializer); catch( TdkException& ex ) { } try { std::cout << "Running application, please wait...\n"; /*** Create canvas param ***/ /* Create a NO projection */ TeNoProjection projection; /* Images width and height (definition of the viewport) */ int image_width = 640; int image_height = 480; TdkImageRGB* image = new TdkImageRGB(image_width, image_height); /* World´s initial coordinates of the canvas (definition of the window)*/ double wx1 = 0.0; double wy1 = 0.0; double wx2 = static_cast(image_width); double wy2 = static_cast(image_height); TeBox world = TeBox(wx1,wy1,wx2,wy2); /* Defining the background color (gray) */ int backcolor_red = 127; int backcolor_green = 127; int backcolor_blue = 127; TeColor back_color = TeColor(backcolor_red,backcolor_green, backcolor_blue); /* Instance the canvas */ TdkCDCanvas* canvas = new TdkCDCanvas(*image, world, projection, back_color); if (canvas == NULL) throw TdkException(_TDK_DEBUG,TdkException::OutOfMemory,"TdkCanvas"); /* Calling to the draw and export image funcitons */ DrawText(*canvas); exportarImagem(*canvas, "text.gif"); DrawLine(*canvas); exportarImagem(*canvas, "line.gif"); DrawPoint(*canvas); exportarImagem(*canvas, "point.gif"); DrawPolygon(*canvas); exportarImagem(*canvas, "polygon.gif"); if ( canvas ) { delete canvas; canvas = NULL; } std::cout << "End of the execution .. \nImages saved " << "with success on project's directory\n"; std::cout << "Press any key to exit."; getchar(); } catch ( TdkException& ex ) { std::cout << ex.trace(); } } void exportarImagem(TdkCanvas& canvas, char path[]){ TdkImageFormatted * imagem = TdkSetup::getGraphicalService().createImage(&canvas, TDK_IMAGE_FORMAT_GIF); FILE *hFile = NULL; hFile = fopen( path, "wb" ); if ( hFile != NULL ) { fwrite(imagem->getBuffer(), 1, imagem->getBufferSize(), hFile); fclose( hFile ); } if ( imagem ) { delete imagem; imagem = NULL; } } void DrawPoint (TdkCanvas& canvas) { // Clearing the canvas canvas.clear(); // Setting up point size and point color canvas.setPointSize(50); canvas.setPointColor(0, 255, 255, 0); // Drawing two types of Points for example: STAR and CIRCLE // First at position (150,150) and second one at position (350,350) canvas.setPointType(TePtTypeStar); canvas.drawPoint(150,150); canvas.setPointType(TePtTypeCircle); canvas.drawPoint(350,350); } void DrawLine (TdkCanvas& canvas) { // Clearing the canvas canvas.clear(); TeLine2D line; TeCoord2D point1, point2; // Setting up points, color, width and type of the line point1.setXY(60, 50); point2.setXY(600, 50); line.add(point1); line.add(point2); canvas.setLineColor(0, 255, 255, 0); canvas.setLineWidth(1); canvas.setLineType(TeLnTypeDashed); canvas.drawLine(line); line.clear(); // Setting up points, color, width and type of the line point1.setXY(60, 350); point2.setXY(600, 350); line.add(point1); line.add(point2); canvas.setLineColor(0, 255, 255, 0); canvas.setLineWidth(1); canvas.setLineType(TeLnTypeDotted); canvas.drawLine(line); } void DrawPolygon(TdkCanvas& canvas) { // Clearing the canvas canvas.clear(); TeCoord2D cord; TeLinearRing ring; TePolygon poly; // Setting up the coordinates of the polygon cord.setXY(230, 150); ring.add(cord); cord.setXY(450, 250); ring.add(cord); cord.setXY(350, 450); ring.add(cord); cord.setXY(230, 150); ring.add(cord); poly.add(ring); // Setting up polygon type, border type, border width, border color and polygon color canvas.setPolygonType(TePolyTypeDiagonalCross); canvas.setPolygonBorderType(TeLnTypeDashDotDot); canvas.setPolygonBorderWidth(1); canvas.setPolygonBorderColor(0, 0, 0, 0); canvas.setPolygonColor(0, 255, 255, 0); canvas.drawPolygon(poly); } void DrawText(TdkCanvas& canvas) { // Clearing the canvas canvas.clear(); int ret = -1; // Setting up background type, text angle, text size, text color canvas.setTextBackgroundType(0); canvas.setTextAngle(0); canvas.setTextSize(30); canvas.setTextColor(0, 255, 255, 0); canvas.drawText(150, 200, "TDK TUTORIAL"); }