Skip to main content
Bounty Awarded with 50 reputation awarded by CommunityBot
added 445 characters in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable:.

if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), 
        [](sprite const &a, sprite const &b) { return a.texture == b.texture; }))
     ++drawCount;
if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), [](sprite const &a, sprite const &b) { return a.texture == b.texture; })) ++drawCount;

[Actually, re-reading, that's wrong--the idea's correct, but I misunderstood your code a little bit, and translated it incorrectly.]

The obvious next step to take would be to avoid the linear search for the texture for every sprite. You haven't shown enough of the rest of the system to see an obvious way to do that, but chances seem pretty decent that you can probably avoid it.

Looking at the OpenGL part, I don't see an obvious way to improve performance a lot. You're already using glDrawRangeElements to draw all the sprites in one call. I suppose you might be able to switch from GL_TRIANGLES to something like GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP to reduce the number of vertices you use, but 1) I didn't try to look at the code carefully enough to be sure you can use those, and 2) even if you can, it probably won't make a huge difference anyway.

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable:

if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), 
        [](sprite const &a, sprite const &b) { return a.texture == b.texture; }))
     ++drawCount;

The obvious next step to take would be to avoid the linear search for the texture for every sprite. You haven't shown enough of the rest of the system to see an obvious way to do that, but chances seem pretty decent that you can probably avoid it.

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable.

if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), [](sprite const &a, sprite const &b) { return a.texture == b.texture; })) ++drawCount;

[Actually, re-reading, that's wrong--the idea's correct, but I misunderstood your code a little bit, and translated it incorrectly.]

The obvious next step to take would be to avoid the linear search for the texture for every sprite. You haven't shown enough of the rest of the system to see an obvious way to do that, but chances seem pretty decent that you can probably avoid it.

Looking at the OpenGL part, I don't see an obvious way to improve performance a lot. You're already using glDrawRangeElements to draw all the sprites in one call. I suppose you might be able to switch from GL_TRIANGLES to something like GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP to reduce the number of vertices you use, but 1) I didn't try to look at the code carefully enough to be sure you can use those, and 2) even if you can, it probably won't make a huge difference anyway.

added 445 characters in body
Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable:

if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), 
        [](sprite const &a, sprite const &b) { return a.texture == b.texture; }))
     ++drawCount;

The obvious next step to take would be to avoid the linear search for the texture for every sprite. You haven't shown enough of the rest of the system to see an obvious way to do that, but chances seem pretty decent that you can probably avoid it.

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable.

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable:

if (sprites.end() != std::find_if(sprites.begin()+count=1, sprites.end(), 
        [](sprite const &a, sprite const &b) { return a.texture == b.texture; }))
     ++drawCount;

The obvious next step to take would be to avoid the linear search for the texture for every sprite. You haven't shown enough of the rest of the system to see an obvious way to do that, but chances seem pretty decent that you can probably avoid it.

Source Link
Jerry Coffin
  • 34.1k
  • 4
  • 77
  • 145

I would avoid the testing for calling Begin and End appropriately by creating a separate class that does the begin actions during construction and the end actions during destruction.

As for speeding up the code, I think the first real step is to clarify the code a bit so it's easier to see what's really going on and what's needed. Just for example, you have:

    for( int i = count + 1; i < sprites.size(); i++ )
    {
        if( sprites[count].texture == sprites[i].texture )
        {
            drawCount++;
        }
        else
        {
            break;
        }
    }

It looks like this could be rewritten to use std::find_if instead, and end up quite a bit simpler and more readable.