Recasting and Drawing in SDL
- by user1078123
I have some code that essentially draws a column on the screen of a wall in a raycasting-type 3d engine. I am trying to optimize it, as it takes about 10 milliseconds do draw a million pixels using this, and the vast majority of game time is spent in this loop. However, I don't quite understand what's occurring, particularly the recasting (I modified the "pixel manipulation" sample code from the SDL documentation). "canvas" is the surface I am drawing to, and "hello" is the surface containing the texture for the column.
int c = (curcol)* canvas->format->BytesPerPixel;
void *canvaspixels = canvas->pixels;
Uint16 texpitch = hello->pitch;
int lim = (drawheight +startdraw) * canvpitch +c + (int) canvaspixels;
Uint8 *k = (Uint8 *)hello->pixels + (hit)* hello->format->BytesPerPixel;
for (int j= (startdraw)*(canvpitch)+c + (int) canvaspixels; (j< lim); j+= canvpitch){
Uint8 *q = (Uint8 *) ((int(h))*(texpitch)+k);
*(Uint32 *)j = *(Uint32 *)q;
h += s;
}
We have void pointers (not sure how those are even represented), 8, 16, and 32 bit ints (h and s are floats), all being intermingled, and while it works, it is quite confusing.