From 40799d02bde23a3a198d7a059137436b8f8e9bd7 Mon Sep 17 00:00:00 2001 From: ikework Date: Fri, 24 Aug 2007 07:09:34 +0000 Subject: [PATCH] did some cleaning in CreditsMenu & ScrolledText (1) CreditsMenu::CreditsMenu changed file-open-mode from "ra" to "r" (2) added fclose'ing file in CreditsMenu's ctor (3) CreditsMenu::m_string_list removed since base-class ScrolledText already has that member, made it private in base-class to prevent confusion, since base-class has member-func setText to *set* that member ;) made all other member-vars of ScrolledText private too .. (4) changed typedef of StringList from typedef std::vector StringList; to typedef std::vector StringList; there was a memory-leak in class CreditsMenu, strings were pushed back into StringList via strdup, which (m)allocated memory for the string, so we should have freed it by calling free for each item in vector, so i choosed the all-inclusive std::string :) and moved typedef of StringList into class ScrolledText from global scope (5) removed cleaning-up-code for m_string_list from CreditsMenu's dtor, nothing to clean for us, since vector's and string's dtor takes care of it (6) changed ScrolledText::setText (StringList const &sl_) param to const reference for the sake of efficiency .. we dont want to copy here hope we dont get problems with windows newline-style \r\n .. tried to take care of that ;) git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1223 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/gui/credits_menu.cpp | 19 +++++++++---------- src/gui/credits_menu.hpp | 3 +-- src/gui/scrolled_text.cpp | 4 ++-- src/gui/scrolled_text.hpp | 7 +++++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/gui/credits_menu.cpp b/src/gui/credits_menu.cpp index 239160ce4..72cac7908 100644 --- a/src/gui/credits_menu.cpp +++ b/src/gui/credits_menu.cpp @@ -30,35 +30,34 @@ CreditsMenu::CreditsMenu() { std::string filename; + StringList credits_text_list; try { filename = loader->getPath("data/CREDITS"); - FILE *fd = fopen(filename.c_str(), "ra"); + //FIXME: we should change to c++ - filestreams + FILE *fd = fopen(filename.c_str(), "r"); char s[1024]; char *p; while(fgets(s, 1023, fd)) { - p = strdup(s); - m_string_list.push_back(p); + credits_text_list.push_back(std::string(s)); } // while + fclose(fd); + fd = NULL; } catch(std::runtime_error& e) { printf(_("Couldn't load '%s'\n"),filename.c_str()); - m_string_list.push_back(_("CREDIT file was not installed properly!!")); - m_string_list.push_back(_("Please check 'data/CREDIT'!!")); + credits_text_list.push_back(_("CREDIT file was not installed properly!!")); + credits_text_list.push_back(_("Please check 'data/CREDIT'!!")); } - setText(m_string_list); + setText(credits_text_list); } // CreditsMenu //----------------------------------------------------------------------------- CreditsMenu::~CreditsMenu() { - while(m_string_list.size()>0) - { - m_string_list.pop_back(); - } } // ~CreditsMenu /* EOF */ diff --git a/src/gui/credits_menu.hpp b/src/gui/credits_menu.hpp index 761bf0d10..b0ab410ab 100644 --- a/src/gui/credits_menu.hpp +++ b/src/gui/credits_menu.hpp @@ -29,12 +29,11 @@ class CreditsMenu: public ScrolledText { -protected: +private: int m_xLeft, m_xRight, m_yBottom, m_yTop; float m_yPos, m_ySpeed; int m_numberOfLines; int m_fontSize; - StringList m_string_list; public: CreditsMenu(); ~CreditsMenu(); diff --git a/src/gui/scrolled_text.cpp b/src/gui/scrolled_text.cpp index 1b84f4c08..2685cc0d9 100644 --- a/src/gui/scrolled_text.cpp +++ b/src/gui/scrolled_text.cpp @@ -46,7 +46,7 @@ ScrolledText::~ScrolledText() } // ~ScrolledText //----------------------------------------------------------------------------- -void ScrolledText::setText(StringList sl_) +void ScrolledText::setText(StringList const &sl_) { m_string_list=sl_; if(m_rect) glDeleteLists(m_rect, 1); @@ -82,7 +82,7 @@ void ScrolledText::update(float dt) { if((m_y_pos-i*m_font_size < m_y_top + m_y_bottom ) && m_y_pos-i*m_font_size > -m_font_size) - font_gui->Print(m_string_list[i], 24, + font_gui->Print(m_string_list[i].c_str(), 24, m_x_left,(int)m_y_pos-i*m_font_size); } glMatrixMode(GL_PROJECTION); diff --git a/src/gui/scrolled_text.hpp b/src/gui/scrolled_text.hpp index 8cfa4bab3..435dff15e 100644 --- a/src/gui/scrolled_text.hpp +++ b/src/gui/scrolled_text.hpp @@ -25,20 +25,23 @@ #include "base_gui.hpp" #include "player.hpp" -typedef std::vector StringList; class ScrolledText: public BaseGUI { protected: + typedef std::vector StringList; + +private: int m_x_left, m_x_right, m_y_bottom, m_y_top; float m_y_pos, m_y_speed; int m_font_size; StringList m_string_list; int m_rect; + public: ScrolledText(); ~ScrolledText(); - void setText (StringList sl_); + void setText (StringList const &sl_); void select (); void update (float dt);