|
| 1 | +#include <unified.hpp> |
| 2 | + |
| 3 | +#include <unified/core/math/point4.hpp> |
| 4 | + |
| 5 | +#include <unified/graphics/2d/drawable/vertex_array.hpp> |
| 6 | +#include <unified/graphics/shader.hpp> |
| 7 | + |
| 8 | +#include <unified/core/system/sleep.hpp> |
| 9 | + |
| 10 | +#include <imgui_layer/imgui_layer.hpp> |
| 11 | + |
| 12 | +using namespace Unified; |
| 13 | +using namespace Unified::Graphics; |
| 14 | + |
| 15 | +class FluidLayer; |
| 16 | +class ImGuiLayer; |
| 17 | + |
| 18 | +class ExampleInterfaces : public Application |
| 19 | +{ |
| 20 | +public: |
| 21 | + |
| 22 | + Time start_time; |
| 23 | + |
| 24 | + float wave_matrix[4] = { 0.9, 0.9, 0.9, -0.9 }; |
| 25 | + |
| 26 | + Vertex2d quad[4] = |
| 27 | + { { { -1.0, -1.0 }, { 1.0, 0.0, 0.0, 1.0 } }, |
| 28 | + { { -1.0, 1.0 }, { 0.0, 1.0, 0.0, 1.0 } }, |
| 29 | + { { 1.0, 1.0 }, { 0.0, 0.0, 1.0, 1.0 } }, |
| 30 | + { { 1.0, -1.0 }, { 1.0, 1.0, 0.0, 1.0 } } }; |
| 31 | + |
| 32 | + void window_resize_event(const WindowResizeEvent &event) { |
| 33 | + set_viewport(event.size); |
| 34 | + } |
| 35 | + |
| 36 | +public: |
| 37 | + |
| 38 | + ExampleInterfaces() : Application("ExampleFluid"), start_time(get_current_time()) { |
| 39 | + push_layer<FluidLayer>(this); |
| 40 | + push_layer<ImGuiLayer>(this); |
| 41 | + set_frame_limit(60); |
| 42 | + } |
| 43 | + |
| 44 | + virtual bool OnUpdate(Time) override { |
| 45 | + clear(); |
| 46 | + process_layers(); |
| 47 | + swap_buffers(); |
| 48 | + return poll_events(); |
| 49 | + } |
| 50 | + |
| 51 | + virtual void OnEvent(EventDispatcher &dispatcher) override { |
| 52 | + dispatcher.dispatch<WindowResizeEvent>(BIND_EVENT_FN(&ExampleInterfaces::window_resize_event, this)); |
| 53 | + } |
| 54 | + |
| 55 | +}; |
| 56 | + |
| 57 | +class FluidLayer : public Layer |
| 58 | +{ |
| 59 | +public: |
| 60 | + |
| 61 | + ExampleInterfaces *application; |
| 62 | + |
| 63 | + Graphics2D::VertexArray vertex_array; |
| 64 | + |
| 65 | + Shader shader; |
| 66 | + |
| 67 | + FluidLayer(ExampleInterfaces *application) |
| 68 | + : application(application), vertex_array(PrimitiveType::Polygon, 4), shader( |
| 69 | + #include "fluid.vert" |
| 70 | + , |
| 71 | + #include "fluid.frag" |
| 72 | + ) { } |
| 73 | + |
| 74 | + virtual void OnUpdate(Time) override { |
| 75 | + shader.set_float("start_time", static_cast<float>(get_current_time().asSeconds() - application->start_time.asSeconds())); |
| 76 | + |
| 77 | + auto resolution = application->get_size(); |
| 78 | + shader.set_float2("resolution", { static_cast<float>(resolution.x), static_cast<float>(resolution.y) }); |
| 79 | + shader.set_float4("wave_matrix", application->wave_matrix); |
| 80 | + |
| 81 | + vertex_array.write(application->quad, sizeof(application->quad)); |
| 82 | + application->draw(vertex_array, &shader); |
| 83 | + } |
| 84 | + |
| 85 | +}; |
| 86 | + |
| 87 | +class ImGuiLayer : public Modules::ImGuiLayer |
| 88 | +{ |
| 89 | +public: |
| 90 | + |
| 91 | + ExampleInterfaces *application; |
| 92 | + |
| 93 | + ImGuiLayer(ExampleInterfaces *application) : application(application) { |
| 94 | + Create(application); |
| 95 | + } |
| 96 | + |
| 97 | + virtual void OnUpdate(Time) override { |
| 98 | + ImGui::Begin("ExampleFluid"); |
| 99 | + if (ImGui::CollapsingHeader("Waves")) { |
| 100 | + ImGui::InputFloat4("Wave Matrix", application->wave_matrix); |
| 101 | + } |
| 102 | + if (ImGui::CollapsingHeader("Background")) { |
| 103 | + ImGui::ColorEdit3("Down Left", (float*)&application->quad[0].color); |
| 104 | + ImGui::ColorEdit3("Down Right", (float*)&application->quad[3].color); |
| 105 | + ImGui::ColorEdit3("Up Left", (float*)&application->quad[1].color); |
| 106 | + ImGui::ColorEdit3("Up Right", (float*)&application->quad[2].color); |
| 107 | + } |
| 108 | + ImGui::End(); |
| 109 | + } |
| 110 | + |
| 111 | + ~ImGuiLayer() { |
| 112 | + Destroy(); |
| 113 | + } |
| 114 | + |
| 115 | +}; |
| 116 | + |
| 117 | +Application *UNIFIED_NAMESPACE::CreateApplication() { |
| 118 | + return new ExampleInterfaces(); |
| 119 | +} |
0 commit comments