Store Land surface in memory:
authorunc0rr
Sun, 27 Jan 2008 19:29:00 +0000
changeset 768 2886dafa5bcf
parent 767 697728ffe39f
child 769 788efc1d649f
Store Land surface in memory: - Fixes strange land texture disappering - Speed up operations, changing land after its initialization
hedgewars/tunsetborder.inc
hedgewars/uLand.pas
hedgewars/uLandGraphics.pas
hedgewars/uStore.pas
--- a/hedgewars/tunsetborder.inc	Sun Jan 27 18:39:02 2008 +0000
+++ b/hedgewars/tunsetborder.inc	Sun Jan 27 19:29:00 2008 +0000
@@ -1,6 +1,6 @@
 (*
  * Hedgewars, a worms-like game
- * Copyright (c) 2006-2007 Andrey Korotaev <unC0Rr@gmail.com>
+ * Copyright (c) 2006-2008 Andrey Korotaev <unC0Rr@gmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,5 +23,5 @@
         ty:= hwRound(Y);
         if ((ty and $FFFFFC00) = 0) and
            ((tx and $FFFFF800) = 0) and
-           (Land[ty, tx] = $FFFFFF) then SetLandPixel(ty, tx)
+           (Land[ty, tx] = $FFFFFF) then LandPixels[ty, tx]:= cExplosionBorderColor
 	end;
--- a/hedgewars/uLand.pas	Sun Jan 27 18:39:02 2008 +0000
+++ b/hedgewars/uLand.pas	Sun Jan 27 19:29:00 2008 +0000
@@ -25,6 +25,7 @@
 
 var  Land: TLandArray;
      LandSurface: PSDL_Surface;
+     LandPixels: TLandArray;
      LandTexture: PTexture = nil;
 
 procedure GenMap;
@@ -630,22 +631,26 @@
 end;
 
 procedure UpdateLandTexture(Y, Height: LongInt);
-var p: pointer;
+var i: integer;
 begin
 if LandTexture <> nil then
    begin
+   glBindTexture(GL_TEXTURE_2D, LandTexture^.id);
+
+   glTexSubImage2D(GL_TEXTURE_2D, 0, 0, Y, 2048, Height, GL_RGBA, GL_UNSIGNED_BYTE, @LandPixels[Y, 0]);
+   end else
+   begin
+   LandTexture:= Surface2Tex(LandSurface);
+
    if SDL_MustLock(LandSurface) then
       SDLTry(SDL_LockSurface(LandSurface) >= 0, true);
 
-   glBindTexture(GL_TEXTURE_2D, LandTexture^.id);
-
-   p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * Y];
-   glTexSubImage2D(GL_TEXTURE_2D, 0, 0, Y, 2048, Height, GL_RGBA, GL_UNSIGNED_BYTE, p);
+   Move(LandSurface^.pixels^, LandPixels, 2048 * 1024 * 4);
 
    if SDL_MustLock(LandSurface) then
-      SDL_UnlockSurface(LandSurface);
-   end else
-   LandTexture:= Surface2Tex(LandSurface)
+      SDL_UnlockSurface(LandSurface)
+   end;
+
 end;
 
 initialization
--- a/hedgewars/uLandGraphics.pas	Sun Jan 27 18:39:02 2008 +0000
+++ b/hedgewars/uLandGraphics.pas	Sun Jan 27 19:29:00 2008 +0000
@@ -120,47 +120,17 @@
   if (dx = dy) then ChangeCircleLines(x, y, dx, dy, doSet)
 end;
 
-procedure ClearLandPixel(y, x: LongInt);
-var p: PByteArray;
-begin
-p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
-case LandSurface^.format^.BytesPerPixel of
-     2: PWord(@(p^[x * 2]))^:= 0;
-     3: begin
-        p^[x * 3 + 0]:= 0;
-        p^[x * 3 + 1]:= 0;
-        p^[x * 3 + 2]:= 0;
-        end;
-     4: PLongword(@(p^[x * 4]))^:= 0;
-     end
-end;
-
-procedure SetLandPixel(y, x: LongInt);
-var p: PByteArray;
-begin
-p:= @PByteArray(LandSurface^.pixels)^[LandSurface^.pitch * y];
-case LandSurface^.format^.BytesPerPixel of
-     2: PWord(@(p^[x * 2]))^:= cExplosionBorderColor;
-     3: begin
-        p^[x * 3 + 0]:= cExplosionBorderColor and $FF;
-        p^[x * 3 + 1]:= (cExplosionBorderColor shr 8) and $FF;
-        p^[x * 3 + 2]:= cExplosionBorderColor shr 16;
-        end;
-     4: PLongword(@(p^[x * 4]))^:= cExplosionBorderColor;
-     end
-end;
-
 procedure FillLandCircleLines0(x, y, dx, dy: LongInt);
 var i: LongInt;
 begin
 if ((y + dy) and $FFFFFC00) = 0 then
-   for i:= max(x - dx, 0) to min(x + dx, 2047) do ClearLandPixel(y + dy, i);
+   for i:= max(x - dx, 0) to min(x + dx, 2047) do LandPixels[y + dy, i]:= 0;
 if ((y - dy) and $FFFFFC00) = 0 then
-   for i:= max(x - dx, 0) to min(x + dx, 2047) do ClearLandPixel(y - dy, i);
+   for i:= max(x - dx, 0) to min(x + dx, 2047) do LandPixels[y - dy, i]:= 0;
 if ((y + dx) and $FFFFFC00) = 0 then
-   for i:= max(x - dy, 0) to min(x + dy, 2047) do ClearLandPixel(y + dx, i);
+   for i:= max(x - dy, 0) to min(x + dy, 2047) do LandPixels[y + dx, i]:= 0;
 if ((y - dx) and $FFFFFC00) = 0 then
-   for i:= max(x - dy, 0) to min(x + dy, 2047) do ClearLandPixel(y - dx, i);
+   for i:= max(x - dy, 0) to min(x + dy, 2047) do LandPixels[y - dx, i]:= 0;
 end;
 
 procedure FillLandCircleLinesEBC(x, y, dx, dy: LongInt);
@@ -168,16 +138,16 @@
 begin
 if ((y + dy) and $FFFFFC00) = 0 then
    for i:= max(x - dx, 0) to min(x + dx, 2047) do
-       if Land[y + dy, i] = COLOR_LAND then SetLandPixel(y + dy, i);
+       if Land[y + dy, i] = COLOR_LAND then LandPixels[y + dy, i]:= cExplosionBorderColor;
 if ((y - dy) and $FFFFFC00) = 0 then
    for i:= max(x - dx, 0) to min(x + dx, 2047) do
-       if Land[y - dy, i] = COLOR_LAND then SetLandPixel(y - dy, i);
+       if Land[y - dy, i] = COLOR_LAND then LandPixels[y - dy, i]:= cExplosionBorderColor;
 if ((y + dx) and $FFFFFC00) = 0 then
    for i:= max(x - dy, 0) to min(x + dy, 2047) do
-       if Land[y + dx, i] = COLOR_LAND then SetLandPixel(y + dx, i);
+       if Land[y + dx, i] = COLOR_LAND then LandPixels[y + dx, i]:= cExplosionBorderColor;
 if ((y - dx) and $FFFFFC00) = 0 then
    for i:= max(x - dy, 0) to min(x + dy, 2047) do
-       if Land[y - dx, i] = COLOR_LAND then SetLandPixel(y - dx, i);
+       if Land[y - dx, i] = COLOR_LAND then LandPixels[y - dx, i]:= cExplosionBorderColor;
 end;
 
 procedure DrawExplosion(X, Y, Radius: LongInt);
@@ -185,9 +155,6 @@
 begin
 FillRoundInLand(X, Y, Radius, 0);
 
-if SDL_MustLock(LandSurface) then
-   SDLTry(SDL_LockSurface(LandSurface) >= 0, true);
-
   dx:= 0;
   dy:= Radius;
   d:= 3 - 2 * Radius;
@@ -220,23 +187,19 @@
      end;
   if (dx = dy) then FillLandCircleLinesEBC(x, y, dx, dy);
 
-if SDL_MustLock(LandSurface) then
-   SDL_UnlockSurface(LandSurface);
-
-UpdateLandTexture(Y - Radius, 2 * Radius)
+d:= max(Y - Radius, 0);
+dy:= min(Y + Radius, 1023) - d;
+UpdateLandTexture(d, dy)
 end;
 
 procedure DrawHLinesExplosions(ar: PRangeArray; Radius: LongInt; y, dY: LongInt; Count: Byte);
 var tx, ty, i: LongInt;
 begin
-if SDL_MustLock(LandSurface) then
-   SDL_LockSurface(LandSurface);
-
 for i:= 0 to Pred(Count) do
     begin
     for ty:= max(y - Radius, 0) to min(y + Radius, 1023) do
         for tx:= max(0, ar^[i].Left - Radius) to min(2047, ar^[i].Right + Radius) do
-            ClearLandPixel(ty, tx);
+            LandPixels[ty, tx]:= 0;
     inc(y, dY)
     end;
 
@@ -248,14 +211,11 @@
     for ty:= max(y - Radius, 0) to min(y + Radius, 1023) do
         for tx:= max(0, ar^[i].Left - Radius) to min(2047, ar^[i].Right + Radius) do
             if Land[ty, tx] = $FFFFFF then
-                  SetLandPixel(ty, tx);
+                  LandPixels[ty, tx]:= cExplosionBorderColor;
     inc(y, dY)
     end;
 
-if SDL_MustLock(LandSurface) then
-   SDL_UnlockSurface(LandSurface);
-
-//UpdateLandTexture
+UpdateLandTexture(0, 1024)
 end;
 
 //
@@ -265,9 +225,6 @@
 var nx, ny, dX8, dY8: hwFloat;
     i, t, tx, ty: Longint;
 begin  // (-dY, dX) is (dX, dY) rotated by PI/2
-if SDL_MustLock(LandSurface) then
-   SDL_LockSurface(LandSurface);
-
 nx:= X + dY * (HalfWidth + 8);
 ny:= Y - dX * (HalfWidth + 8);
 
@@ -301,7 +258,7 @@
          if Land[ty, tx] = COLOR_LAND then
            begin
            Land[ty, tx]:= 0;
-           ClearLandPixel(ty, tx);
+           LandPixels[ty, tx]:= 0;
            end
         end;
     for t:= 0 to 7 do
@@ -320,10 +277,7 @@
     ny:= ny + dX;
     end;
 
-if SDL_MustLock(LandSurface) then
-   SDL_UnlockSurface(LandSurface);
-
-//UpdateLandTexture
+UpdateLandTexture(0, 1024)
 end;
 
 function TryPlaceOnLand(cpX, cpY: LongInt; Obj: TSprite; Frame: LongInt; doPlace: boolean): boolean;
--- a/hedgewars/uStore.pas	Sun Jan 27 18:39:02 2008 +0000
+++ b/hedgewars/uStore.pas	Sun Jan 27 19:29:00 2008 +0000
@@ -249,10 +249,10 @@
     WriteLnToConsole(msgOK);
     val(s, cExplosionBorderColor, c);
     TryDo(c = 0, 'Theme data corrupted', true);
-    cExplosionBorderColor:= SDL_MapRGB(LandSurface^.format,
-                                       cExplosionBorderColor shr 16,
-                                       cExplosionBorderColor shr 8,
-                                       cExplosionBorderColor and $FF);
+    cExplosionBorderColor:= (cExplosionBorderColor shr 16) or
+                            (cExplosionBorderColor and $FF) or
+                            (cExplosionBorderColor shl 16) or
+                            $FF000000
     end;
 
 begin