drawing a texture with SDL

Started by
0 comments, last by benryves 18 years, 8 months ago
Say I have a 2D array representing an RGB image, and a single graphical surface in an SDL window. What's the fastest way to draw this image at a given position in the window? I once tried something with multiple surfaces and blitting, but I was confused about it, and for some reason on some computers it was flickering (not flickering in the low FPS way, but flickering between black and the image). Drawing the image pixel by pixel didn't flicker anywhere, but is slow. And I don't want to use OpenGL this time. [EDIT] I think I'll go with OpenGL again anyway, I want to do alpha translucency, multiplying with a color, and such too. But I'm still interested in the answer to the above question :)
Advertisement
I'd create a pointer to the top-left point for the sprite inside the screen buffer. I'd have a for(y) loop that would go from the top to the bottom of the sprite - for each y setting up a pointer to the pixel row inside your 2D array. Next I'd have a for(x) loop that just copied each pixel over (*dest++=*source++). At the end of that for(x) loop, I'd increment the pointer to the screen by (the width of the screen - width of the sprite). Naturally, this wouldn't work for sprites that are part off the screen (you'd have to do the clipping yourself), in which case it would possibly be easier to just copy the entire 2D array to a new temporary surface and use SDL to handle the blitting.
Of course, this all makes a lot of assumptions (such as the data in the 2D array being at the same bpp as the screen surface).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement