Verified Commit 59815d08 authored by James Westman's avatar James Westman
Browse files

map-layer: Properly calculate grid offset

Due to unsigned integer overflows, the grid calculation logic only
worked for tile sizes that were multiples of 2.
parent 8908d636
......@@ -100,15 +100,13 @@ modadd (int current,
int shift,
int size)
{
/* This is scary, but the idea behind it is simple: the regular modulo operator
* does *not* wrap around when giving it negative numbers. For example, -1 % 8
* yields -1 instead of 7.
*
* The following pair of lines do exactly that, on top of adding a number. This
* is so that we can do e.g. (0 + -1) % 8 = 7.
/* Adds two numbers, and gives the least positive result modulo @size (as
* opposed to the C '%' operator, which can give negative results).
* For example, we can have (0 + -1) % 8 = 7.
*/
current = current % size;
return (size + current + shift) % size;
int r = (current + shift) % size;
return r < 0 ? r + size : r;
}
static void
......@@ -491,7 +489,7 @@ shumate_map_layer_size_allocate (GtkWidget *widget,
ShumateViewport *viewport;
GtkAllocation child_allocation;
guint tile_size;
guint zoom_level;
double zoom_level;
double latitude, longitude;
guint longitude_x, latitude_y;
int x_offset, y_offset;
......@@ -504,17 +502,17 @@ shumate_map_layer_size_allocate (GtkWidget *widget,
zoom_level = shumate_viewport_get_zoom_level (viewport);
latitude = shumate_location_get_latitude (SHUMATE_LOCATION (viewport));
longitude = shumate_location_get_longitude (SHUMATE_LOCATION (viewport));
latitude_y = (guint) shumate_map_source_get_y (self->map_source, zoom_level, latitude);
longitude_x = (guint) shumate_map_source_get_x (self->map_source, zoom_level, longitude);
source_rows = shumate_map_source_get_row_count (self->map_source, zoom_level);
source_columns = shumate_map_source_get_column_count (self->map_source, zoom_level);
latitude_y = modadd (shumate_map_source_get_y (self->map_source, zoom_level, latitude), -height/2, tile_size * source_rows);
longitude_x = modadd (shumate_map_source_get_x (self->map_source, zoom_level, longitude), -width/2, tile_size * source_columns);
// This is the (column,row) of the top left ShumateTile
tile_initial_row = (latitude_y - height/2)/tile_size;
tile_initial_column = (longitude_x - width/2)/tile_size;
tile_initial_row = latitude_y / tile_size;
tile_initial_column = longitude_x / tile_size;
x_offset = (longitude_x - tile_initial_column * tile_size) - width/2;
y_offset = (latitude_y - tile_initial_row * tile_size) - height/2;
x_offset = longitude_x % tile_size;
y_offset = latitude_y % tile_size;
child_allocation.y = -y_offset;
child_allocation.width = tile_size;
child_allocation.height = tile_size;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment