I'm learning SDL. I'd like to create rectangular surface filled with a flat color (not an image). Below is my code -- it compiles fine, but doesn't work.

I'm passing the function these parameters:

SDL_Surface*  m_screen = SDL_SetVideoMode(
  SDL_MapRGB(m_screen->format,0xFF, 0xFF, 0xFF)
Button button(m_screen,0,0,50,50,255,0,0)
Button::Button(SDL_Surface* screen,int x,int y,int w,int h,int R, int G, int B) {
      SDL_Rect box;
      SDL_Surface * ButtonSurface;
      ButtonSurface = NULL ;
      Uint32 rmask, gmask, bmask, amask;
         rmask = 0xff000000;
         gmask = 0x00ff0000;
         bmask = 0x0000ff00;
         amask = 0x000000ff;
         rmask = 0x000000ff;
         gmask = 0x0000ff00;
         bmask = 0x00ff0000;
         amask = 0xff000000;

      box.x = x;
      box.y = y;
      box.w = w;
      box.h = h;

      ButtonSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, box.w,box.h, 32,
                                    rmask, gmask, bmask, amask);

      if(ButtonSurface == NULL) {
         LOG_MSG("Button::Button Button failed");

      SDL_FillRect(screen,&box,SDL_MapRGB ( ButtonSurface->format, R, G, B ));


What am I doing wrong here?


2 Answers 2


In SDL, whenever you're not seeing things drawing, or in drawing in completely the wrong color, eg. white, your first thought should be, "What is wrong with my colour formats?". You'll want to check your bpp depth as well. I think due to changes from prior versions of SDL (could be wrong here) some sources say this is bits per pixel, others think it's bytes per pixel. In SDL 1.2.14, it's supposed to be bytes per pixel IIRC.

Are you calling this in a loop? Nothing will happen if you're not calling it in a loop -- of course -- your app will just quit immediately. Sorry, had to ask.

Best advice I can give you is go and follow LazyFoo's SDL Tutorials... each time I had something wrong in regards to the basics, that's where I headed (after looking at a bunch of other references), and it always solved the problem. Write minimal test cases based on his examples and you should be A for away.

  • \$\begingroup\$ +1 for LazyFoo...I could have sworn that NeHe also used to have SDL tutorials but I can't find anything from him other than OpenGL. \$\endgroup\$
    – erik
    Commented Aug 26, 2011 at 15:20
  • \$\begingroup\$ @espais NeHe didn't himself make SDL tutorials, but his tutorials were ported to SDL.(among others) \$\endgroup\$
    – akaltar
    Commented Aug 18, 2013 at 9:02

I don't see an SDL_Flip

SDL is double buffered so you need to fill the buffer, then display it.

It also looks like you are mixing up your object and rendering your object (not necessarily a bad thing, but it will start to get confusing when you have multiple objects)

As Nick stated, get yourself over to LazyFoo, those tutorials are great, up to date, cross platform, and he lets his schooling as a software engineer bleed through in some of them, so you get a taste of some advanced concepts like threading. I have not found a good substitute.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .