Lack of const correctness in SDL_BlitSurface?

I think I came across a small problem in the library, but maybe someone
can advise me.

The documentation for ‘SDL_BlitSurface’ says:
“The final blit rectangle is saved in dstrect after all clipping is
performed (srcrect is not modified).” With the emphasis here being on
the ‘srcrect is not modified’ section.

However, when I try and call this from a const member function (in C++),
where the source rectangles are part of that class and therefore
constant, I get this error under Visual C++ 6:

error C2664: ‘SDL_UpperBlit’ : cannot convert parameter 2 from ‘const
SDL_Rect *’ to ‘SDL_Rect *’

Looking through the headers, I see this:

#define SDL_BlitSurface SDL_UpperBlit

/* This is the public blit function, SDL_BlitSurface(), and it performs
rectangle validation and clipping before passing it to
SDL_LowerBlit()
*/
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

Surely this last function prototype should really be:

extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, const SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

… since ‘*srcrect’ should not be getting altered?–
Ben Sizer

It seems that most of SDL has been written with a view to const
correctness. I for one would like to see this improved but it only seems
to come up with those people who use C++. It wouldn’t take too long to
do this but it would change a lot of files.

Kylotan wrote:>

I think I came across a small problem in the library, but maybe someone
can advise me.

The documentation for ‘SDL_BlitSurface’ says:
“The final blit rectangle is saved in dstrect after all clipping is
performed (srcrect is not modified).” With the emphasis here being on
the ‘srcrect is not modified’ section.

However, when I try and call this from a const member function (in C++),
where the source rectangles are part of that class and therefore
constant, I get this error under Visual C++ 6:

error C2664: ‘SDL_UpperBlit’ : cannot convert parameter 2 from ‘const
SDL_Rect *’ to ‘SDL_Rect *’

Looking through the headers, I see this:

#define SDL_BlitSurface SDL_UpperBlit

/* This is the public blit function, SDL_BlitSurface(), and it performs
rectangle validation and clipping before passing it to
SDL_LowerBlit()
*/
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

Surely this last function prototype should really be:

extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, const SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

… since ‘*srcrect’ should not be getting altered?


Ben Sizer

Source rect can get altered. It should be in the docs. If the blit is not
successful due to clipping, it modifies the source and destination rects, and
returns an error code.

In your code, you can copy the rect to a temporary before blitting.

-RayOn Tuesday 26 June 2001 21:57, you wrote:

I think I came across a small problem in the library, but maybe someone
can advise me.

The documentation for ‘SDL_BlitSurface’ says:
“The final blit rectangle is saved in dstrect after all clipping is
performed (srcrect is not modified).” With the emphasis here being on
the ‘srcrect is not modified’ section.

However, when I try and call this from a const member function (in C++),
where the source rectangles are part of that class and therefore
constant, I get this error under Visual C++ 6:

error C2664: ‘SDL_UpperBlit’ : cannot convert parameter 2 from ‘const
SDL_Rect *’ to ‘SDL_Rect *’

Looking through the headers, I see this:

#define SDL_BlitSurface SDL_UpperBlit

/* This is the public blit function, SDL_BlitSurface(), and it performs
rectangle validation and clipping before passing it to
SDL_LowerBlit()
*/
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

Surely this last function prototype should really be:

extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, const SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

… since ‘*srcrect’ should not be getting altered?


Ben Sizer

From the docs for SDL_Blit:

The final blit rectangle is saved in dstrect after all clipping is
performed (srcrect is not modified).

And what about the source surface? Shouldn’t that be const?

Ray Kelm wrote:>

On Tuesday 26 June 2001 21:57, you wrote:

/* This is the public blit function, SDL_BlitSurface(), and it performs
rectangle validation and clipping before passing it to
SDL_LowerBlit()
*/
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

Surely this last function prototype should really be:

extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, const SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);

… since ‘*srcrect’ should not be getting altered?


Ben Sizer

Source rect can get altered. It should be in the docs. If the blit is not
successful due to clipping, it modifies the source and destination rects, and
returns an error code.

In your code, you can copy the rect to a temporary before blitting.

-Ray

Source rect can get altered. It should be in the docs. If the blit is not
successful due to clipping, it modifies the source and destination rects, and
returns an error code.

I don’t think so (I wrote that code)

However, when I try and call this from a const member function (in C++),
where the source rectangles are part of that class and therefore
constant, I get this error under Visual C++ 6:

then cast it. we’ve discussed it before, and there’s little point in
changing it now (see the archives)

However, when I try and call this from a const member function (in C++),
where the source rectangles are part of that class and therefore
constant, I get this error under Visual C++ 6:

When the code was originally written, the source rectangle was modified.
It has since been rewritten so that it isn’t (thanks Mattias), but the
prototype wasn’t modified. It should be safe to cast.

SDL 1.3 will be much more const correct.

See ya,
-Sam Lantinga, Lead Programmer, Loki Software, Inc.