[SDL] About SDL_Draw library
cocobear
cocobear.cn at gmail.com
Thu Oct 11 21:32:39 PDT 2007
于 Thu, 11 Oct 2007 15:28:34 +0100
Jon Dowland <lists at alcopop.org> 写道:
> On Tue, Oct 09, 2007 at 10:55:36PM +0800, cocobear wrote:
> > I am reading the source code of SDL_draw library,here is the
> > prototype of Draw_Pixel function:
> >
> > void Draw_Pixel(SDL_Surface *super,
> > Sint16 x, Sint16 y,
> > Uint32 color);
> >
> > But I can't find the implement of this function.
> > I can only find a function pointer of Draw_Pixel :
> >
> > extern DECLSPEC
> > void (*Draw_Pixel)(SDL_Surface *super,
> > Sint16 x, Sint16 y, Uint32 color);
> >
> > this is just a definition of Draw_Pixel(variable) ,isn't it?
>
> Yes. Where did you find that? in SDL_draw.c, I see[1]
>
I find it in: SDL_draw.h line 35.
> 68 void (*Draw_Pixel)(SDL_Surface *super,
> 69 Sint16 x, Sint16 y, Uint32 color) =
> Draw_Pixel_Init;
>
> So calling "Draw_Pixel" the first time actually
>
> * dereferences the Draw_Pixel pointer
> * calls Draw_Pixel_Init()
> * calls Draw_Init
> * re-assigns the Draw_Pixel pointer to a
> function specific to your bit-depth, e.g.
> Draw_Pixel_4
> * re-calls Draw_Pixel (involves a dereference)
> * re-calls Draw_Pixel (involves a dereference)
>
> This seems to be a way of avoiding the user of the library (I.e. you)
> having to call an initialisation function before using the methods.
>
> It's also buggy, if I've read this correctly, drawing the first pixel
> you specify twice.
>
static
void Draw_Pixel_Init(SDL_Surface *super,
Sint16 x, Sint16 y, Uint32 color)
{
Draw_Init();
Draw_Pixel(super, x, y, color);
}
void (*Draw_Pixel)(SDL_Surface *super,
Sint16 x, Sint16 y, Uint32 color) = Draw_Pixel_Init;
Draw_Pixel is a pointer of Draw_Pixel_Init function,but
Draw_Pixel_Init use Draw_Pixel too.
Do you mean it is a bug?
> It also means that each call to Draw_Pixel has an extra bit of
> indirection involved over a normal function call. I did some tests a
> while ago to see which is faster: the function-pointer based approach
> to specifying a colourdepth-specific draw function or the
> switch-based that is in the SDL docs. I was suprised to find that for
> small-medium numbers of calls, the switch-based tended to be faster
> in my environment, with the function-pointer method pulling ahead
> only once you are calling the putpixel method millions of times in
> succession, certainly, many times more than there were pixels in my
> surface (which I imagine no real-world app will do)
>
> [1]
> http://sdl-draw.cvs.sourceforge.net/sdl-draw/sdl-draw/src/SDL_draw.c?view=markup
>
>
It seemed that this library use so many macro,and I spend a lot of time
to understand it.
Regards!
/*
*Welcome to cocobear's home!
*http://cocobear.cn
*/
More information about the SDL
mailing list