Anti-Grain Geometry Tutorial
tutorial_path_1.cpp
1 /*
2  * Copyright (c) 2019 Heng Yuan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <agg_pixfmt_rgba.h>
17 #include <agg_rasterizer_scanline_aa.h>
18 #include <agg_renderer_base.h>
19 #include <agg_renderer_scanline.h>
20 #include <agg_scanline_p.h>
21 
22 #include "path.h"
23 #include "writepng.h"
24 
25 int
26 main (int argc, const char* argv[])
27 {
28  try
29  {
30  const int imageWidth = 100;
31  const int imageHeight = 100;
32 
35 
36  const int pixelSize = PixelFormat::pix_width;
37 
38  agg::rendering_buffer renderBuffer;
39  PixelFormat pixFmt;
40  RendererBaseType rBase;
41 
42  unsigned char *imageBuffer = new unsigned char[imageWidth * imageHeight * pixelSize];
43 
44  // Hook everything up
45  renderBuffer.attach (imageBuffer, imageWidth, imageHeight, imageWidth * pixelSize);
46  pixFmt.attach(renderBuffer);
47  rBase.attach(pixFmt);
48 
49  const agg::rgba8 transparentWhiteColor (0xff, 0xff, 0xff, 0);
50  const agg::rgba8 greenColor (0, 0xff, 0, 0xff);
51  const agg::rgba8 redColor (0xff, 0, 0, 0xff);
52 
53  // clear the buffer with transparent white color
54  rBase.clear(transparentWhiteColor);
55 
57  agg::scanline_p8 scanline;
58 
59  // disable auto close since we will enter them by ourselves.
60  //
61  // There will be a minor difference in the image if the last
62  // vertex which is used to close the polygon appears twice.
63  ras.auto_close(false);
64 
65  // draw a green square
66  {
67  double square[] =
68  {
69  0, 0,
70  0, 40,
71  40, 40,
72  40, 0,
73  0, 0 // close the polygon
74  };
75 
76  SimplePath path (square, sizeof(square) / sizeof(double));
77 
78  ras.reset();
79  ras.add_path(path);
80 
81  agg::render_scanlines_aa_solid(ras, scanline, rBase, greenColor);
82  }
83 
84  // draw a red star
85  {
86  double star[] =
87  {
88  50, 3,
89  20, 97,
90  95, 37,
91  5, 37,
92  80, 97,
93  50, 3 // close the polygon
94  };
95 
96  SimplePath path (star, sizeof(star) / sizeof(double));
97 
98  ras.reset();
99  ras.add_path(path);
100 
101  agg::render_scanlines_aa_solid(ras, scanline, rBase, redColor);
102  }
103 
104  // now write the image out for visualization
105  char fileName[1000] = { 0 };
106  if (argc > 1)
107  {
108  sprintf (fileName, "%s/", argv[1]);
109  }
110  strcat(fileName, "tutorial_path_1.png");
111  writePng<RendererBaseType> (fileName, rBase);
112 
113  delete imageBuffer;
114  }
115  catch (TutorialException& ex)
116  {
117  printf ("%s\n", ex.getMessage());
118  return 1;
119  }
120  catch (...)
121  {
122  printf ("Unknown exception detected.\n");
123  return 1;
124  }
125 
126  return 0;
127 }