Jump to content
  • Advertisement
Sign in to follow this  
Fomas

Point screen-pixels to anywhere else...is it possible?

This topic is 3982 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

Hi everybody! ;) I just came across this post SDL_Surface pixel data trick where one guy was trying to point the screen->pixels to his own data buffer. But he didn't mention whether it worked or not, so I tried it myself and...unfortunately it didn't work for me. Has anybody else tried this? And if it's not supposed to work, why not? Here is a code that I'm trying to run:
SDL_Surface *FillRect(int w, int h, int bpp)
{
    Uint32 flags = SDL_SWSURFACE | SDL_SRCCOLORKEY | SDL_SRCALPHA,
           rMask = 0xff000000,
           gMask = 0x00ff0000,
           bMask = 0x0000ff00,
           aMask = 0x00000000; // No transperency

    SDL_Surface *temp = SDL_CreateRGBSurface(flags, w, h, bpp,
                                             rMask, gMask, bMask, aMask);

    SDL_FillRect(temp, 0, SDL_MapRGB(temp->format, 255, 0, 0));

    return temp;
}

int main ( int argc, char** argv )
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return 1;
    }

    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE |
                                                         SDL_DOUBLEBUF);
    if (!screen) {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Surface *filledRect = FillRect(640, 480, 32);

    /** Trying to alter screen buffer **/
    if (SDL_MUSTLOCK(screen))
        SDL_LockSurface(screen);

    void *screenBackUp = screen->pixels;

    /*
    Uint32 *filledRectPixs = (Uint32*) filledRect->pixels;
    screen->pixels = filledRectPixs;
    */

    screen->pixels = filledRect->pixels;

    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);

    SDL_Flip(screen); /* Update the screen */
    SDL_Delay(2000);

    /** Swap back **/
    if (SDL_MUSTLOCK(screen))
        SDL_LockSurface(screen);

    screen->pixels = screenBackUp;

    if (SDL_MUSTLOCK(screen))
        SDL_UnlockSurface(screen);

    SDL_FreeSurface(filledRect);

    return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Typically if you want to write 2d pixel data to the final frame buffer in you create a quad that is perfectly aligned with the screen (appropriately referred to as a screen aligned quad)and texture map it with a texture that contains your "source pixels".

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Typically if you want to write 2d pixel data to the final frame buffer in you create a quad that is perfectly aligned with the screen (appropriately referred to as a screen aligned quad)and texture map it with a texture that contains your "source pixels".

SDL's drawing capabilities are totally 2D, using a pixel buffer for image data. It has no definitions of quads or such as OpenGL does. His question is about making the pixel pointer from the SDL_Surface bound to the screen point to his own pixel buffer.

Share this post


Link to post
Share on other sites
The surface returned by SDL_SetVideoMode() is most likely a dummy structure for strictly read-only purposes. So the information inside isn't actually used to draw the screen.

For more information you can dig around the source in the SVN or if you compiled the source then just step though the SDL_UpdateRect/Flip() method.

Good Luck.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!