SDL beginner: create Rectangle surface filled with color

Hi
i like to create Rectangle surface with color
that is not image . here is my code that compiles fine but dosnt work :
im passing the function this params:-------------------------------------------------------------------------------------------------------------------
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 im doing here wrong ?

Try changing the last two code lines.
This one:
SDL_FillRect(screen,&box,SDL_MapRGB ( ButtonSurface->format, R, G, B
));
Should be filling the entire Button surface, not just a part of the screen.
Change it to:
SDL_FillRect(ButtonSurface,NULL,SDL_MapRGB ( ButtonSurface->format, R,
G, B ));

This one:
SDL_BlitSurface(ButtonSurface,NULL,screen,&box);
Should be be put somewhere else, not in the constructor. I suggest making a
draw() method and putting it in there.

Jonny DOn Thu, Aug 25, 2011 at 2:25 AM, Meir Yanovich wrote:

Hi
i like to create Rectangle surface with color
that is not image . here is my code that compiles fine but dosnt work :
im passing the function this params:


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 im doing here wrong ?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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

Make sure you called SDL_Init(SDL_INIT_VIDEO) somewhere before this.

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

box.x and box.y happen to be zero here, but if you moved this button
somewhere else, you wouldn’t be filling the whole thing (or possibly any
of it.

If you want to fill the whole thing, just pass a NULL there.

I assume this is eventually going to be a picture and not just a solid
color, which is why you’ve made a surface here. If you just want to fill
colors, SDL_FillRect() is all you need, not SDL_BlitSurface().

what im doing here wrong ?

The actual reason you’re not getting anything on the screen: don’t
forget to call SDL_Flip() to display what you’ve drawn.

–ryan.