Skip to content

Commit 9fa484e

Browse files
author
beryll1um
committed
mod(bounce): zoom function implemented
1 parent c8f52fd commit 9fa484e

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

examples/bounce/bounce.cpp

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

3+
#include <unified/core/system/sleep.hpp>
4+
35
#include <unified/graphics/2d/camera.hpp>
46
#include <unified/graphics/2d/drawable/vertex_array.hpp>
57

@@ -39,12 +41,21 @@ class ExampleBounce : public Application
3941

4042
if (get_key_action(Keyboard::Code::D) == Keyboard::Action::Press)
4143
velocity.x += 0.05;
44+
45+
if (get_key_action(Keyboard::Code::Z) == Keyboard::Action::Press) {
46+
camera.get_projection() *= 1.01;
47+
}
48+
49+
if (get_key_action(Keyboard::Code::X) == Keyboard::Action::Press) {
50+
camera.get_projection() *= 0.99;
51+
}
4252
}
4353

4454
void calculate_circle_position() {
55+
auto projected = camera.get_projection() * position;
4556
for (u32 i = 0; i < circle_vertices_count; ++i) {
4657
double theta = 6.28 * double(i) / circle_vertices_count;
47-
circle[i].point = { position.x + 0.1 * std::cos(theta), position.y + 0.1 * std::sin(theta) };
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) };
4859
}
4960
}
5061

@@ -65,7 +76,7 @@ class ExampleBounce : public Application
6576
virtual bool OnUpdate(Time elapsed) override {
6677
clear();
6778

68-
auto estimated_position = position + camera.get_projection() * velocity * elapsed.asSeconds();
79+
auto estimated_position = position + velocity * elapsed.asSeconds();
6980

7081
if (estimated_position.x >= 0.9 || estimated_position.x <= -0.9)
7182
velocity.x = -(velocity.x / 2.0);

examples/fluid/fluid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExampleInterfaces : public Application
3535

3636
public:
3737

38-
ExampleInterfaces() : Application("ExampleFluid"), start_time(get_current_time()) {
38+
ExampleInterfaces() : Application("ExampleFluid", VideoMode(800, 600), Application::Floating), start_time(get_current_time()) {
3939
push_layer<FluidLayer>(this);
4040
push_layer<ImGuiLayer>(this);
4141
set_frame_limit(60);

examples/fluid/fluid.frag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ in float out_start_time;
99
in vec2 out_resolution;
1010
in vec4 out_color;
1111

12-
// @note: much of magick numbers
12+
// @note: a lot of magick numbers
1313
float position_random(in vec2 position) {
1414
return fract(sin(dot(position.xy, vec2(12.9898, 78.233))) * 43758.5453123);
1515
}

include/unified/core/math/matrix.hpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ struct Matrix
3131
std::fill(&_data[0][0], &_data[_rows][0], fill);
3232
}
3333

34-
UNIFIED_CONSTEXPR _type operator()(u32 row, u32 column) const {
35-
return _data[row][column];
34+
UNIFIED_CONSTEXPR const _type *operator[](u32 row) const {
35+
return _data[row];
3636
}
3737

38-
UNIFIED_CONSTEXPR _type &operator()(u32 row, u32 column) {
39-
return _data[row][column];
38+
UNIFIED_CONSTEXPR _type *operator[](u32 row) {
39+
return _data[row];
4040
}
4141

4242
UNIFIED_FORCE_INLINE u32 rows() const {
@@ -75,30 +75,55 @@ typedef Matrix<int, 4, 4> Matrix4x4i;
7575
typedef Matrix<float, 4, 4> Matrix4x4f;
7676
typedef Matrix<double, 4, 4> Matrix4x4d;
7777

78+
template <class _type, u32 _rows_1, u32 _rows_2>
79+
UNIFIED_CONSTEXPR Matrix<_type, _rows_1, _rows_2> operator*(Matrix<_type, _rows_1, _rows_2> l, const _type &r) {
80+
for (u32 row = 0; row < l.rows(); row++) {
81+
for (u32 col = 0; col < l.columns(); col++) {
82+
l[row][col] *= r;
83+
}
84+
}
85+
return l;
86+
}
87+
88+
template <class _type, u32 _rows_1, u32 _rows_2>
89+
UNIFIED_CONSTEXPR Matrix<_type, _rows_1, _rows_2> operator*=(Matrix<_type, _rows_1, _rows_2> &l, const _type &r) {
90+
return (l = l * r);
91+
}
92+
7893
template <class _type, u32 _rows_1, u32 _rows_2>
7994
UNIFIED_CONSTEXPR Point<_type, _rows_2> operator*(const Matrix<_type, _rows_1, _rows_2> &l, const Point<_type, _rows_2> &r) {
8095
Point<_type, _rows_2> result;
8196
for (u32 row = 0; row < l.rows(); row++) {
8297
for (u32 col = 0; col < l.columns(); col++) {
83-
result[row] += r[col] * l(row, col);
98+
result[row] += r[col] * l[row][col];
8499
}
85100
}
86101
return result;
87102
}
88103

104+
template <class _type, u32 _rows_1, u32 _rows_2>
105+
UNIFIED_CONSTEXPR Point<_type, _rows_2> operator*=(Matrix<_type, _rows_1, _rows_2> &l, const Point<_type, _rows_2> &r) {
106+
return (l = l * r);
107+
}
108+
89109
template <class _type, u32 _rows_1, u32 _rows_2, u32 _columns>
90110
UNIFIED_CONSTEXPR Matrix<_type, _rows_1, _rows_2> operator*(const Matrix<_type, _rows_1, _rows_2> &l, const Matrix<_type, _rows_2, _columns> &r) {
91111
Matrix<_type, _rows_1, _rows_2> result;
92112
for (u32 row = 0; row < l.rows(); row++) {
93113
for (u32 col2 = 0; col2 < r.columns(); col2++) {
94114
for (u32 col = 0; col < l.columns(); col++) {
95-
result(row, col2) += l(row, col) * r(col, col2);
115+
result[row][col2] += l[row][col] * r[col][col2];
96116
}
97117
}
98118
}
99119
return result;
100120
}
101121

122+
template <class _type, u32 _rows_1, u32 _rows_2, u32 _columns>
123+
UNIFIED_CONSTEXPR Matrix<_type, _rows_1, _rows_2> operator*=(Matrix<_type, _rows_1, _rows_2> &l, const Matrix<_type, _rows_2, _columns> &r) {
124+
return (l = l * r);
125+
}
126+
102127
UNIFIED_END_NAMESPACE
103128

104129
#endif

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Camera
1717

1818
virtual ~Camera() { }
1919

20-
const Projection &get_projection() const;
20+
Projection &get_projection();
2121

2222
protected:
2323

src/graphics/2d/camera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ UNIFIED_BEGIN_NAMESPACE
44
UNIFIED_GRAPHICS_2D_BEGIN_NAMESPACE
55

66
Camera::Camera() :
7-
_projection({ { 1.0, 0.0 }, { 0.0, 1.0 } }) { }
7+
_projection({ { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }) { }
88

99
Camera::Camera(const Projection &projection) : _projection(projection) { }
1010

11-
const Projection &Camera::get_projection() const {
11+
Projection &Camera::get_projection() {
1212
return _projection;
1313
}
1414

0 commit comments

Comments
 (0)