From f1563b990f1a60218221cd51d0d4672045f25793 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 27 May 2023 22:21:02 +0200 Subject: [PATCH] Improve LINE_F code --- examples/PoolBox/application/src/main.cpp | 14 +++- include/PSX/Auxiliary/type_traits.hpp | 44 ++++++++++ .../GPU/Primitives/primitive_line_types.hpp | 82 +++++++++++-------- include/PSX/GPU/gpu.hpp | 11 +-- include/PSX/GPU/gpu_types.hpp | 4 + 5 files changed, 108 insertions(+), 47 deletions(-) diff --git a/examples/PoolBox/application/src/main.cpp b/examples/PoolBox/application/src/main.cpp index 6a914f76..883d7bf6 100644 --- a/examples/PoolBox/application/src/main.cpp +++ b/examples/PoolBox/application/src/main.cpp @@ -87,9 +87,17 @@ static constexpr const auto rectangle5 = JabyEngine::GPU::POLY_GT4( JabyEngine::GPU::Color24::White()} ).set_semi_transparent(true); -static constexpr const auto line1 = JabyEngine::GPU::LineMaker::new_line_f(LineColor, { +static constexpr const auto line1 = JabyEngine::GPU::LINE_F::create(LineColor, { {0, 0}, - {JabyEngine::GPU::Display::Width, JabyEngine::GPU::Display::Height}}); + {JabyEngine::GPU::Display::Width, JabyEngine::GPU::Display::Height} +}); + +static constexpr const auto line2 = JabyEngine::GPU::LINE_F::create(LineColor.invert(), + JabyEngine::GPU::Vertex(0, 0), + JabyEngine::GPU::Vertex(16, 0), + JabyEngine::GPU::Vertex(16, 16), + JabyEngine::GPU::Vertex(0, 0) +); static void load_assets() { static const JabyEngine::CDFile Assets[] = { @@ -122,7 +130,6 @@ static void load_assets() { void main() { load_assets(); - printf("Dino: 0x%02X\n", rectangle4.code.value); while(true) { JabyEngine::GPU::render(triangle1); @@ -137,6 +144,7 @@ void main() { JabyEngine::GPU::render(rectangle5); JabyEngine::GPU::render(line1); + JabyEngine::GPU::render(line2); JabyEngine::GPU::swap_buffers_vsync(2); } diff --git a/include/PSX/Auxiliary/type_traits.hpp b/include/PSX/Auxiliary/type_traits.hpp index 5b667ef1..b1a78bb2 100644 --- a/include/PSX/Auxiliary/type_traits.hpp +++ b/include/PSX/Auxiliary/type_traits.hpp @@ -9,6 +9,50 @@ namespace JabyEngine { struct enable_if { typedef T type; }; + + // ############################################# + + template + struct is_same { + static constexpr bool value = false; + }; + + template + struct is_same { + static constexpr bool value = true; + }; + + // ############################################# + + template + struct conditional { + using type = T; + }; + + template + struct conditional { + using type = F; + }; + + // ############################################# + + template + struct conjunction { + static constexpr bool value = true; + }; + + template + struct conjunction : B1 { + }; + + template + struct conjunction : conditional, B1>::type { + }; + + // ############################################# + + template + using variadic_force_same = enable_if...>::value>::type; } #endif //! __JABYENGINE_TYPE_TRAITS_HPP__ \ No newline at end of file diff --git a/include/PSX/GPU/Primitives/primitive_line_types.hpp b/include/PSX/GPU/Primitives/primitive_line_types.hpp index b9cf74e5..ce58ad8b 100644 --- a/include/PSX/GPU/Primitives/primitive_line_types.hpp +++ b/include/PSX/GPU/Primitives/primitive_line_types.hpp @@ -22,66 +22,76 @@ namespace JabyEngine { constexpr T& set_poly_line(bool set = true) { if(set) { - static_cast(this)->head.code.set(Code::PolyLine); + static_cast(this)->code.set(Code::PolyLine); } else { - static_cast(this)->head.code.set(Code::SingleLine); + static_cast(this)->code.set(Code::SingleLine); } return *static_cast(this); } constexpr T& set_semi_transparent(bool set = true) { if(set) { - static_cast(this)->head.code.set(Code::SemiTransparent); + static_cast(this)->code.set(Code::SemiTransparent); } else { - static_cast(this)->head.code.set(Code::NonTransparent); + static_cast(this)->code.set(Code::NonTransparent); } return *static_cast(this); } }; + + struct LineHead : public LineCodeInterface { + Color24 color; + Code code; + }; + + union LineUnion { + LineHead head; + Vertex vertex; + Color24 color; + + static constexpr LineUnion create_head(const Color24& color, const LineCode& code, bool is_poly) { + return LineUnion{.head = LineHead{.color = color, .code = code}.set_poly_line(is_poly)}; + } + + static constexpr LineUnion create_vertex(const Vertex& vertex) { + return LineUnion{.vertex = {vertex.x, vertex.y}}; + } + + static constexpr LineUnion create_terminate() { + return LineUnion{.vertex = {static_cast(0x5000), static_cast(0x5000)}}; + } + }; + + template + struct Line { + LineUnion data[N]; + }; } - struct LINE_F : public internal::LineCodeInterface { - struct Head { - static constexpr auto IdentityCode = Code().set(Code::FlatShading).set(Code::SingleLine).set(Code::NonTransparent); - - Color24 color; - Code code = IdentityCode; - }; + struct LINE_F { + typedef ::JabyEngine::GPU::internal::LineCode Code; + typedef ::JabyEngine::GPU::internal::LineUnion LineUnion; - typedef Vertex Body; + static constexpr auto IdentityCode = Code().set(Code::FlatShading).set(Code::SingleLine).set(Code::NonTransparent); - union { - Head head; - Vertex vertex; - }; - - static constexpr LINE_F new_line(const Color24& color, bool is_poly) { - return LINE_F{.head = {.color = color, .code = Head::IdentityCode}}.set_poly_line(is_poly); + static constexpr internal::Line<3> create(const Color24& color, const Vertex (&vertices)[2]) { + return internal::Line<3>{LineUnion::create_head(color, IdentityCode, false), LineUnion::create_vertex(vertices[0]), LineUnion::create_vertex(vertices[1])}; } - static constexpr LINE_F new_point(const Vertex& vertex) { - return {.vertex = vertex}; - } - - static constexpr LINE_F new_terminate() { - return {.vertex = {static_cast(0xF000), static_cast(0xF000u)}}; + template + static constexpr internal::Line create(const Color24& color, const ARGS&...args) { + return internal::Line{LineUnion::create_head(color, IdentityCode, true), LineUnion::create_vertex(args)..., LineUnion::create_terminate()}; } }; - struct LineMaker { - // Make this it's own outside type?? - template - struct Type { - T head; - T body[N]; + namespace internal { + template + struct is_render_primitive> { + static constexpr bool value = true; }; - - static constexpr Type new_line_f(const Color24& color, const Vertex (&vertices)[2]) { - return Type{.head = LINE_F::new_line(color, false), .body = {LINE_F::new_point(vertices[0]), LINE_F::new_point(vertices[1])}}; - } - }; + } } } diff --git a/include/PSX/GPU/gpu.hpp b/include/PSX/GPU/gpu.hpp index 17630838..782c8a58 100644 --- a/include/PSX/GPU/gpu.hpp +++ b/include/PSX/GPU/gpu.hpp @@ -45,14 +45,9 @@ namespace JabyEngine { internal::render(reinterpret_cast(&primitive), sizeof(T)/sizeof(uint32_t)); } - template - static void render(const LINE_F (&line)[N]) { - internal::render(reinterpret_cast(&line), N); - } - - template - static void render(const LineMaker::Type& line_type) { - internal::render(reinterpret_cast(&line_type), sizeof(line_type)/sizeof(uint32_t)); + template + static enable_if::value>::type render(const LINE_F (&primitives)[N]) { + internal::render(reinterpret_cast(&primitives), (sizeof(T)/sizeof(uint32_t))*N); } uint8_t swap_buffers_vsync(uint8_t syncs, bool clear_screen = true); diff --git a/include/PSX/GPU/gpu_types.hpp b/include/PSX/GPU/gpu_types.hpp index c1caeb13..407dbf47 100644 --- a/include/PSX/GPU/gpu_types.hpp +++ b/include/PSX/GPU/gpu_types.hpp @@ -68,6 +68,10 @@ namespace JabyEngine { static constexpr Color24 Blue() { return Color24(0x0, 0x0, 0xFF); } + + constexpr Color24 invert() const { + return Color24(0xFF - this->red, 0xFF - this->green, 0xFF - this->blue); + } }; class Color {