[SDL] Trying to display a colored surface

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


Hi Carlos

> And I made a mistake at the begining of my test declaration you must
> understant "SDL_Surface *test", and not "SDL_Surface test"

Sorry, i didn't even notice this. C isn't my main programming language.

>
> > I will explain better my problem...
> > I have no error but I still get a black screen

So SDL is initialising OK by the look of it.

> >
> > I do :
> >
> > ***
> > SDL_Surface test;
> > test =
>
> SDL_CreateRGBSurface(SDL_SWSURFACE,100,100,16,rmask,gmask,bmask,amask);
> //mask are already setup
>
> > //this creates for me a 100x100 square, right ?

yes. test should be 100x100 surface now

> > ***
> >
> > So I thought that my memory surface would be fully size and create.
> > then I create just one rectangle to give position into the screen
> >
> > ***
> > SDL_Rect r;
> > r.x = 150; r.y=150; //this mean that I want to place "test" at position
>
> 150,150 in the screen, right ?
>

As long as you use this rect as your destination, then yes.


> > and I fill my rectangles to be sure I see somethink
> > (here starts the main loop)
> >
> > ***
> > SDL_FillRect(screen, NULL, 0x00000000); //screen set to black, screen is
> > a
>
> 1024x768x16 window
>
> > SDL_FillRect(test, NULL, 0xFFFFFF00); //test to white
> > ***
> >
> > finally I blit
> >
> > ***
> > SDL_BlitSurface(test,NULL,screen,&d); //In the official documentation, it
>

Where is your rect called r?
I reckon that if you placed &r there instead of &d, you'd be fine.

remember, the destination is the area on the target surface you want to blit 
to.

> is written that passing NULL to second argument takes entire source
> surface, so won't be my "test" surface entirely blitted ???

yes, by passing NULL, the whole of your test surface will be used.

>
> > SDL_Flip(screen)
> > ***
> > The result is that I still get a black screen... :/ no white square at

Try changing your SDL_BlitSurface(test,NULL,screen,&d); to 
SDL_BlitSurface(test,NULL,screen,&r); saying as the rect r is set up with the 
screen co-ordinates you want to blit to.

Also, you may want to use the function SDL_MapRGB instead of supplying hex 
values for colours.

So instead of SDL_FillRect(test, NULL, 0xFFFFFF00);
You would have SDL_FillRect(test, NULL, SDL_MapRBG(test->format,255,255,255));
Please forgive any inaccuracies in my C, it's been a while :-)

Hope this helps

Jason

>
> position (150,150)
>
> > Mon, 15 Jul 2002 08:42:21 +0000
> >
> > Jason Farmer <jason at cerebral-bicycle.co.uk> a écrit:
> > > 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
> > >
> > > _______________________________________________
> > > 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
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl





More information about the SDL mailing list