diff src/l_align.cc @ 66:794b7bb40d7f

Initial work on cleaning up and improving the texture alignment functionality.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 26 Sep 2011 10:16:55 +0300
parents 2898afb3c9a0
children df543ecd0746
line wrap: on
line diff
--- a/src/l_align.cc	Mon Sep 26 09:47:41 2011 +0300
+++ b/src/l_align.cc	Mon Sep 26 10:16:55 2011 +0300
@@ -43,7 +43,7 @@
    get the absolute height from which the textures are drawn
 */
 
-int GetTextureRefHeight(int sidedef)        /* SWAP! */
+int GetTextureRefHeight(int sidedef)
 {
     int l, sector;
     int otherside = OBJ_NO_NONE;
@@ -100,7 +100,7 @@
       way to select only one side of a two-sided wall.
 */
 
-void AlignTexturesY(SelPtr * sdlist)        /* SWAP! */
+void AlignTexturesY(SelPtr * sdlist, int opt_sdef, bool opt_check)
 {
     int h, refh;
 
@@ -119,6 +119,7 @@
         SideDefs[(*sdlist)->objnum].yoff = (refh - h) % 128;
         UnSelectObject(sdlist, (*sdlist)->objnum);
     }
+    
     MadeChanges = 1;
 }
 
@@ -143,7 +144,7 @@
    the following needs to be altered radically.
 */
 
-void AlignTexturesX(SelPtr * sdlist)        /* SWAP! */
+void AlignTexturesX(SelPtr * sdlist, int opt_sdef, bool opt_check, bool opt_offset, int opt_mode)
 {
     /* FIRST texture name used in the highlited objects */
     char texname[WAD_TEX_NAME + 1];
@@ -156,13 +157,6 @@
     i16 texlength;                   /* the length of texture to format to */
     int length;                      /* length of linedef under scrutiny */
     i16 dummy;                       /* holds useless data */
-    int type_off;                    /* do we have an initial offset to use */
-    int type_tex;                    /* do we check for same textures */
-    int type_sd;                     /* do we align sidedef 1 or sidedef 2 */
-
-    type_sd = 0;                     /* which sidedef to align, 1=SideDef1, 2=SideDef2 */
-    type_tex = 0;                    /* do we test for similar textures, 0 = no, 1 = yes */
-    type_off = 0;                    /* do we have an inital offset, 0 = no, 1 = yes */
 
     vert1 = -1;
     vert2 = -1;                      /* 1st time round the while loop the -1 value is needed */
@@ -170,129 +164,53 @@
     xoffset = 0;
     useroffset = 0;
 
-    switch (vDisplayMenu(250, 110,
-    "Aligning textures (X offset) :",
-    " Sidedef 1, Check for identical textures.     ", YK_, 0,
-    " Sidedef 1, As above, but with initial offset.", YK_, 0,
-    " Sidedef 1, No texture checking.              ", YK_, 0,
-    " Sidedef 1, As above, but with initial offset.", YK_, 0,
-    " Sidedef 2, Check for identical textures.     ", YK_, 0,
-    " Sidedef 2, As above, but with initial offset.", YK_, 0,
-    " Sidedef 2, No texture checking.              ", YK_, 0,
-    " Sidedef 2, As above, but with initial offset.", YK_, 0,
-    NULL))
-    {
-    case 1:                        /* Sidedef 1 with checking for same textures   */
-        type_sd = 1;
-        type_tex = 1;
-        type_off = 0;
-        break;
-
-    case 2:                        /* Sidedef 1 as above, but with inital offset  */
-        type_sd = 1;
-        type_tex = 1;
-        type_off = 1;
-        break;
-
-    case 3:                        /* Sidedef 1 regardless of same textures       */
-        type_sd = 1;
-        type_tex = 0;
-        type_off = 0;
-        break;
-
-    case 4:                        /* Sidedef 1 as above, but with inital offset  */
-        type_sd = 1;
-        type_tex = 0;
-        type_off = 1;
-        break;
-
-    case 5:                        /* Sidedef 2 with checking for same textures   */
-        type_sd = 2;
-        type_tex = 1;
-        type_off = 0;
-        break;
-
-    case 6:                        /* Sidedef 2 as above, but with initial offset */
-        type_sd = 2;
-        type_tex = 1;
-        type_off = 1;
-        break;
-
-    case 7:                        /* Sidedef 2 regardless of same textures       */
-        type_sd = 2;
-        type_tex = 0;
-        type_off = 0;
-        break;
-
-    case 8:                        /* Sidedef 2 as above, but with initial offset */
-        type_sd = 2;
-        type_tex = 0;
-        type_off = 1;
-        break;
-    default:
-        return;
-    }
-
     if (!*sdlist)
     {
         Notify(-1, -1, "Error in AlignTexturesX: list is empty", 0);
         return;
     }
 
-    /* throw out all 1/2nd SideDefs until a 1/2st is found */
-    ldef = 0;
-    sdef = (*sdlist)->objnum;
-
-    while (ldef <= NumLineDefs && (
-           (type_sd == 1 && LineDefs[ldef].sidedef1 != sdef) ||
-           (type_sd == 2 && LineDefs[ldef].sidedef2 != sdef)               
-           ))
+    if (opt_mode == -1)
     {
-        ldef++;
-        if ((type_sd == 1 && LineDefs[ldef].sidedef2 == sdef) ||
-            (type_sd == 2 && LineDefs[ldef].sidedef1 == sdef))
+        /* No defined mode, we will try to "guess" which texture
+         * the user wants to align ... and choose the operating mode
+         * based on that.
+         */
+        SDPtr pdef = &SideDefs[(*sdlist)->objnum];
+        if (ValidTextureName(pdef->tex3))
+        {
+            strncpy(texname, pdef->tex3, WAD_TEX_NAME);
+            opt_mode = 3;
+        }
+        else if (ValidTextureName(pdef->tex2))
         {
-            int tmp = (*sdlist)->objnum;
-            UnSelectObject(sdlist, tmp);
-            if (!*sdlist)
-            {
-                sprintf(errormessage, "*sdlist == NULL while unselecting sidedefs (type_sd=%d, sdef=%d, ldef=%d, tmp=%d).",
-                    type_sd, sdef, ldef, tmp);
-                Notify(-1, -1, errormessage, 0);
-                return;
-            }
-            sdef = (*sdlist)->objnum;
-            ldef = 0;
+            strncpy(texname, pdef->tex2, WAD_TEX_NAME);
+            opt_mode = 2;
+        }
+        else if (ValidTextureName(pdef->tex1))
+        {
+            strncpy(texname, pdef->tex1, WAD_TEX_NAME);
+            opt_mode = 1;
         }
-    }
-
-    /* get texture name of the sidedef in the *sdlist) */
-    SDPtr pdef = &SideDefs[(*sdlist)->objnum];
-    int mode = -1;
-    if (ValidTextureName(pdef->tex3))
-    {
-        strncpy(texname, pdef->tex3, WAD_TEX_NAME);
-        mode = 3;
-    }
-    else if (ValidTextureName(pdef->tex2))
-    {
-        strncpy(texname, pdef->tex2, WAD_TEX_NAME);
-        mode = 2;
-    }
-    else if (ValidTextureName(pdef->tex1))
-    {
-        strncpy(texname, pdef->tex1, WAD_TEX_NAME);
-        mode = 1;
+        else
+        {
+            sprintf(errormessage, "No texture(s) for sidedef #%d.",
+                    (*sdlist)->objnum);
+            Notify(-1, -1, errormessage, 0);
+            return;
+        }
     }
     else
     {
-        sprintf(errormessage, "No texture(s) for sidedef #%d.",
-                (*sdlist)->objnum);
-        Notify(-1, -1, errormessage, 0);
-        return;
+        /* Find first sidedef that has desired texture set */
+
+        // FIXME! Not implemented yet ... -ccr
     }
 
-    GetWallTextureSize(&texlength, &dummy, texname);        /* clunky, but it works */
+    /* Get texture width, do some sanity checking as the
+     * WAD reading may fail.
+     */
+    GetWallTextureSize(&texlength, &dummy, texname);
 
     if (texlength < 4)
     {
@@ -302,8 +220,8 @@
         return;
     }
 
-    /* get initial offset to use (if required) */
-    if (type_off == 1)
+    /* Get initial offset to use, if requested */
+    if (opt_offset)
     {
         int x0, y0, key;
         char prompt[80];
@@ -331,7 +249,7 @@
         ldef = 0;
         sdef = (*sdlist)->objnum;
 
-        if (type_sd == 1)        /* throw out all 2nd SideDefs untill a 1st is found */
+        if (opt_sdef == 1)        /* throw out all 2nd SideDefs untill a 1st is found */
         {
             while (LineDefs[ldef].sidedef1 != sdef && ldef <= NumLineDefs)
             {
@@ -341,7 +259,7 @@
                     UnSelectObject(sdlist, (*sdlist)->objnum);
                     if (*sdlist == NULL)
                     {
-                        sprintf(errormessage, "Error in type_sd==1, *sdlist == NULL");
+                        sprintf(errormessage, "Error in opt_sdef==1, *sdlist == NULL");
                         Notify(-1, -1, errormessage, 0);
                         return;
                     }
@@ -351,7 +269,7 @@
             }
         }
         else
-        if (type_sd == 2)        /* throw out all 1st SideDefs untill a 2nd is found */
+        if (opt_sdef == 2)        /* throw out all 1st SideDefs untill a 2nd is found */
         {
             while (LineDefs[ldef].sidedef2 != sdef && ldef <= NumLineDefs)
             {
@@ -361,7 +279,7 @@
                     UnSelectObject(sdlist, (*sdlist)->objnum);
                     if (*sdlist == NULL)
                     {
-                        sprintf(errormessage, "Error in type_sd==2, *sdlist == NULL");
+                        sprintf(errormessage, "Error in opt_sdef==2, *sdlist == NULL");
                         Notify(-1, -1, errormessage, 0);
                         return;
                     }
@@ -372,27 +290,26 @@
         }
 
         /* do we test for same textures for the sidedef in question?? */
-        if (type_tex == 1)
+        sdef = (*sdlist)->objnum;
+        if (opt_check)
         {
-            pdef = &SideDefs[(*sdlist)->objnum];
-            if ((mode == 3 && strncmp(pdef->tex3, texname, WAD_TEX_NAME)) ||
-                (mode == 2 && strncmp(pdef->tex2, texname, WAD_TEX_NAME)) ||
-                (mode == 1 && strncmp(pdef->tex1, texname, WAD_TEX_NAME)))
+            SDPtr pdef = &SideDefs[sdef];
+            if ((opt_mode == 3 && strncmp(pdef->tex3, texname, WAD_TEX_NAME)) ||
+                (opt_mode == 2 && strncmp(pdef->tex2, texname, WAD_TEX_NAME)) ||
+                (opt_mode == 1 && strncmp(pdef->tex1, texname, WAD_TEX_NAME)))
             {
-                sprintf(errormessage, "No texture for sidedef #%d.",
-                        (*sdlist)->objnum);
+                sprintf(errormessage, "No texture or discontinuous texture on sidedef #%d.",
+                        sdef);
                 Notify(-1, -1, errormessage, 0);
                 return;
             }
         }
 
-        sdef = (*sdlist)->objnum;
-
         /* find out which linedef holds that sidedef */
         int n;
         for (ldef = -1, n = 0; n < NumLineDefs; n++)
-            if ((type_sd == 1 && LineDefs[n].sidedef1 == sdef) ||
-                (type_sd == 2 && LineDefs[n].sidedef2 == sdef)) 
+            if ((opt_sdef == 1 && LineDefs[n].sidedef1 == sdef) ||
+                (opt_sdef == 2 && LineDefs[n].sidedef2 == sdef)) 
             {
                 ldef = n;
                 break;
@@ -420,16 +337,11 @@
         /* is this the first time round here */
         if (vert1 != vert2)
         {
-            if (type_off == 1)        /* do we have an initial offset ? */
-            {
-                SideDefs[sdef].xoff = useroffset;
-                xoffset = useroffset;
-            }
-            else
-                SideDefs[sdef].xoff = 0;
+            /* Do we have an initial offset? */
+            xoffset = opt_offset ? useroffset : 0;
         }
-        else                        /* put new xoffset into the sidedef */
-            SideDefs[sdef].xoff = xoffset;
+
+        SideDefs[sdef].xoff = xoffset;
 
         /* calculate length of linedef */
         vert2 = LineDefs[ldef].end;