From fd1a97507b8570c89472210f9db70d9a9e36e513 Mon Sep 17 00:00:00 2001 From: Quantum Date: Mon, 2 Aug 2021 00:10:44 -0400 Subject: [PATCH] Rendering now uses translations and matrix modes exist --- src/tiles/dirt.cpp | 2 +- src/tiles/grass.cpp | 2 +- src/tiles/rainbow.cpp | 2 +- src/tiles/stone.cpp | 2 +- src/tiles/tile.cpp | 2 +- src/util/render.cpp | 82 +++++++++++++++++++++++++++++++++++++------ src/util/render.h | 22 +++++++++--- 7 files changed, 94 insertions(+), 20 deletions(-) diff --git a/src/tiles/dirt.cpp b/src/tiles/dirt.cpp index 97d8e03..d593869 100644 --- a/src/tiles/dirt.cpp +++ b/src/tiles/dirt.cpp @@ -1,8 +1,8 @@ #include "tiles/dirt.h" void Dirt::render(Render r) { + r.translate(node().pos() * (TILE_SIZE + 1)); r.fill_rectangle_sz( - node().pos() * (TILE_SIZE + 1), node().size() * TILE_SIZE, {100, 50, 10} ); diff --git a/src/tiles/grass.cpp b/src/tiles/grass.cpp index d5b7ca5..e6d491e 100644 --- a/src/tiles/grass.cpp +++ b/src/tiles/grass.cpp @@ -1,8 +1,8 @@ #include "tiles/grass.h" void Grass::render(Render r) { + r.translate(node().pos() * (TILE_SIZE + 1)); r.fill_rectangle_sz( - node().pos() * (TILE_SIZE + 1), node().size() * TILE_SIZE, {7, 176, 52} ); diff --git a/src/tiles/rainbow.cpp b/src/tiles/rainbow.cpp index d7a2ed7..d4cde08 100644 --- a/src/tiles/rainbow.cpp +++ b/src/tiles/rainbow.cpp @@ -7,8 +7,8 @@ Entity *Rainbow::create_entity() { void Rainbow::render(Render r) { auto &entity = (entity::Rainbow &)node().entity(); + r.translate(node().pos() * (TILE_SIZE + 1)); r.fill_rectangle_sz( - node().pos() * (TILE_SIZE + 1), node().size() * TILE_SIZE, entity.color() ); diff --git a/src/tiles/stone.cpp b/src/tiles/stone.cpp index fa90dd3..4a06a05 100644 --- a/src/tiles/stone.cpp +++ b/src/tiles/stone.cpp @@ -1,8 +1,8 @@ #include "tiles/stone.h" void Stone::render(Render r) { + r.translate(node().pos() * (TILE_SIZE + 1)); r.fill_rectangle_sz( - node().pos() * (TILE_SIZE + 1), node().size() * TILE_SIZE, {42, 42, 42} ); diff --git a/src/tiles/tile.cpp b/src/tiles/tile.cpp index d611556..60d95da 100644 --- a/src/tiles/tile.cpp +++ b/src/tiles/tile.cpp @@ -8,8 +8,8 @@ Entity *Tile::create_entity() { } void Tile::render(Render r) { + r.translate(node().pos() * (TILE_SIZE + 1)); r.rectangle_sz( - node().pos() * (TILE_SIZE + 1), node().size() * TILE_SIZE, {255, 0, 0} ); diff --git a/src/util/render.cpp b/src/util/render.cpp index c67b3ae..dce478a 100644 --- a/src/util/render.cpp +++ b/src/util/render.cpp @@ -1,7 +1,7 @@ #include "util/render.h" -void Render::rectangle(Vector2 start, Vector2 end, Color color) { - auto screen_start = m_matrix * m_view_matrix * Vector3(start, 1); +void Render::rectangle(Vector2 end, Color color) { + auto screen_start = m_matrix * m_view_matrix * Vector3(0, 0, 1); auto screen_end = m_matrix * m_view_matrix * Vector3(end, 1); m_pge->DrawRect( screen_start.x(), @@ -12,8 +12,9 @@ void Render::rectangle(Vector2 start, Vector2 end, Color color) { ); } -void Render::rectangle_sz(Vector2 start, Vector2 size, Color color) { - auto screen_start = m_matrix * m_view_matrix * Vector3(start, 1); +#include +void Render::rectangle_sz(Vector2 size, Color color) { + auto screen_start = m_matrix * m_view_matrix * Vector3(0, 0, 1); // auto screen_size = m_matrix * Vector3(size, 1); m_pge->DrawRect( screen_start.x(), @@ -24,8 +25,8 @@ void Render::rectangle_sz(Vector2 start, Vector2 size, Color color) { ); } -void Render::fill_rectangle(Vector2 start, Vector2 end, Color color) { - auto screen_start = m_matrix * m_view_matrix * Vector3(start, 1); +void Render::fill_rectangle(Vector2 end, Color color) { + auto screen_start = m_matrix * m_view_matrix * Vector3(0, 0, 1); auto screen_end = m_matrix * m_view_matrix * Vector3(end, 1); m_pge->FillRect( screen_start.x(), @@ -36,8 +37,8 @@ void Render::fill_rectangle(Vector2 start, Vector2 end, Color color) { ); } -void Render::fill_rectangle_sz(Vector2 start, Vector2 size, Color color) { - auto screen_start = m_matrix * m_view_matrix * Vector3(start, 1); +void Render::fill_rectangle_sz(Vector2 size, Color color) { + auto screen_start = m_matrix * m_view_matrix * Vector3(0, 0, 1); // auto screen_size = m_matrix * Vector3(size, 1); m_pge->FillRect( screen_start.x(), @@ -57,16 +58,75 @@ Vector2 Render::camera_pos() { return m_camera_pos; } +void Render::set_matrix_mode(MatrixMode mode) { + m_matrix_mode = mode; +} + +void Render::load_identity() { + switch(m_matrix_mode) { + case MODEL: + m_matrix = Matrix3x3(1); + break; + case PROJECTION: + m_view_matrix = Matrix3x3(1); + break; + } +} + +void Render::push_matrix() { + switch(m_matrix_mode) { + case MODEL: + m_matrix_stack.push(m_matrix); + break; + case PROJECTION: + m_view_stack.push(m_view_matrix); + break; + } +} + +void Render::pop_matrix() { + switch(m_matrix_mode) { + case MODEL: + m_matrix = m_matrix_stack.top(); + m_matrix_stack.pop(); + break; + case PROJECTION: + m_view_matrix = m_view_stack.top(); + m_view_stack.pop(); + break; + } +} + void Render::translate(Vector2 trans) { - m_matrix *= translation_matrix(trans); + switch(m_matrix_mode) { + case MODEL: + m_matrix *= translation_matrix(trans); + break; + case PROJECTION: + m_view_matrix *= translation_matrix(trans); + break; + } } void Render::rotate(float theta) { - m_matrix *= rotation_matrix(theta); + switch(m_matrix_mode) { + case MODEL: + m_matrix *= rotation_matrix(theta); + break; + case PROJECTION: + m_view_matrix *= rotation_matrix(theta); + break; + } } void Render::scale(Vector2 scale) { - m_matrix *= scale_matrix(scale); + switch(m_matrix_mode) { + case MODEL: + m_matrix *= scale_matrix(scale); + break; + case PROJECTION: + m_view_matrix *= scale_matrix(scale); + } } Matrix3x3 Render::translation_matrix(Vector2 trans) { diff --git a/src/util/render.h b/src/util/render.h index 0f79126..9cb8660 100644 --- a/src/util/render.h +++ b/src/util/render.h @@ -9,19 +9,30 @@ #include "util/matrix3x3.h" +enum MatrixMode { + MODEL, + PROJECTION +}; + class Render { public: Render(olc::PixelGameEngine *pge) : m_pge(pge) {} - void rectangle(Vector2 start, Vector2 end, Color color = {0, 0, 0}); - void rectangle_sz(Vector2 start, Vector2 size, Color color = {0, 0, 0}); + void rectangle(Vector2 end, Color color = {0, 0, 0}); + void rectangle_sz(Vector2 size, Color color = {0, 0, 0}); - void fill_rectangle(Vector2 start, Vector2 end, Color color = {0, 0, 0}); - void fill_rectangle_sz(Vector2 start, Vector2 size, Color color = {0, 0, 0}); + void fill_rectangle(Vector2 end, Color color = {0, 0, 0}); + void fill_rectangle_sz(Vector2 size, Color color = {0, 0, 0}); void set_camera_pos(Vector2 camera_pos); Vector2 camera_pos(); + void set_matrix_mode(MatrixMode mode); + + void load_identity(); + void push_matrix(); + void pop_matrix(); + void translate(Vector2 trans); void rotate(float theta); void scale(Vector2 scale); @@ -34,8 +45,11 @@ class Render { private: olc::PixelGameEngine *m_pge; + MatrixMode m_matrix_mode{MODEL}; + Vector2 m_camera_pos; Matrix3x3 m_view_matrix{1}; Matrix3x3 m_matrix{1}; + std::stack> m_view_stack; std::stack> m_matrix_stack; };