src/viewport.cpp
changeset 7565 a2948dd92c0e
parent 7552 064d734e9900
child 7574 0d76e3392fa4
equal deleted inserted replaced
7564:2c73e371df89 7565:a2948dd92c0e
  1359 
  1359 
  1360 	dpi->left -= w->left;
  1360 	dpi->left -= w->left;
  1361 	dpi->top -= w->top;
  1361 	dpi->top -= w->top;
  1362 }
  1362 }
  1363 
  1363 
       
  1364 static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
       
  1365 {
       
  1366 	/* Centre of the viewport is hot spot */
       
  1367 	x += vp->virtual_width / 2;
       
  1368 	y += vp->virtual_height / 2;
       
  1369 
       
  1370 	/* Convert viewport coordinates to map coordinates
       
  1371 	 * Calculation is scaled by 4 to avoid rounding errors */
       
  1372 	int vx = -x + y * 2;
       
  1373 	int vy =  x + y * 2;
       
  1374 
       
  1375 	/* clamp to size of map */
       
  1376 	vx = clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
       
  1377 	vy = clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
       
  1378 
       
  1379 	/* Convert map coordinates to viewport coordinates */
       
  1380 	x = (-vx + vy) / 2;
       
  1381 	y = ( vx + vy) / 4;
       
  1382 
       
  1383 	/* Remove centreing */
       
  1384 	x -= vp->virtual_width / 2;
       
  1385 	y -= vp->virtual_height / 2;
       
  1386 }
       
  1387 
  1364 void UpdateViewportPosition(Window *w)
  1388 void UpdateViewportPosition(Window *w)
  1365 {
  1389 {
  1366 	const ViewPort *vp = w->viewport;
  1390 	const ViewPort *vp = w->viewport;
  1367 
  1391 
  1368 	if (WP(w, vp_d).follow_vehicle != INVALID_VEHICLE) {
  1392 	if (WP(w, vp_d).follow_vehicle != INVALID_VEHICLE) {
  1369 		const Vehicle* veh = GetVehicle(WP(w,vp_d).follow_vehicle);
  1393 		const Vehicle* veh = GetVehicle(WP(w,vp_d).follow_vehicle);
  1370 		Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
  1394 		Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
  1371 
  1395 
  1372 		SetViewportPosition(w, pt.x, pt.y);
  1396 		SetViewportPosition(w, pt.x, pt.y);
  1373 	} else {
  1397 	} else {
  1374 		int x;
  1398 		/* Ensure the destination location is within the map */
  1375 		int y;
  1399 		ClampViewportToMap(vp, WP(w, vp_d).dest_scrollpos_x, WP(w, vp_d).dest_scrollpos_y);
  1376 		int vx;
  1400 
  1377 		int vy;
  1401 		int delta_x = WP(w, vp_d).dest_scrollpos_x - WP(w, vp_d).scrollpos_x;
  1378 
  1402 		int delta_y = WP(w, vp_d).dest_scrollpos_y - WP(w, vp_d).scrollpos_y;
  1379 		/* Center of the viewport is hot spot */
       
  1380 		x = WP(w,vp_d).scrollpos_x + vp->virtual_width / 2;
       
  1381 		y = WP(w,vp_d).scrollpos_y + vp->virtual_height / 2;
       
  1382 
       
  1383 		int dest_x = WP(w,vp_d).dest_scrollpos_x + vp->virtual_width / 2;
       
  1384 		int dest_y = WP(w,vp_d).dest_scrollpos_y + vp->virtual_height / 2;
       
  1385 
       
  1386 		int delta_x = dest_x - x;
       
  1387 		int delta_y = dest_y - y;
       
  1388 
  1403 
  1389 		if (delta_x != 0 || delta_y != 0) {
  1404 		if (delta_x != 0 || delta_y != 0) {
  1390 			if (_patches.smooth_scroll) {
  1405 			if (_patches.smooth_scroll) {
  1391 				int max_scroll = ScaleByMapSize1D(512);
  1406 				int max_scroll = ScaleByMapSize1D(512);
  1392 				/* Not at our desired positon yet... */
  1407 				/* Not at our desired positon yet... */
  1393 				x += clamp(delta_x / 8, -max_scroll, max_scroll);
  1408 				WP(w, vp_d).scrollpos_x += clamp(delta_x / 4, -max_scroll, max_scroll);
  1394 				y += clamp(delta_y / 8, -max_scroll, max_scroll);
  1409 				WP(w, vp_d).scrollpos_y += clamp(delta_y / 4, -max_scroll, max_scroll);
  1395 			} else {
  1410 			} else {
  1396 				x = dest_x;
  1411 				WP(w, vp_d).scrollpos_x = WP(w, vp_d).dest_scrollpos_x;
  1397 				y = dest_y;
  1412 				WP(w, vp_d).scrollpos_y = WP(w, vp_d).dest_scrollpos_y;
  1398 			}
  1413 			}
  1399 		}
  1414 		}
  1400 
  1415 
  1401 		/* Convert viewport coordinates to map coordinates
  1416 		ClampViewportToMap(vp, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y);
  1402 		 * Calculation is scaled by 4 to avoid rounding errors */
       
  1403 		vx = -x + y * 2;
       
  1404 		vy =  x + y * 2;
       
  1405 		/* clamp to size of map */
       
  1406 		vx = clamp(vx, 0 * 4, MapMaxX() * TILE_SIZE * 4);
       
  1407 		vy = clamp(vy, 0 * 4, MapMaxY() * TILE_SIZE * 4);
       
  1408 		/* Convert map coordinates to viewport coordinates */
       
  1409 		x = (-vx + vy) / 2;
       
  1410 		y = ( vx + vy) / 4;
       
  1411 		/* Set position */
       
  1412 		WP(w, vp_d).scrollpos_x = x - vp->virtual_width / 2;
       
  1413 		WP(w, vp_d).scrollpos_y = y - vp->virtual_height / 2;
       
  1414 
       
  1415 		SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y);
  1417 		SetViewportPosition(w, WP(w, vp_d).scrollpos_x, WP(w, vp_d).scrollpos_y);
  1416 	}
  1418 	}
  1417 }
  1419 }
  1418 
  1420 
  1419 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
  1421 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)