11#include < unified.hpp>
22
3+ #include < unified/graphics/2d/camera.hpp>
34#include < unified/graphics/2d/drawable/vertex_array.hpp>
4- #include < unified/graphics/camera.hpp>
5-
6- #include < unified/core/math/point4.hpp>
75
86#include < imgui_layer/imgui_layer.hpp>
97
@@ -17,10 +15,10 @@ class ExampleBounce : public Application
1715{
1816public:
1917
20- Camera camera;
18+ Graphics2D:: Camera camera;
2119
22- Point2d position ;
23- Point4d velocity;
20+ Color color ;
21+ Point2d position, velocity;
2422
2523 Graphics::Vertex2d circle[32 ];
2624 const u32 circle_vertices_count = sizeof (circle) / sizeof (*circle);
@@ -29,55 +27,58 @@ class ExampleBounce : public Application
2927 set_viewport (event.size );
3028 }
3129
32- public:
30+ void keyboard_handle () {
31+ if (get_key_action (Keyboard::Code::W) == Keyboard::Action::Press)
32+ velocity.y += 0.05 ;
3333
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 );
34+ if (get_key_action (Keyboard::Code::S) == Keyboard::Action::Press)
35+ velocity.y -= 0.05 ;
36+
37+ if (get_key_action (Keyboard::Code::A) == Keyboard::Action::Press)
38+ velocity.x -= 0.05 ;
39+
40+ if (get_key_action (Keyboard::Code::D) == Keyboard::Action::Press)
41+ velocity.x += 0.05 ;
42+ }
43+
44+ void calculate_circle_position () {
3945 for (u32 i = 0 ; i < circle_vertices_count; ++i) {
40- circle[i].color = { 1 .f , 0 .f , 1 .f , 1 .f };
46+ double theta = 6.28 * float (i) / circle_vertices_count;
47+ circle[i].point = { position.x + 0.1 * std::cos (theta), position.y + 0.1 * std::sin (theta) };
4148 }
4249 }
4350
44- ~ExampleBounce () {
45- Modules::ImGuiLayer::Destroy ();
51+ void calculate_circle_color () {
52+ for (u32 i = 0 ; i < circle_vertices_count; ++i) {
53+ circle[i].color = color;
54+ }
4655 }
4756
48- virtual bool OnUpdate (Time elapsed) override {
49- clear ();
57+ public:
5058
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;
59+ ExampleBounce () : Application(" ExampleBounce" , VideoMode(600 , 600 ), Window::Floating) {
60+ push_layer<CubeLayer>(this );
61+ push_layer<ImGuiLayer>(this );
62+ set_frame_limit (60 );
63+ }
6064
61- if ( this -> get_key_action (Keyboard::Code::W) == Keyboard::Action::Press)
62- velocity. y += 0.0001 ;
65+ virtual bool OnUpdate (Time elapsed) override {
66+ clear () ;
6367
64- if (this ->get_key_action (Keyboard::Code::S) == Keyboard::Action::Press)
65- velocity.y -= 0.0001 ;
68+ keyboard_handle ();
6669
67- if (this ->get_key_action (Keyboard::Code::A) == Keyboard::Action::Press)
68- velocity.x -= 0.0001 ;
70+ auto estimated_position = position + camera.get_projection () * velocity * elapsed.asSeconds ();
6971
70- if (this ->get_key_action (Keyboard::Code::D) == Keyboard::Action::Press)
71- velocity.x += 0.0001 ;
72+ if (estimated_position.x >= 0.9 || estimated_position.x <= -0.9 )
73+ velocity.x = -(velocity.x / 2.0 );
74+ else if (estimated_position.y >= 0.9 || estimated_position.y <= -0.9 )
75+ velocity.y = -(velocity.y / 2.0 );
76+ else
77+ position = estimated_position, calculate_circle_position ();
7278
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- }
79+ calculate_circle_color ();
7880
7981 process_layers ();
80-
8182 swap_buffers ();
8283 return poll_events ();
8384 }
@@ -96,7 +97,9 @@ class CubeLayer : public Layer
9697
9798 Graphics2D::VertexArray vertex_array;
9899
99- CubeLayer (ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32 ) { }
100+ CubeLayer (ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32 ) {
101+ std::fill (application->circle , application->circle + 32 , Vertex2d ({ 1 .f , 0 .f , 1 .f , 1 .f }));
102+ }
100103
101104 virtual void OnUpdate (Time) override {
102105 vertex_array.write (application->circle , sizeof (application->circle ));
@@ -111,13 +114,26 @@ class ImGuiLayer : public Modules::ImGuiLayer
111114
112115 ExampleBounce *application;
113116
114- ImGuiLayer (ExampleBounce *application) : application(application) { }
117+ ImGuiLayer (ExampleBounce *application) : application(application) {
118+ Create (application);
119+ }
115120
116121 virtual void OnUpdate (Time) override {
117122 ImGui::Begin (" ExampleBounce" );
123+ if (ImGui::CollapsingHeader (" Info" )) {
124+ ImGui::Text (" Position: %lf, %lf" , application->position .x , application->position .y );
125+ ImGui::Text (" Velocity: %lf, %lf" , application->velocity .x , application->velocity .y );
126+ }
127+ if (ImGui::CollapsingHeader (" Properties" )) {
128+ ImGui::ColorEdit3 (" Ball" , (float *)&application->color );
129+ }
118130 ImGui::End ();
119131 }
120132
133+ ~ImGuiLayer () {
134+ Destroy ();
135+ }
136+
121137};
122138
123139Application *UNIFIED_NAMESPACE::CreateApplication () {
0 commit comments