Wednesday 15 September 2010

c++ - How to update single pixels efficiently on SDL 1.2 surface? -



c++ - How to update single pixels efficiently on SDL 1.2 surface? -

i'm writing little thing calculates rgb values each pixel of image.

i want display each 1 after calculated. problem can't figure out how create fast. calculating finish image , displaying takes few seconds, displaying each pixel right takes few minutes.

it of import can see progress on image calculation. there way solve task efficiently in sdl 1.2?

my code looks follows now:

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 surface default values image = sdl_creatergbsurface(sdl_swsurface,img.width(),img.height(),32,0,0,0,0); if(image == null) { std::cout << "failed creating sdl surface"; } // create array pixels uint32 *pixels = (uint32 *)image->pixels; //apply image screen sdl_blitsurface(image, null, screen, null); //update screen sdl_flip(screen); // compute image (int = 0; < img.width(); i++) { (int j = 0; j < img.height(); j++) { img(i, j) = computecolor(i, j, img.width(), img.height()); pixels[i * img.width() + j] = sdl_maprgb(image->format,(uint8)getr(img(i,j)), (uint8)getg(img(i,j)),(uint8)getb(img(i,j))); sdl_blitsurface(image, null, screen, null); sdl_updaterect(screen, i, j, i, j); } } //pause sdl_delay(2000); //free loaded image sdl_freesurface(image); //quit sdl sdl_quit();

you not need intermediate image, can set pixels straight onto screen surface. may phone call sdl_flip() or sdl_updaterect() each line, not each pixel, much faster.

also, looping through lines instead of columns give little speed boost, because pixels on single line located near each other in physical memory, , sequential memory access faster on modern cpus.

// create array pixels uint32 *pixels = (uint32 *)screen->pixels; // clear screen sdl_fillrect(screen, null, 0); //update screen sdl_flip(screen); // compute image (int j = 0; j < img.height(); j++) { (int = 0; < img.width(); i++) { img(i, j) = computecolor(i, j, img.width(), img.height()); pixels[i + j * img.width()] = sdl_maprgb(image->format,(uint8)getr(img(i,j)), (uint8)getg(img(i,j)),(uint8)getb(img(i,j))); } sdl_updaterect(screen, i, j, img.width(), 1); } // display final result sdl_flip(screen); sdl_delay(2000);

you can create separate int variables img.height() , img.width(), won't doing function phone call each iteration, compiler able optimize anyway.

c++ sdl rgb pixel sdl-1.2

No comments:

Post a Comment