What's wrong with this code?!

Started by
1 comment, last by BradDaBug 21 years, 10 months ago

  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.
I like the DARK layout!
Advertisement
It will only copy the first byte of each pixel...

Joakim Asplund
http://megajocke.cjb.net
Joakim Asplundhttp://megajocke.y2.org
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 ]

This topic is closed to new replies.

Advertisement