XWindow Background Color

Hi,

Does anybody know how can I change the background color of a X11 window
with SDL ?

Thanks

Hm… This question doesn’t make too much sense.

Since you’re using SDL, the fact that the target is X11 doesn’t
really matter.

So, I’ll change your question to:

“How can I change the background color of an SDL window?”

In that case, by “background”, I assume you mean you simply want to
draw a color into your SDL window (perhaps before blitting sprites, text,
or other objects into the window).

So the question becomes:

“How can I fill an SDL window with a certain color?”

And, since an SDL window is really just another SDL surface, we can
finally boil the question down to:

“How can I fill an SDL surface with a certain color?”

The answer to that’s fairly simple. Just use “SDL_FillRect()”.
For example:

SDL_Surface * my_window;
Uint8 red, green, blue;

/* Init. SDL, open a window… */

/* Pick some color to fill the window with: */
red = …;
green = …;
blue = …;

/* Fill the window with that color: */

SDL_FillRect(my_window, NULL,
SDL_MapRGB(my_window->format,
red,
green,
blue));

If you end up using the same color a lot, you can simply
call the “SDL_MapRGB” function once, and store the ‘pixel’ color result
into a variable (of type Uint32):

SDL_Surface * my_window;
Uint32 background_color

background_color = SDL_MapRGB(my_window->format,
…, /* Red /
…, /
Green /
…); /
Blue */

SDL_FillRect(my_window, NULL, background_color);

Just so you know, I’m using the “NULL” for the value of ‘dstrect’,
the ‘rectangle’ where the fill should occur. From SDL_FillRect’s man page:

“If dstrect is NULL, the whole surface will be filled with color.”

If you only wanted to fill a certain block, you’d do:

SDL_Rect dest_rect;

dest_rect.x = …; /* Left /
dest_rect.y = …; /
Top /
dest_rect.w = …; /
Width /
dest_rect.h = …; /
Height */

SDL_FillRect(my_window, &dest_rect, background_color);

(Remember, SDL_FillRect wants a pointer to your SDL_Rect variable!)

Note that this DOESN’T mean you can simply do this:

SDL_Rect * dest_rect_ptr;

dest_rect_ptr->x = …;
… and so forth

SDL_FillRect(my_window, dest_rect_ptr, background_color);

Notice how “dest_rect_ptr” ITSELF never got ‘assigned’ anything.
It’s an uninitialized pointer! D’oh!

If you insist on doing it this way (you shouldn’t; I’m only saying this
to make a point about pointers - and not even really to you specifically,
for all I know, you’re a C guru… I just like adding to the collective
of human knowledge via mailing lists :^) ), you’ll need to do:

dest_rect_ptr = malloc(sizeof(SDL_Rect));

and then be sure assign ALL elements of the ‘SDL_Rect’ structure
(x, y, w, and h).

Whew! I hope that answered your question! If not, let me know :wink:

Good luck!

-bill!
I heart SDLOn Wed, Sep 11, 2002 at 04:57:04PM -0100, Asier wrote:

Hi,

Does anybody know how can I change the background color of a X11 window
with SDL ?

Whew! I hope that answered your question! If not, let me know :wink:

Yes, that was pretty comprehensive. :slight_smile:

While on topic of exploiting specific X11 behavior through SDL, I’d like
to ask another question, though.

Some X11 programs exploit the technique of drawing to background pixmap
of their main window. One of the advantages this approach is the X
server doing screen refresh for you even when your program is stopped
under debugger. I found this extremely useful during debugging many,
many times. Is there a way to force SDL to do this?

Thanks.

latimeriusOn Wed, Sep 11, 2002 at 09:32:18AM -0700, Bill Kendrick wrote:

Some X11 programs exploit the technique of drawing to background pixmap
of their main window. One of the advantages this approach is the X
server doing screen refresh for you even when your program is stopped
under debugger. I found this extremely useful during debugging many,
many times. Is there a way to force SDL to do this?

No. It turned out not to be faster than drawing to a shared memory segment,
and is not supported on many non-XFree86 server / hardware configurations.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment