Skip to content

Commit 3fca2e8

Browse files
committed
bflibrary: Switch blitting in SDL2 to use proper API
In SDL1, some custom blitting implemenrtation was required. This is no longer needed when using SDL2.
1 parent 06f16ff commit 3fca2e8

3 files changed

Lines changed: 32 additions & 150 deletions

File tree

bflibrary/src/x86-win-sdl/sscreen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ TbResult LbScreenUnlock(void)
580580
static TbResult LbIPhysicalScreenLock(void)
581581
{
582582
if (lbHasSecondSurface && SDL_MUSTLOCK(to_SDLSurf(lbScreenSurface))) {
583-
if (SDL_LockSurface (to_SDLSurf(lbScreenSurface)) != 0) {
583+
if (SDL_LockSurface(to_SDLSurf(lbScreenSurface)) != 0) {
584584
LOGERR("cannot lock screen surface: %s", SDL_GetError());
585585
return Lb_FAIL;
586586
}

bflibrary/src/x86-win-sdl2/sscreen.c

Lines changed: 15 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ TbResult LbScreenUnlock(void)
593593
static TbResult LbIPhysicalScreenLock(void)
594594
{
595595
if (lbHasSecondSurface && SDL_MUSTLOCK(to_SDLSurf(lbScreenSurface))) {
596-
if (SDL_LockSurface (to_SDLSurf(lbScreenSurface)) != 0) {
596+
if (SDL_LockSurface(to_SDLSurf(lbScreenSurface)) < 0) {
597597
LOGERR("cannot lock screen surface: %s", SDL_GetError());
598598
return Lb_FAIL;
599599
}
@@ -625,7 +625,8 @@ TbResult LbScreenSetDoubleBuffering(TbBool state)
625625

626626
TbBool LbScreenIsDoubleBufferred(void)
627627
{
628-
return ((to_SDLSurf(lbScreenSurface)->flags & SDL_DOUBLEBUF) != 0);
628+
// SDL2 always does double buffering
629+
return true;
629630
}
630631

631632
ulong LbScreenSetWScreenInVideo(ulong flag)
@@ -685,136 +686,6 @@ TbBool LbHwCheckIsModeAvailable(TbScreenMode mode)
685686
return (closestBPP != 0);
686687
}
687688

688-
static void LbI_SDL_BlitScaled_to8bpp(long src_w, long src_h, ubyte *src_buf,
689-
long dst_w, long dst_h, ubyte *dst_buf)
690-
{
691-
/* denominator of a (source) pixel's fraction part */
692-
const long denom_i = 2 * dst_h;
693-
const long denom_j = 2 * dst_w;
694-
695-
/* number of whole units in each (source) step */
696-
const long dsrc_i = 2 * src_h / denom_i;
697-
const long dsrc_j = 2 * src_w / denom_j;
698-
699-
/* numerator of fractional part of each (source) step */
700-
const long dsrc_num_i = (2 * src_h) - dsrc_i * denom_i;
701-
const long dsrc_num_j = (2 * src_w) - dsrc_j * denom_j;
702-
703-
/* number of whole units in a (source) half-step */
704-
const long halfdsrc_i = src_h / denom_i;
705-
const long halfdsrc_j = src_w / denom_j;
706-
707-
long dst_offset = 0;
708-
long src_offset = halfdsrc_i * src_w + halfdsrc_j;
709-
710-
/* start at fractional part of each (source) half-step */
711-
long src_num_i = src_h - halfdsrc_i * denom_i;
712-
long src_num_j = src_w - halfdsrc_j * denom_j;
713-
714-
for (long i = 0; i != dst_h; ++i) {
715-
if (src_num_i > denom_i) {
716-
src_num_i -= denom_i;
717-
src_offset += src_w;
718-
}
719-
for (long j = 0; j != dst_w; ++j) {
720-
if (src_num_j > denom_j) {
721-
src_num_j -= denom_j;
722-
++src_offset;
723-
}
724-
dst_buf[dst_offset] = src_buf[src_offset];
725-
dst_offset++;
726-
src_offset += dsrc_j;
727-
src_num_j += dsrc_num_j;
728-
}
729-
src_offset += (dsrc_i - 1) * src_w;
730-
src_num_i += dsrc_num_i;
731-
}
732-
}
733-
734-
static void LbI_SDL_BlitScaled_totcbpp(long src_w, long src_h, ubyte *src_buf,
735-
SDL_Color *pal, long rshift, long gshift, long bshift,
736-
long dst_w, long dst_h, long dst_bpp, ubyte *dst_buf)
737-
{
738-
/* denominator of a (source) pixel's fraction part */
739-
const long denom_i = 2 * dst_h;
740-
const long denom_j = 2 * dst_w;
741-
742-
/* number of whole units in each (source) step */
743-
const long dsrc_i = 2 * src_h / denom_i;
744-
const long dsrc_j = 2 * src_w / denom_j;
745-
746-
/* numerator of fractional part of each (source) step */
747-
const long dsrc_num_i = (2 * src_h) - dsrc_i * denom_i;
748-
const long dsrc_num_j = (2 * src_w) - dsrc_j * denom_j;
749-
750-
/* number of whole units in a (source) half-step */
751-
const long halfdsrc_i = src_h / denom_i;
752-
const long halfdsrc_j = src_w / denom_j;
753-
754-
long dst_offset = 0;
755-
long src_offset = halfdsrc_i * src_w + halfdsrc_j;
756-
757-
/* start at fractional part of each (source) half-step */
758-
long src_num_i = src_h - halfdsrc_i * denom_i;
759-
long src_num_j = src_w - halfdsrc_j * denom_j;
760-
761-
for (long i = 0; i != dst_h; ++i) {
762-
if (src_num_i > denom_i) {
763-
src_num_i -= denom_i;
764-
src_offset += src_w;
765-
}
766-
for (long j = 0; j != dst_w; ++j) {
767-
SDL_Color c;
768-
if (src_num_j > denom_j) {
769-
src_num_j -= denom_j;
770-
++src_offset;
771-
}
772-
c = pal[src_buf[src_offset]];
773-
*((long *)(dst_buf+dst_offset)) = (c.r << rshift) + (c.g << gshift) + (c.b << bshift);
774-
dst_offset += dst_bpp;
775-
src_offset += dsrc_j;
776-
src_num_j += dsrc_num_j;
777-
}
778-
src_offset += (dsrc_i - 1) * src_w;
779-
src_num_i += dsrc_num_i;
780-
}
781-
}
782-
783-
/** @internal
784-
* Provides simplified SDL_BlitScaled() functionality for SDL1.
785-
*/
786-
int LbI_SDL_BlitScaled(SDL_Surface *src, SDL_Surface *dst)
787-
{
788-
long dst_bpp;
789-
790-
/* shortcircuit for 1:1 */
791-
if (src->w == dst->w && src->h == dst->h)
792-
return SDL_BlitSurface(src, NULL, dst, NULL);
793-
794-
if (src->format->BytesPerPixel != 1)
795-
LOGERR("unsupported source bit length");
796-
797-
if (SDL_MUSTLOCK(src) && SDL_LockSurface(src) < 0)
798-
LOGERR("cannot lock source surface: %s", SDL_GetError());
799-
800-
if (SDL_MUSTLOCK(dst) && SDL_LockSurface(dst) < 0)
801-
LOGERR("cannot lock destination Surface: %s", SDL_GetError());
802-
803-
dst_bpp = dst->format->BytesPerPixel;
804-
if (dst_bpp == 1)
805-
LbI_SDL_BlitScaled_to8bpp(src->w, src->h, src->pixels,
806-
dst->w, dst->h, dst->pixels);
807-
else
808-
LbI_SDL_BlitScaled_totcbpp(src->w, src->h, src->pixels,
809-
src->format->palette->colors, dst->format->Rshift, dst->format->Gshift, dst->format->Bshift,
810-
dst->w, dst->h, dst_bpp, dst->pixels);
811-
812-
if (SDL_MUSTLOCK(dst)) SDL_UnlockSurface(dst);
813-
if (SDL_MUSTLOCK(src)) SDL_UnlockSurface(src);
814-
815-
return 0;
816-
}
817-
818689
/** Check if the lbDrawSurface is linked to WScreen buffer; fix if neccessary.
819690
*
820691
* This call is required to handle WScreen pointer changes by the app side.
@@ -872,17 +743,17 @@ TbResult LbScreenSwap(void)
872743
// Put the data from Draw Surface onto Screen Surface
873744
if ((ret == Lb_SUCCESS) && (lbHasSecondSurface))
874745
{
875-
blresult = LbI_SDL_BlitScaled(to_SDLSurf(lbDrawSurface),
876-
to_SDLSurf(lbScreenSurface));
746+
blresult = SDL_BlitScaled(to_SDLSurf(lbDrawSurface), NULL,
747+
to_SDLSurf(lbScreenSurface), NULL);
877748
if (blresult < 0) {
878749
LOGERR("blit failed: %s", SDL_GetError());
879750
ret = Lb_FAIL;
880751
}
881752
}
882753
// Flip the image displayed on Screen Surface
883754
if (ret == Lb_SUCCESS) {
884-
// calls SDL_UpdateRect for entire screen if not double buffered
885-
blresult = SDL_Flip(to_SDLSurf(lbScreenSurface));
755+
// Copy the window surface to the screen
756+
blresult = SDL_UpdateWindowSurface(lbWindow);
886757
if (blresult < 0) {
887758
// In some cases this situation seems to be quite common
888759
LOGERR("flip failed: %s", SDL_GetError());
@@ -908,17 +779,17 @@ TbResult LbScreenSwapClear(TbPixel colour)
908779
// Put the data from Draw Surface onto Screen Surface
909780
if ((ret == Lb_SUCCESS) && (lbHasSecondSurface))
910781
{
911-
blresult = LbI_SDL_BlitScaled(to_SDLSurf(lbDrawSurface),
912-
to_SDLSurf(lbScreenSurface));
782+
blresult = SDL_BlitScaled(to_SDLSurf(lbDrawSurface), NULL,
783+
to_SDLSurf(lbScreenSurface), NULL);
913784
if (blresult < 0) {
914785
LOGERR("blit failed: %s", SDL_GetError());
915786
ret = Lb_FAIL;
916787
}
917788
}
918789
// Flip the image displayed on Screen Surface
919790
if (ret == Lb_SUCCESS) {
920-
// calls SDL_UpdateRect for entire screen if not double buffered
921-
blresult = SDL_Flip(to_SDLSurf(lbScreenSurface));
791+
// Copy the window surface to the screen
792+
blresult = SDL_UpdateWindowSurface(lbWindow);
922793
if (blresult < 0) {
923794
// In some cases this situation seems to be quite common
924795
LOGERR("flip failed: %s", SDL_GetError());
@@ -962,8 +833,8 @@ TbResult LbScreenSwapBox(ubyte *sourceBuf, long sourceX, long sourceY,
962833
{
963834
SDL_Rect clipRect = {destX, destY, width, height};
964835
SDL_SetClipRect(to_SDLSurf(lbScreenSurface), &clipRect);
965-
blresult = LbI_SDL_BlitScaled(to_SDLSurf(lbDrawSurface),
966-
to_SDLSurf(lbScreenSurface));
836+
blresult = SDL_BlitScaled(to_SDLSurf(lbDrawSurface), NULL,
837+
to_SDLSurf(lbScreenSurface), NULL);
967838
SDL_SetClipRect(to_SDLSurf(lbScreenSurface), NULL);
968839
if (blresult < 0) {
969840
LOGERR("blit failed: %s", SDL_GetError());
@@ -972,8 +843,8 @@ TbResult LbScreenSwapBox(ubyte *sourceBuf, long sourceX, long sourceY,
972843
}
973844
// Flip the image displayed on Screen Surface
974845
if (ret == Lb_SUCCESS) {
975-
// calls SDL_UpdateRect for entire screen if not double buffered
976-
blresult = SDL_Flip(to_SDLSurf(lbScreenSurface));
846+
// Copy the window surface to the screen
847+
blresult = SDL_UpdateWindowSurface(lbWindow);
977848
if (blresult < 0) {
978849
// In some cases this situation seems to be quite common
979850
LOGERR("flip failed: %s", SDL_GetError());

bflibrary/src/x86-win-sdl2/sscrsurf.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ TbResult LbScreenSurfaceCreate(struct SSurface *surf, ulong w, ulong h)
5151
}
5252
format = to_SDLSurf(lbDrawSurface)->format;
5353

54-
surf->surf_data = (OSSurfaceHandle)SDL_CreateRGBSurface(SDL_SRCCOLORKEY,
54+
surf->surf_data = (OSSurfaceHandle)SDL_CreateRGBSurface(0,
5555
w, h, format->BitsPerPixel,
5656
format->Rmask, format->Gmask, format->Bmask, format->Amask);
5757

@@ -85,6 +85,7 @@ TbResult LbScreenSurfaceBlit(struct SSurface *surf, ulong x, ulong y,
8585
// Convert TbRect to SDL rectangles
8686
SDL_Rect srcRect;
8787
SDL_Rect destRect;
88+
Uint32 clkey;
8889

8990
LbIScreenDrawSurfaceCheck();
9091

@@ -112,13 +113,23 @@ TbResult LbScreenSurfaceBlit(struct SSurface *surf, ulong x, ulong y,
112113
//to access front buffer in SDL
113114
}
114115

115-
if ((blflags & SSBlt_FLAG4) != 0) {
116+
if ((blflags & SSBlt_FLAG4) != 0)
117+
{
118+
if (to_SDLSurf(surf->surf_data)->format->BitsPerPixel == 8) {
119+
// here we know we want to use a specific color index as key, no need for mapping
120+
clkey = 255;
121+
} else {
122+
clkey = SDL_MapRGB(to_SDLSurf(surf->surf_data)->format, 0x0, 0xff, 0xff);
123+
}
116124
// enable color key
117-
SDL_SetColorKey(to_SDLSurf(surf->surf_data), SDL_SRCCOLORKEY, 255);
125+
SDL_SetColorKey(to_SDLSurf(surf->surf_data), SDL_TRUE, clkey);
126+
if (SDL_HasColorKey(to_SDLSurf(surf->surf_data)) == -1)
127+
LOGWARN("DrawSurface refused to enable color key; no transparency.");
118128
}
119-
else {
129+
else
130+
{
120131
// disable color key
121-
SDL_SetColorKey(to_SDLSurf(surf->surf_data), 0, 255);
132+
SDL_SetColorKey(to_SDLSurf(surf->surf_data), SDL_FALSE, 0);
122133
}
123134

124135
if ((blflags & SSBlt_FLAG10) != 0) {

0 commit comments

Comments
 (0)