05_finally_fill_rect.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <cstdio>
  2. #include <cerrno>
  3. #include "simple/graphical/initializer.h"
  4. #include "simple/graphical/software_window.h"
  5. #include "simple/graphical/algorithm/blit.h"
  6. #include "simple/graphical/algorithm/fill.h"
  7. #include "common.h"
  8. using namespace simple::graphical;
  9. using namespace simple::support::literals;
  10. constexpr auto half2D = float2::one() * 0.5f;
  11. int main() try
  12. {
  13. initializer init;
  14. software_window win("Look at rects", {640, 480}, window::flags::borderless);
  15. auto center = win.surface().size() / 2;
  16. auto dark = win.surface().format().color({80_u8,80_u8,80_u8});
  17. auto bright = win.surface().format().color({160_u8,160_u8,160_u8});
  18. fill(win.surface(), dark);
  19. // draws a checker pattern (see common.h)
  20. checker_up(win.surface(), int2::one() * 10, bright);
  21. win.update();
  22. SDL_Delay(1313);
  23. surface alpha_layer(win.surface().size(), pixel_format(pixel_format::type::rgba8888));
  24. auto light_blue = alpha_layer.format().color({0_u8, 177_u8, 177_u8, 177_u8});
  25. auto light_pink = alpha_layer.format().color({177_u8, 0_u8, 177_u8, 177_u8});
  26. // fill on surface does not blend, it overwrites
  27. fill(alpha_layer, light_blue, anchored_rect{ {{100, 300}, center}, half2D } );
  28. fill(alpha_layer, light_pink, anchored_rect{ {{300, 100}, center}, half2D } );
  29. blit(alpha_layer, win.surface()); // blit blends
  30. win.update();
  31. SDL_Delay(1313);
  32. surface alpha_layer2(alpha_layer.size(), alpha_layer.format());
  33. // so these will be blended with the previous two
  34. fill(alpha_layer2, light_blue, anchored_rect{ {{150, 150}, center + 75}, half2D } );
  35. fill(alpha_layer2, light_pink, anchored_rect{ {{150, 150}, center - 75}, half2D } );
  36. blit(alpha_layer2, win.surface()); // thanks to this blit
  37. win.update();
  38. SDL_Delay(1313 * 3);
  39. return 0;
  40. }
  41. catch(...)
  42. {
  43. if(errno)
  44. std::perror("ERROR");
  45. const char* sdl_error = SDL_GetError();
  46. if(*sdl_error)
  47. std::puts(sdl_error);
  48. throw;
  49. }