This topic is now archived and is closed to further replies.


What's wrong with this code?!

Recommended Posts

BradDaBug    913

  Uint8 *pixels = (Uint8*)dest_surface->pixels;
Uint8 *oldpixels = (Uint8*)src_surface->pixels;

pixels[CurrentDestX * dest_surface->format->BytesPerPixel  + CurrentDestY * dest_surface->pitch] = oldpixels[CurrentDestX  * src_surface->format->BytesPerPixel + CurrentDestY * src_surface->pitch];  
The surfaces are SDL surfaces, btw. I want to copy one surface onto another. Don''t say I could just use blit, cause I know that. I''m trying to write code that will smooth a scaled surface, but I can''t even get this little bit of code to work. What''s happening is if I have that X * Bpp stuff, the surface is completely black. If I get rid of the Bpp part, it draws 1/4 of the surface, starting from the left side. It draws all the vertical part, but stops 1/4 of the way over. What''s wierd is that the 1/4 part it draws is drawn perfectly, not distorted like it should be if its not moving over far enough in memory.

Share this post

Link to post
Share on other sites
Kylotan    10010
To elaborate: you''re using Uint8 (one byte), but skipping bytes according to the bytes-per-pizel. So if it''s a 32-bit surface, you copy 1 byte, skip 4, copy 1, skip 4, and so on. The simplest way to get around this is to drop the bytes per pixel bit in that expression, and loop so that X goes from zero to width * bytes per pixel. eg. 128 pixels wide, 32-bit surface, X goes from 0 to 512, copying each byte as it goes.

Alternatively, write separate routines using different data types depending on the bytes per pixel, and keep the bytes per pixel multiplier in there. (Warning - gets marginally more complex for 24-bit images.)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post

Link to post
Share on other sites