Browse Source

base: remove some sprintf() calls in profit of string ops.

legacy
Sam Hocevar sam 12 years ago
parent
commit
6e282a674a
5 changed files with 23 additions and 41 deletions
  1. +3
    -5
      src/font.cpp
  2. +3
    -4
      src/gpu/shader.cpp
  3. +3
    -6
      src/sample.cpp
  4. +11
    -20
      src/text.cpp
  5. +3
    -6
      src/tileset.cpp

+ 3
- 5
src/font.cpp View File

@@ -31,7 +31,7 @@ class FontData
friend class Font; friend class Font;


private: private:
char *name;
String m_name;
TileSet *tileset; TileSet *tileset;
ivec2 size; ivec2 size;
}; };
@@ -43,8 +43,7 @@ private:
Font::Font(char const *path) Font::Font(char const *path)
: data(new FontData()) : data(new FontData())
{ {
data->name = (char *)malloc(7 + strlen(path) + 1);
sprintf(data->name, "<font> %s", path);
data->m_name = String("<font> ") + path;


data->tileset = Tiler::Register(path, ivec2(0), ivec2(16)); data->tileset = Tiler::Register(path, ivec2(0), ivec2(16));
data->size = data->tileset->GetSize(0); data->size = data->tileset->GetSize(0);
@@ -55,7 +54,6 @@ Font::Font(char const *path)
Font::~Font() Font::~Font()
{ {
Tiler::Deregister(data->tileset); Tiler::Deregister(data->tileset);
free(data->name);
delete data; delete data;
} }


@@ -66,7 +64,7 @@ void Font::TickDraw(float seconds)


char const *Font::GetName() char const *Font::GetName()
{ {
return data->name;
return data->m_name.C();
} }


void Font::Print(vec3 pos, char const *str, vec2 scale) void Font::Print(vec3 pos, char const *str, vec2 scale)


+ 3
- 4
src/gpu/shader.cpp View File

@@ -344,14 +344,13 @@ ShaderUniform Shader::GetUniformLocation(char const *uni) const
ShaderUniform ret; ShaderUniform ret;
#if defined USE_D3D9 || defined _XBOX #if defined USE_D3D9 || defined _XBOX
/* Global variables are prefixed with "$" */ /* Global variables are prefixed with "$" */
char tmpname[128];
sprintf(tmpname, "$%s", uni);
String tmpname = String("$") + uni;
D3DXCONSTANT_DESC cdesc; D3DXCONSTANT_DESC cdesc;
D3DXHANDLE hr; D3DXHANDLE hr;
UINT count; UINT count;


count = 0; count = 0;
hr = data->frag_table->GetConstantByName(nullptr, tmpname);
hr = data->frag_table->GetConstantByName(nullptr, tmpname.C());
if (hr) if (hr)
data->frag_table->GetConstantDesc(hr, &cdesc, &count); data->frag_table->GetConstantDesc(hr, &cdesc, &count);
if (count) if (count)
@@ -361,7 +360,7 @@ ShaderUniform Shader::GetUniformLocation(char const *uni) const
} }


count = 0; count = 0;
hr = data->vert_table->GetConstantByName(nullptr, tmpname);
hr = data->vert_table->GetConstantByName(nullptr, tmpname.C());
if (hr) if (hr)
data->vert_table->GetConstantDesc(hr, &cdesc, &count); data->vert_table->GetConstantDesc(hr, &cdesc, &count);
if (count) if (count)


+ 3
- 6
src/sample.cpp View File

@@ -45,7 +45,7 @@ class SampleData
friend class Sample; friend class Sample;


private: private:
char *name, *path;
String m_name;
#if defined USE_SDL_MIXER #if defined USE_SDL_MIXER
Mix_Chunk *m_chunk; Mix_Chunk *m_chunk;
int m_channel; int m_channel;
@@ -59,9 +59,7 @@ private:
Sample::Sample(char const *path) Sample::Sample(char const *path)
: data(new SampleData()) : data(new SampleData())
{ {
data->name = (char *)malloc(9 + strlen(path) + 1);
data->path = data->name + 9;
sprintf(data->name, "<sample> %s", path);
data->m_name = String("<sample> ") + path;


#if defined USE_SDL_MIXER #if defined USE_SDL_MIXER
Array<String> pathlist = System::GetPathList(path); Array<String> pathlist = System::GetPathList(path);
@@ -88,7 +86,6 @@ Sample::~Sample()
#if defined USE_SDL_MIXER #if defined USE_SDL_MIXER
Mix_FreeChunk(data->m_chunk); Mix_FreeChunk(data->m_chunk);
#endif #endif
free(data->name);
delete data; delete data;
} }


@@ -99,7 +96,7 @@ void Sample::TickGame(float seconds)


char const *Sample::GetName() char const *Sample::GetName()
{ {
return data->name;
return data->m_name.C();
} }


void Sample::Play() void Sample::Play()


+ 11
- 20
src/text.cpp View File

@@ -32,8 +32,8 @@ class TextData
friend class Text; friend class Text;


private: private:
int font, align, length;
char *text;
int font, align;
String m_text;
vec3 pos; vec3 pos;
}; };


@@ -45,8 +45,8 @@ Text::Text(char const *text, char const *font)
: data(new TextData()) : data(new TextData())
{ {
data->font = Forge::Register(font); data->font = Forge::Register(font);
data->text = text ? strdup(text) : nullptr;
data->length = text ? strlen(text) : 0;
if (text)
data->m_text = text;
data->pos = vec3(0, 0, 0); data->pos = vec3(0, 0, 0);


m_drawgroup = DRAWGROUP_HUD; m_drawgroup = DRAWGROUP_HUD;
@@ -54,20 +54,12 @@ Text::Text(char const *text, char const *font)


void Text::SetText(char const *text) void Text::SetText(char const *text)
{ {
if (data->text)
free(data->text);
data->text = text ? strdup(text) : nullptr;
data->length = text ? strlen(text) : 0;
data->m_text = text;
} }


void Text::SetInt(int val) void Text::SetInt(int val)
{ {
if (data->text)
free(data->text);
char text[128];
sprintf(text, "%i", val);
data->text = strdup(text);
data->length = strlen(text);
data->m_text = String::Printf("%i", val);
} }


void Text::SetPos(vec3 pos) void Text::SetPos(vec3 pos)
@@ -90,22 +82,21 @@ void Text::TickDraw(float seconds)
{ {
Entity::TickDraw(seconds); Entity::TickDraw(seconds);


if (data->text)
int length = data->m_text.Count();
if (length)
{ {
Font *font = Forge::GetFont(data->font); Font *font = Forge::GetFont(data->font);
vec3 delta(0.0f); vec3 delta(0.0f);
if (data->align == ALIGN_RIGHT) if (data->align == ALIGN_RIGHT)
delta.x -= data->length * font->GetSize().x;
delta.x -= length * font->GetSize().x;
else if (data->align == ALIGN_CENTER) else if (data->align == ALIGN_CENTER)
delta.x -= data->length * font->GetSize().x / 2;
font->Print(data->pos + delta, data->text);
delta.x -= length * font->GetSize().x / 2;
font->Print(data->pos + delta, data->m_text.C());
} }
} }


Text::~Text() Text::~Text()
{ {
if (data->text)
free(data->text);
Forge::Deregister(data->font); Forge::Deregister(data->font);
delete data; delete data;
} }


+ 3
- 6
src/tileset.cpp View File

@@ -49,7 +49,7 @@ class TileSetData
friend class TileSet; friend class TileSet;


private: private:
char *name, *path;
String m_name;
int *tiles, ntiles; int *tiles, ntiles;
ivec2 size, isize, count; ivec2 size, isize, count;
vec2 scale; vec2 scale;
@@ -66,9 +66,7 @@ private:
TileSet::TileSet(char const *path, ivec2 size, ivec2 count) TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
: data(new TileSetData()) : data(new TileSetData())
{ {
data->name = (char *)malloc(10 + strlen(path) + 1);
data->path = data->name + 10;
sprintf(data->name, "<tileset> %s", path);
data->m_name = String("<tileset> ") + path;


data->tiles = nullptr; data->tiles = nullptr;
data->m_texture = 0; data->m_texture = 0;
@@ -100,7 +98,6 @@ TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
TileSet::~TileSet() TileSet::~TileSet()
{ {
free(data->tiles); free(data->tiles);
free(data->name);
delete data; delete data;
} }


@@ -159,7 +156,7 @@ void TileSet::TickDraw(float seconds)


char const *TileSet::GetName() char const *TileSet::GetName()
{ {
return data->name;
return data->m_name.C();
} }


ivec2 TileSet::GetCount() const ivec2 TileSet::GetCount() const


Loading…
Cancel
Save