(1) fixed memory-leak related to vector GrandPrixSelect::m_all_cups. items were allocated with new, but never deleted. changed vector to vector of objects:

old:
  std::vector<CupData*> m_all_cups;
new:
  std::vector<CupData> m_all_cups;

related: 
changed RaceManager::setGrandPrix(const CupData &cup_) to const reference rather than pointer to CupData

(2) made vector CupData::m_tracks private and added getter

(3) CupData::m_name was passed in GrandPrixSelect::GrandPrixSelect() to WidgetSet as const char*
    then it was passed to Widget::_text. changed the latter to std::string and passed it as std::string
    
(4) changed Widget::count_text  to std::string as well .. 

(5) tracked down all uses of Widget::count_text & Widget::_text to plib. changed
all funcs in between to work with std::string rather than const char*

Font::getBBox
WidgetSet::set_label
WidgetSet::get_label
WidgetSet::start
WidgetSet::state
WidgetSet::label

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1238 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
ikework 2007-09-10 17:03:31 +00:00
parent 894cd8a604
commit 9d89de895e
8 changed files with 47 additions and 37 deletions

View File

@ -21,6 +21,10 @@
#ifndef HEADER_CUPDATA_H #ifndef HEADER_CUPDATA_H
#define HEADER_CUPDATA_H #define HEADER_CUPDATA_H
#include <string>
#include <vector>
#include <cassert>
#include "herring_manager.hpp" #include "herring_manager.hpp"
/** Simple class that hold the data relevant to a 'cup', aka. a number /** Simple class that hold the data relevant to a 'cup', aka. a number
@ -31,14 +35,13 @@ class CupData
std::string m_filename; // Original filename, only for error handling needed std::string m_filename; // Original filename, only for error handling needed
std::string m_description; // Description for this track std::string m_description; // Description for this track
std::string m_herring_style; // herring style which overwrites the track default std::string m_herring_style; // herring style which overwrites the track default
public:
/** The ident of the tracks in this cup in their right order, ident /** The ident of the tracks in this cup in their right order, ident
means the filename of the .track file without .track extension means the filename of the .track file without .track extension
(ie. 'volcano') */ (ie. 'volcano') */
std::vector<std::string> m_tracks; std::vector<std::string> m_tracks;
public:
/** Load the CupData from the given filename */ /** Load the CupData from the given filename */
CupData (const std::string filename); CupData (const std::string filename);
CupData () {}; // empty for initialising CupData () {}; // empty for initialising
@ -46,6 +49,8 @@ public:
const std::string& getDescription () const { return m_description; } const std::string& getDescription () const { return m_description; }
const std::string& getHerringStyle() const { return m_herring_style;} const std::string& getHerringStyle() const { return m_herring_style;}
const std::string& getFilename () const { return m_filename; } const std::string& getFilename () const { return m_filename; }
const std::string& getTrack(size_t track_index) const { assert(track_index < m_tracks.size()); return m_tracks[track_index]; }
size_t getTrackCount() const { return m_tracks.size(); }
} }
; // CupData ; // CupData

View File

@ -36,12 +36,12 @@ public:
const static int CENTER_OF_SCREEN=-1; const static int CENTER_OF_SCREEN=-1;
enum FontSize {SMALL=18, MEDIUM=24, LARGE=30 }; enum FontSize {SMALL=18, MEDIUM=24, LARGE=30 };
Font(char* fontname); Font(char* fontname);
Font(std::string fontname) { Font(fontname.c_str()); } Font(const std::string &fontname) { Font(fontname.c_str()); }
~Font(); ~Font();
void getBBox(const char *text, int size, bool italic, void getBBox(const std::string &text, int size, bool italic,
float *left, float *right, float *bot, float *top) float *left, float *right, float *bot, float *top)
{ {
m_fnt->getBBox(text, size, italic, left, right, bot, top); m_fnt->getBBox(text.c_str(), size, italic, left, right, bot, top);
} }
// The actual main function which does everything // The actual main function which does everything
@ -53,7 +53,7 @@ public:
float scale_x=1.0f, float scale_y=1.0f, float scale_x=1.0f, float scale_y=1.0f,
int left=-1, int right=-1, int top=-1, int bottom=-1, int left=-1, int right=-1, int top=-1, int bottom=-1,
bool doShadow=false); bool doShadow=false);
void Print( std::string text, int size, void Print( std::string const &text, int size,
FontAlignType fontalign_x, int x, FontAlignType fontalign_x, int x,
FontAlignType fontalign_y, int y, FontAlignType fontalign_y, int y,
int red=255, int green=255, int blue=255, int red=255, int green=255, int blue=255,
@ -68,7 +68,7 @@ public:
// Convenience functions to reduce the number of parameters // Convenience functions to reduce the number of parameters
// -------------------------------------------------------- // --------------------------------------------------------
void Print( const char *text, int size, int x, int y, void Print( const std::string &text, int size, int x, int y,
int red=255, int green=255, int blue=255, int red=255, int green=255, int blue=255,
int left=-1, int right=-1, int top=-1, int bottom=-1) int left=-1, int right=-1, int top=-1, int bottom=-1)
{ {

View File

@ -46,12 +46,16 @@ GrandPrixSelect::GrandPrixSelect()
if (StringUtils::has_suffix(*i, ".cup")) if (StringUtils::has_suffix(*i, ".cup"))
{ {
std::string fullPath= "data/" + (std::string)*i; std::string fullPath= "data/" + (std::string)*i;
CupData *cup = new CupData(fullPath.c_str()); CupData cup(fullPath.c_str());
m_all_cups.push_back(cup); m_all_cups.push_back(cup);
if(nId==0) if(nId==0)
widgetSet -> start(m_menu_id, cup->getName().c_str(), GUI_SML, nId, 0); {
widgetSet -> start(m_menu_id, cup.getName(), GUI_SML, nId, 0);
}
else else
widgetSet -> state(m_menu_id, cup->getName().c_str(), GUI_SML, nId, 0); {
widgetSet -> state(m_menu_id, cup.getName(), GUI_SML, nId, 0);
}
nId++; nId++;
} // if } // if
} // for i } // for i
@ -81,14 +85,14 @@ void GrandPrixSelect::update(float dt)
glOrtho(0.0, user_config->m_width, 0.0, user_config->m_height, -1.0, +1.0); glOrtho(0.0, user_config->m_width, 0.0, user_config->m_height, -1.0, +1.0);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND); glEnable(GL_BLEND);
CupData *cup = m_all_cups[CLICKED_TOKEN]; const CupData &cup = m_all_cups[CLICKED_TOKEN];
glPushMatrix(); glPushMatrix();
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
const GLfloat BACKGROUND_COLOUR[4] = { 0.3f, 0.3f, 0.3f, 0.5f }; const GLfloat BACKGROUND_COLOUR[4] = { 0.3f, 0.3f, 0.3f, 0.5f };
glColor4fv(BACKGROUND_COLOUR); glColor4fv(BACKGROUND_COLOUR);
glCallList(m_rect); glCallList(m_rect);
glPopMatrix(); glPopMatrix();
font_gui->Print(cup->getDescription(), GUI_MED, font_gui->Print(cup.getDescription(), GUI_MED,
Font::ALIGN_CENTER, -1, Font::ALIGN_BOTTOM, 10); Font::ALIGN_CENTER, -1, Font::ALIGN_BOTTOM, 10);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);

View File

@ -27,7 +27,7 @@
class GrandPrixSelect: public BaseGUI class GrandPrixSelect: public BaseGUI
{ {
private: private:
std::vector<CupData*> m_all_cups; std::vector<CupData> m_all_cups;
int m_rect; int m_rect;
public: public:
GrandPrixSelect(); GrandPrixSelect();

View File

@ -91,7 +91,7 @@ GrandPrixMode::start_race(int n)
raceSetup.m_mode = RaceSetup::RM_GRAND_PRIX; raceSetup.m_mode = RaceSetup::RM_GRAND_PRIX;
raceSetup.m_difficulty = m_difficulty; raceSetup.m_difficulty = m_difficulty;
raceSetup.m_num_laps = 2; raceSetup.m_num_laps = 2;
raceSetup.m_track = m_cup.m_tracks[n]; raceSetup.m_track = m_cup.getTrack(n);
raceSetup.m_karts.resize(m_karts.size()); raceSetup.m_karts.resize(m_karts.size());
raceSetup.m_players.resize(m_players.size()); raceSetup.m_players.resize(m_players.size());
raceSetup.setHerringStyle(m_cup.getHerringStyle()); raceSetup.setHerringStyle(m_cup.getHerringStyle());
@ -126,7 +126,7 @@ void
GrandPrixMode::next() GrandPrixMode::next()
{ {
m_track += 1; m_track += 1;
if (m_track < int(m_cup.m_tracks.size())) if (m_track < int(m_cup.getTrackCount()))
{ {
scene->clear(); scene->clear();
start_race(m_track); start_race(m_track);
@ -142,7 +142,7 @@ GrandPrixMode::next()
void void
GrandPrixMode::exit_race() GrandPrixMode::exit_race()
{ {
if (m_track < int(m_cup.m_tracks.size())) if (m_track < int(m_cup.getTrackCount()))
{ {
RaceMode::exit_race(); RaceMode::exit_race();
} }

View File

@ -148,7 +148,7 @@ public:
void setPlayerKart(int player, const std::string& kart); void setPlayerKart(int player, const std::string& kart);
void setNumPlayers(int num); void setNumPlayers(int num);
void reset(); void reset();
void setGrandPrix(CupData *cup_) { m_cup = *cup_; } void setGrandPrix(const CupData &cup_) { m_cup = cup_; }
void setDifficulty(RaceDifficulty diff_) { m_difficulty = diff_; } void setDifficulty(RaceDifficulty diff_) { m_difficulty = diff_; }
void setNumLaps(int num) { m_num_laps = num; } void setNumLaps(int num) { m_num_laps = num; }
void setTrack(const std::string& track_) { m_track = track_; } void setTrack(const std::string& track_) { m_track = track_; }

View File

@ -19,6 +19,8 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <sstream>
#include "constants.hpp" #include "constants.hpp"
#include "widget_set.hpp" #include "widget_set.hpp"
#include "loader.hpp" #include "loader.hpp"
@ -277,7 +279,7 @@ int WidgetSet::add_widget(int parent, int type)
m_widgets[id].color0 = gui_wht; m_widgets[id].color0 = gui_wht;
m_widgets[id].color1 = gui_wht; m_widgets[id].color1 = gui_wht;
m_widgets[id].scale = 1.0f; m_widgets[id].scale = 1.0f;
m_widgets[id].count_text = (char*)NULL; m_widgets[id].count_text = "";
/* Insert the new widget into the parents's widget list. */ /* Insert the new widget into the parents's widget list. */
@ -314,7 +316,7 @@ int WidgetSet::vstack(int parent) { return add_widget(parent, GUI_VSTACK); }
int WidgetSet::filler(int parent) { return add_widget(parent, GUI_FILLER); } int WidgetSet::filler(int parent) { return add_widget(parent, GUI_FILLER); }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void WidgetSet::set_label(int id, const char *text) void WidgetSet::set_label(int id, const std::string &text)
{ {
float l,r,b,t; float l,r,b,t;
font_gui->getBBox(text, m_widgets[id].size, 0.0f, &l, &r, &b, &t); font_gui->getBBox(text, m_widgets[id].size, 0.0f, &l, &r, &b, &t);
@ -338,7 +340,9 @@ void WidgetSet::set_label(int id, const char *text)
void WidgetSet::set_count(int id, int value) void WidgetSet::set_count(int id, int value)
{ {
m_widgets[id].value = value; m_widgets[id].value = value;
sprintf(m_widgets[id].count_text,"%d",value); std::stringstream ss;
ss << value;
m_widgets[id].count_text = ss.str();
float l,r,b,t; float l,r,b,t;
font_gui->getBBox(m_widgets[id].count_text, m_widgets[id].size, 0, &l, &r, &b, &t); font_gui->getBBox(m_widgets[id].count_text, m_widgets[id].size, 0, &l, &r, &b, &t);
m_widgets[id].yOffset = (int)(b); m_widgets[id].yOffset = (int)(b);
@ -395,7 +399,7 @@ int WidgetSet::image(int parent, int textureId, int w, int h, int rect)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int WidgetSet::start(int parent, const char *text, int size, int token, int value) int WidgetSet::start(int parent, const std::string &text, int size, int token, int value)
{ {
int id; int id;
@ -406,7 +410,7 @@ int WidgetSet::start(int parent, const char *text, int size, int token, int valu
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int WidgetSet::state(int parent, const char *text, int size, int token, int value) int WidgetSet::state(int parent, const std::string &text, int size, int token, int value)
{ {
int id; int id;
@ -432,7 +436,7 @@ int WidgetSet::state(int parent, const char *text, int size, int token, int valu
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int WidgetSet::label(int parent, const char *text, int size, int rect, const float *c0, int WidgetSet::label(int parent, const std::string &text, int size, int rect, const float *c0,
const float *c1) const float *c1)
{ {
int id; int id;
@ -466,8 +470,9 @@ int WidgetSet::count(int parent, int value, int size, int rect)
m_widgets[id].color0 = gui_yel; m_widgets[id].color0 = gui_yel;
m_widgets[id].color1 = gui_red; m_widgets[id].color1 = gui_red;
m_widgets[id].rect = rect; m_widgets[id].rect = rect;
m_widgets[id].count_text = new char[20]; std::stringstream ss;
sprintf(m_widgets[id].count_text,"%d",value); ss << value;
m_widgets[id].count_text = ss.str();
float l,r,b,t; float l,r,b,t;
font_gui->getBBox(m_widgets[id].count_text, size, 0, &l, &r, &b, &t); font_gui->getBBox(m_widgets[id].count_text, size, 0, &l, &r, &b, &t);
m_widgets[id].yOffset = (int)(b); m_widgets[id].yOffset = (int)(b);
@ -952,10 +957,6 @@ int WidgetSet::delete_widget(int id)
glDeleteLists(m_widgets[id].rect_obj, 1); glDeleteLists(m_widgets[id].rect_obj, 1);
/* Mark this widget unused. */ /* Mark this widget unused. */
if(m_widgets[id].type==GUI_COUNT)
{
delete m_widgets[id].count_text;
}
m_widgets[id].type = GUI_FREE; m_widgets[id].type = GUI_FREE;
m_widgets[id].text_img = 0; m_widgets[id].text_img = 0;
m_widgets[id].rect_obj = 0; m_widgets[id].rect_obj = 0;

View File

@ -121,8 +121,8 @@ struct Widget
int car; int car;
int cdr; int cdr;
const char *_text; std::string _text;
char *count_text; std::string count_text;
int text_width; int text_width;
GLuint text_img; GLuint text_img;
GLuint rect_obj; GLuint rect_obj;
@ -146,12 +146,12 @@ public:
/* /*
* Get/set functions * Get/set functions
*/ */
void set_label(int id, const char *); void set_label(int id, const std::string&);
void set_multi(int id, const char *); void set_multi(int id, const char *);
void set_count(int id, int); void set_count(int id, int);
void set_clock(int id, int); void set_clock(int id, int);
const char* get_label(int id) { return m_widgets[id]._text; } const std::string& get_label(int id) { return m_widgets[id]._text; }
int get_token(int id) const; int get_token(int id) const;
int get_value(int id) const; int get_value(int id) const;
@ -174,13 +174,13 @@ public:
int image(int parent, int, int, int, int rect=GUI_ALL); int image(int parent, int, int, int, int rect=GUI_ALL);
//a normal text menu entry, except that it is automatically immediately activated //a normal text menu entry, except that it is automatically immediately activated
int start(int parent, const char *, int, int, int value=GUI_OFF); int start(int parent, const std::string&, int, int, int value=GUI_OFF);
//a normal text menu entry //a normal text menu entry
int state(int parent, const char *, int, int, int value=GUI_OFF); int state(int parent, const std::string&, int, int, int value=GUI_OFF);
//a text label (cannot be selected). c0 and c1 are two colours that the text is shaded with //a text label (cannot be selected). c0 and c1 are two colours that the text is shaded with
int label(int parent, const char *text, int size=GUI_MED, int rect=GUI_ALL, int label(int parent, const std::string &text, int size=GUI_MED, int rect=GUI_ALL,
const float *c0=0, const float *c1=0); const float *c0=0, const float *c1=0);
//Create a multi-line text box using a vertical array of labels. //Create a multi-line text box using a vertical array of labels.