Can't get one SDL_Surface to blit on top of another

44 Views Asked by At

I am trying to get a colored box to blit on top of a black surface that I'm using as my window background. But when I compile and run this program all I get is the black window screen:

#include <iostream>
#include <string>
#include <SDL.h>

using namespace std;

int main(int argc, char** args)
{
    //Initializes SDL, and all subsystems
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        cout << "SDL failed to initialize\n";
    }
    else
    {
        cout << "SDL initialized\n";
    }

    //create a window
    SDL_Window * window = NULL;
    window = (SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000,600, SDL_WINDOW_BORDERLESS));

    //create window surface
    SDL_Surface * winSurface = NULL;
    winSurface = SDL_GetWindowSurface( window );

    //fill with a color
    SDL_FillRect( winSurface, NULL, SDL_MapRGB( winSurface->format,0,0,0));

    SDL_Surface * block = block = SDL_CreateRGBSurface(0,100,100,32,0,255,0,255);
    SDL_Rect dest1;
    dest1.x = 200;
    dest1.y = 200;
    dest1.h = 100;
    dest1.w = 100;
    SDL_BlitScaled(block, NULL, winSurface, &dest1);

    //updates the screen
    SDL_UpdateWindowSurface( window );

    cout << SDL_GetError();

    SDL_Event event;
    bool run = true;
    while (run==true)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    run=false;
                    //free up memory
                    SDL_FreeSurface(block); 
                    SDL_DestroyWindow( window );
                    //Quit SDL
                    SDL_Quit();
                }
            }
        }
    }

    return 0;
}

Where am I messing up or what do I need to add to this?

1

There are 1 best solutions below

0
genpfault On

You never actually fill block with a non-black color. And winSurface is cleared to black. Black-on-black is rather hard to see :)

Fill block with a different color (and fix the *mask parameters for SDL_CreateRGBSurface(); the ones you have are kinda...weird, did you think they specified a default color-fill or something?):

SDL_Surface* block = SDL_CreateRGBSurface( 0, 100, 100, 32, 0, 0, 0, 0 );
SDL_FillRect( block, NULL, SDL_MapRGB( block->format, 128, 128, 128 ) );

Result:

screenshot of a grey square on a black background

All together:

#include <SDL.h>
#include <iostream>
#include <string>

int main( int argc, char** args )
{
    //Initializes SDL, and all subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        std::cout << "SDL failed to initialize\n";
    }
    else
    {
        std::cout << "SDL initialized\n";
    }

    //create a window
    SDL_Window* window = SDL_CreateWindow(
        "Test",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        1000,
        600,
        SDL_WINDOW_BORDERLESS );

    //create window surface
    SDL_Surface* winSurface = SDL_GetWindowSurface( window );

    //fill with a color
    SDL_FillRect( winSurface, NULL, SDL_MapRGB( winSurface->format, 0, 0, 0 ) );

    SDL_Surface* block = SDL_CreateRGBSurface( 0, 100, 100, 32, 0, 0, 0, 0 );
    SDL_FillRect( block, NULL, SDL_MapRGB( block->format, 128, 128, 128 ) );

    SDL_Rect dest1;
    dest1.x = 200;
    dest1.y = 200;
    dest1.h = 100;
    dest1.w = 100;
    SDL_BlitScaled( block, NULL, winSurface, &dest1 );

    SDL_Event event;
    bool run = true;
    while( run == true )
    {
        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE )
            {
                run = false;
                break;
            }
        }

        //updates the screen
        SDL_UpdateWindowSurface( window );
    }

    //free up memory
    SDL_FreeSurface( block );
    SDL_DestroyWindow( window );

    //Quit SDL
    SDL_Quit();
    return 0;
}