]> Pileus Git - grits/blob - src/objects/grits-tile.h
Move low level tile loading to GritsTile
[grits] / src / objects / grits-tile.h
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 #ifndef __GRITS_TILE_H__
19 #define __GRITS_TILE_H__
20
21 #include <glib.h>
22 #include <glib-object.h>
23 #include "grits-object.h"
24
25 #define GRITS_TYPE_TILE            (grits_tile_get_type())
26 #define GRITS_TILE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),   GRITS_TYPE_TILE, GritsTile))
27 #define GRITS_IS_TILE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),   GRITS_TYPE_TILE))
28 #define GRITS_TILE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST   ((klass), GRITS_TYPE_TILE, GritsTileClass))
29 #define GRITS_IS_TILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE   ((klass), GRITS_TYPE_TILE))
30 #define GRITS_TILE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),   GRITS_TYPE_TILE, GritsTileClass))
31
32 typedef struct _GritsTile      GritsTile;
33 typedef struct _GritsTileClass GritsTileClass;
34
35 struct _GritsTile {
36         GritsObject  parent_instance;
37
38         /* Pointer to the tile data */
39         gpointer data;
40         gboolean load;
41
42         /* Drawing order */
43         gint zindex;
44
45         /* North,South,East,West limits */
46         GritsBounds edge;
47
48         /* Texture mapping coordinates */
49         GritsBounds coords;
50
51         /* Pointers to parent/child nodes */
52         GritsTile *parent;
53         GritsTile *children[2][2];
54
55         /* Last access time (for garbage collection) */
56         time_t atime;
57
58         /* Projection used by tile data */
59         GritsProj proj;
60
61         /* Internal data to the tile */
62         guint      tex;
63         GdkPixbuf *pixbuf;
64         guchar    *pixels;
65         gint       width;
66         gint       height;
67         gint       alpha;
68 };
69
70 struct _GritsTileClass {
71         GritsObjectClass parent_class;
72 };
73
74 /**
75  * GritsTileLoadFunc:
76  * @tile:      the tile to load
77  * @user_data: data paseed to the function
78  *
79  * Used to load the image data associated with a tile. For GritsOpenGL, this
80  * function should store the OpenGL texture number in the tiles data field.
81  */
82 typedef void (*GritsTileLoadFunc)(GritsTile *tile, gpointer user_data);
83
84 /**
85  * GritsTileFreeFunc:
86  * @tile:      the tile to free
87  * @user_data: data paseed to the function
88  *
89  * Used to free the image data associated with a tile
90  */
91 typedef void (*GritsTileFreeFunc)(GritsTile *tile, gpointer user_data);
92
93 /* Forech functions */
94 /**
95  * grits_tile_foreach:
96  * @parent: the #GritsTile to iterate over
97  * @child:  a pointer to a #GritsTile to store the current subtile 
98  *
99  * Iterate over each imediate subtile of @parent. 
100  */
101 #define grits_tile_foreach(parent, child) \
102         for (int _x = 0; _x < G_N_ELEMENTS(parent->children); _x++) \
103         for (int _y = 0; child = parent->children[_x][_y], \
104                 _y < G_N_ELEMENTS(parent->children[_x]); _y++)
105
106 /**
107  * grits_tile_foreach_index:
108  * @parent: the #GritsTile to iterate over
109  * @x:      integer to store the x index of the current subtile
110  * @y:      integer to store the y index of the current subtile
111  *
112  * Iterate over each imediate subtile of @parent. 
113  */
114 #define grits_tile_foreach_index(parent, x, y) \
115         for (x = 0; x < G_N_ELEMENTS(parent->children); x++) \
116         for (y = 0; y < G_N_ELEMENTS(parent->children[x]); y++)
117
118 /* Path to string table, keep in sync with tile->children */
119 extern gchar *grits_tile_path_table[2][2];
120
121 GType grits_tile_get_type(void);
122
123 /* Allocate a new Tile */
124 GritsTile *grits_tile_new(GritsTile *parent,
125         gdouble n, gdouble s, gdouble e, gdouble w);
126
127 /* Return a string representation of the tile's path */
128 gchar *grits_tile_get_path(GritsTile *child);
129
130 /* Update a root tile */
131 /* Based on eye distance */
132 void grits_tile_update(GritsTile *root, GritsPoint *eye,
133                 gdouble res, gint width, gint height,
134                 GritsTileLoadFunc load_func, gpointer user_data);
135
136 /* Load tile data from pixel buffer */
137 gboolean grits_tile_load_pixels(GritsTile *tile, guchar *pixels,
138                 gint width, gint height, gint channels);
139
140 /* Load tile data from a GdkPixbuf */
141 gboolean grits_tile_load_pixbuf(GritsTile *tile, GdkPixbuf *pixbuf);
142
143 /* Load tile data from an image file */
144 gboolean grits_tile_load_file(GritsTile *tile, const gchar *file);
145
146 /* Find the leaf tile containing lat-lon */
147 GritsTile *grits_tile_find(GritsTile *root, gdouble lat, gdouble lon);
148
149 /* Delete nodes that haven't been accessed since atime */
150 GritsTile *grits_tile_gc(GritsTile *root, time_t atime,
151                 GritsTileFreeFunc free_func, gpointer user_data);
152
153 /* Free a tile and all it's children */
154 void grits_tile_free(GritsTile *root,
155                 GritsTileFreeFunc free_func, gpointer user_data);
156
157 #endif