comparison src/video/SDL_surface.c @ 292:eadc0746dfaf

Added SDL_LockRect() and SDL_UnlockRect() Incorporated XFree86 extension libraries into the source
author Sam Lantinga <slouken@libsdl.org>
date Tue, 05 Mar 2002 19:55:32 +0000
parents c9cd3b564e4b
children fab1ddc4d7bf
comparison
equal deleted inserted replaced
291:68a8a8237c09 292:eadc0746dfaf
628 * if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) 628 * if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE )
629 * video->LockHWSurface(video, surface); 629 * video->LockHWSurface(video, surface);
630 */ 630 */
631 int SDL_LockSurface (SDL_Surface *surface) 631 int SDL_LockSurface (SDL_Surface *surface)
632 { 632 {
633 if ( surface->locked < 0 ) {
634 SDL_SetError("Surface has a rectangle lock");
635 return(-1);
636 }
633 if ( ! surface->locked ) { 637 if ( ! surface->locked ) {
634 /* Perform the lock */ 638 /* Perform the lock */
635 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) { 639 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
636 SDL_VideoDevice *video = current_video; 640 SDL_VideoDevice *video = current_video;
637 SDL_VideoDevice *this = current_video; 641 SDL_VideoDevice *this = current_video;
651 ++surface->locked; 655 ++surface->locked;
652 656
653 /* Ready to go.. */ 657 /* Ready to go.. */
654 return(0); 658 return(0);
655 } 659 }
660 int SDL_LockRect (SDL_Surface *surface, SDL_Rect *rect, void **pixels, int *pitch)
661 {
662 int retval = 0;
663
664 /* Check to see if the surface is already locked */
665 *pixels = NULL;
666 if ( surface->locked != 0 ) {
667 SDL_SetError("Surface is already locked");
668 return(-1);
669 }
670
671 /* Clip the lock to the clipping rectangle of the surface */
672 {
673 SDL_Rect *clip = &surface->clip_rect;
674 int dx, dy;
675 int h = rect->h;
676 int w = rect->w;
677
678 dx = clip->x - rect->x;
679 if(dx > 0) {
680 w -= dx;
681 rect->x += dx;
682 }
683 dx = rect->x + w - clip->x - clip->w;
684 if(dx > 0)
685 w -= dx;
686
687 dy = clip->y - rect->y;
688 if(dy > 0) {
689 h -= dy;
690 rect->y += dy;
691 }
692 dy = rect->y + h - clip->y - clip->h;
693 if(dy > 0)
694 h -= dy;
695
696 if(w > 0 && h > 0) {
697 rect->w = w;
698 rect->h = h;
699 } else {
700 rect->w = 0;
701 rect->h = 0;
702 SDL_SetError("Rectangle was clipped");
703 return(-1);
704 }
705 }
706
707 /* Perform the lock */
708 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
709 SDL_VideoDevice *video = current_video;
710 SDL_VideoDevice *this = current_video;
711 if ( video->LockHWSurfaceRect ) {
712 retval = video->LockHWSurfaceRect(this, surface, rect, pixels, pitch);
713 if ( retval == 0 ) {
714 surface->locked = -1;
715 return 0;
716 }
717 }
718 }
719 if ( SDL_MUSTLOCK(surface) ) {
720 retval = SDL_LockSurface(surface);
721 if ( retval < 0 ) {
722 return retval;
723 }
724 }
725 surface->locked = -1;
726 *pixels = (Uint8 *)surface->pixels + rect->y * surface->pitch + rect->x * surface->format->BytesPerPixel;
727 *pitch = surface->pitch;
728
729 /* Ready to go.. */
730 return(0);
731 }
656 /* 732 /*
657 * Unlock a previously locked surface 733 * Unlock a previously locked surface
658 * -- Do not call this from any blit function, as SDL_DrawCursor() may recurse 734 * -- Do not call this from any blit function, as SDL_DrawCursor() may recurse
659 * Instead, use: 735 * Instead, use:
660 * if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) 736 * if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE )
661 * video->UnlockHWSurface(video, surface); 737 * video->UnlockHWSurface(video, surface);
662 */ 738 */
663 void SDL_UnlockSurface (SDL_Surface *surface) 739 void SDL_UnlockSurface (SDL_Surface *surface)
664 { 740 {
665 /* Only perform an unlock if we are locked */ 741 /* Only perform an unlock if we are locked */
742 if ( surface->locked < 0 ) {
743 return;
744 }
666 if ( ! surface->locked || (--surface->locked > 0) ) { 745 if ( ! surface->locked || (--surface->locked > 0) ) {
667 return; 746 return;
668 } 747 }
669 748
670 /* Perform the unlock */ 749 /* Perform the unlock */
679 /* Update RLE encoded surface with new data */ 758 /* Update RLE encoded surface with new data */
680 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) { 759 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
681 surface->flags &= ~SDL_RLEACCEL; /* stop lying */ 760 surface->flags &= ~SDL_RLEACCEL; /* stop lying */
682 SDL_RLESurface(surface); 761 SDL_RLESurface(surface);
683 } 762 }
763 }
764 }
765 void SDL_UnlockRect (SDL_Surface *surface)
766 {
767 /* Only perform an unlock if we are locked */
768 if ( surface->locked != -1 ) {
769 return;
770 }
771
772 /* Perform the unlock */
773 if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
774 SDL_VideoDevice *video = current_video;
775 SDL_VideoDevice *this = current_video;
776 if ( video->LockHWSurfaceRect ) {
777 video->UnlockHWSurfaceRect(this, surface);
778 return;
779 }
780 }
781 if ( SDL_MUSTLOCK(surface) ) {
782 surface->locked = 1;
783 SDL_UnlockSurface(surface);
784 } else {
785 surface->locked = 0;
684 } 786 }
685 } 787 }
686 788
687 /* 789 /*
688 * Convert a surface into the specified pixel format. 790 * Convert a surface into the specified pixel format.