Skip to content

Commit 7af3211

Browse files
author
beryll1um
committed
impl(camera, bounce): camera class and bounce example implemented
1 parent ea1f3ad commit 7af3211

File tree

16 files changed

+245
-42
lines changed

16 files changed

+245
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CMakeLists.txt.user
2+
CMakeSettings.json
23
build
34
out
45
.vs

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 3.12)
22

33
project(UnifiedEngine VERSION 0.0.3)
44

5-
set(CMAKE_CXX_STANDARD 17)
6-
75
set(UNIFIED_PROJECT ${PROJECT_NAME})
86
set(UNIFIED_VERSION ${PROJECT_VERSION})
97

@@ -67,6 +65,7 @@ option(UNIFORM_BUILD_EXAMPLES "Build the ${UNIFIED_PROJECT} test programs" TRUE)
6765
if (UNIFORM_BUILD_EXAMPLES)
6866
message("-- Build the ${UNIFIED_PROJECT} test programs")
6967
set(UNIFIED_EXAMPLES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/examples")
68+
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/bounce")
7069
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/interfaces")
7170
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/layers")
7271
add_subdirectory("${UNIFIED_EXAMPLES_DIR}/texture")

examples/bounce/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project(bounce)
2+
3+
add_executable(${PROJECT_NAME} "${PROJECT_NAME}.cpp")
4+
target_link_libraries(${PROJECT_NAME} PUBLIC ${UNIFIED_PROJECT})

examples/bounce/bounce.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

examples/interfaces/interfaces.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <unified.hpp>
22

33
#include <unified/graphics/2d/drawable/vertex_array.hpp>
4+
#include <unified/core/system/sleep.hpp>
45

56
#include <imgui_layer/imgui_layer.hpp>
67

@@ -12,7 +13,7 @@ class TriangleLayer : public Layer
1213
public:
1314

1415
const Application *application;
15-
16+
1617
Graphics::Vertex2d *triangle;
1718

1819
Graphics2D::VertexArray vertex_array;
@@ -32,13 +33,13 @@ class ImGuiLayer : public Modules::ImGuiLayer
3233

3334
Graphics::Vertex2d *triangle;
3435

35-
ImGuiLayer(Application *application, Graphics::Vertex2d *triangle) : triangle(triangle) { }
36+
ImGuiLayer(Graphics::Vertex2d *triangle) : triangle(triangle) { }
3637

3738
virtual void OnUpdate(Time) override {
3839
ImGui::Begin("ExampleInterfaces");
3940
ImGui::ColorEdit3("LD", (float*)(&triangle[0].color));
4041
ImGui::ColorEdit3("CU", (float*)(&triangle[1].color));
41-
ImGui::ColorEdit3("RD", (float*)(&triangle[2].color));
42+
ImGui::ColorEdit3("RD", (float*)(&triangle[2].color));
4243
ImGui::End();
4344
}
4445

@@ -61,9 +62,9 @@ class ExampleInterfaces : public Application
6162

6263
ExampleInterfaces() : Application("ExampleInterfaces") {
6364
Modules::ImGuiLayer::Create(this);
64-
65-
push_layer<ImGuiLayer>(this, triangle);
65+
6666
push_layer<TriangleLayer>(this, triangle);
67+
push_layer<ImGuiLayer>(triangle);
6768

6869
set_frame_limit(60);
6970
}
@@ -74,7 +75,7 @@ class ExampleInterfaces : public Application
7475

7576
virtual bool OnUpdate(Time) override {
7677
clear();
77-
update_layers();
78+
process_layers();
7879
swap_buffers();
7980
return poll_events();
8081
}

examples/layers/layers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class ExampleLayers : public Application
8787
public:
8888

8989
ExampleLayers() : Application("ExampleLayers") {
90-
push_layer<ExampleLayer3>(this);
91-
push_layer<ExampleLayer2>(this);
9290
push_layer<ExampleLayer1>(this);
91+
push_layer<ExampleLayer2>(this);
92+
push_layer<ExampleLayer3>(this);
9393
set_frame_limit(60);
9494
}
9595

9696
virtual bool OnUpdate(Time) override {
9797
clear();
98-
update_layers();
98+
process_layers();
9999
swap_buffers();
100100
return poll_events();
101101
}

examples/texture/texture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ExampleLayers : public Application
4545

4646
virtual bool OnUpdate(Time) override {
4747
clear();
48-
update_layers();
48+
process_layers();
4949
swap_buffers();
5050
return poll_events();
5151
}

include/unified/application/application.hpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# include <unified/core/clock.hpp>
88

99
# include <utility>
10-
# include <forward_list>
10+
# include <deque>
1111

1212
# define BIND_EVENT_FN(method, object) std::bind(method, object, std::placeholders::_1)
1313

@@ -17,6 +17,8 @@ class Application : public Window, public UNIFIED_GRAPHICS_NAMESPACE::RenderTarg
1717
{
1818
public:
1919

20+
using layers_t = std::deque<Layer*>;
21+
2022
Application(string title = "Unified", VideoMode video_mode = VideoMode(800, 600), u32 style = Window::Resizable);
2123

2224
virtual ~Application() { }
@@ -32,20 +34,23 @@ class Application : public Window, public UNIFIED_GRAPHICS_NAMESPACE::RenderTarg
3234

3335
public:
3436

35-
void update_layers();
37+
UNIFIED_NODISCARD const layers_t &layers() const;
3638

3739
template <class _layer, class... _args>
3840
_layer *push_layer(_args&&... args) {
3941
_layer *new_layer = new _layer(std::forward<_args>(args)...);
40-
_layers.push_front(dynamic_cast<Layer*>(new_layer));
42+
_layers.push_back(dynamic_cast<Layer*>(new_layer));
4143
return new_layer;
4244
}
4345

44-
void pop_layer();
46+
protected:
4547

46-
void dispatch_layers(EventDispatcher &dispatcher);
48+
UNIFIED_NODISCARD layers_t &layers();
4749

48-
protected:
50+
void process_layer(Layer *layer);
51+
void process_layers();
52+
53+
void dispatch_layers(EventDispatcher &dispatcher);
4954

5055
virtual bool OnUpdate(Time) = 0;
5156
virtual void OnEvent(EventDispatcher &dispatcher);
@@ -57,7 +62,7 @@ class Application : public Window, public UNIFIED_GRAPHICS_NAMESPACE::RenderTarg
5762
Clock _frame_clock;
5863
Time _frame_duration;
5964

60-
std::forward_list<Layer*> _layers;
65+
layers_t _layers;
6166

6267
};
6368

include/unified/application/layer.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class Layer
2525

2626
virtual void OnEvent(EventDispatcher&) { }
2727

28+
public:
29+
30+
bool active = true;
31+
2832
};
2933

3034
UNIFIED_END_NAMESPACE

include/unified/core/math/matrix.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ struct Matrix
5555
return reinterpret_cast<_type const*>(_data);
5656
}
5757

58+
UNIFIED_FORCE_INLINE _type *data() {
59+
return reinterpret_cast<_type*>(_data);
60+
}
61+
5862
protected:
5963

6064
_type _data[_rows][_columns];
@@ -71,18 +75,18 @@ typedef Matrix<int, 4, 4> Matrix4x4i;
7175
typedef Matrix<float, 4, 4> Matrix4x4f;
7276
typedef Matrix<double, 4, 4> Matrix4x4d;
7377

74-
template <class _type, uint32_t _rows, uint32_t _columns>
75-
UNIFIED_CONSTEXPR Point<_type, _columns> operator*(const Matrix<_type, _rows, _columns> &l, const Point<_type, _columns> &r) {
76-
Point<_type, _columns> result;
78+
template <class _type, u32 _rows_1, u32 _rows_2>
79+
UNIFIED_CONSTEXPR Point<_type, _rows_2> operator*(const Matrix<_type, _rows_1, _rows_2> &l, const Point<_type, _rows_2> &r) {
80+
Point<_type, _rows_2> result;
7781
for (u32 row = 0; row < l.rows(); row++) {
7882
for (u32 col = 0; col < l.columns(); col++) {
79-
result[row] += l(row, col) * r[col];
83+
result[row] += r[col] * l(row, col);
8084
}
8185
}
8286
return result;
8387
}
8488

85-
template <class _type, uint32_t _rows_1, uint32_t _rows_2, uint32_t _columns>
89+
template <class _type, u32 _rows_1, u32 _rows_2, u32 _columns>
8690
UNIFIED_CONSTEXPR Matrix<_type, _rows_1, _rows_2> operator*(const Matrix<_type, _rows_1, _rows_2> &l, const Matrix<_type, _rows_2, _columns> &r) {
8791
Matrix<_type, _rows_1, _rows_2> result;
8892
for (u32 row = 0; row < l.rows(); row++) {

0 commit comments

Comments
 (0)