changeset 34:fc416dd81a39

Code cleanups and simplification.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 24 Sep 2011 16:25:18 +0300
parents a68786b9c74b
children a2898079d6fe
files src/drawmap.cc
diffstat 1 files changed, 118 insertions(+), 149 deletions(-) [+]
line wrap: on
line diff
--- a/src/drawmap.cc	Sat Sep 24 15:59:33 2011 +0300
+++ b/src/drawmap.cc	Sat Sep 24 16:25:18 2011 +0300
@@ -356,24 +356,19 @@
 /*
  *        draw_linedefs - draw the linedefs
  */
-static int ld_check(int x1, int y1, int x2, int y2)
+static int ld_check(VPtr v1, VPtr v2)
 {
-    int mapx0 = MAPX(0),
-        mapx9 = MAPX(ScrMaxX),
-        mapy0 = MAPY(ScrMaxY),
-        mapy9 = MAPY(0);
+    const int mapx0 = MAPX(0), mapx9 = MAPX(ScrMaxX),
+              mapy0 = MAPY(ScrMaxY), mapy9 = MAPY(0);
 
-    return ((x1 < mapx0 && x2 < mapx0) || (x1 > mapx9 && x2 > mapx9) ||
-            (y1 < mapy0 && y2 < mapy0) || (y1 > mapy9 && y2 > mapy9));
+    return ((v1->x < mapx0 && v2->x < mapx0) || (v1->x > mapx9 && v2->x > mapx9) ||
+            (v1->y < mapy0 && v2->y < mapy0) || (v1->y > mapy9 && v2->y > mapy9));
 }
 
 
 static void draw_linedefs(edit_t * e)
 {
-    int mapx0 = MAPX(0),
-        mapx9 = MAPX(ScrMaxX),
-        mapy0 = MAPY(ScrMaxY),
-        mapy9 = MAPY(0);
+    int new_colour, current_colour = INT_MIN;        /* Some impossible colour no. */
 
     switch (e->obj_type)
     {
@@ -383,18 +378,16 @@
 
             for (int n = 0; n < NumLineDefs; n++)
             {
-                int x1 = Vertices[LineDefs[n].start].x;
-                int x2 = Vertices[LineDefs[n].end].x;
-                int y1 = Vertices[LineDefs[n].start].y;
-                int y2 = Vertices[LineDefs[n].end].y;
+                VPtr v1 = &Vertices[LineDefs[n].start],
+                     v2 = &Vertices[LineDefs[n].end];
 
-                if (ld_check(x1, y1, x2, y2))
+                if (ld_check(v1, v2))
                     continue;
-                
+
                 new_colour = (LineDefs[n].flags & 1) ? WHITE : LIGHTGREY;
                 if (new_colour != current_colour)
                     set_colour(current_colour = new_colour);
-                DrawMapLine(x1, y1, x2, y2);
+                DrawMapLine(v1->x, v1->y, v2->x, v2->y);
             }
             break;
         }
@@ -403,141 +396,124 @@
         set_colour(LIGHTGREY);
         for (int n = 0; n < NumLineDefs; n++)
         {
-            int x1 = Vertices[LineDefs[n].start].x;
-            int x2 = Vertices[LineDefs[n].end].x;
-            int y1 = Vertices[LineDefs[n].start].y;
-            int y2 = Vertices[LineDefs[n].end].y;
+            VPtr v1 = &Vertices[LineDefs[n].start],
+                 v2 = &Vertices[LineDefs[n].end];
 
-            if (ld_check(x1, y1, x2, y2))
+            if (ld_check(v1, v2))
                 continue;
 
-            DrawMapVector(x1, y1, x2, y2);
+            DrawMapVector(v1->x, v1->y, v2->x, v2->y);
         }
         break;
 
     case OBJ_LINEDEFS:
+        for (int n = 0; n < NumLineDefs; n++)
         {
-            int current_colour = INT_MIN;        /* Some impossible colour no. */
-            int new_colour;
+            VPtr v1 = &Vertices[LineDefs[n].start],
+                 v2 = &Vertices[LineDefs[n].end];
 
-            for (int n = 0; n < NumLineDefs; n++)
-            {
-                int x1 = Vertices[LineDefs[n].start].x;
-                int x2 = Vertices[LineDefs[n].end].x;
-                int y1 = Vertices[LineDefs[n].start].y;
-                int y2 = Vertices[LineDefs[n].end].y;
-                if (x1 < mapx0 && x2 < mapx0
-                    || x1 > mapx9 && x2 > mapx9
-                    || y1 < mapy0 && y2 < mapy0 || y1 > mapy9 && y2 > mapy9)
-                    continue;
-                if (LineDefs[n].type != 0)        /* AYM 19980207: was "> 0" */
-                {
-                    if (LineDefs[n].tag != 0)        /* AYM 19980207: was "> 0" */
-                        new_colour = LIGHTMAGENTA;
-                    else
-                        new_colour = LIGHTGREEN;
-                }
-                else if (LineDefs[n].flags & 1)
-                    new_colour = WHITE;
-                else
-                    new_colour = LIGHTGREY;
+            if (ld_check(v1, v2))
+                continue;
+
+            if (LineDefs[n].type != 0)        /* AYM 19980207: was "> 0" */
+                new_colour = (LineDefs[n].tag != 0) ? LIGHTMAGENTA : LIGHTGREEN;
+            else
+                new_colour = (LineDefs[n].flags & 1) ? WHITE : LIGHTGREY;
 
-                // Signal errors by drawing the linedef in red. Needs work.
-                // Tag on a typeless linedef
-                if (LineDefs[n].type == 0 && LineDefs[n].tag != 0)
-                    new_colour = LIGHTRED;
-                // No first sidedef
-                if (!is_sidedef(LineDefs[n].sidedef1))
-                    new_colour = LIGHTRED;
-                // Bad second sidedef
-                if (!is_sidedef(LineDefs[n].sidedef2)
-                    && LineDefs[n].sidedef2 != -1)
-                    new_colour = LIGHTRED;
+            // Signal errors by drawing the linedef in red. Needs work.
+            // Tag on a typeless linedef
+            if (LineDefs[n].type == 0 && LineDefs[n].tag != 0)
+                new_colour = LIGHTRED;
+            // No first sidedef
+            if (!is_sidedef(LineDefs[n].sidedef1))
+                new_colour = LIGHTRED;
+            // Bad second sidedef
+            if (!is_sidedef(LineDefs[n].sidedef2) && LineDefs[n].sidedef2 != -1)
+                new_colour = LIGHTRED;
+
+            if (new_colour != current_colour)
+                set_colour(current_colour = new_colour);
 
-                if (new_colour != current_colour)
-                    set_colour(current_colour = new_colour);
-                DrawMapLine(x1, y1, x2, y2);
+            DrawMapLine(v1->x, v1->y, v2->x, v2->y);
 
-                if (e->show_object_numbers)
+            if (e->show_object_numbers)
+            {
+                int scnx0 = SCREENX(v1->x);
+                int scnx1 = SCREENX(v2->x);
+                int scny0 = SCREENY(v1->y);
+                int scny1 = SCREENY(v2->y);
+                int label_width = ((int) log10(n) + 1) * FONTW;
+                if (abs(scnx1 - scnx0) > label_width + 4 ||
+                    abs(scny1 - scny0) > label_width + 4)
                 {
-                    int scnx0 = SCREENX(x1);
-                    int scnx1 = SCREENX(x2);
-                    int scny0 = SCREENY(y1);
-                    int scny1 = SCREENY(y2);
-                    int label_width = ((int) log10(n) + 1) * FONTW;
-                    if (abs(scnx1 - scnx0) > label_width + 4
-                        || abs(scny1 - scny0) > label_width + 4)
-                    {
-                        int scnx = (scnx0 + scnx1) / 2 - label_width / 2;
-                        int scny = (scny0 + scny1) / 2 - FONTH / 2;
-                        draw_obj_no(scnx, scny, n, LINEDEF_NO);
-                    }
+                    int scnx = (scnx0 + scnx1) / 2 - label_width / 2;
+                    int scny = (scny0 + scny1) / 2 - FONTH / 2;
+                    draw_obj_no(scnx, scny, n, LINEDEF_NO);
                 }
             }
-            break;
         }
+        break;
 
     case OBJ_SECTORS:
+        int current_colour = INT_MIN;        /* Some impossible colour no. */
+        int new_colour;
+
+        for (int n = 0; n < NumLineDefs; n++)
         {
-            int current_colour = INT_MIN;        /* Some impossible colour no. */
-            int new_colour;
+            VPtr v1 = &Vertices[LineDefs[n].start],
+                 v2 = &Vertices[LineDefs[n].end];
 
-            for (int n = 0; n < NumLineDefs; n++)
-            {
-                int x1 = Vertices[LineDefs[n].start].x;
-                int x2 = Vertices[LineDefs[n].end].x;
-                int y1 = Vertices[LineDefs[n].start].y;
-                int y2 = Vertices[LineDefs[n].end].y;
+            if (ld_check(v1, v2))
+                continue;
 
-                if (ld_check(x1, y1, x2, y2))
-                    continue;
+            int sd1 = OBJ_NO_NONE;
+            int sd2 = OBJ_NO_NONE;
+            int s1 = OBJ_NO_NONE;
+            int s2 = OBJ_NO_NONE;
 
-                int sd1 = OBJ_NO_NONE;
-                int sd2 = OBJ_NO_NONE;
-                int s1 = OBJ_NO_NONE;
-                int s2 = OBJ_NO_NONE;
-                // FIXME should flag negative sidedef numbers as errors
-                // FIXME should flag unused tag as errors
-                if ((sd1 = LineDefs[n].sidedef1) < 0 || sd1 >= NumSideDefs
-                    || (s1 = SideDefs[sd1].sector) < 0 || s1 >= NumSectors
-                    || (sd2 = LineDefs[n].sidedef2) >= NumSideDefs
-                    || sd2 >= 0 && ((s2 = SideDefs[sd2].sector) < 0
-                                    || s2 >= NumSectors))
-                {
-                    new_colour = LIGHTRED;
-                }
-                else
+            // FIXME should flag negative sidedef numbers as errors
+            // FIXME should flag unused tag as errors
+            if ((sd1 = LineDefs[n].sidedef1) < 0 || sd1 >= NumSideDefs||
+                (s1 = SideDefs[sd1].sector) < 0 || s1 >= NumSectors ||
+                (sd2 = LineDefs[n].sidedef2) >= NumSideDefs ||
+                (sd2 >= 0 && ((s2 = SideDefs[sd2].sector) < 0 || s2 >= NumSectors)))
+            {
+                new_colour = LIGHTRED;
+            }
+            else
+            {
+                bool have_tag = false,
+                     have_type = false;
+
+                if (Sectors[s1].tag != 0)
+                    have_tag = true;
+                if (Sectors[s1].special != 0)
+                    have_type = true;
+
+                if (sd2 >= 0)
                 {
-                    bool have_tag = false;
-                    bool have_type = false;
-                    if (Sectors[s1].tag != 0)
+                    if (Sectors[s2].tag != 0)
                         have_tag = true;
-                    if (Sectors[s1].special != 0)
+                    if (Sectors[s2].special != 0)
                         have_type = true;
-                    if (sd2 >= 0)
-                    {
-                        if (Sectors[s2].tag != 0)
-                            have_tag = true;
-                        if (Sectors[s2].special != 0)
-                            have_type = true;
-                    }
-                    if (have_tag && have_type)
-                        new_colour = SECTOR_TAGTYPE;
-                    else if (have_tag)
-                        new_colour = SECTOR_TAG;
-                    else if (have_type)
-                        new_colour = SECTOR_TYPE;
-                    else if (LineDefs[n].flags & 1)
-                        new_colour = WHITE;
-                    else
-                        new_colour = LIGHTGREY;
+
                 }
-                if (new_colour != current_colour)
-                    set_colour(current_colour = new_colour);
-                DrawMapLine(x1, y1, x2, y2);
+                if (have_tag && have_type)
+                    new_colour = SECTOR_TAGTYPE;
+                else if (have_tag)
+                    new_colour = SECTOR_TAG;
+                else if (have_type)
+                    new_colour = SECTOR_TYPE;
+                else
+                    new_colour = (LineDefs[n].flags & 1) ? WHITE : LIGHTGREY;
             }
-            break;
+
+            if (new_colour != current_colour)
+                set_colour(current_colour = new_colour);
+
+            DrawMapLine(v1->x, v1->y, v2->x, v2->y);
         }
+        break;
     }
 }
 
@@ -549,6 +525,8 @@
 {
     // The radius of the largest thing.
     int max_radius = get_max_thing_radius();
+    static const short xsign[] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 };
+    static const short ysign[] = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };
 
     /* A thing is guaranteed to be totally off-screen
        if its centre is more than <max_radius> units
@@ -561,33 +539,26 @@
     push_colour(THING_REM);
     for (int n = 0; n < NumThings; n++)
     {
-        int mapx = Things[n].xpos;
-        int mapy = Things[n].ypos;
-        int corner_x;
-        int corner_y;
+        const int mapx = Things[n].xpos,
+                  mapy = Things[n].ypos;
+
         if (mapx < mapx0 || mapx > mapx9 || mapy < mapy0 || mapy > mapy9)
             continue;
-        int m = get_thing_radius(Things[n].type);
+
+        const int m = get_thing_radius(Things[n].type),
+                  direction = angle_to_direction(Things[n].angle);
+
         if (e->obj_type == OBJ_THINGS)
             set_colour(get_thing_colour(Things[n].type));
-#ifdef ROUND_THINGS
-        DrawMapLine(mapx - m, mapy, mapx + m, mapy);
-        DrawMapLine(mapx, mapy - m, mapx, mapy + m);
-        DrawMapCircle(mapx, mapy, m);
-#else
+
         DrawMapLine(mapx - m, mapy - m, mapx + m, mapy - m);
         DrawMapLine(mapx + m, mapy - m, mapx + m, mapy + m);
         DrawMapLine(mapx + m, mapy + m, mapx - m, mapy + m);
         DrawMapLine(mapx - m, mapy + m, mapx - m, mapy - m);
-#endif
-        {
-            size_t direction = angle_to_direction(Things[n].angle);
-            static const short xsign[] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 };
-            static const short ysign[] = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };
-            corner_x = m * xsign[direction];
-            corner_y = m * ysign[direction];
-        }
-        DrawMapLine(mapx, mapy, mapx + corner_x, mapy + corner_y);
+
+        DrawMapLine(mapx, mapy,
+            mapx + (m * xsign[direction]),
+            mapy + (m * ysign[direction]));
     }
     pop_colour();
 }
@@ -635,11 +606,9 @@
     }
     bool operator<(const Thing_npixels & other) const
     {
-        if (this->npixels > other.npixels        // Decreasing npixels major
-            || this->npixels == other.npixels        // Increasing type minor
-            && this->type < other.type)
-            return true;
-        return false;
+        return ((this->npixels > other.npixels ||
+                 this->npixels == other.npixels) &&
+                 this->type < other.type);
     }
     i16 thing_no;
     unsigned long npixels;