11#include < unified.hpp>
22
3- #include < unified/core/system/sleep .hpp>
3+ #include < unified/core/math/point3 .hpp>
44
55#include < unified/graphics/2d/camera.hpp>
66#include < unified/graphics/2d/drawable/vertex_array.hpp>
1010using namespace Unified ;
1111using namespace Unified ::Graphics;
1212
13- class CircleLayer ;
13+ class BallLayer ;
1414class ImGuiLayer ;
1515
1616class ExampleBounce : public Application
1717{
1818public:
1919
20- Graphics2D::Camera camera ;
20+ Point2d camera_position = { 0.0 , 0.0 } ;
2121
22- Color color ;
23- Point2d position, velocity ;
22+ Point2d ball_position = { 0.0 , 0.0 } ;
23+ Point2d ball_velocity = { 0.0 , 0.0 } ;
2424
25- Graphics::Vertex2d circle[32 ];
26- const u32 circle_vertices_count = sizeof (circle) / sizeof (*circle);
25+ Color ball_color = { 0 .8f , 0 .8f , 0 .8f };
2726
28- void window_resize_event (const WindowResizeEvent &event) {
29- set_viewport (event.size );
30- }
27+ Vertex2d ball_vertices[64 ];
28+ u32 ball_vertices_count = sizeof (ball_vertices) / sizeof (*ball_vertices);
29+
30+ Graphics2D::Camera camera;
31+ Graphics2D::VertexArray vertex_array;
32+
33+ public:
3134
3235 void keyboard_handle () {
3336 if (get_key_action (Keyboard::Code::W) == Keyboard::Action::Press)
34- velocity.y += 0.05 ;
35-
36- if (get_key_action (Keyboard::Code::S) == Keyboard::Action::Press)
37- velocity.y -= 0.05 ;
37+ ball_velocity.y += 0.01 ;
3838
3939 if (get_key_action (Keyboard::Code::A) == Keyboard::Action::Press)
40- velocity.x -= 0.05 ;
40+ ball_velocity.x -= 0.01 ;
41+
42+ if (get_key_action (Keyboard::Code::S) == Keyboard::Action::Press)
43+ ball_velocity.y -= 0.01 ;
4144
4245 if (get_key_action (Keyboard::Code::D) == Keyboard::Action::Press)
43- velocity .x += 0.05 ;
46+ ball_velocity .x += 0.01 ;
4447
45- if (get_key_action (Keyboard::Code::Z) == Keyboard::Action::Press) {
46- camera.get_projection () *= 1.01 ;
47- }
48+ if (get_key_action (Keyboard::Code::Up) == Keyboard::Action::Press)
49+ camera_position.y += 0.01 ;
4850
49- if (get_key_action (Keyboard::Code::X) == Keyboard::Action::Press) {
50- camera.get_projection () *= 0.99 ;
51- }
51+ if (get_key_action (Keyboard::Code::Left) == Keyboard::Action::Press)
52+ camera_position.x -= 0.01 ;
53+
54+ if (get_key_action (Keyboard::Code::Down) == Keyboard::Action::Press)
55+ camera_position.y -= 0.01 ;
56+
57+ if (get_key_action (Keyboard::Code::Right) == Keyboard::Action::Press)
58+ camera_position.x += 0.01 ;
5259 }
5360
54- void calculate_circle_position () {
55- auto projected = camera.get_projection () * position;
56- for (u32 i = 0 ; i < circle_vertices_count; ++i) {
57- double theta = 6.28 * double (i) / circle_vertices_count;
58- circle[i].point = { projected.x + camera.get_projection ()[0 ][0 ] * 0.1 * std::cos (theta), projected.y + camera.get_projection ()[1 ][1 ] * 0.1 * std::sin (theta) };
61+ void calculate_ball_position () {
62+ for (u32 i = 0 ; i < ball_vertices_count; ++i) {
63+ double theta = 6.28 * double (i) / ball_vertices_count;
64+ ball_vertices[i].point = camera.get_projection () * Point3d (ball_position.x + 0.1 * std::cos (theta), ball_position.y + 0.1 * std::sin (theta), 1.0 );
5965 }
6066 }
6167
62- void calculate_circle_color () {
63- for (u32 i = 0 ; i < circle_vertices_count ; ++i) {
64- circle [i].color = color ;
68+ void calculate_ball_color () {
69+ for (u32 i = 0 ; i < ball_vertices_count ; ++i) {
70+ ball_vertices [i].color = ball_color ;
6571 }
6672 }
6773
74+ void window_resize_event (const WindowResizeEvent &event) {
75+ set_viewport (event.size );
76+ }
77+
6878public:
6979
70- ExampleBounce () : Application(" ExampleBounce" , VideoMode(600 , 600 ), !Window::Resizable) {
71- push_layer<CircleLayer>(this );
80+ ExampleBounce () : Application(" ExampleBounce" , VideoMode(600 , 600 ), !Window::Resizable), camera(camera_position),
81+ vertex_array (PrimitiveType::Polygon, 64 ) {
82+ push_layer<BallLayer>(this );
7283 push_layer<ImGuiLayer>(this );
7384 set_frame_limit (60 );
7485 }
7586
7687 virtual bool OnUpdate (Time elapsed) override {
77- clear ();
88+ clear ({ 0.15 , 0.15 , 0.15 } );
7889
79- auto estimated_position = position + velocity * elapsed.asSeconds ();
90+ camera.set_position (camera_position);
91+
92+ auto estimated_position = ball_position + ball_velocity * elapsed.asSeconds ();
8093
8194 if (estimated_position.x >= 0.9 || estimated_position.x <= -0.9 )
82- velocity .x = -(velocity .x / 2.0 );
95+ ball_velocity .x = -(ball_velocity .x / 2.0 );
8396 else if (estimated_position.y >= 0.9 || estimated_position.y <= -0.9 )
84- velocity .y = -(velocity .y / 2.0 );
97+ ball_velocity .y = -(ball_velocity .y / 2.0 );
8598 else
86- position = estimated_position, keyboard_handle (), calculate_circle_position ();
99+ ball_position = estimated_position, keyboard_handle (), calculate_ball_position ();
87100
88- calculate_circle_color ();
101+ calculate_ball_color ();
102+ keyboard_handle ();
89103
90104 process_layers ();
91105 swap_buffers ();
@@ -98,23 +112,23 @@ class ExampleBounce : public Application
98112
99113};
100114
101- class CircleLayer : public Layer
115+ class BallLayer : public Layer
102116{
103117public:
104118
105119 ExampleBounce *application;
106120
107- Graphics2D::VertexArray vertex_array;
108-
109- CircleLayer (ExampleBounce *application) : application(application), vertex_array(PrimitiveType::Polygon, 32 ) {
110- std::fill (application->circle , application->circle + 32 , Vertex2d ({ 1 .f , 0 .f , 1 .f , 1 .f }));
111- }
121+ BallLayer (ExampleBounce *application) : application(application), _ball_polygon(PrimitiveType::Polygon, 64 ) { }
112122
113123 virtual void OnUpdate (Time) override {
114- vertex_array .write (application->circle , sizeof (application->circle ));
115- application->draw (vertex_array );
124+ _ball_polygon .write (application->ball_vertices , sizeof (application->ball_vertices ));
125+ application->draw (_ball_polygon );
116126 }
117127
128+ protected:
129+
130+ Graphics2D::VertexArray _ball_polygon;
131+
118132};
119133
120134class ImGuiLayer : public Modules ::ImGuiLayer
@@ -129,13 +143,23 @@ class ImGuiLayer : public Modules::ImGuiLayer
129143
130144 virtual void OnUpdate (Time) override {
131145 ImGui::Begin (" ExampleBounce" );
132- if (ImGui::CollapsingHeader (" Info" )) {
133- ImGui::Text (" Position: %lf, %lf" , application->position .x , application->position .y );
134- ImGui::Text (" Velocity: %lf, %lf" , application->velocity .x , application->velocity .y );
146+
147+ if (ImGui::CollapsingHeader (" Camera" )) {
148+ ImGui::Text (" Camera position: { %lf, %lf }" , application->camera_position .x , application->camera_position .y );
149+ if (ImGui::Button (" Camera reset" )) {
150+ application->camera_position = { 0.0 , 0.0 };
151+ }
135152 }
136- if (ImGui::CollapsingHeader (" Properties" )) {
137- ImGui::ColorEdit3 (" Ball" , (float *)&application->color );
153+
154+ if (ImGui::CollapsingHeader (" Ball" )) {
155+ ImGui::Text (" Ball position: { %lf, %lf }" , application->ball_position .x , application->ball_position .y );
156+ ImGui::Text (" Ball velocity: { %lf, %lf }" , application->ball_velocity .x , application->ball_velocity .y );
157+ if (ImGui::Button (" Ball reset" )) {
158+ application->ball_position = { 0.0 , 0.0 };
159+ application->ball_velocity = { 0.0 , 0.0 };
160+ }
138161 }
162+
139163 ImGui::End ();
140164 }
141165
0 commit comments