|
| 1 | +#include <unified.hpp> |
| 2 | + |
| 3 | +#include <unified/graphics/2d/drawable/vertex_array.hpp> |
| 4 | +#include <unified/graphics/camera.hpp> |
| 5 | + |
| 6 | +#include <unified/core/math/point4.hpp> |
| 7 | + |
| 8 | +#include <imgui_layer/imgui_layer.hpp> |
| 9 | + |
| 10 | +using namespace Unified; |
| 11 | +using namespace Unified::Graphics; |
| 12 | + |
| 13 | +class CubeLayer; |
| 14 | +class ImGuiLayer; |
| 15 | + |
| 16 | +class ExampleBounce : public Application |
| 17 | +{ |
| 18 | +public: |
| 19 | + |
| 20 | + Camera camera; |
| 21 | + |
| 22 | + Point2d position; |
| 23 | + Point4d velocity; |
| 24 | + |
| 25 | + Graphics::Vertex2d circle[32]; |
| 26 | + const u32 circle_vertices_count = sizeof(circle) / sizeof(*circle); |
| 27 | + |
| 28 | + void window_resize_event(const WindowResizeEvent &event) { |
| 29 | + set_viewport(event.size); |
| 30 | + } |
| 31 | + |
| 32 | +public: |
| 33 | + |
| 34 | + ExampleBounce() : Application("ExampleBounce", VideoMode(600, 600), !Window::Resizable) { |
| 35 | + Modules::ImGuiLayer::Create(this); |
| 36 | + push_layer<CubeLayer>(this); |
| 37 | + push_layer<ImGuiLayer>(this); |
| 38 | + set_frame_limit(60); |
| 39 | + for (u32 i = 0; i < circle_vertices_count; ++i) { |
| 40 | + circle[i].color = { 1.f, 0.f, 1.f, 1.f }; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + virtual ~ExampleBounce() { |
| 45 | + Modules::ImGuiLayer::Destroy(); |
| 46 | + } |
| 47 | + |
| 48 | + virtual bool OnUpdate(Time elapsed) override { |
| 49 | + clear(); |
| 50 | + |
| 51 | + auto offset = camera.get_projection() * (velocity * static_cast<double>(elapsed.asMilliseconds())); |
| 52 | + auto estimate_position = position + Point2d(offset.x, offset.y); |
| 53 | + |
| 54 | + if (estimate_position.x >= 0.9 || estimate_position.x <= -0.9) { |
| 55 | + velocity.x = -(velocity.x / 2.0); |
| 56 | + } else if (estimate_position.y >= 0.9 || estimate_position.y <= -0.9) { |
| 57 | + velocity.y = -(velocity.y / 2.0); |
| 58 | + } else { |
| 59 | + position = estimate_position; |
| 60 | + |
| 61 | + if (this->get_key_action(Keyboard::Code::W) == Keyboard::Action::Press) |
| 62 | + velocity.y += 0.0001; |
| 63 | + |
| 64 | + if (this->get_key_action(Keyboard::Code::S) == Keyboard::Action::Press) |
| 65 | + velocity.y -= 0.0001; |
| 66 | + |
| 67 | + if (this->get_key_action(Keyboard::Code::A) == Keyboard::Action::Press) |
| 68 | + velocity.x -= 0.0001; |
| 69 | + |
| 70 | + if (this->get_key_action(Keyboard::Code::D) == Keyboard::Action::Press) |
| 71 | + velocity.x += 0.0001; |
| 72 | + |
| 73 | + for (u32 i = 0; i < circle_vertices_count; ++i) { |
| 74 | + double theta = 2.0 * 3.14 * float(i) / circle_vertices_count; |
| 75 | + circle[i].point = { position.x + (0.1 * std::cos(theta)), position.y + (0.1 * std::sin(theta)) }; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + process_layers(); |
| 80 | + |
| 81 | + swap_buffers(); |
| 82 | + return poll_events(); |
| 83 | + } |
| 84 | + |
| 85 | + virtual void OnEvent(EventDispatcher &dispatcher) override { |
| 86 | + dispatcher.dispatch<WindowResizeEvent>(BIND_EVENT_FN(&ExampleBounce::window_resize_event, this)); |
| 87 | + } |
| 88 | + |
| 89 | +}; |
| 90 | + |
| 91 | +class CubeLayer : public Layer |
| 92 | +{ |
| 93 | +public: |
| 94 | + |
| 95 | + ExampleBounce *application; |
| 96 | + |
| 97 | + Graphics2D::VertexArray vertex_array; |
| 98 | + |
| 99 | + CubeLayer(ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32) { } |
| 100 | + |
| 101 | + virtual void OnUpdate(Time) override { |
| 102 | + vertex_array.write(application->circle, sizeof(application->circle)); |
| 103 | + application->draw(vertex_array); |
| 104 | + } |
| 105 | + |
| 106 | +}; |
| 107 | + |
| 108 | +class ImGuiLayer : public Modules::ImGuiLayer |
| 109 | +{ |
| 110 | +public: |
| 111 | + |
| 112 | + ExampleBounce *application; |
| 113 | + |
| 114 | + ImGuiLayer(ExampleBounce *application) : application(application) { } |
| 115 | + |
| 116 | + virtual void OnUpdate(Time) override { |
| 117 | + ImGui::Begin("ExampleBounce"); |
| 118 | + ImGui::End(); |
| 119 | + } |
| 120 | + |
| 121 | +}; |
| 122 | + |
| 123 | +Application *UNIFIED_NAMESPACE::CreateApplication() { |
| 124 | + return new ExampleBounce(); |
| 125 | +} |
0 commit comments