Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

What do you mean by 'recasting'? If you just mean that the same Uint32* cast appears on both sides of an assignment then that's just to ensure that q, which is a UInt8 pointer, gets read from as if it were a UInt32 pointer, and written into j, which is an int, but needs to be treated as a UInt32 pointer (to accept the value from q). But it's just copying a 32-bit value across. The pitch values tell the routine how far to move to read/write the next value. That's about it really.

I have no idea why an int is being cast to a pointer - that looks like bad code to me - but it's not something that will affect the speed.

I would be surprised if you can optimise that loop as it looks fairly optimal to me. (Although drawing columns like that is rarely optimal, which is why blitting typically does it by rows.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.