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?
You never actually fill
blockwith a non-black color. AndwinSurfaceis cleared to black. Black-on-black is rather hard to see :)Fill
blockwith a different color (and fix the*maskparameters forSDL_CreateRGBSurface(); the ones you have are kinda...weird, did you think they specified a default color-fill or something?):Result:
All together: