11_nanovg.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <cstdio>
  2. #include <cerrno>
  3. #if ENABLE_OPENGL_EXAMPLES
  4. #include <chrono>
  5. #include <string>
  6. #include <thread>
  7. #include "simple/graphical/initializer.h"
  8. #include "simple/graphical/gl_window.h"
  9. #include "simple/support/misc.hpp"
  10. #include "common.h"
  11. #include "external/nanovg.h"
  12. #define NANOVG_GL2_IMPLEMENTATION
  13. #include "external/nanovg_gl.h"
  14. using namespace std::literals;
  15. using namespace std::chrono_literals;
  16. using namespace simple::graphical;
  17. using simple::support::ston;
  18. // based on
  19. // https://www.khanacademy.org/computer-programming/bouncing-ball/863892852
  20. class black_ball_sketch
  21. {
  22. // time
  23. int t = 0;
  24. // how high the ball is, where 0 is on the ground
  25. float y = 0;
  26. public:
  27. void draw(NVGcontext*);
  28. // how many times did the animation loop
  29. size_t loops = 0;
  30. } black_ball_sketch;
  31. int main(int argc, char const* argv[]) try
  32. {
  33. switch(argc)
  34. {
  35. case 0: std::puts("Command not specified??");
  36. case 1: std::puts("Number of jumps not specified!");
  37. return -1;
  38. }
  39. const char* record_prefix = argc > 2 ? argv[2] : nullptr;
  40. const auto jumps = ston<size_t>(argv[1]);
  41. initializer init;
  42. gl_window::global.request<gl_window::attribute::major_version>(2);
  43. gl_window::global.request<gl_window::attribute::stencil>(8);
  44. gl_window win("nanovg", int2(400, 400), window::flags::borderless);
  45. surface snapshot(win.size(), pixel_format(pixel_format::type::rgb24));
  46. glewInit();
  47. struct NVGcontext* vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
  48. const auto frame_time = 33ms;
  49. for
  50. (
  51. auto [frame, next_frame] = std::tuple{0, std::chrono::high_resolution_clock::now()};
  52. black_ball_sketch.loops < jumps;
  53. next_frame += frame_time,
  54. ++frame,
  55. record_prefix ? void() : std::this_thread::sleep_until(next_frame)
  56. )
  57. {
  58. glClearColor(1,1,1,1);
  59. glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  60. nvgBeginFrame(vg, win.size().x(), win.size().y(), 1);
  61. black_ball_sketch.draw(vg);
  62. nvgEndFrame(vg);
  63. win.update();
  64. if(record_prefix)
  65. {
  66. gl_read_pixels(snapshot);
  67. snapshot.save((record_prefix + to_string(frame, 4) + ".bmp").c_str());
  68. }
  69. }
  70. nvgDeleteGL2(vg);
  71. return 0;
  72. }
  73. catch(...)
  74. {
  75. if(errno)
  76. std::perror("ERROR");
  77. const char* sdl_error = SDL_GetError();
  78. if(*sdl_error)
  79. std::puts(sdl_error);
  80. throw;
  81. }
  82. void black_ball_sketch::draw(NVGcontext* vg)
  83. {
  84. // y follows the path of a parabola with respect to t!
  85. y = -0.02*t*t + 3.5*t;
  86. // shadow grows lighter with y
  87. float grey = (0.1 * y + 200);
  88. // shadow grows bigger with y
  89. float shadowSize = 0.2 * y + 50;
  90. // draw the shadow
  91. nvgBeginPath(vg);
  92. nvgEllipse(vg, 200, 290, shadowSize/2, 5);
  93. nvgFillColor(vg, nvgRGB(grey, grey, grey));
  94. nvgFill(vg);
  95. // ball grows fatter with the inverse of y
  96. float width = -0.1 * y + 70;
  97. float eye_width= -0.4*y + 100;
  98. // ball grows taller with y
  99. float height = 0.1 * y + 50;
  100. float eye_height= 0.1*y +3;
  101. // since y is a positive height, we need to flip it
  102. // to look right on the inverted coordinate plane
  103. float correctedY = 250 - y;
  104. // draw the ball
  105. nvgBeginPath(vg);
  106. nvgEllipse(vg, 200, correctedY, width/2, height/2);
  107. nvgFillColor(vg, nvgRGB(0, 0, 0));
  108. nvgFill(vg);
  109. nvgBeginPath(vg);
  110. nvgEllipse(vg, 185, correctedY-10, eye_width*0.2/2, eye_height/2);
  111. nvgEllipse(vg, 215, correctedY-10, eye_width*0.2/2, eye_height/2);
  112. nvgFillColor(vg, nvgRGB(250, 247, 247));
  113. nvgFill(vg);
  114. if (y < 0) // if y becomes negative, the ball has hit the ground
  115. {
  116. t = 0; // reset t to make it "bounce" up again
  117. ++loops;
  118. }
  119. t += 5;
  120. }
  121. #else
  122. int main() { std::puts("Example not enabled!"); return 0; }
  123. #endif