common.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef SIMPLE_GRAPHICAL_EXAMPLES_COMMON_H
  2. #define SIMPLE_GRAPHICAL_EXAMPLES_COMMON_H
  3. #include <string>
  4. #include "simple/graphical/surface.h"
  5. #include "simple/graphical/algorithm/fill.h"
  6. #if ENABLE_OPENGL_EXAMPLES
  7. #include <GL/glew.h>
  8. #include <GL/gl.h>
  9. #endif
  10. using namespace simple::graphical;
  11. template <typename Filler>
  12. inline void checker_up(const surface& board, int2 tile_size, Filler filler)
  13. {
  14. using simple::support::intersects;
  15. rect board_rect{board.size()};
  16. rect tile = rect{tile_size};
  17. while( intersects<int2>(board_rect, tile) )
  18. {
  19. filler(board, tile);
  20. for(size_t i = 0; i < int2::dimensions; ++i)
  21. {
  22. auto step = tile.size * 2 * int2::unit(i);
  23. auto t = tile;
  24. while( t.position += step, intersects<int2>(board_rect, t) )
  25. filler(board, t);
  26. }
  27. tile.position += tile.size;
  28. }
  29. }
  30. inline void checker_up(const surface& board, int2 tile_size, color tile_color)
  31. {
  32. checker_up(board, tile_size, [tile_color](auto&& surf, auto&& rect){ fill(surf, tile_color, rect); });
  33. }
  34. std::string to_string(int value, int padding)
  35. {
  36. int digits = 1;
  37. int copy = value;
  38. while(copy / 10)
  39. {
  40. copy /= 10;
  41. ++digits;
  42. }
  43. auto str = std::to_string(value);
  44. while(digits < padding)
  45. {
  46. str = '0' + str;
  47. ++digits;
  48. }
  49. return str;
  50. }
  51. #if ENABLE_OPENGL_EXAMPLES
  52. void gl_read_pixels(surface& to)
  53. {
  54. auto pixels = std::get<pixel_writer<rgb_pixel, surface::byte>>(to.pixels());
  55. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  56. glReadPixels(0,0, to.size().x(), to.size().y(), GL_RGB, GL_UNSIGNED_BYTE, pixels.row());
  57. }
  58. #endif
  59. #endif /* end of include guard */