Changeset 2546


Ignore:
Timestamp:
Mar 5, 2013, 1:36:41 PM (7 years ago)
Author:
sam
Message:

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

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/font.cpp

    r2216 r2546  
    3232
    3333private:
    34     char *name;
     34    String m_name;
    3535    TileSet *tileset;
    3636    ivec2 size;
     
    4444  : data(new FontData())
    4545{
    46     data->name = (char *)malloc(7 + strlen(path) + 1);
    47     sprintf(data->name, "<font> %s", path);
     46    data->m_name = String("<font> ") + path;
    4847
    4948    data->tileset = Tiler::Register(path, ivec2(0), ivec2(16));
     
    5655{
    5756    Tiler::Deregister(data->tileset);
    58     free(data->name);
    5957    delete data;
    6058}
     
    6765char const *Font::GetName()
    6866{
    69     return data->name;
     67    return data->m_name.C();
    7068}
    7169
  • trunk/src/gpu/shader.cpp

    r2506 r2546  
    345345#if defined USE_D3D9 || defined _XBOX
    346346    /* Global variables are prefixed with "$" */
    347     char tmpname[128];
    348     sprintf(tmpname, "$%s", uni);
     347    String tmpname = String("$") + uni;
    349348    D3DXCONSTANT_DESC cdesc;
    350349    D3DXHANDLE hr;
     
    352351
    353352    count = 0;
    354     hr = data->frag_table->GetConstantByName(nullptr, tmpname);
     353    hr = data->frag_table->GetConstantByName(nullptr, tmpname.C());
    355354    if (hr)
    356355        data->frag_table->GetConstantDesc(hr, &cdesc, &count);
     
    362361
    363362    count = 0;
    364     hr = data->vert_table->GetConstantByName(nullptr, tmpname);
     363    hr = data->vert_table->GetConstantByName(nullptr, tmpname.C());
    365364    if (hr)
    366365        data->vert_table->GetConstantDesc(hr, &cdesc, &count);
  • trunk/src/sample.cpp

    r2496 r2546  
    4646
    4747private:
    48     char *name, *path;
     48    String m_name;
    4949#if defined USE_SDL_MIXER
    5050    Mix_Chunk *m_chunk;
     
    6060  : data(new SampleData())
    6161{
    62     data->name = (char *)malloc(9 + strlen(path) + 1);
    63     data->path = data->name + 9;
    64     sprintf(data->name, "<sample> %s", path);
     62    data->m_name = String("<sample> ") + path;
    6563
    6664#if defined USE_SDL_MIXER
     
    8987    Mix_FreeChunk(data->m_chunk);
    9088#endif
    91     free(data->name);
    9289    delete data;
    9390}
     
    10097char const *Sample::GetName()
    10198{
    102     return data->name;
     99    return data->m_name.C();
    103100}
    104101
  • trunk/src/text.cpp

    r2506 r2546  
    3333
    3434private:
    35     int font, align, length;
    36     char *text;
     35    int font, align;
     36    String m_text;
    3737    vec3 pos;
    3838};
     
    4646{
    4747    data->font = Forge::Register(font);
    48     data->text = text ? strdup(text) : nullptr;
    49     data->length = text ? strlen(text) : 0;
     48    if (text)
     49        data->m_text = text;
    5050    data->pos = vec3(0, 0, 0);
    5151
     
    5555void Text::SetText(char const *text)
    5656{
    57     if (data->text)
    58         free(data->text);
    59     data->text = text ? strdup(text) : nullptr;
    60     data->length = text ? strlen(text) : 0;
     57    data->m_text = text;
    6158}
    6259
    6360void Text::SetInt(int val)
    6461{
    65     if (data->text)
    66         free(data->text);
    67     char text[128];
    68     sprintf(text, "%i", val);
    69     data->text = strdup(text);
    70     data->length = strlen(text);
     62    data->m_text = String::Printf("%i", val);
    7163}
    7264
     
    9183    Entity::TickDraw(seconds);
    9284
    93     if (data->text)
     85    int length = data->m_text.Count();
     86    if (length)
    9487    {
    9588        Font *font = Forge::GetFont(data->font);
    9689        vec3 delta(0.0f);
    9790        if (data->align == ALIGN_RIGHT)
    98             delta.x -= data->length * font->GetSize().x;
     91            delta.x -= length * font->GetSize().x;
    9992        else if (data->align == ALIGN_CENTER)
    100             delta.x -= data->length * font->GetSize().x / 2;
    101         font->Print(data->pos + delta, data->text);
     93            delta.x -= length * font->GetSize().x / 2;
     94        font->Print(data->pos + delta, data->m_text.C());
    10295    }
    10396}
     
    10598Text::~Text()
    10699{
    107     if (data->text)
    108         free(data->text);
    109100    Forge::Deregister(data->font);
    110101    delete data;
  • trunk/src/tileset.cpp

    r2506 r2546  
    5050
    5151private:
    52     char *name, *path;
     52    String m_name;
    5353    int *tiles, ntiles;
    5454    ivec2 size, isize, count;
     
    6767  : data(new TileSetData())
    6868{
    69     data->name = (char *)malloc(10 + strlen(path) + 1);
    70     data->path = data->name + 10;
    71     sprintf(data->name, "<tileset> %s", path);
     69    data->m_name = String("<tileset> ") + path;
    7270
    7371    data->tiles = nullptr;
     
    10199{
    102100    free(data->tiles);
    103     free(data->name);
    104101    delete data;
    105102}
     
    160157char const *TileSet::GetName()
    161158{
    162     return data->name;
     159    return data->m_name.C();
    163160}
    164161
Note: See TracChangeset for help on using the changeset viewer.