JonHolland
Still Fresh
- Joined
- Dec 17, 2006
- Messages
- 26
Today I spent some time creating some primitive graphic functions using SDL, here is a quick code snippet:
GFX_HLine and GFX_VLine are both simple for/next loops. My thought that a faster version of the above function would be this:
In this function, the only calls to other functions is to PutPixel (I suppose I could further speed it up by using a macro for putpixel instead of a function) and is using 2 loops instead of four.
However, all that said, the increase in speed is very small, and the function is slightly harder to read, and a good bit larger in size.
My few Questions:
Code:
/* GFX_Rect (SDL_Surface, X, Y, X2, Y2, Color)
Usage: Draws a hollow rectangle in specified color on specified Surface
SDL_Surface: The Surface to draw on
X,Y: Starting Coordinates
X2,Y2: Ending Coordinates
Color: Color value in hexidecimal (or Use SDL_MapRGB)
Example:
GFX_Rect (surface,0,0,100,100,SDL_MapRGB(surface->format,0,255,0));
Draws a green box that is 100x100.
*/
void GFX_Rect (SDL_Surface *surface, int x, int y, int x2, int y2, Uint32 color)
{
// Draw the top and bottom of the Rect
GFX_HLine (surface,x,y,x2,color); // Top
GFX_HLine (surface,x,y2,x2,color); // Bottom
// Draw the sides
GFX_VLine (surface,x,y,y2,color); // Left Side
GFX_VLine (surface,x2,y,y2,color); // Right Side
}
GFX_HLine and GFX_VLine are both simple for/next loops. My thought that a faster version of the above function would be this:
Code:
void GFX_FastRect (SDL_Surface *surface, int x, int y, int x2, int y2, Uint32 color)
{
int iCount;
// Draw Top and Bottom Lines
for (iCount = x; iCount <= x2; iCount++)
{
GFX_PutPixel(surface,iCount,y,color);
GFX_PutPixel(surface,iCount,y2,color);
}
// Draw Left and Right Sides
for (iCount = y; iCount <= y2; iCount++)
{
GFX_PutPixel(surface,x,iCount,color);
GFX_PutPixel(surface,x2,iCount,color);
}
}
In this function, the only calls to other functions is to PutPixel (I suppose I could further speed it up by using a macro for putpixel instead of a function) and is using 2 loops instead of four.
However, all that said, the increase in speed is very small, and the function is slightly harder to read, and a good bit larger in size.
My few Questions:
- How many CPU cycles are used when you call a function.
- Are For loops slower than while loops?
- How would you further optimize this function just for kicks?