# How can I optimize per-pixel drawing in SDL?

I've been messing around with C++ the last few days and I'm trying to figure out how to draw the screen pixel by pixel each frame.

It sounded simple enough at first, but then I realized for loops during the games main loop is nothing but a slow mess. How do I safely and with speed show visual effects?

• I don't understand your point about the sin function, and the relate issue concerning "it's not my method, since I use the standard method" is not answerable without your implementation details, so I removed them. Feel free to add them back if you can clarify them appropriately. – Josh Mar 17 '15 at 16:29

Consequently, if you want to do per-pixel manipulation efficiently, you are better off constructing your own in-memory buffer of the pixel data (something as simple as a char * pointing at a buffer of 4 * width * height bytes, assuming you want four-component RGBA pixels) and directly manipulating that.
Further, when actually manipulating the pixels, it's better not to actually set each pixel individually, if possible. Most of the time you'll be rendering objects that consist of more than one pixel, and in those cases you should use specialized routines to blit the data. Focus on accessing the memory of the destination buffer (and source buffer, if you are copying a sprite) in a cache-coherent fashion. Copy entire rows of data when possible (for opaque sprites, for example, you can memcpy the rows of the sprite).