Rendering now uses translations and matrix modes exist

This commit is contained in:
Thomas Muller 2021-08-02 00:10:44 -04:00
parent 27af3a586d
commit fd1a97507b
Signed by: thomas
GPG key ID: AF006EB730564952
7 changed files with 94 additions and 20 deletions

View file

@ -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}
);

View file

@ -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}
);

View file

@ -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()
);

View file

@ -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}
);

View file

@ -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}
);

View file

@ -1,7 +1,7 @@
#include "util/render.h"
void Render::rectangle(Vector2<int> start, Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(start, 1);
void Render::rectangle(Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
auto screen_end = m_matrix * m_view_matrix * Vector3<float>(end, 1);
m_pge->DrawRect(
screen_start.x(),
@ -12,8 +12,9 @@ void Render::rectangle(Vector2<int> start, Vector2<int> end, Color color) {
);
}
void Render::rectangle_sz(Vector2<int> start, Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(start, 1);
#include <iostream>
void Render::rectangle_sz(Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
// auto screen_size = m_matrix * Vector3<float>(size, 1);
m_pge->DrawRect(
screen_start.x(),
@ -24,8 +25,8 @@ void Render::rectangle_sz(Vector2<int> start, Vector2<int> size, Color color) {
);
}
void Render::fill_rectangle(Vector2<int> start, Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(start, 1);
void Render::fill_rectangle(Vector2<int> end, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
auto screen_end = m_matrix * m_view_matrix * Vector3<float>(end, 1);
m_pge->FillRect(
screen_start.x(),
@ -36,8 +37,8 @@ void Render::fill_rectangle(Vector2<int> start, Vector2<int> end, Color color) {
);
}
void Render::fill_rectangle_sz(Vector2<int> start, Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(start, 1);
void Render::fill_rectangle_sz(Vector2<int> size, Color color) {
auto screen_start = m_matrix * m_view_matrix * Vector3<float>(0, 0, 1);
// auto screen_size = m_matrix * Vector3<float>(size, 1);
m_pge->FillRect(
screen_start.x(),
@ -57,16 +58,75 @@ Vector2<int> 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<float>(1);
break;
case PROJECTION:
m_view_matrix = Matrix3x3<float>(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<float> 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) {
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<float> scale) {
switch(m_matrix_mode) {
case MODEL:
m_matrix *= scale_matrix(scale);
break;
case PROJECTION:
m_view_matrix *= scale_matrix(scale);
}
}
Matrix3x3<float> Render::translation_matrix(Vector2<float> trans) {

View file

@ -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<int> start, Vector2<int> end, Color color = {0, 0, 0});
void rectangle_sz(Vector2<int> start, Vector2<int> size, Color color = {0, 0, 0});
void rectangle(Vector2<int> end, Color color = {0, 0, 0});
void rectangle_sz(Vector2<int> size, Color color = {0, 0, 0});
void fill_rectangle(Vector2<int> start, Vector2<int> end, Color color = {0, 0, 0});
void fill_rectangle_sz(Vector2<int> start, Vector2<int> size, Color color = {0, 0, 0});
void fill_rectangle(Vector2<int> end, Color color = {0, 0, 0});
void fill_rectangle_sz(Vector2<int> size, Color color = {0, 0, 0});
void set_camera_pos(Vector2<int> camera_pos);
Vector2<int> camera_pos();
void set_matrix_mode(MatrixMode mode);
void load_identity();
void push_matrix();
void pop_matrix();
void translate(Vector2<float> trans);
void rotate(float theta);
void scale(Vector2<float> scale);
@ -34,8 +45,11 @@ class Render {
private:
olc::PixelGameEngine *m_pge;
MatrixMode m_matrix_mode{MODEL};
Vector2<int> m_camera_pos;
Matrix3x3<float> m_view_matrix{1};
Matrix3x3<float> m_matrix{1};
std::stack<Matrix3x3<float>> m_view_stack;
std::stack<Matrix3x3<float>> m_matrix_stack;
};