viewport.c
changeset 4188 c817518c79cc
parent 4187 50b94420d9a2
child 4189 258f98b17f57
equal deleted inserted replaced
4187:50b94420d9a2 4188:c817518c79cc
  1042 
  1042 
  1043 			ps->unk16 |= 1;
  1043 			ps->unk16 |= 1;
  1044 
  1044 
  1045 			while (*++psd2 != NULL) {
  1045 			while (*++psd2 != NULL) {
  1046 				ParentSpriteToDraw* ps2 = *psd2;
  1046 				ParentSpriteToDraw* ps2 = *psd2;
  1047 				bool mustswap = false;
  1047 				ParentSpriteToDraw** psd3;
  1048 
  1048 
  1049 				if (ps2->unk16 & 1) continue;
  1049 				if (ps2->unk16 & 1) continue;
  1050 
  1050 
  1051 				/* Decide which comparator to use, based on whether the bounding
  1051 				/* Decide which comparator to use, based on whether the bounding
  1052 				 * boxes overlap
  1052 				 * boxes overlap
  1053 				 */
  1053 				 */
  1054 				if (ps->xmax > ps2->xmin && ps->xmin < ps2->xmax && // overlap in X?
  1054 				if (ps->xmax > ps2->xmin && ps->xmin < ps2->xmax && // overlap in X?
  1055 						ps->ymax > ps2->ymin && ps->ymin < ps2->ymax && // overlap in Y?
  1055 						ps->ymax > ps2->ymin && ps->ymin < ps2->ymax && // overlap in Y?
  1056 						ps->zmax > ps2->zmin && ps->zmin < ps2->zmax) { // overlap in Z?
  1056 						ps->zmax > ps2->zmin && ps->zmin < ps2->zmax) { // overlap in Z?
  1057 					// Use X+Y+Z as the sorting order, so sprites nearer the bottom of the screen,
  1057 					/* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
  1058 					// and with higher Z elevation, draw in front.
  1058 					 * the screen and with higher Z elevation, are drawn in front.
  1059 					// Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
  1059 					 * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
  1060 					// i.e. X=(left+right)/2, etc.
  1060 					 * i.e. X=(left+right)/2, etc.
  1061 					// However, since we only care about order, don't actually calculate the division / 2.
  1061 					 * However, since we only care about order, don't actually divide / 2
  1062 					mustswap =
  1062 					 */
  1063 						ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax >
  1063 					if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
  1064 						ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax;
  1064 							ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
       
  1065 						continue;
       
  1066 					}
  1065 				} else {
  1067 				} else {
  1066 					// No overlap; use the original TTD sort algorithm.
  1068 					if (ps->xmax < ps2->xmin ||
  1067 					mustswap =
  1069 							ps->ymax < ps2->ymin ||
  1068 						ps->xmax >= ps2->xmin &&
  1070 							ps->zmax < ps2->zmin || (
  1069 						ps->ymax >= ps2->ymin &&
  1071 								ps->xmin < ps2->xmax &&
  1070 						ps->zmax >= ps2->zmin && (
  1072 								ps->ymin < ps2->ymax &&
  1071 							ps->xmin >= ps2->xmax ||
  1073 								ps->zmin < ps2->zmax
  1072 							ps->ymin >= ps2->ymax ||
  1074 							)) {
  1073 							ps->zmin >= ps2->zmax
  1075 						continue;
  1074 						);
  1076 					}
  1075 				}
  1077 				}
  1076 				if (mustswap) {
  1078 
  1077 					// Swap the two sprites ps and ps2 using bubble-sort algorithm.
  1079 				// Swap the two sprites ps and ps2 using bubble-sort algorithm.
  1078 					ParentSpriteToDraw** psd3 = psd;
  1080 				psd3 = psd;
  1079 
  1081 				do {
  1080 					do {
  1082 					ParentSpriteToDraw* temp = *psd3;
  1081 						ParentSpriteToDraw* temp = *psd3;
  1083 					*psd3 = ps2;
  1082 						*psd3 = ps2;
  1084 					ps2 = temp;
  1083 						ps2 = temp;
  1085 
  1084 
  1086 					psd3++;
  1085 						psd3++;
  1087 				} while (psd3 <= psd2);
  1086 					} while (psd3 <= psd2);
       
  1087 				}
       
  1088 			}
  1088 			}
  1089 		} else {
  1089 		} else {
  1090 			psd++;
  1090 			psd++;
  1091 		}
  1091 		}
  1092 	}
  1092 	}