Advertisement
koronabora

Nana example #2

May 3rd, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /////////// MainWindow.h
  2.  
  3. #pragma once
  4. #include <nana/gui.hpp>
  5. #include <nana/threads/pool.hpp>
  6. #include <thread>
  7. #include <iostream>
  8. #include <atomic>
  9.  
  10. using namespace nana;
  11. class MainWindow : public form
  12. {
  13. public:
  14. MainWindow();
  15. ~MainWindow();
  16.  
  17. static void draw(paint::graphics& graph);
  18. private:
  19. void mainLoop();
  20. void wait(unsigned int wait = 0);
  21. threads::pool pool_;
  22. std::atomic_bool flag = true;
  23.  
  24. static int x;
  25. static int y;
  26. };
  27.  
  28. /////////// MainWindow.cpp
  29.  
  30. #include "MainWindow.h"
  31.  
  32. int MainWindow::x = 5;
  33. int MainWindow::y = 5;
  34.  
  35. MainWindow::MainWindow()
  36. {
  37. caption("Tetris");
  38. this->events().key_press([&](const nana::arg_keyboard&arg)
  39. {
  40. });
  41. this->events().expose(nana::threads::pool_push(pool_, *this, &MainWindow::mainLoop));
  42. }
  43.  
  44. MainWindow::~MainWindow()
  45. {
  46. flag = false;
  47. pool_.wait_for_finished();
  48. }
  49.  
  50. void MainWindow::mainLoop()
  51. {
  52. while (flag)
  53. {
  54. x++;
  55. y++;
  56. nana::API::refresh_window(*this);
  57. wait(1000);
  58. }
  59. }
  60.  
  61. void MainWindow::wait(unsigned int wait)
  62. {
  63. std::this_thread::sleep_for(std::chrono::milliseconds{ wait });
  64. }
  65.  
  66. void MainWindow::draw(paint::graphics& graph)
  67. {
  68. graph.rectangle(rectangle{ x, y, 50, 50 }, true, colors::red);
  69. graph.line(point(x, y), point(55, 55), colors::blue);
  70. }
  71.  
  72. /////////// main.cpp
  73.  
  74. #include "MainWindow.h"
  75.  
  76. int main()
  77. {
  78. MainWindow window;
  79. drawing dw(window);
  80. dw.draw(&window.draw);
  81. dw.update();
  82. window.show();
  83. ::nana::exec();
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement