• Advertisement


This topic is now archived and is closed to further replies.

can someone explain this? SDL tutorial...

This topic is 5586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I''m reading "Linux Game Programming" (now available online for free in PDF and DVI formats), but am confused by something that the author fails to explain well (IMHO).
SDL often emulates nonstandard screen resolutions with higher resolutions, and the pitch member of the pixel format structure indicates the actual width of the framebuffer. You should always use pitch instead of w for calculating offsets into the pixels buffer or else your application might not work on some configurations
Then it goes on into an example... (this is just part of it)
/* We can now safely write to the video surface. We''ll draw a niec gradient pattern by varying our red and blue components along the X and Y axes. Notice the formula used to calculate the offset into the framebuffer for each pixel. (The pitch is the number of bytes per scanline in memory.) */
for(x = 0; x < 256; x++){
    for(y = 0; y < 256; y++){
        Uint16 pixel_color;
        int offset;
        pixel_color = CreateHicolorPixel(screen->format,x,0,y);
        offset = (screen->pitch / 2 * y + x);
        raw_pixels[offset] = pixel_color;
CreateHiColorPixel''s prototype is - Uint16 CreateHicolorPixel(SDL_PixelFormat *fmt, Uint8 red, Uint8 green, Uint8 blue) Could someone please explain this code? Specifically, the offset and pitch parts of it. The author of this book seems to assume more than he says he does... "Thanks!

Share this post

Link to post
Share on other sites

  • Advertisement