c++ - How to update single pixels efficiently on SDL 1.2 surface? -
I am writing a small item that calculates RGB values for each pixel of the image.
I want to display each after the calculation. The problem is that I did not know how to make it faster, it takes a few seconds to calculate the entire image and then display it, where each pixel takes a few minutes to appear.
It is important that I can see progress on the image count. Is there any way to solve this task efficiently in SDL 1.2?
My code now looks like this:
SDL_Surface * screen = NULL; SDL_Surface * image = NULL; SDL_Init (SDL_INIT_EVERYTHING); // Set screen screen = SDL_SetVideoMode (img.width), img.height (), 32, SDL_SWSURFACE); // Create the surface with the image of the basic values = SDL_CreateRGBSurface (SDL_SWSURFACE, img.width), img.height (), 32,0,0,0,0,0,0); If (image == faucet) {std :: cout & lt; & Lt; "SDL failed to form surface"; } // Create an array for pixels Uint32 * pixel = (Uint32 *) image- & gt; Pixels; // Apply the image to the SDL_ BlitSurface screen (image, zero, screen, tap); // Update Screen SDL_Flip (screen); // compute image (int i = 0; i Format, (Uint8) getR (IMG (I),), (UINT8) getG (IMG (I, J)), (UIT 8) getB (img (i, j))); SDL_BlitSurface (image, tap, screen, tap); SDL_UpdateRect (Screen, I, J, I, J); }} // stop SDL Della (2000); // Free the loaded image SDL_FreeSurface (image); // Exit SDL SDL_Quit;
You do not need an intermediate image, you can put pixels directly on the surface of the screen Are there. For each pixel, you can call SDL_Flip () or SDL_FupetRect () for each pixel, it will be very fast.
In addition, looping through lines rather than columns, will boost you a little speed, because the pixels are located in a physical memory on a single line, and the modern CPU's sequential memory access It's fast.
You can also create a different variable variable for img.height () and img.width (), so that you can not call each repetition function, but the compiler usually does anyway Able to customize
Comments
Post a Comment