]> Pileus Git - grits/blob - src/gis-tile.c
ab45616a0e9b2d6d67b65d6b564908d2facb59b6
[grits] / src / gis-tile.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <config.h>
19 #include <glib.h>
20
21 #include "gis-world.h"
22 #include "gis-tile.h"
23
24 gchar *gis_tile_path_table[2][2] = {
25         {".00", ".01"},
26         {".10", ".11"},
27 };
28
29 GisTile *gis_tile_new(GisTile *parent,
30         gdouble n, gdouble s, gdouble e, gdouble w)
31 {
32         GisTile *self = g_new0(GisTile, 1);
33         self->parent = parent;
34         self->edge.n = n;
35         self->edge.s = s;
36         self->edge.e = e;
37         self->edge.w = w;
38         return self;
39 }
40
41 gchar *gis_tile_get_path(GisTile *child)
42 {
43         /* This could be easily cached if necessasairy */
44         int x, y;
45         GList *parts = NULL;
46         for (GisTile *parent = child->parent; parent; child = parent, parent = child->parent)
47                 gis_tile_foreach_index(child, x, y)
48                         if (parent->children[x][y] == child)
49                                 parts = g_list_prepend(parts, gis_tile_path_table[x][y]);
50         GString *path = g_string_new("");
51         for (; parts; parts = parts->next)
52                 g_string_append(path, parts->data);
53         g_list_free(parts);
54         return g_string_free(path, FALSE);
55 }
56
57
58 void gis_tile_free(GisTile *self, GisTileFreeFunc free_func, gpointer user_data)
59 {
60         if (!self)
61                 return;
62         GisTile *child;
63         gis_tile_foreach(self, child)
64                 gis_tile_free(child, free_func, user_data);
65         if (free_func)
66                 free_func(self, user_data);
67         g_free(self);
68 }