From d058aa23d1ee4ec3f40a2b0598cd745003b01c75 Mon Sep 17 00:00:00 2001 From: vng Date: Thu, 10 Feb 2011 09:35:41 +0200 Subject: [PATCH] Some minor code corrections: - avoid using vector for small buffers; - avoid many parameters count in functions; --- map/drawer_yg.cpp | 44 ++++++++++++++++++++------------------------ map/drawer_yg.hpp | 20 +++++++++++++++----- map/framework.cpp | 35 +++++++++++++++-------------------- map/framework.hpp | 5 ++--- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/map/drawer_yg.cpp b/map/drawer_yg.cpp index 12f79cfb6c..12225452d6 100644 --- a/map/drawer_yg.cpp +++ b/map/drawer_yg.cpp @@ -137,15 +137,13 @@ void DrawerYG::drawSymbol(m2::PointD const & pt, rule_ptr_t pRule, yg::EPosition m_pScreen->drawSymbol(pt, id, pos, depth); } -void DrawerYG::drawPath(vector const & pts, rule_ptr_t * rules, int * depthVec, size_t count) +void DrawerYG::drawPath(vector const & pts, di::DrawRule const * rules, size_t count) { // if any rule needs caching - cache as a whole vector - // check whether we needs caching bool flag = false; - for (int i = 0; i < count; ++i) { - if (rules[i]->GetID() == drule::BaseRule::empty_id) + if (rules[i].m_rule->GetID() == drule::BaseRule::empty_id) { flag = true; break; @@ -157,10 +155,10 @@ void DrawerYG::drawPath(vector const & pts, rule_ptr_t * rules, int if (flag) { - /// collect yg::PenInfo into array and pack them as a whole + // collect yg::PenInfo into array and pack them as a whole for (int i = 0; i < count; ++i) { - rule_ptr_t pRule = rules[i]; + rule_ptr_t pRule = rules[i].m_rule; vector pattern; double offset; pRule->GetPattern(pattern, offset); @@ -174,20 +172,22 @@ void DrawerYG::drawPath(vector const & pts, rule_ptr_t * rules, int styleIDs[i] = m_pSkin->invalidHandle(); } + // map array of pens if (m_pSkin->mapPenInfo(&penInfos[0], &styleIDs[0], count)) + { for (int i = 0; i < count; ++i) - rules[i]->SetID(styleIDs[i]); + rules[i].m_rule->SetID(styleIDs[i]); + } else - LOG(LINFO, ("couldn't successfully pack a sequence of path styles as a whole")); + { + LOG(LERROR, ("couldn't successfully pack a sequence of path styles as a whole")); + return; + } } + // draw path with array of rules for (int i = 0; i < count; ++i) - m_pScreen->drawPath(&pts[0], pts.size(), rules[i]->GetID(), depthVec[i]); -} - -void DrawerYG::drawPath(vector const & pts, rule_ptr_t pRule, int depth) -{ - drawPath(pts, &pRule, &depth, 1); + m_pScreen->drawPath(&pts[0], pts.size(), rules[i].m_rule->GetID(), rules[i].m_depth); } void DrawerYG::drawArea(vector const & pts, rule_ptr_t pRule, int depth) @@ -273,16 +273,15 @@ void DrawerYG::SetScale(int level) m_scale = scales::GetM2PFactor(level); } -void DrawerYG::Draw(di::DrawInfo const * pInfo, rule_ptr_t * rules, int * depthVec, size_t count) +void DrawerYG::Draw(di::DrawInfo const * pInfo, di::DrawRule const * rules, size_t count) { - buffer_vector pathRules; - buffer_vector pathDepthes; + buffer_vector pathRules; /// separating path rules from other for (unsigned i = 0; i < count; ++i) { - rule_ptr_t pRule = rules[i]; + rule_ptr_t pRule = rules[i].m_rule; string symbol; pRule->GetSymbol(symbol); @@ -291,22 +290,19 @@ void DrawerYG::Draw(di::DrawInfo const * pInfo, rule_ptr_t * rules, int * depthV bool const isPath = !pInfo->m_pathes.empty(); if (!isCaption && isPath && !isSymbol && (pRule->GetColor() != -1)) - { pathRules.push_back(rules[i]); - pathDepthes.push_back(depthVec[i]); - } } if (!pathRules.empty()) { for (list::const_iterator i = pInfo->m_pathes.begin(); i != pInfo->m_pathes.end(); ++i) - drawPath(i->m_path, &pathRules[0], &pathDepthes[0], pathRules.size()); + drawPath(i->m_path, pathRules.data(), pathRules.size()); } for (unsigned i = 0; i < count; ++i) { - rule_ptr_t pRule = rules[i]; - int depth = depthVec[i]; + rule_ptr_t pRule = rules[i].m_rule; + int const depth = rules[i].m_depth; bool const isCaption = pRule->GetTextHeight() >= 0.0; diff --git a/map/drawer_yg.hpp b/map/drawer_yg.hpp index 422a240f57..1fd6b71a69 100644 --- a/map/drawer_yg.hpp +++ b/map/drawer_yg.hpp @@ -40,12 +40,22 @@ namespace di string m_name; }; + + struct DrawRule + { + typedef drule::BaseRule const * rule_ptr_t; + + rule_ptr_t m_rule; + int m_depth; + + DrawRule() : m_rule(0) {} + DrawRule(rule_ptr_t p, int d) : m_rule(p), m_depth(d) {} + }; } class DrawerYG { -private: - typedef drule::BaseRule const * rule_ptr_t; + typedef di::DrawRule::rule_ptr_t rule_ptr_t; double m_scale; double m_visualScale; @@ -64,8 +74,7 @@ private: protected: void drawSymbol(m2::PointD const & pt, rule_ptr_t pRule, yg::EPosition pos, int depth); void drawCircle(m2::PointD const & pt, rule_ptr_t pRule, yg::EPosition pos, int depth); - void drawPath(vector const & pts, rule_ptr_t pRule, int depth); - void drawPath(vector const & pts, rule_ptr_t * rules, int * depthVec, size_t count); + void drawPath(vector const & pts, di::DrawRule const * rules, size_t count); void drawArea(vector const & pts, rule_ptr_t pRule, int depth); void drawText(m2::PointD const & pt, string const & name, rule_ptr_t pRule, int depth); @@ -101,5 +110,6 @@ public: void SetVisualScale(double visualScale); void SetScale(int level); - void Draw(di::DrawInfo const * pInfo, rule_ptr_t * rules, int * depthVec, size_t count); + + void Draw(di::DrawInfo const * pInfo, di::DrawRule const * rules, size_t count); }; diff --git a/map/framework.cpp b/map/framework.cpp index 0ad786c22c..b9d4cc9f33 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -46,9 +46,8 @@ namespace fwork , m_drawCount(0) #endif { - m_keys.reserve(16); + m_keys.reserve(reserve_rules_count); - // calc current scale (pixels in 1 global unit) GetDrawer()->SetScale(m_zoom); } @@ -87,22 +86,6 @@ namespace fwork m_keys.erase(unique(m_keys.begin(), m_keys.end(), equal_key()), m_keys.end()); } - void DrawProcessor::ConvertKeysToRules(int layer) - { - m_rules.resize(m_keys.size()); - m_depthVec.resize(m_keys.size()); - // push rules to drawing queue - for (size_t i = 0; i < m_keys.size(); ++i) - { - int depth = m_keys[i].m_priority; - if (layer != 0) - depth = (layer * drule::layer_base_priority) + (depth % drule::layer_base_priority); - - m_rules[i] = drule::rules().Find(m_keys[i]); - m_depthVec[i] = depth; - } - } - #define GET_POINTS(functor_t, for_each_fun, assign_fun) \ { \ functor_t fun(m_convertor, m_rect); \ @@ -169,13 +152,25 @@ namespace fwork PreProcessKeys(); // get drawing rules for the m_keys array - ConvertKeysToRules(layer); + size_t const count = m_keys.size(); + + buffer_vector rules; + rules.resize(count); + + for (size_t i = 0; i < count; ++i) + { + int depth = m_keys[i].m_priority; + if (layer != 0) + depth = (layer * drule::layer_base_priority) + (depth % drule::layer_base_priority); + + rules[i] = di::DrawRule(drule::rules().Find(m_keys[i]), depth); + } #ifdef PROFILER_DRAWING m_drawCount += m_keys.size(); #endif - pDrawer->Draw(ptr.get(), &m_rules[0], &m_depthVec[0], m_keys.size()); + pDrawer->Draw(ptr.get(), rules.data(), count); return true; } diff --git a/map/framework.hpp b/map/framework.hpp index 98ceff798a..6034591ef3 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -54,8 +54,6 @@ namespace fwork shared_ptr m_paintEvent; vector m_keys; - vector m_rules; - vector m_depthVec; int m_zoom; @@ -66,7 +64,8 @@ namespace fwork inline DrawerYG * GetDrawer() const { return m_paintEvent->drawer().get(); } void PreProcessKeys(); - void ConvertKeysToRules(int layer); + + static const int reserve_rules_count = 16; public: DrawProcessor(m2::RectD const & r,