fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. struct events {};
  7. struct image {};
  8.  
  9. struct render_target {
  10. void draw(image const &image, int x, int y) {}
  11. };
  12.  
  13.  
  14. struct drawable {
  15. virtual void draw(render_target &target) const = 0;
  16. virtual ~drawable() = default;
  17. };
  18.  
  19. struct updatable {
  20. virtual void update(int delta_time) = 0;
  21. virtual ~updatable() = default;
  22. };
  23.  
  24. struct events_consumer {
  25. virtual void consume(events const &events) = 0;
  26. virtual ~events_consumer() = default;
  27. };
  28.  
  29.  
  30.  
  31. namespace scaffolding {
  32. struct entity: drawable, updatable, events_consumer {
  33. int x, y;
  34. image img;
  35.  
  36. entity(image img, int x, int y): img(img), x(x), y(y){}
  37.  
  38. virtual void draw(render_target &target) const override {
  39. target.draw(img, x, y);
  40. }
  41.  
  42. virtual void consume(events const &evts) override {}
  43.  
  44. virtual void update(int dt) override {}
  45. };
  46.  
  47. using entities = std::vector<entity>;
  48.  
  49. struct game_skeleton: drawable, updatable, events_consumer {
  50. entities entities_container;
  51.  
  52. bool is_running() const {
  53. return false; //normalny u'd like it to be this.is_running variable
  54. }
  55.  
  56. virtual void update(int dt) override {
  57. for(auto &&e: get_entities()) {
  58. e.update(dt);
  59. }
  60. }
  61.  
  62. virtual void draw(render_target &target) const override {
  63. for(auto &&e: get_entities()) {
  64. e.draw(target);
  65. }
  66. }
  67.  
  68. virtual void consume(events const &evts) {
  69. for(auto &&e: get_entities()) {
  70. e.consume(evts);
  71. }
  72. }
  73.  
  74. entities &get_entities() { return entities_container; }
  75. entities const &get_entities() const { return entities_container; }
  76.  
  77. void register_entity(entity &&e, std::string const &id) {}
  78. entity &get_entity(std::string const &id) {}
  79. entity const &get_entity(std::string const &id) const {}
  80. void unregister_entity(std::string const &id) {}
  81. };
  82.  
  83. struct game_runner {
  84. game_skeleton &game;
  85. render_target &target;
  86.  
  87. int fetch_dt() {
  88. return {};
  89. }
  90.  
  91. events fetch_events() {
  92. return {};
  93. }
  94.  
  95. void run() {
  96. while(game.is_running()) {
  97. stream_events();
  98. stream_time();
  99. }
  100. }
  101.  
  102. void stream_events() {
  103. game.consume(fetch_events());
  104. }
  105.  
  106. void stream_time() {
  107. game.update(fetch_dt());
  108. game.draw(target);
  109. }
  110. };
  111. }
  112.  
  113. namespace ur {
  114. image load_img(std::string const &) { return {}; }
  115.  
  116. struct flower_entity: scaffolding::entity {
  117. flower_entity(int x, int y): entity(load_img("flower.png"), x, y) {}
  118. };
  119.  
  120. struct fancy_player_entity: scaffolding::entity {
  121. fancy_player_entity(int x, int y): entity(load_img("sausage.png"), x, y) {}
  122.  
  123. virtual void update(int dt) override {
  124. //apply some kind of physics mb?
  125. }
  126.  
  127. virtual void consume(events const &) override {
  128. //mb handle the keyboard or smtg
  129. }
  130. };
  131.  
  132. struct game: scaffolding::game_skeleton {
  133. game() {
  134. register_entity(flower_entity(5, 5), "flower-1");
  135. register_entity(fancy_player_entity(10, 10), "player");
  136. register_entity(flower_entity(5, 6), "flower-2");
  137. }
  138. };
  139. }
  140.  
  141. int main() {
  142. auto target = render_target {};
  143. auto game = ur::game {};
  144. scaffolding::game_runner {game, target}.run();
  145. return 0;
  146. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty