Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void canvas::draw(const bool force)
- {
- log_scope2(log_gui_draw, "Canvas: drawing.");
- if(!is_dirty_ && !force && !texture_.null()) {
- DBG_GUI_D << "Canvas: nothing to draw.\n";
- return;
- }
- if(is_dirty_) {
- get_screen_size_variables(variables_);
- variables_.add("width", wfl::variant(w_));
- variables_.add("height", wfl::variant(h_));
- }
- // Do we have a custom drawing function?
- const bool have_custom_draw = draw_func_ != nullptr;
- // If cached texture is null or size has changed, throw it out and create a new one.
- const bool do_texture_reset = !texture_ || size_changed_;
- // Reset texture, if applicable.
- if(do_texture_reset) {
- DBG_GUI_D << "Canvas: resetting canvas.\n";
- texture_.reset(w_, h_, SDL_TEXTUREACCESS_TARGET);
- size_changed_ = false;
- }
- // Something went wrong! Bail! The texture ctor will print the error if applicable.
- if(!texture_) {
- return;
- }
- // Set the render target. *Must* be called after the above block in case the texture's
- // been recreated or else the game will crash upon trying to write to a null texture.
- render_target_setter target_setter(texture_);
- // Clear the texture, if applicable. *Must* be called after setting the render target
- // since SDL_RenderClear operates on the current target - in this case the canvas texture.
- // Calling it prior would clear the screen.
- //
- // There are three cases in which this should happen:
- //
- // - The texture was reset:
- // This prevents weird graphics bleed-through with certain driver configurations.
- //
- // - The cache was invalidated:
- // This means we're drawing all the canvas shapes, so we want a clean texture. Since
- // drawn shapes are removed from drawing queue once they've been rendered, only clearing
- // the texture if this is true allows subsequently added shapes to be drawn on top of
- // the already-rendered ones.
- //
- // - A custom drawing function was set:
- // Custom drawing functions don't use shapes, so we always want a clean texture prior
- // to calling those.
- if(do_texture_reset || cache_invalidated_ || have_custom_draw) {
- set_draw_color(renderer_, 0, 0, 0, 0);
- SDL_RenderClear(renderer_); // TODO: move to its own wrapper.
- }
- // If we have a custom drawing function, call it now and exit.
- if(have_custom_draw) {
- draw_func_(texture_);
- is_dirty_ = false;
- return;
- }
- if(cache_invalidated_) {
- if(shapes_.empty()) {
- shapes_.swap(drawn_shapes_);
- } else {
- std::copy(drawn_shapes_.begin(), drawn_shapes_.end(), std::inserter(shapes_, shapes_.begin()));
- drawn_shapes_.clear();
- }
- }
- // Draw shapes.
- for(auto& shape : shapes_) {
- lg::scope_logger inner_scope_logging_object__(log_gui_draw, "Canvas: draw shape.");
- shape->draw(w_, h_, renderer_, variables_);
- }
- // The shapes have been drawn and the draw result has been cached. Clear the list.
- std::copy(shapes_.begin(), shapes_.end(), std::back_inserter(drawn_shapes_));
- shapes_.clear();
- is_dirty_ = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement