main.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Taken from
  2. // http://cgit.freedesktop.org/cairomm/plain/examples/surfaces/image-surface.cc
  3. /* M_PI is defined in math.h in the case of Microsoft Visual C++, Solaris,
  4. * et. al.
  5. */
  6. #if defined(_MSC_VER)
  7. #define _USE_MATH_DEFINES
  8. #endif
  9. #include <cairomm/context.h>
  10. #include <cairomm/surface.h>
  11. #include <cairommconfig.h>
  12. #include <iostream>
  13. #include <string>
  14. #include <cmath>
  15. int main()
  16. {
  17. Cairo::RefPtr<Cairo::ImageSurface> surface =
  18. Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 600, 400);
  19. Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface);
  20. cr->save(); // save the state of the context
  21. cr->set_source_rgb(0.86, 0.85, 0.47);
  22. cr->paint(); // fill image with the color
  23. cr->restore(); // color is back to black now
  24. cr->save();
  25. // draw a border around the image
  26. cr->set_line_width(20.0); // make the line wider
  27. cr->rectangle(0.0, 0.0, surface->get_width(), surface->get_height());
  28. cr->stroke();
  29. cr->set_source_rgba(0.0, 0.0, 0.0, 0.7);
  30. // draw a circle in the center of the image
  31. cr->arc(surface->get_width() / 2.0, surface->get_height() / 2.0,
  32. surface->get_height() / 4.0, 0.0, 2.0 * M_PI);
  33. cr->stroke();
  34. // draw a diagonal line
  35. cr->move_to(surface->get_width() / 4.0, surface->get_height() / 4.0);
  36. cr->line_to(surface->get_width() * 3.0 / 4.0,
  37. surface->get_height() * 3.0 / 4.0);
  38. cr->stroke();
  39. cr->restore();
  40. #ifdef CAIRO_HAS_PNG_FUNCTIONS
  41. std::string filename = "image.png";
  42. surface->write_to_png(filename);
  43. std::cout << "Wrote png file \"" << filename << "\"" << std::endl;
  44. #else
  45. std::cout
  46. << "You must compile cairo with PNG support for this example to work."
  47. << std::endl;
  48. #endif
  49. }