Render to cache that only updates on click

This is to prevent slowdown when drawing per frame. Previously, the
program would draw each individual triangle each frame. Now the draws
are cached to a texture that is drawn each frame, and the cache is
recreated on click or undo/redo events.
This commit is contained in:
Stephen Seo 2020-07-22 19:03:33 +09:00
parent 3d266137d7
commit a6517b8982
2 changed files with 38 additions and 2 deletions

View file

@ -38,6 +38,16 @@ bgColor(sf::Color::Black)
pointCircle.setOutlineThickness(1.0f); pointCircle.setOutlineThickness(1.0f);
saveFilenameBuffer.fill(0); saveFilenameBuffer.fill(0);
if(!drawCache.create(800, 600)) {
#ifndef NDEBUG
puts("ERROR: Failed to initialize RenderTexture (draw cache)");
#endif
flags.reset(8);
} else {
flags.set(8);
drawCacheSprite.setTexture(drawCache.getTexture(), true);
}
} }
Tri::State::~State() { Tri::State::~State() {
@ -56,6 +66,7 @@ void Tri::State::handle_events() {
if(event.key.code == sf::Keyboard::H) { if(event.key.code == sf::Keyboard::H) {
flags.flip(0); flags.flip(0);
} else if(event.key.code == sf::Keyboard::U) { } else if(event.key.code == sf::Keyboard::U) {
flags.set(7);
if(currentTri_state > 0) { if(currentTri_state > 0) {
switch(currentTri_state) { switch(currentTri_state) {
case FIRST: case FIRST:
@ -72,6 +83,7 @@ void Tri::State::handle_events() {
--trisIndex; --trisIndex;
} }
} else if(event.key.code == sf::Keyboard::R) { } else if(event.key.code == sf::Keyboard::R) {
flags.set(7);
if(currentTri_state != CurrentState::NONE if(currentTri_state != CurrentState::NONE
&& currentTri_state < currentTri_maxState) { && currentTri_state < currentTri_maxState) {
switch(currentTri_state) { switch(currentTri_state) {
@ -109,7 +121,8 @@ void Tri::State::handle_events() {
} }
} }
} else if(event.type == sf::Event::MouseButtonPressed) { } else if(event.type == sf::Event::MouseButtonPressed) {
if(!flags.test(2) && !flags.test(5) && !flags.test(6)) { if(!is_in_clickable_menu()) {
flags.set(7);
switch(currentTri_state) { switch(currentTri_state) {
case CurrentState::NONE: case CurrentState::NONE:
currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y); currentTri[0] = sf::Vector2f(event.mouseButton.x, event.mouseButton.y);
@ -184,7 +197,18 @@ void Tri::State::update() {
} }
void Tri::State::draw() { void Tri::State::draw() {
if(flags.test(8)) {
// draw cache initialized
if(flags.test(7)) {
// draw cache dirty
flags.reset(7);
draw_to_target(&drawCache);
drawCache.display();
}
window.draw(drawCacheSprite);
} else {
draw_to_target(&window); draw_to_target(&window);
}
// draw gui stuff // draw gui stuff
ImGui::SFML::Render(window); ImGui::SFML::Render(window);
@ -276,3 +300,7 @@ std::string_view Tri::State::failed_save_message() const {
void Tri::State::close_save() { void Tri::State::close_save() {
flags.reset(6); flags.reset(6);
} }
bool Tri::State::is_in_clickable_menu() const {
return flags.test(2) || flags.test(5) || flags.test(6);
}

View file

@ -24,6 +24,8 @@ namespace Tri {
* 4 - bg color picker color dirty * 4 - bg color picker color dirty
* 5 - display bg color picker * 5 - display bg color picker
* 6 - draw save * 6 - draw save
* 7 - draw cache dirty
* 8 - draw cache initialized
*/ */
typedef std::bitset<64> BitsetType; typedef std::bitset<64> BitsetType;
BitsetType flags; BitsetType flags;
@ -50,6 +52,9 @@ namespace Tri {
FilenameBufferType saveFilenameBuffer; FilenameBufferType saveFilenameBuffer;
std::string failedSaveMessage; std::string failedSaveMessage;
sf::RenderTexture drawCache;
sf::Sprite drawCacheSprite;
public: public:
void handle_events(); void handle_events();
void update(); void update();
@ -73,6 +78,9 @@ namespace Tri {
bool do_save(); bool do_save();
std::string_view failed_save_message() const; std::string_view failed_save_message() const;
void close_save(); void close_save();
private:
bool is_in_clickable_menu() const;
}; };
} }