How can I create a rectangular SDL surface filled with a particular color?

Posted by user3689 on Game Development See other posts from Game Development or by user3689
Published on 2011-08-25T04:56:57Z Indexed on 2012/03/21 23:41 UTC
Read the original article Hit count: 163

Filed under:
|

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?

© Game Development or respective owner

Related posts about c++

Related posts about sdl