[SDL] Trying to display a colored surface

Jason Farmer jason at cerebral-bicycle.co.uk
Mon Jul 15 00:48:01 PDT 2002


Hi Carlos,

If you don't include all of the code in your example, other people can't 
compile it on their systems. This makes finding bugs difficult.
You have omitted the screen setup code, the bug may be in there and we 
wouldn't know. Also, you haven't said what he actual problem is or what you 
see on the screen. Does the screen go black or do you get an error of some 
kind?

I think you may be getting your rects confused.
You haven't defined a width or height for the rect r so they will be random 
values.

Also, you are passing nil to the SDL_BlitSurface function for your destination 
rect. You would only ever do this if your surface that you are blitting 
covers the whole of the screen. 

May I suggest that you create a destination rect. (the destination rect tells 
SDL where to draw the image, but you have left yours blank)

SDL_Rect d;

d.x = 100;
d.y = 100;
d.w = 20;
d.h = 20;

then alter your source rect (the source rect tells SDL where to get the data 
from to draw to the screen)

r.x = 0;
r.y = 0;
r.w = 20;
r.h = 20;

SDL_BlitSurface(test,&d,screen,&r);

All this aside, If you just want to display a coloured square, why not take a 
look at SDL_FillRect

Hope this helps

Jason

> I already know how to setup a screen window, that's why I don't specify the
> code for the screen variable. Below the comments " /* window  ... */ ",
> just after le SDL_Rect declaration, you must understand that I put a window
> setup for "screen" (which uses le SDL_SetVideoMode)
>
> Just the think I want to do, is drawing a little square into the screen
> WITH the "test" variable...
>
> > > So i do this
> > >
> > > *****
> > >
> > > SDL_Surface *screen;
> > > SDL_Surface *test;
> > > SDL_Rect r;
> > > /* window setup an screen setup */
> > >
> > > test =
> > > SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,16,rmask,gmask,bmask,amask);
> > > //square setup
> > >
> > > r.x = 100; r.y = 100;
> > > while(1) //main loop
> > > {
> > >   SDL_FillRect(screen, NULL, 0x00000000); //I color my screen to black
> > >   SDL_FillRect(test, NULL, 0xFFFFFF00); //I want to color my square to
> > > white SDL_BlitSurface(test,NULL,screen,&r); //then draw it, this doen't
> > > work
> > >
> > >   SDL_Flip(screen);
> > > }
> > >
> > > what can I do to just display a little colored square if not what I
> > > writen ???
> >
> > To setup a window you must use SDL_SetVideoMode().
> > SDL_CreateRGBSurface() creates an offscreen surface. I suggest you to
> > read some tutorials.
> >
> >
> >
> > _______________________________________________
> > SDL mailing list
> > SDL at libsdl.org
> > http://www.libsdl.org/mailman/listinfo/sdl
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl






More information about the SDL mailing list