Skip to content

Commit f62e043

Browse files
author
beryll1um
committed
mod(bounce): bounce example improved (again)
1 parent 0be2501 commit f62e043

File tree

8 files changed

+160
-46
lines changed

8 files changed

+160
-46
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ option(UNIFORM_BUILD_EXAMPLES "Build the ${UNIFIED_PROJECT} test programs" TRUE)
6767
if (UNIFORM_BUILD_EXAMPLES)
6868
message("-- Build the ${UNIFIED_PROJECT} test programs")
6969
set(UNIFIED_EXAMPLES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/examples")
70-
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/bounce")
71-
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/interfaces")
72-
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/layers")
73-
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/texture")
70+
directories_list(EXAMPLE_DIRECTORIES ${UNIFIED_EXAMPLES_DIR})
71+
foreach(EXAMPLE_NAME ${EXAMPLE_DIRECTORIES})
72+
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/${EXAMPLE_NAME}")
73+
endforeach()
7474
endif ()

examples/bounce/bounce.cpp

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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
{
1816
public:
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

123139
Application *UNIFIED_NAMESPACE::CreateApplication() {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef _UNIFIED_GRAPHICS_2D_CAMERA_HPP
2+
#define _UNIFIED_GRAPHICS_2D_CAMERA_HPP
3+
4+
# include <unified/core/math/matrix.hpp>
5+
6+
UNIFIED_BEGIN_NAMESPACE
7+
UNIFIED_GRAPHICS_2D_BEGIN_NAMESPACE
8+
9+
typedef Matrix<double, 2, 2> Projection;
10+
11+
class Camera
12+
{
13+
public:
14+
15+
Camera();
16+
Camera(const Projection &projection);
17+
18+
virtual ~Camera() { }
19+
20+
const Projection &get_projection() const;
21+
22+
protected:
23+
24+
Projection _projection;
25+
26+
};
27+
28+
UNIFIED_GRAPHICS_2D_END_NAMESPACE
29+
UNIFIED_END_NAMESPACE
30+
31+
#endif

include/unified/graphics/2d/vertex.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct Vertex<_type, 2>
1616

1717
UNIFIED_CONSTEXPR Vertex(const Point<_type, 2> &point) : point(point), color(), texture() { }
1818

19+
UNIFIED_CONSTEXPR Vertex(const Color &color) : point(), color(color), texture() { }
20+
1921
UNIFIED_CONSTEXPR Vertex(const Point<_type, 2> &point, const Color &color) : point(point), color(color), texture() { }
2022

2123
UNIFIED_CONSTEXPR Vertex(const Point<_type, 2> &point, const Point<_type, 2> &vertex) : point(point), color(), texture(vertex) { }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef _UNIFIED_GRAPHICS_3D_CAMERA_HPP
2+
#define _UNIFIED_GRAPHICS_3D_CAMERA_HPP
3+
4+
# include <unified/core/math/matrix.hpp>
5+
6+
UNIFIED_BEGIN_NAMESPACE
7+
UNIFIED_GRAPHICS_3D_BEGIN_NAMESPACE
8+
9+
typedef Matrix<double, 4, 4> Projection;
10+
11+
class Camera
12+
{
13+
public:
14+
15+
Camera();
16+
Camera(const Projection &projection);
17+
18+
virtual ~Camera() { }
19+
20+
const Projection &get_projection() const;
21+
22+
protected:
23+
24+
Projection _projection;
25+
26+
};
27+
28+
UNIFIED_GRAPHICS_3D_END_NAMESPACE
29+
UNIFIED_END_NAMESPACE
30+
31+
#endif

include/unified/graphics/3d/vertex.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct Vertex<_type, 3>
1616

1717
UNIFIED_CONSTEXPR Vertex(const Point<_type, 3> &point) : point(point), color(), texture() { }
1818

19+
UNIFIED_CONSTEXPR Vertex(const Color &color) : point(), color(color), texture() { }
20+
1921
UNIFIED_CONSTEXPR Vertex(const Point<_type, 3> &point, const Color &color) : point(point), color(color), texture() { }
2022

2123
UNIFIED_CONSTEXPR Vertex(const Point<_type, 3> &point, const Point<_type, 2> &vertex) : point(point), color(), texture(vertex) { }

src/graphics/2d/camera.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <unified/graphics/2d/camera.hpp>
2+
3+
UNIFIED_BEGIN_NAMESPACE
4+
UNIFIED_GRAPHICS_2D_BEGIN_NAMESPACE
5+
6+
Camera::Camera() :
7+
_projection({ { 1.0, 0.0 }, { 0.0, 1.0 } }) { }
8+
9+
Camera::Camera(const Projection &projection) : _projection(projection) { }
10+
11+
const Projection &Camera::get_projection() const {
12+
return _projection;
13+
}
14+
15+
UNIFIED_GRAPHICS_2D_END_NAMESPACE
16+
UNIFIED_END_NAMESPACE

src/graphics/3d/camera.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <unified/graphics/3d/camera.hpp>
2+
3+
UNIFIED_BEGIN_NAMESPACE
4+
UNIFIED_GRAPHICS_3D_BEGIN_NAMESPACE
5+
6+
Camera::Camera() :
7+
_projection({ { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 } }) { }
8+
9+
Camera::Camera(const Projection &projection) : _projection(projection) { }
10+
11+
const Projection &Camera::get_projection() const {
12+
return _projection;
13+
}
14+
15+
UNIFIED_GRAPHICS_3D_END_NAMESPACE
16+
UNIFIED_END_NAMESPACE

0 commit comments

Comments
 (0)