#include "util/render.h" 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(), screen_start.y(), screen_end.x() - screen_start.x(), screen_end.y() - screen_start.y(), olc::Pixel(color.r(), color.g(), color.b()) ); } #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(), screen_start.y(), size.x(), size.y(), olc::Pixel(color.r(), color.g(), color.b()) ); } 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(), screen_start.y(), screen_end.x() - screen_start.x() + 1, screen_end.y() - screen_start.y() + 1, olc::Pixel(color.r(), color.g(), color.b()) ); } 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(), screen_start.y(), size.x() + 1, size.y() + 1, olc::Pixel(color.r(), color.g(), color.b()) ); } void Render::draw_lines(std::vector> points, Color color) { auto prev_cache = m_matrix * m_view_matrix * Vector3(points[0], 1); for(size_t i = 1; i < points.size(); i++) { auto start = prev_cache; auto end = m_matrix * m_view_matrix * Vector3(points[i], 1); m_pge->DrawLine( start.x(), start.y(), end.x(), end.y(), olc::Pixel(color.r(), color.g(), color.b()) ); prev_cache = end; } } void Render::set_camera_pos(Vector2 camera_pos) { m_camera_pos = camera_pos; m_view_matrix = translation_matrix(camera_pos); } 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) { switch(m_matrix_mode) { case MODEL: m_matrix = translation_matrix(trans) * m_matrix; break; case PROJECTION: m_view_matrix = translation_matrix(trans) * m_view_matrix; break; } } void Render::rotate(float theta) { switch(m_matrix_mode) { case MODEL: m_matrix = rotation_matrix(theta) * m_matrix; break; case PROJECTION: m_view_matrix = rotation_matrix(theta) * m_view_matrix; break; } } void Render::scale(Vector2 scale) { switch(m_matrix_mode) { case MODEL: m_matrix = scale_matrix(scale) * m_matrix; break; case PROJECTION: m_view_matrix = scale_matrix(scale) * m_view_matrix; } } Matrix3x3 Render::translation_matrix(Vector2 trans) { return Matrix3x3( {1., 0., trans[0]}, {0., 1., trans[1]}, {0., 0., 1.} ); } Matrix3x3 Render::rotation_matrix(float theta) { return Matrix3x3( {cos(theta), -sin(theta), 0}, {sin(theta), cos(theta), 0}, {0., 0., 1.} ); } Matrix3x3 Render::scale_matrix(Vector2 scale) { return Matrix3x3( {scale[0], 0., 0.}, {0., scale[1], 0.}, {0., 0., 1.} ); }