MCPcopy Create free account
hub / github.com/AlexInLog/ReactivePlusPlus / main

Function main

src/examples/rpp/sfml/snake/main.cpp:36–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34#include <iostream>
35
36int main()
37{
38 auto window_size = get_window_size(s_rows_count, s_columns_count);
39 sf::RenderWindow window(sf::VideoMode(window_size.x, window_size.y), "Snake");
40
41 const auto events = get_events_observable(window) | rpp::ops::publish();
42 const auto presents = get_presents_stream(events);
43
44 auto start = rpp::schedulers::clock_type::now();
45 size_t frames_delta = 0;
46 presents.subscribe([&window](const present_event&) {
47 window.display();
48 window.clear(sf::Color{0, 128, 0});
49 });
50
51 presents | rpp::ops::observe_on(rpp::schedulers::new_thread{}) | rpp::ops::subscribe([&start, &frames_delta](const present_event& p) {
52 const auto diff = p.frame_number - frames_delta;
53 if (diff > 50)
54 {
55 const auto now = rpp::schedulers::clock_type::now();
56 std::cout << "FPS: " << ((static_cast<double>(diff) / static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(now - start).count())) * 1000000000.0) << std::endl;
57 frames_delta = p.frame_number;
58 start = now;
59 }
60 });
61
62 get_shapes_to_draw(events).subscribe([&window](const auto& shape) {
63 window.draw(shape);
64 });
65
66 const auto root_subscription = events
67 | rpp::ops::ref_count()
68 | rpp::ops::filter([](const CustomEvent& ev) { return std::holds_alternative<sf::Event>(ev); })
69 | rpp::ops::filter([](const CustomEvent& ev) {
70 return std::get<sf::Event>(ev).type == sf::Event::Closed;
71 })
72 | rpp::ops::take(1)
73 | rpp::ops::subscribe_with_disposable([&](const auto&) {
74 window.close();
75 });
76
77 // this one will be blocking call and it will unblock when close requested
78 while (!root_subscription.is_disposed())
79 {
80 while (g_run_loop.is_any_ready_schedulable())
81 g_run_loop.dispatch();
82 }
83
84 return EXIT_SUCCESS;
85}

Callers

nothing calls this directly

Calls 15

get_window_sizeFunction · 0.85
get_events_observableFunction · 0.85
publishFunction · 0.85
get_presents_streamFunction · 0.85
nowFunction · 0.85
observe_onFunction · 0.85
subscribeFunction · 0.85
get_shapes_to_drawFunction · 0.85
ref_countFunction · 0.85
filterFunction · 0.85
takeFunction · 0.85

Tested by

no test coverage detected