Thread: Combining SDL and AlphaBlend - referencing buffers

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Combining SDL and AlphaBlend - referencing buffers

    I've now got SDL working, however, I have a big unknown. For over 3 years, I've been using AlphaBlend for all of my drawing and I don't want to change that. I've got the front and back buffers set up and working perfectly fine. Now that I've got SDL going, of which I only need for fullscreen mode, how do I set up SDL to reference the buffers that AlphaBlend uses? This is my basic setup:

    Code:
    ...
    // at the top, with the globals
    HDC HDCScreen; // pointer for front buffer, for displaying on screen
    BITMAPINFOHEADER ScreenBufferInfo;
    LPBITMAPINFOHEADER ScreenBufferInfoPointer;
    LPVOID ScreenBufferPointer;
    unsigned char ScreenBuffer[3145728]; // 1024x768 at 32-bit color
    HDC HDCBack; // pointer for back buffer, for drawing into
    HBITMAP HBMPBack;
    BITMAPINFOHEADER BackBufferInfo;
    LPBITMAPINFOHEADER BackBufferInfoPointer;
    LPVOID BackBufferPointer;
    unsigned char BackBuffer[3145728]; // 1024x768 at 32-bit color
    ...
    
    // the SDL surfaces
    SDL_Surface *SDLScreen; // the front buffer for SDL
    SDL_Surface *SDLBack; // the back buffer for SDL
    ...
    
    // in my initialization function where the buffers are initialized and set up:
    HDCScreen = GetDC(WindowHandle); // set the screen buffer DC
    ScreenBufferInfoPointer = &ScreenBufferInfo; // set the pointer
    ScreenBufferPointer = &ScreenBuffer;
    
    BackBufferInfoPointer = &BackBufferInfo; // set the pointer
    BackBufferPointer = &BackBuffer;
    HDCBack = CreateCompatibleDC(HDCScreen); // create a compatible DC, of which is to be used for drawing into the back buffer
    HBMPBack = CreateCompatibleBitmap(HDCScreen, ScreenWidth, ScreenHeight); // create compatible bitmap, used for drawing into, but not on screen
    HDCBitmap = CreateCompatibleDC(HDCScreen); // for AlphaBlend
    SelectObject(HDCBack, HBMPBack); // select the created bitmap
    ...
    
    // initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) != 0) // SDL doesn't initialize
    {
       // Display error with sprintf and Windows' MessageBox function
    }
    
    else
    {
       // 1024x768 at 32-bit color is widely supported and not an issue
       SDLScreen = SDL_SetVideoMode(1024, 768, 32, SDL_FULLSCREEN); // start in full screen
    }
    
    // in another function, after all of my AlphaBlend calls:
    // how do I reference the pixel data in BackBuffer?
    SDL_BlitSurface(SDLBack, NULL, SDLScreen, NULL);
    
    // after the program terminates, don't forget to free resources
    SDL_FreeSurface(SDLScreen);
    SDL_FreeSurface(SDLBack);
    SDL_Quit(); // quit SDL
    // DeleteObject numerous times for the several images I have
    The part in bold near the bottom is what I don't know - how do I reference the pixel data in BackBuffer?

    Edit: The only thing I want to use SDL for is just fullscreen and vsync, nothing more.
    Last edited by ulillillia; 05-19-2012 at 10:06 PM. Reason: Mentioned what I only intend on using SDL for
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Is it is even possible to get SDL to reference data in a predefined array? Either I get an all-out full-on crash upon starting when attempting to set SDL_Back, or an "unhandled exception" error. I've run out of ideas on how to do it. Only the initialization, setting of SDLScreen, and quitting parts work. I cannot get SDLBack to work no matter what I do. I've tried these, all to no avail:

    Code:
    SDLBack = HDCBack; // crashes program and makes it unresponsive
    SDLBack = HBMPBack; // same as above
    SDLBack = BackBuffer; // obviously won't work - BackBuffer is an array, not a struct
    SDLBack->Pixels = BackBuffer; // unhandled exception occurs upon reaching this line
    // leave the SDLBack out and all I get is an all-black image but at least the program runs if I switch to my main program's window and not SDL's window
    How do I get SDLBack to reference the pixel data in BackBuffer? That's all I'm after. There's gotta be some function related to SDL_LoadBMP that, instead of reading an image file from the hard drive, reads pixel data from BackBuffer after AlphaBlend has drawn into it (of which BackBuffer's pixel data changes 60 times a second (120 at the most)).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Figured I can't edit my post to update progress, so I have to make a new one....

    I found the SDL_CreateRGBSurfaceFrom function that takes existing pixel data, but I either get an all-black image or read access violation error with the blit. Basically, this is the idea:

    Code:
    // AlphaBlend stuff
    HDC HDCBack; // pointer for back buffer, for drawing into - I can't access this with SDL
    HBITMAP HBMPBack; // I can't access this with SDL
    BITMAPINFOHEADER BackBufferInfo; // irrelevant
    LPBITMAPINFOHEADER BackBufferInfoPointer; // irrelevant
    LPVOID BackBufferPointer; // I can access this, but it's all black
    unsigned char BackBuffer[3145728]; // back buffer pixel data 1024x768 at 32-bit color
    
    // right after SDL initialization:
    SDLBack = SDL_CreateRGBSurfaceFrom(BackBufferPointer, 1024, 768, 32, 4096, 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF); // set the back buffer's pixels to that of the back buffer's data, supposedly BGRA after the TGA format
    
    if (SDLBack == NULL) // if an error otherwise occurred
    {
    	// sprintf and MessageBox to indicate error
    }
    
    else
    {
    	SDLScreen = SDL_SetVideoMode(1024, 768, 32, 0); // test so I don't have to use alt+tab every time to quit
    	// SDLScreen = SDL_SetVideoMode(1024, 768, 32, SDL_FULLSCREEN); // start PM in full screen (this, until release, should only be done in the debug menu)
    }
    
    // after all the AlphaBlend drawing is done
    BitBlt(HDCScreen, 0, 0, ScreenWidth, ScreenHeight, HDCBack, 0, 0, SRCCOPY); // swaps the back and front buffers for displaying
    SDL_UpdateRect(SDLBack, 0, 0, 1024, 768); // test case - seems to have no effect
    SDL_BlitSurface(SDLBack, NULL, SDLScreen, NULL); // often causes read access violation
    SDL_UpdateRect(SDLScreen, 0, 0, 1024, 768); // supposedly the real one I need
    How do I get SDLBack to reference the pixel data in BackBuffer as is drawn with AlphaBlend? That's all I'm after.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AlphaBlend and BlendFunction - dynamic opacity
    By ulillillia in forum Windows Programming
    Replies: 0
    Last Post: 11-19-2009, 05:45 AM
  2. Referencing and De-referencing Question
    By audinue in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 08:22 AM
  3. You know... I still don't get buffers.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 06-15-2006, 06:47 AM
  4. copying buffers
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 06:54 AM

Tags for this Thread