Refactor track_design_draw_preview

This commit is contained in:
Ted John 2016-09-04 12:16:56 +01:00
parent 8bc69b549f
commit 9c645e0a0f
3 changed files with 52 additions and 58 deletions

View File

@ -1687,11 +1687,11 @@ void track_design_draw_preview(rct_track_td6 *td6, uint8 *pixels)
rct_viewport* view = RCT2_ADDRESS(0x9D8161, rct_viewport);
rct_drawpixelinfo* dpi = RCT2_ADDRESS(0x9D8151, rct_drawpixelinfo);
int left, top, right, bottom;
int center_x = (gTrackPreviewMin.x + gTrackPreviewMax.x) / 2 + 16;
int center_y = (gTrackPreviewMin.y + gTrackPreviewMax.y) / 2 + 16;
int center_z = (gTrackPreviewMin.z + gTrackPreviewMax.z) / 2;
rct_xyz32 centre;
centre.x = (gTrackPreviewMin.x + gTrackPreviewMax.x) / 2 + 16;
centre.y = (gTrackPreviewMin.y + gTrackPreviewMax.y) / 2 + 16;
centre.z = (gTrackPreviewMin.z + gTrackPreviewMax.z) / 2;
int width = gTrackPreviewMax.x - gTrackPreviewMin.x;
int height = gTrackPreviewMax.y - gTrackPreviewMin.y;
@ -1710,19 +1710,12 @@ void track_design_draw_preview(rct_track_td6 *td6, uint8 *pixels)
width = 370 << zoom_level;
height = 217 << zoom_level;
int x = center_y - center_x - width / 2;
int y = (center_y + center_x) / 2 - center_z - height / 2;
gCurrentRotation = 0;
view->width = 370;
view->height = 217;
view->view_width = width;
view->view_height = height;
view->x = 0;
view->y = 0;
view->view_x = x;
view->view_y = y;
view->zoom = zoom_level;
view->flags = VIEWPORT_FLAG_HIDE_BASE | VIEWPORT_FLAG_INVISIBLE_SPRITES;
@ -1734,57 +1727,25 @@ void track_design_draw_preview(rct_track_td6 *td6, uint8 *pixels)
dpi->pitch = 0;
dpi->bits = pixels;
top = y;
left = x;
bottom = y + height;
right = x + width;
rct_xy32 offset = { width / 2, height / 2 };
for (int i = 0; i < 4; i++) {
gCurrentRotation = i;
viewport_paint(view, dpi, left, top, right, bottom);
rct_xy32 pos2d = translate_3d_to_2d_with_z(i, centre);
pos2d.x -= offset.x;
pos2d.y -= offset.y;
dpi->bits += TRACK_PREVIEW_IMAGE_SIZE;
sint32 left = pos2d.x;
sint32 top = pos2d.y;
sint32 right = left + width;
sint32 bottom = top + height;
gCurrentRotation = 1;
x = -center_y - center_x - width / 2;
y = (center_y - center_x) / 2 - center_z - height / 2;
view->view_x = left;
view->view_y = top;
viewport_paint(view, dpi, left, top, right, bottom);
view->view_x = x;
view->view_y = y;
top = y;
left = x;
bottom = y + height;
right = x + width;
viewport_paint(view, dpi, left, top, right, bottom);
dpi->bits += TRACK_PREVIEW_IMAGE_SIZE;
gCurrentRotation = 2;
x = center_x - center_y - width / 2;
y = (-center_y - center_x) / 2 - center_z - height / 2;
view->view_x = x;
view->view_y = y;
top = y;
left = x;
bottom = y + height;
right = x + width;
viewport_paint(view, dpi, left, top, right, bottom);
dpi->bits += TRACK_PREVIEW_IMAGE_SIZE;
gCurrentRotation = 3;
x = center_x + center_y - width / 2;
y = (center_x - center_y) / 2 - center_z - height / 2;
view->view_x = x;
view->view_y = y;
top = y;
left = x;
bottom = y + height;
right = x + width;
viewport_paint(view, dpi, left, top, right, bottom);
dpi->bits += TRACK_PREVIEW_IMAGE_SIZE;
}
ride_delete(rideIndex);
track_design_preview_restore_map(mapBackup);

View File

@ -4905,6 +4905,30 @@ static void translate_3d_to_2d(int rotation, int *x, int *y)
*y = ry;
}
rct_xy32 translate_3d_to_2d_with_z(sint32 rotation, rct_xyz32 pos)
{
rct_xy32 result;
switch (rotation & 3) {
case 0:
result.x = pos.y - pos.x;
result.y = (pos.x + pos.y) / 2 - pos.z;
break;
case 1:
result.x = -pos.x - pos.y;
result.y = (pos.y - pos.x) / 2 - pos.z;
break;
case 2:
result.x = pos.x - pos.y;
result.y = (-pos.x - pos.y) / 2 - pos.z;
break;
case 3:
result.x = pos.x + pos.y;
result.y = (pos.x - pos.y) / 2 - pos.z;
break;
}
return result;
}
static void map_invalidate_tile_under_zoom(int x, int y, int z0, int z1, int maxZoom)
{
if (gOpenRCT2Headless) return;

View File

@ -275,6 +275,14 @@ typedef struct rct_xyz16 {
} rct_xyz16;
assert_struct_size(rct_xyz16, 6);
typedef struct rct_xy32 {
sint32 x, y;
} rct_xy32;
typedef struct rct_xyz32 {
sint32 x, y, z;
} rct_xyz32;
typedef struct rct_xy_element {
int x, y;
rct_map_element *element;
@ -492,6 +500,7 @@ bool map_large_scenery_get_origin(
);
void map_offset_with_rotation(sint16 *x, sint16 *y, sint16 offsetX, sint16 offsetY, uint8 rotation);
rct_xy32 translate_3d_to_2d_with_z(sint32 rotation, rct_xyz32 pos);
rct_map_element *map_get_track_element_at(int x, int y, int z);
rct_map_element *map_get_track_element_at_of_type(int x, int y, int z, int trackType);