# Possible Optimization

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have done some timing to see where I lose speed in my current pixel drawing routine and I have tracked it down to the following line where the function memcpy is used. int offset=((ly)+(x*main.pixelwidth)); memcpy(szSurface+offset,&bgpal[pixel],main.pixelwidth); bgpal is an array that holds the palette for the specified pixel. I was wondering if anyone has any optimization ideas. I'm using direct draw btw. Thanks in advance

##### Share on other sites
why do you do it per pixel?

##### Share on other sites
Quote:
 Original post by Basirorwhy do you do it per pixel?

I interpret data, sort of like bitmaps do, but not exactly the same.

##### Share on other sites
Rather than using memcpy, write 4 different blitting functions -- one for each pixel size, and use normal assignment to set the pixel's value. There might be some clever optimizations that you can use, but try this first.

Your 32-bit per pixel blitting code might look something like this:
    unsigned __int32 bgpal[256];    ...    unsigned __int8 * srcrow = srcSurface + src_y * srcpitch + src_x;    unsigned __int8 * dstrow = dstSurface + dst_y * dstpitch + dst_x * 4;         for ( int y = 0; y < h; ++y )    {        unsigned __int8  * src = srcrow;        unsigned __int32 * dst = dstrow;            for ( int x = 0; x <= w; ++x )        {            dst[x] = bgpal[ src[x] ];        }        srcrow += srcpitch;        dstrow += dstpitch;    }

##### Share on other sites
try to just assign the values directly instead of using memcpy if you are going to do it pixel-by-pixel

##### Share on other sites
I have tried the following code:

init:
unsigned int *lszSurface = ddsd.lpSurface;

draw_func:
int offset=((ly)+(x*main.pixelwidth));
lszSurface[offset] = bgpal[pixel];

The image isnt exactly comming out right, there space in between every pixel (approx. 4?), it may be 3am in the morning, but I don't see anything wrong...

##### Share on other sites
Quote:
 Original post by aprenticeint offset=((ly)+(x*main.pixelwidth));lszSurface[offset] = bgpal[pixel];

Assuming main.pixelwidth == pitch, it should be:
offset = ly * main.pixelwidth + x

##### Share on other sites
thanks for your help guys, I've got it working now :)

1. 1
2. 2
3. 3
4. 4
Rutin
19
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632927
• Total Posts
3009250
• ### Who's Online (See full list)

There are no registered users currently online

×