I'm having a question about this method. I'm working on a game and are using it to lock my background and then alter it pizel by pixel. I'm doing a fade-to-black and a fade-from-black.
However, I've experienced that it is extremely slow to do this. Of course, modifying 640x480 pixels one by one may not be recommended, but this is rediciously slow (~2-3 fps) especially considering I'm using a P4 2.4GHz 1Gb RAM.
I made some tests and it seems the lag only occurs when reading the data from the surface (to alter it):
This simply sets the data to some value and it runs smoothly:
Code:
if(!FAILED(SecondarySurface->Lock(NULL, &SurfaceDesc, DDLOCK_WAIT, NULL)))
{
//Get a pointer to the surface
SurfacePointer = (DWORD*)SurfaceDesc.lpSurface;
for(Y = 0; Y < SCREENHEIGHT; Y++)
{
for(X = 0; X < SCREENWIDTH; X++)
{
(*SurfacePointer) = rand() % 0xFFFFFF;
SurfacePointer++;
}
}
//Unlock the surface
SecondarySurface->Unlock(NULL);
}
This however is extremely laggy (I used the RGB macros before, but tried making my own too since I thought it was the macros that were so slow):
Code:
if(!FAILED(SecondarySurface->Lock(NULL, &SurfaceDesc, DDLOCK_WAIT, NULL)))
{
//Get a pointer to the surface
SurfacePointer = (DWORD*)SurfaceDesc.lpSurface;
for(Y = 0; Y < SCREENHEIGHT; Y++)
{
for(X = 0; X < SCREENWIDTH; X++)
{
TempColour = (*SurfacePointer);
TempR = (BYTE)((INT)((TempColour & 0xFF0000) >> 16) * FadePercentage / 100);
TempG = (BYTE)((INT)((TempColour & 0x00FF00) >> 8) * FadePercentage / 100);
TempB = (BYTE)((INT)(TempColour & 0x0000FF) * FadePercentage / 100);
(*SurfacePointer) = (TempR << 16) | (TempG << 8) | TempB;
SurfacePointer++;
}
}
//Unlock the surface
SecondarySurface->Unlock(NULL);
}
I thought it might be too much calculations on each pixel, so I tried just this:
Code:
TempColour = (*SurfacePointer);
(*SurfacePointer) = TempColour + 10;
But that lags like h*ll too.
It seems like the problem is the reading from the surface, but how can fix this? Help plz?
EDIT:
I'm using Visual C/C++ on WinXP