Advertisement
Vultraz

Untitled

Aug 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. void canvas::draw(const bool force)
  2. {
  3.     log_scope2(log_gui_draw, "Canvas: drawing.");
  4.     if(!is_dirty_ && !force && !texture_.null()) {
  5.         DBG_GUI_D << "Canvas: nothing to draw.\n";
  6.         return;
  7.     }
  8.  
  9.     if(is_dirty_) {
  10.         get_screen_size_variables(variables_);
  11.         variables_.add("width", wfl::variant(w_));
  12.         variables_.add("height", wfl::variant(h_));
  13.     }
  14.  
  15.     // Do we have a custom drawing function?
  16.     const bool have_custom_draw = draw_func_ != nullptr;
  17.  
  18.     // If cached texture is null or size has changed, throw it out and create a new one.
  19.     const bool do_texture_reset = !texture_ || size_changed_;
  20.  
  21.     // Reset texture, if applicable.
  22.     if(do_texture_reset) {
  23.         DBG_GUI_D << "Canvas: resetting canvas.\n";
  24.  
  25.         texture_.reset(w_, h_, SDL_TEXTUREACCESS_TARGET);
  26.  
  27.         size_changed_ = false;
  28.     }
  29.  
  30.     // Something went wrong! Bail! The texture ctor will print the error if applicable.
  31.     if(!texture_) {
  32.         return;
  33.     }
  34.  
  35.     // Set the render target. *Must* be called after the above block in case the texture's
  36.     // been recreated or else the game will crash upon trying to write to a null texture.
  37.     render_target_setter target_setter(texture_);
  38.  
  39.     // Clear the texture, if applicable. *Must* be called after setting the render target
  40.     // since SDL_RenderClear operates on the current target - in this case the canvas texture.
  41.     // Calling it prior would clear the screen.
  42.     //
  43.     // There are three cases in which this should happen:
  44.     //
  45.     // - The texture was reset:
  46.     //   This prevents weird graphics bleed-through with certain driver configurations.
  47.     //
  48.     // - The cache was invalidated:
  49.     //   This means we're drawing all the canvas shapes, so we want a clean texture. Since
  50.     //   drawn shapes are removed from drawing queue once they've been rendered, only clearing
  51.     //   the texture if this is true allows subsequently added shapes to be drawn on top of
  52.     //   the already-rendered ones.
  53.     //
  54.     // - A custom drawing function was set:
  55.     //   Custom drawing functions don't use shapes, so we always want a clean texture prior
  56.     //   to calling those.
  57.     if(do_texture_reset || cache_invalidated_ || have_custom_draw) {
  58.         set_draw_color(renderer_, 0, 0, 0, 0);
  59.  
  60.         SDL_RenderClear(renderer_); // TODO: move to its own wrapper.
  61.     }
  62.  
  63.     // If we have a custom drawing function, call it now and exit.
  64.     if(have_custom_draw) {
  65.         draw_func_(texture_);
  66.  
  67.         is_dirty_ = false;
  68.         return;
  69.     }
  70.  
  71.     if(cache_invalidated_) {
  72.         if(shapes_.empty()) {
  73.             shapes_.swap(drawn_shapes_);
  74.         } else {
  75.             std::copy(drawn_shapes_.begin(), drawn_shapes_.end(), std::inserter(shapes_, shapes_.begin()));
  76.             drawn_shapes_.clear();
  77.         }
  78.     }
  79.  
  80.     // Draw shapes.
  81.     for(auto& shape : shapes_) {
  82.         lg::scope_logger inner_scope_logging_object__(log_gui_draw, "Canvas: draw shape.");
  83.  
  84.         shape->draw(w_, h_, renderer_, variables_);
  85.     }
  86.  
  87.     // The shapes have been drawn and the draw result has been cached. Clear the list.
  88.     std::copy(shapes_.begin(), shapes_.end(), std::back_inserter(drawn_shapes_));
  89.     shapes_.clear();
  90.  
  91.     is_dirty_ = false;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement