From a987fc42a6647757de6aa30404dacbff950ef084 Mon Sep 17 00:00:00 2001 From: nullifiedcat Date: Tue, 19 Jun 2018 17:49:30 +0300 Subject: [PATCH] wip records --- include/glez/CMakeLists.txt | 3 +- include/glez/detail/CMakeLists.txt | 3 +- include/glez/detail/record.hpp | 43 +++++++++++++++++++ include/glez/glez.hpp | 3 ++ include/glez/record.hpp | 24 +++++++++++ src/detail/CMakeLists.txt | 3 +- src/detail/record.cpp | 67 ++++++++++++++++++++++++++++++ src/draw.cpp | 21 ++++++++-- src/glez.cpp | 17 ++++++++ 9 files changed, 177 insertions(+), 7 deletions(-) create mode 100644 include/glez/detail/record.hpp create mode 100644 include/glez/record.hpp create mode 100644 src/detail/record.cpp diff --git a/include/glez/CMakeLists.txt b/include/glez/CMakeLists.txt index 9173a0c..60f0dc1 100644 --- a/include/glez/CMakeLists.txt +++ b/include/glez/CMakeLists.txt @@ -3,6 +3,7 @@ target_sources(glez PRIVATE "${CMAKE_CURRENT_LIST_DIR}/glez.hpp" "${CMAKE_CURRENT_LIST_DIR}/font.hpp" "${CMAKE_CURRENT_LIST_DIR}/texture.hpp" - "${CMAKE_CURRENT_LIST_DIR}/draw.hpp") + "${CMAKE_CURRENT_LIST_DIR}/draw.hpp" + "${CMAKE_CURRENT_LIST_DIR}/record.hpp") add_subdirectory(detail) \ No newline at end of file diff --git a/include/glez/detail/CMakeLists.txt b/include/glez/detail/CMakeLists.txt index acc2367..3f52fda 100644 --- a/include/glez/detail/CMakeLists.txt +++ b/include/glez/detail/CMakeLists.txt @@ -2,4 +2,5 @@ target_sources(glez PRIVATE "${CMAKE_CURRENT_LIST_DIR}/font.hpp" "${CMAKE_CURRENT_LIST_DIR}/texture.hpp" "${CMAKE_CURRENT_LIST_DIR}/render.hpp" - "${CMAKE_CURRENT_LIST_DIR}/program.hpp") \ No newline at end of file + "${CMAKE_CURRENT_LIST_DIR}/program.hpp" + "${CMAKE_CURRENT_LIST_DIR}/record.hpp") \ No newline at end of file diff --git a/include/glez/detail/record.hpp b/include/glez/detail/record.hpp new file mode 100644 index 0000000..00d7807 --- /dev/null +++ b/include/glez/detail/record.hpp @@ -0,0 +1,43 @@ +/* + Created on 19.06.18. +*/ + +#pragma once + +#include +#include +#include "render.hpp" +#include "../../../ftgl/vertex-buffer.h" + +namespace glez::detail::record +{ + +class RecordedCommands +{ +public: + struct segment + { + std::size_t start{}; + std::size_t size{}; + uint32_t texture{}; + }; + + RecordedCommands(); + ~RecordedCommands(); + + void reset(); + void store(glez::detail::render::vertex *vertices, size_t vcount, uint32_t *indices, size_t icount); + void bind(uint32_t texture); + void render(); +protected: + void drawSegment(std::size_t index); + + uint32_t currentTexture{ 0 }; + size_t nextStart{ 0 }; + ftgl::vertex_buffer_t *vertex_buffer{}; + std::vector segments{}; +}; + +extern RecordedCommands *currentRecord; + +} \ No newline at end of file diff --git a/include/glez/glez.hpp b/include/glez/glez.hpp index a07b7df..bfa60be 100644 --- a/include/glez/glez.hpp +++ b/include/glez/glez.hpp @@ -15,5 +15,8 @@ void shutdown(); void resize(int width, int height); void begin(); +void recordBegin(); +void recordEnd(); +void recordReplay(); void end(); }; \ No newline at end of file diff --git a/include/glez/record.hpp b/include/glez/record.hpp new file mode 100644 index 0000000..7561bb3 --- /dev/null +++ b/include/glez/record.hpp @@ -0,0 +1,24 @@ +/* + Created on 19.06.18. +*/ + +#pragma once + +namespace glez::detail::record +{ +class RecordedCommands; +} + +namespace glez::record +{ + +class Record +{ +public: + Record(); + ~Record(); + + detail::record::RecordedCommands *commands{ nullptr }; +}; + +} \ No newline at end of file diff --git a/src/detail/CMakeLists.txt b/src/detail/CMakeLists.txt index 9ce8350..6b02f3c 100644 --- a/src/detail/CMakeLists.txt +++ b/src/detail/CMakeLists.txt @@ -2,4 +2,5 @@ target_sources(glez PRIVATE "${CMAKE_CURRENT_LIST_DIR}/program.cpp" "${CMAKE_CURRENT_LIST_DIR}/render.cpp" "${CMAKE_CURRENT_LIST_DIR}/texture.cpp" - "${CMAKE_CURRENT_LIST_DIR}/font.cpp") \ No newline at end of file + "${CMAKE_CURRENT_LIST_DIR}/font.cpp" + "${CMAKE_CURRENT_LIST_DIR}/record.cpp") \ No newline at end of file diff --git a/src/detail/record.cpp b/src/detail/record.cpp new file mode 100644 index 0000000..0e6434f --- /dev/null +++ b/src/detail/record.cpp @@ -0,0 +1,67 @@ +/* + Created on 19.06.18. +*/ + +#include +#include +#include + +namespace glez::detail::record +{ + +void RecordedCommands::drawSegment(std::size_t index) +{ + glDrawElements(GL_TRIANGLES, segments[index].size, GL_UNSIGNED_INT, (void *)(segments[index].start * sizeof(glez::detail::render::vertex))); +} + +void RecordedCommands::render() +{ + vertex_buffer_render_setup(vertex_buffer, GL_TRIANGLES); + vertex_buffer_render_finish(vertex_buffer); +} + +void RecordedCommands::bind(uint32_t texture) +{ + if (currentTexture != texture) + { + segments.push_back(segment{ nextStart, vertex_buffer->indices->size - nextStart, texture }); + currentTexture = texture; + } +} + +void +RecordedCommands::store(glez::detail::render::vertex *vertices, size_t vcount, + uint32_t *indices, size_t icount) +{ + vertex_buffer_push_back(vertex_buffer, vertices, vcount, indices, icount); +} + +RecordedCommands::RecordedCommands() +{ + vertex_buffer = vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,drawmode:1i"); +} + +RecordedCommands::~RecordedCommands() +{ + vertex_buffer_delete(vertex_buffer); +} + +void RecordedCommands::reset() +{ + vertex_buffer_clear(vertex_buffer); + segments.clear(); +} + +RecordedCommands *currentRecord{ nullptr }; + +} + +glez::record::Record::Record() +{ + commands = new glez::detail::record::RecordedCommands{}; +} + +glez::record::Record::~Record() +{ + delete commands; +} diff --git a/src/draw.cpp b/src/draw.cpp index c769925..2d48734 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace indices { @@ -95,7 +96,10 @@ void internal_draw_string(float x, float y, const std::string &string, if (glyph->height > size_y) size_y = glyph->height; - vertex_buffer_push_back(glez::detail::program::buffer, vertices, 4, + if (glez::detail::record::currentRecord) + glez::detail::record::currentRecord->store(vertices, 4, indices::rectangle, 6); + else + vertex_buffer_push_back(glez::detail::program::buffer, vertices, 4, indices::rectangle, 6); } @@ -151,7 +155,10 @@ void line(float x, float y, float dx, float dy, rgba color, float thickness) vertices[3].position = { ex + nx + px, ey + ny + py }; vertices[0].position = { ex + nx - px, ey + ny - py }; - ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, + if (detail::record::currentRecord) + detail::record::currentRecord->store(vertices, 4, indices::rectangle, 6); + else + ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, indices::rectangle, 6); } @@ -170,7 +177,10 @@ void rect(float x, float y, float w, float h, rgba color) vertices[2].position = { x + w, y + h }; vertices[3].position = { x + w, y }; - ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, + if (detail::record::currentRecord) + detail::record::currentRecord->store(vertices, 4, indices::rectangle, 6); + else + ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, indices::rectangle, 6); } @@ -275,7 +285,10 @@ void rect_textured(float x, float y, float w, float h, rgba color, texture &text vertices[2].uv = { s1, t1 }; vertices[3].uv = { s1, t0 }; - ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, + if (detail::record::currentRecord) + detail::record::currentRecord->store(vertices, 4, indices::rectangle, 6); + else + ftgl::vertex_buffer_push_back(detail::program::buffer, vertices, 4, indices::rectangle, 6); } } \ No newline at end of file diff --git a/src/glez.cpp b/src/glez.cpp index 864e0c6..9fd30a5 100644 --- a/src/glez.cpp +++ b/src/glez.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include namespace glez { @@ -43,4 +45,19 @@ void preInit() { detail::font::init(); } + +void glez::recordBegin(record::Record& rc) +{ + detail::record::currentRecord = rc.commands; +} + +void glez::recordEnd() +{ + +} + +void glez::recordReplay() +{ + +} } \ No newline at end of file