Anti-Grain Geometry Tutorial
tutorial_path_2.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 redColor (0xff, 0, 0, 0xff);
51 
52  // clear the buffer with transparent white color
53  rBase.clear(transparentWhiteColor);
54 
56  agg::scanline_p8 scanline;
57 
58  ras.auto_close(false);
59 
60  // draw a red star
61  {
62  double star[] =
63  {
64  50, 3,
65  20, 97,
66  95, 37,
67  5, 37,
68  80, 97,
69  50, 3 // close the polygon
70  };
71 
72  SimplePath path (star, sizeof(star) / sizeof(double));
73 
74  ras.reset();
75  ras.filling_rule(agg::fill_even_odd);
76  ras.add_path(path);
77 
78  agg::render_scanlines_aa_solid(ras, scanline, rBase, redColor);
79  }
80 
81  // now write the image out for visualization
82  char fileName[1000] = { 0 };
83  if (argc > 1)
84  {
85  sprintf (fileName, "%s/", argv[1]);
86  }
87  strcat(fileName, "tutorial_path_2.png");
88  writePng<RendererBaseType> (fileName, rBase);
89 
90  delete imageBuffer;
91  }
92  catch (TutorialException& ex)
93  {
94  printf ("%s\n", ex.getMessage());
95  return 1;
96  }
97  catch (...)
98  {
99  printf ("Unknown exception detected.\n");
100  return 1;
101  }
102 
103  return 0;
104 }