Exposing clipping function

Sam,

Would you have any problems with exposing SDL’s clipping
function, so that I can clip rects without having to do
a blit? Is there some stock solution to this (such as
setting the alpha-channel to 0… then doing a blit…)

Comments?

Thanks,

-Loren

I was thinking something akin to the following:

— src/SDL-1.1.2/src/video/SDL_surface.c Thu Mar 30 13:47:55 2000
+++ src/SDL-1.1.2/src/video/SDL_surface.c_new Thu Apr 27 08:16:22 2000
@@ -257,7 +257,7 @@
}
return(do_blit(src, srcrect, dst, dstrect));
}
-int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
+int SDL_ClipBlitRects (SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect)
{
SDL_Rect fullsrc, fulldst;
@@ -460,8 +460,41 @@
src->pixels, srcrect->x, srcrect->y,
dst->pixels, dstrect->x, dstrect->y);
#endif

  • /* Do the blit */
  • return(SDL_LowerBlit(src, srcrect, dst, dstrect));
  • return(0);
    +}+
    +int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
  •   			SDL_Surface *dst, SDL_Rect *dstrect)
    

+{

  • SDL_Rect fullsrc, fulldst;
  • int clip_ret_val;
  • /* Set default rectangles */
  • if ( dstrect == NULL ) {
  •   fulldst.x = 0;
    
  •   fulldst.y = 0;
    
  •   fulldst.w = dst->w;
    
  •   fulldst.h = dst->h;
    
  •   dstrect = &fulldst;
    
  • }
  • if ( srcrect == NULL ) {
  •   fullsrc.x = 0;
    
  •   fullsrc.y = 0;
    
  •   fullsrc.w = src->w;
    
  •   fullsrc.h = src->h;
    
  •   srcrect = &fullsrc;
    
  • }
  • clip_ret_val = SDL_ClipBlitRects (src, srcrect, dst, dstrect);
  • if ( (clip_ret_val == 0) &&
  •   	 ( (srcrect->w == 0) || (srcrect->h == 0) ||
    
  •   	   (dstrect->w == 0) || (dstrect->h = 0) )
    
  • ) {
  •   /* Do the blit */
    
  •   return(SDL_LowerBlit(src, srcrect, dst, dstrect));
    
  • } else {
  •   return(clip_ret_val);
    
  • }
    }

/*
— src/SDL-1.1.2/include/SDL_video.h Thu Mar 16 07:20:37 2000
+++ src/SDL-1.1.2/include/SDL_video.h_new Thu Apr 27 08:20:19 2000
@@ -513,6 +513,13 @@
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect dstrect);
+/
This is the public function, SDL_ClipBlitRects(), that does the same

  • function that does the same rectangle validation and clipping as
  • SDL_BlitSurface() without doing the actual blitting.
    +*/
    +extern DECLSPEC int SDL_ClipBlitRects
  •   	(SDL_Surface *src, SDL_Rect *srcrect,
    
  •   	 SDL_Surface *dst, SDL_Rect *dstrect);
    

/* This is a semi-private blit function and it performs low-level surface
blitting only.
*/


Great news! Get free KNXmail here!
http://www.knx1070.com

Sam,

Would you have any problems with exposing SDL’s clipping
function, so that I can clip rects without having to do
a blit? Is there some stock solution to this (such as
setting the alpha-channel to 0… then doing a blit…)

Comments?

Yeah – is SDL’s clipping routine really optimized enough to make doing it
worthwhile? IMO you’d be probably better off writing your own localized
clipper. The one advantage I see is that you could use SDL’s predefined
rectangle struct and that’s no big deal – you could do that anyways. I’d
just prefer to see people writing their own more specialized routines for
when they need to.

Nicholas

----- Original Message -----
From: sondheim at knxmail.com (sondheim@knxmail.com)
To: sdl at lokigames.com
Date: Thursday, April 27, 2000 9:34 AM
Subject: [SDL] Exposing clipping function

Yeah – is SDL’s clipping routine really optimized enough to make doing it
worthwhile?

If it isn’t, then it should be fixed, right? There is no point in having
code in SDL not fit for use. Perhaps the user could set a clipping rectangle
that defaults to the whole screen. Rewriting the RLE blitting/clipping
routines are on my TODO list, but not at the top at the moment :slight_smile:

IMO you’d be probably better off writing your own localized
clipper.

It is true that domain-specific clipping will be required for the cases when
your region is non-rectangular (say, a cockpit window). But rectangular
clipping is still pretty useful.

Would you have any problems with exposing SDL’s clipping
function, so that I can clip rects without having to do
a blit? Is there some stock solution to this (such as
setting the alpha-channel to 0… then doing a blit…)

Yeah – is SDL’s clipping routine really optimized enough to make doing it
worthwhile? IMO you’d be probably better off writing your own localized
clipper. The one advantage I see is that you could use SDL’s predefined
rectangle struct and that’s no big deal – you could do that anyways. I’d
just prefer to see people writing their own more specialized routines for
when they need to.

Nicholas

My concern here isn’t so much in efficiency as it is with
obtaining the same result. With most processes there is
more than one way to compute their result, and (in fact)
the result may vary slightly depending on the implemation,
and the goals that the implementor had in mind. With a
clipping this is likely not to be an issue, but I wanted
to use SDL’s clipping function to ensure that I get the
same result as SDL… not for purposes of being more
efficient.

Comment?

Thanks again,

-Loren


Great news! Get free KNXmail here!
http://www.knx1070.com

My concern here isn’t so much in efficiency as it is with
obtaining the same result. With most processes there is
more than one way to compute their result, and (in fact)
the result may vary slightly depending on the implemation,
and the goals that the implementor had in mind. With a
clipping this is likely not to be an issue, but I wanted
to use SDL’s clipping function to ensure that I get the
same result as SDL… not for purposes of being more
efficient.

Key word here, “goals that the implementor had in mind” – in some cases my
goals may differ quite a lot. I would recommend that in the event you want
your clipping routine to give the same results as SDL, you do that, and not
pollute SDL’s interface and add a routine which may well cause a good deal
of confusion to some more newbie programmers in the future than you and
me – however, these are just my thoughts (opinionated deviant that I am)

-Loren

Cheers,

Nicholas

ps. why aren’t you using your Loki account?

----- Original Message -----
From: sondheim at knxmail.com (sondheim@knxmail.com)
To: sdl at lokigames.com
Date: Thursday, April 27, 2000 6:05 PM
Subject: Re: [SDL] Exposing clipping function

I accidentally sent this just to Nick by mistake:

My concern here isn’t so much in efficiency as it is with
obtaining the same result. With most processes there is
more than one way to compute their result, and (in fact)
the result may vary slightly depending on the implemation,
and the goals that the implementor had in mind. With a
clipping this is likely not to be an issue, but I wanted
to use SDL’s clipping function to ensure that I get the
same result as SDL… not for purposes of being more
efficient.

Key word here, “goals that the implementor had in mind” – in some cases my
goals may differ quite a lot. I would recommend that in the event you want
your clipping routine to give the same results as SDL, you do that, and not
pollute SDL’s interface and add a routine which may well cause a good deal
of confusion to some more newbie programmers in the future than you and
me – however, these are just my thoughts (opinionated deviant that I am)

As far as dealing with the status quo, agreeing with you vs.
not is sort of a wash. It would be trivial for me to just
lift Sam’s code into my own application. This would have
the desired effect. Everything that I know about
programmming, however, tells me that this is a bad idea.
What if Sam’s code changes. I have no way of changing the
algorythm after it leaves my hands. The best way to do
this is a minor API change. I am recommending only adding
one new entry point. No change in behavior for existing
programs. I did change the internals of SDL_UpperBlit,
but only to call the new function for its clipping… This
should work %100 the same way. How would this confuse
people?

Anyone? Comments?

Regards,

-Loren


Great news! Get free KNXmail here!
http://www.knx1070.com

Sam,

Would you have any problems with exposing SDL’s clipping
function, so that I can clip rects without having to do
a blit? Is there some stock solution to this (such as
setting the alpha-channel to 0… then doing a blit…)

Comments?

Thanks,

-Loren

Doh!!!

Well I feel like an idiot now. I just realized that that
the patch I submitted last week not only didn’t work, It
broke all SDL blitting! :frowning:

Anyway, here is a patch that I tested and it works
properly:

Sorry,

-Loren

=====BEGIN=====
diff -ruN src/SDL-1.1.2/include/SDL_video.h src/SDL-1.1.2-new/include/SDL_video.h
— src/SDL-1.1.2/include/SDL_video.h Thu Mar 16 07:20:37 2000
+++ src/SDL-1.1.2-new/include/SDL_video.h Wed May 3 17:02:32 2000
@@ -513,6 +513,13 @@
extern DECLSPEC int SDL_UpperBlit
(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect dstrect);
+/
This is the public function, SDL_ClipBlitRects(), that does the same

  • function that does the same rectangle validation and clipping as
  • SDL_BlitSurface() without doing the actual blitting.
    +*/
    +extern DECLSPEC int SDL_ClipBlitRects
  •   	(SDL_Surface *src, SDL_Rect *srcrect,
    
  •   	 SDL_Surface *dst, SDL_Rect *dstrect);
    

/* This is a semi-private blit function and it performs low-level surface
blitting only.
*/
diff -ruN src/SDL-1.1.2/src/video/SDL_surface.c src/SDL-1.1.2-new/src/video/SDL_surface.c
— src/SDL-1.1.2/src/video/SDL_surface.c Thu Mar 30 13:47:55 2000
+++ src/SDL-1.1.2-new/src/video/SDL_surface.c Wed May 3 18:53:18 2000
@@ -257,7 +257,7 @@
}
return(do_blit(src, srcrect, dst, dstrect));
}
-int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
+int SDL_ClipBlitRects (SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect)
{
SDL_Rect fullsrc, fulldst;
@@ -460,8 +460,41 @@
src->pixels, srcrect->x, srcrect->y,
dst->pixels, dstrect->x, dstrect->y);
#endif

  • /* Do the blit */
  • return(SDL_LowerBlit(src, srcrect, dst, dstrect));
  • return(0);
    +}+
    +int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
  •   			SDL_Surface *dst, SDL_Rect *dstrect)
    

+{

  • SDL_Rect fullsrc, fulldst;
  • int clip_ret_val;
  • /* Set default rectangles */
  • if ( dstrect == NULL ) {
  •   fulldst.x = 0;
    
  •   fulldst.y = 0;
    
  •   fulldst.w = dst->w;
    
  •   fulldst.h = dst->h;
    
  •   dstrect = &fulldst;
    
  • }
  • if ( srcrect == NULL ) {
  •   fullsrc.x = 0;
    
  •   fullsrc.y = 0;
    
  •   fullsrc.w = src->w;
    
  •   fullsrc.h = src->h;
    
  •   srcrect = &fullsrc;
    
  • }
  • clip_ret_val = SDL_ClipBlitRects (src, srcrect, dst, dstrect);
  • if ( (clip_ret_val == 0) &&
  •   	 ( (srcrect->w != 0) && (srcrect->h != 0) ||
    
  •   	   (dstrect->w != 0) && (dstrect->h != 0) )
    
  • ) {
  •   /* Do the blit */
    
  •   return(SDL_LowerBlit(src, srcrect, dst, dstrect));
    
  • } else {
  •   return(clip_ret_val);
    
  • }
    }

/*
======END======


Great news! Get free KNXmail here!
http://www.knx1070.com