Changeset - r24571:7c34ed3bbfcb
[Not reviewed]
master
0 2 0
Matt Kimber - 3 years ago 2021-01-03 14:02:53
github@mattkimber.org.uk
Codechange: consider vehicle co-ordinates when identifying viewport candidate as using only the hash generates false positives
2 files changed with 19 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/vehicle.cpp
Show inline comments
 
@@ -1124,11 +1124,15 @@ void ViewportAddVehicles(DrawPixelInfo *
 
	const int t = dpi->top;
 
	const int b = dpi->top + dpi->height;
 

	
 
	/* Border size of MAX_VEHICLE_PIXEL_xy */
 
	const int xb = MAX_VEHICLE_PIXEL_X * ZOOM_LVL_BASE;
 
	const int yb = MAX_VEHICLE_PIXEL_Y * ZOOM_LVL_BASE;
 

	
 
	/* The hash area to scan */
 
	int xl, xu, yl, yu;
 

	
 
	if (dpi->width + (MAX_VEHICLE_PIXEL_X * ZOOM_LVL_BASE) < GEN_HASHX_SIZE) {
 
		xl = GEN_HASHX(l - MAX_VEHICLE_PIXEL_X * ZOOM_LVL_BASE);
 
	if (dpi->width + xb < GEN_HASHX_SIZE) {
 
		xl = GEN_HASHX(l - xb);
 
		xu = GEN_HASHX(r);
 
	} else {
 
		/* scan whole hash row */
 
@@ -1136,8 +1140,8 @@ void ViewportAddVehicles(DrawPixelInfo *
 
		xu = GEN_HASHX_MASK;
 
	}
 

	
 
	if (dpi->height + (MAX_VEHICLE_PIXEL_Y * ZOOM_LVL_BASE) < GEN_HASHY_SIZE) {
 
		yl = GEN_HASHY(t - MAX_VEHICLE_PIXEL_Y * ZOOM_LVL_BASE);
 
	if (dpi->height + yb < GEN_HASHY_SIZE) {
 
		yl = GEN_HASHY(t - yb);
 
		yu = GEN_HASHY(b);
 
	} else {
 
		/* scan whole column */
 
@@ -1158,9 +1162,14 @@ void ViewportAddVehicles(DrawPixelInfo *
 
						b >= v->coord.top) {
 
					DoDrawVehicle(v);
 
				}
 
				else {
 
				else if (l <= v->coord.right + xb &&
 
					t <= v->coord.bottom + yb &&
 
					r >= v->coord.left - xb &&
 
					b >= v->coord.top - yb)
 
				{
 
					/*
 
					 * Indicate that this vehicle was considered for rendering in a viewport,
 
					 * is within the bounds where a sprite could be valid for rendering
 
					 * and we therefore need to update sprites more frequently in case a callback
 
					 * will change the bounding box to one which will cause the sprite to be
 
					 * displayed.
 
@@ -1168,9 +1177,6 @@ void ViewportAddVehicles(DrawPixelInfo *
 
					 * This reduces the chances of flicker when sprites enter the screen, if they
 
					 * are part of a newgrf vehicle set which changes bounding boxes within a
 
					 * single vehicle direction.
 
					 *
 
					 * TODO: this will consider too many false positives, use the bounding box
 
					 * information or something which better narrows down the candidates.
 
					 */
 
					v->sprite_cache.is_viewport_candidate = true;
 
				}
src/vehicle_base.h
Show inline comments
 
@@ -186,10 +186,10 @@ struct VehicleSpriteSeq {
 
 * or calculating the viewport.
 
 */
 
struct MutableSpriteCache {
 
	Direction last_direction;                 ///< Last direction we obtained sprites for
 
	mutable bool is_viewport_candidate;       ///< The vehicle has been in the hash for a shown viewport recently
 
	mutable bool sprite_has_viewport_changes; ///< There have been viewport changes since the sprite was last updated
 
	mutable VehicleSpriteSeq sprite_seq;      ///< Vehicle appearance.
 
	Direction last_direction;         ///< Last direction we obtained sprites for
 
	bool is_viewport_candidate;       ///< The vehicle has been in the hash for a shown viewport recently
 
	bool sprite_has_viewport_changes; ///< There have been viewport changes since the sprite was last updated
 
	VehicleSpriteSeq sprite_seq;      ///< Vehicle appearance.
 
};
 

	
 
/** A vehicle pool for a little over 1 million vehicles. */
 
@@ -337,7 +337,7 @@ public:
 
	NewGRFCache grf_cache;              ///< Cache of often used calculated NewGRF values
 
	VehicleCache vcache;                ///< Cache of often used vehicle values.
 

	
 
	MutableSpriteCache sprite_cache;    ///< Cache of sprites and values related to recalculating them, see #MutableSpriteCache
 
	mutable MutableSpriteCache sprite_cache; ///< Cache of sprites and values related to recalculating them, see #MutableSpriteCache
 

	
 
	Vehicle(VehicleType type = VEH_INVALID);
 

	
0 comments (0 inline, 0 general)