Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  SCREEN_BPP,
  SDL_SWSURFACE
);
SDL_FillRect(
  m_screen,
  &m_screen->clip_rect,
  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;
      #if SDL_BYTEORDER == SDL_BIG_ENDIAN
         rmask = 0xff000000;
         gmask = 0x00ff0000;
         bmask = 0x0000ff00;
         amask = 0x000000ff;
      #else
         rmask = 0x000000ff;
         gmask = 0x0000ff00;
         bmask = 0x00ff0000;
         amask = 0xff000000;
      #endif

      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 ));

      //ut.ApplySurface(0,0,ButtonSurface,screen);
      SDL_BlitSurface(ButtonSurface,NULL,screen,&box);
   }

What am I doing wrong here?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
+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. –  espais Aug 26 '11 at 15:20
    
@espais NeHe didn't himself make SDL tutorials, but his tutorials were ported to SDL.(among others) –  akaltar Aug 18 '13 at 9:02
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.