hedgewars/uLandGraphics.pas
changeset 11532 bf86c6cb9341
parent 11507 bd9a2f1b0080
child 11588 7c8fd2f66e9b
equal deleted inserted replaced
11531:d1cf1ff6cabb 11532:bf86c6cb9341
   703 TryPlaceOnLand:= false;
   703 TryPlaceOnLand:= false;
   704 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   704 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   705 
   705 
   706 if outOfMap then doPlace:= false; // just using for a check
   706 if outOfMap then doPlace:= false; // just using for a check
   707 
   707 
   708 TryDo(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true);
   708 if checkFails(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true) then exit;
   709 
   709 
   710 Image:= SpritesData[Obj].Surface;
   710 Image:= SpritesData[Obj].Surface;
   711 w:= SpritesData[Obj].Width;
   711 w:= SpritesData[Obj].Width;
   712 h:= SpritesData[Obj].Height;
   712 h:= SpritesData[Obj].Height;
   713 if flipVert then flipSurface(Image, true);
   713 if flipVert then flipSurface(Image, true);
   717 
   717 
   718 if SDL_MustLock(Image) then
   718 if SDL_MustLock(Image) then
   719     if SDLCheck(SDL_LockSurface(Image) >= 0, 'TryPlaceOnLand', true) then exit;
   719     if SDLCheck(SDL_LockSurface(Image) >= 0, 'TryPlaceOnLand', true) then exit;
   720 
   720 
   721 bpp:= Image^.format^.BytesPerPixel;
   721 bpp:= Image^.format^.BytesPerPixel;
   722 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
   722 if checkFails(bpp = 4, 'It should be 32 bpp sprite', true) then
       
   723 begin
       
   724     if SDL_MustLock(Image) then
       
   725         SDL_UnlockSurface(Image);
       
   726     exit
       
   727 end;
   723 // Check that sprite fits free space
   728 // Check that sprite fits free space
   724 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   729 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   725 case bpp of
   730 case bpp of
   726     4: for y:= 0 to Pred(h) do
   731     4: for y:= 0 to Pred(h) do
   727         begin
   732         begin
   822     p: PByteArray;
   827     p: PByteArray;
   823     Image: PSDL_Surface;
   828     Image: PSDL_Surface;
   824 begin
   829 begin
   825 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   830 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   826 
   831 
   827 TryDo(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true);
   832 if checkFails(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true) then exit;
   828 
   833 
   829 Image:= SpritesData[Obj].Surface;
   834 Image:= SpritesData[Obj].Surface;
   830 w:= SpritesData[Obj].Width;
   835 w:= SpritesData[Obj].Width;
   831 h:= SpritesData[Obj].Height;
   836 h:= SpritesData[Obj].Height;
   832 if flipVert then flipSurface(Image, true);
   837 if flipVert then flipSurface(Image, true);
   836 
   841 
   837 if SDL_MustLock(Image) then
   842 if SDL_MustLock(Image) then
   838     if SDLCheck(SDL_LockSurface(Image) >= 0, 'EraseLand', true) then exit;
   843     if SDLCheck(SDL_LockSurface(Image) >= 0, 'EraseLand', true) then exit;
   839 
   844 
   840 bpp:= Image^.format^.BytesPerPixel;
   845 bpp:= Image^.format^.BytesPerPixel;
   841 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
   846 if checkFails(bpp = 4, 'It should be 32 bpp sprite', true) then
       
   847 begin
       
   848     if SDL_MustLock(Image) then
       
   849         SDL_UnlockSurface(Image);
       
   850     exit
       
   851 end;
   842 // Check that sprite fits free space
   852 // Check that sprite fits free space
   843 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   853 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   844 case bpp of
   854 
   845     4: for y:= 0 to Pred(h) do
   855     for y:= 0 to Pred(h) do
   846         begin
   856         begin
   847         for x:= 0 to Pred(w) do
   857         for x:= 0 to Pred(w) do
   848             if ((PLongword(@(p^[x * 4]))^) and AMask) <> 0 then
   858             if ((PLongword(@(p^[x * 4]))^) and AMask) <> 0 then
   849                 if ((cpY + y) <= Longint(topY)) or ((cpY + y) >= LAND_HEIGHT) or
   859                 if ((cpY + y) <= Longint(topY)) or ((cpY + y) >= LAND_HEIGHT) or
   850                    ((cpX + x) <= Longint(leftX)) or ((cpX + x) >= Longint(rightX)) then
   860                    ((cpX + x) <= Longint(leftX)) or ((cpX + x) >= Longint(rightX)) then
   852                    if SDL_MustLock(Image) then
   862                    if SDL_MustLock(Image) then
   853                        SDL_UnlockSurface(Image);
   863                        SDL_UnlockSurface(Image);
   854                    exit
   864                    exit
   855                    end;
   865                    end;
   856         p:= PByteArray(@(p^[Image^.pitch]))
   866         p:= PByteArray(@(p^[Image^.pitch]))
   857         end
   867         end;
   858     end;
       
   859 
   868 
   860 // Checked, now place
   869 // Checked, now place
   861 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   870 p:= PByteArray(@(PByteArray(Image^.pixels)^[ Image^.pitch * row * h + col * w * 4 ]));
   862 case bpp of
   871     for y:= 0 to Pred(h) do
   863     4: for y:= 0 to Pred(h) do
       
   864         begin
   872         begin
   865         for x:= 0 to Pred(w) do
   873         for x:= 0 to Pred(w) do
   866             if ((PLongword(@(p^[x * 4]))^) and AMask) <> 0 then
   874             if ((PLongword(@(p^[x * 4]))^) and AMask) <> 0 then
   867                    begin
   875                    begin
   868                 if (cReducedQuality and rqBlurryLand) = 0 then
   876                 if (cReducedQuality and rqBlurryLand) = 0 then
   873                 else
   881                 else
   874                     begin
   882                     begin
   875                     gX:= (cpX + x) div 2;
   883                     gX:= (cpX + x) div 2;
   876                     gY:= (cpY + y) div 2;
   884                     gY:= (cpY + y) div 2;
   877                     end;
   885                     end;
   878 		        if (not eraseOnLFMatch or (Land[cpY + y, cpX + x] and LandFlags <> 0)) and
   886                 if (not eraseOnLFMatch or (Land[cpY + y, cpX + x] and LandFlags <> 0)) and
   879                     ((PLongword(@(p^[x * 4]))^) and AMask <> 0) then
   887                     ((PLongword(@(p^[x * 4]))^) and AMask <> 0) then
   880                     begin
   888                     begin
   881                     if not onlyEraseLF then
   889                     if not onlyEraseLF then
   882                         begin
   890                         begin
   883                         LandPixels[gY, gX]:= 0;
   891                         LandPixels[gY, gX]:= 0;
   886                     else Land[cpY + y, cpX + x]:= Land[cpY + y, cpX + x] and (not LandFlags)
   894                     else Land[cpY + y, cpX + x]:= Land[cpY + y, cpX + x] and (not LandFlags)
   887                     end
   895                     end
   888                 end;
   896                 end;
   889         p:= PByteArray(@(p^[Image^.pitch]));
   897         p:= PByteArray(@(p^[Image^.pitch]));
   890         end;
   898         end;
   891     end;
       
   892 if SDL_MustLock(Image) then
   899 if SDL_MustLock(Image) then
   893     SDL_UnlockSurface(Image);
   900     SDL_UnlockSurface(Image);
   894 
   901 
   895 if flipVert then flipSurface(Image, true);
   902 if flipVert then flipSurface(Image, true);
   896 if flipHoriz then flipSurface(Image, false);
   903 if flipHoriz then flipSurface(Image, false);
   908     Image, finalSurface: PSDL_Surface;
   915     Image, finalSurface: PSDL_Surface;
   909 begin
   916 begin
   910 GetPlaceCollisionTex:= nil;
   917 GetPlaceCollisionTex:= nil;
   911 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   918 numFramesFirstCol:= SpritesData[Obj].imageHeight div SpritesData[Obj].Height;
   912 
   919 
   913 TryDo(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true);
   920 checkFails(SpritesData[Obj].Surface <> nil, 'Assert SpritesData[Obj].Surface failed', true);
   914 Image:= SpritesData[Obj].Surface;
   921 Image:= SpritesData[Obj].Surface;
   915 w:= SpritesData[Obj].Width;
   922 w:= SpritesData[Obj].Width;
   916 h:= SpritesData[Obj].Height;
   923 h:= SpritesData[Obj].Height;
   917 row:= Frame mod numFramesFirstCol;
   924 row:= Frame mod numFramesFirstCol;
   918 col:= Frame div numFramesFirstCol;
   925 col:= Frame div numFramesFirstCol;
   920 if SDL_MustLock(Image) then
   927 if SDL_MustLock(Image) then
   921     if SDLCheck(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true) then
   928     if SDLCheck(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true) then
   922         exit;
   929         exit;
   923 
   930 
   924 bpp:= Image^.format^.BytesPerPixel;
   931 bpp:= Image^.format^.BytesPerPixel;
   925 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
   932 checkFails(bpp = 4, 'It should be 32 bpp sprite', true);
   926 
   933 
   927 
   934 
   928 
   935 
   929 finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RMask, GMask, BMask, AMask);
   936 finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, RMask, GMask, BMask, AMask);
   930 
   937 
   931 TryDo(finalSurface <> nil, 'GetPlaceCollisionTex: fail to create surface', true);
   938 checkFails(finalSurface <> nil, 'GetPlaceCollisionTex: fail to create surface', true);
   932 
   939 
   933 if SDL_MustLock(finalSurface) then
   940 if SDL_MustLock(finalSurface) then
   934     if SDLCheck(SDL_LockSurface(finalSurface) >= 0, 'GetPlaceCollisionTex', true) then
   941     SDLCheck(SDL_LockSurface(finalSurface) >= 0, 'GetPlaceCollisionTex', true);
   935         exit;
   942 
       
   943 if not allOK then
       
   944     begin
       
   945     if SDL_MustLock(Image) then
       
   946         SDL_UnlockSurface(Image);
       
   947 
       
   948     if SDL_MustLock(finalSurface) then
       
   949         SDL_UnlockSurface(finalSurface);
       
   950 
       
   951     if finalSurface <> nil then
       
   952         SDL_FreeSurface(finalSurface);
       
   953     end;
   936 
   954 
   937 p:= PLongWordArray(@(PLongWordArray(Image^.pixels)^[ (Image^.pitch div 4) * row * h + col * w ]));
   955 p:= PLongWordArray(@(PLongWordArray(Image^.pixels)^[ (Image^.pitch div 4) * row * h + col * w ]));
   938 pt:= PLongWordArray(finalSurface^.pixels);
   956 pt:= PLongWordArray(finalSurface^.pixels);
   939 
   957 
   940 for y:= 0 to Pred(h) do
   958 for y:= 0 to Pred(h) do