]> Pileus Git - grits/blob - src/plugins/bmng.c
Convert for expose callback to callback objects
[grits] / src / plugins / bmng.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 <time.h>
19 #include <GL/gl.h>
20
21 #include <gis.h>
22
23 #include "bmng.h"
24
25 #define MAX_RESOLUTION 500
26 #define TILE_WIDTH     1024
27 #define TILE_HEIGHT    512
28
29 struct _LoadTileData {
30         GisPluginBmng *self;
31         GisTile       *tile;
32         GdkPixbuf     *pixbuf;
33 };
34 static gboolean _load_tile_cb(gpointer _data)
35 {
36         struct _LoadTileData *data = _data;
37         GisPluginBmng *self   = data->self;
38         GisTile       *tile   = data->tile;
39         GdkPixbuf     *pixbuf = data->pixbuf;
40         g_free(data);
41
42         /* Create Texture */
43         g_debug("GisPluginBmng: _load_tile_cb start");
44         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
45         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
46         gint      width  = gdk_pixbuf_get_width(pixbuf);
47         gint      height = gdk_pixbuf_get_height(pixbuf);
48
49         guint *tex = g_new0(guint, 1);
50         gis_viewer_begin(self->viewer);
51         glGenTextures(1, tex);
52         glBindTexture(GL_TEXTURE_2D, *tex);
53
54         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
55         glPixelStorei(GL_PACK_ALIGNMENT, 1);
56         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
57                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
58         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
59         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
60         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
61         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
62         glFlush();
63         gis_viewer_end(self->viewer);
64
65         tile->data = tex;
66         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
67         g_object_unref(pixbuf);
68         return FALSE;
69 }
70
71 static void _load_tile(GisTile *tile, gpointer _self)
72 {
73         GisPluginBmng *self = _self;
74         g_debug("GisPluginBmng: _load_tile start %p", g_thread_self());
75         char *path = gis_wms_make_local(self->wms, tile);
76         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
77         data->self   = self;
78         data->tile   = tile;
79         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
80         if (!data->pixbuf)
81                 g_warning("GisPluginBmng: _load_tile - Error loading pixbuf %s", path);
82         g_free(path);
83         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
84         g_debug("GisPluginBmng: _load_tile end %p", g_thread_self());
85 }
86
87 static gboolean _free_tile_cb(gpointer data)
88 {
89         glDeleteTextures(1, data);
90         g_free(data);
91         return FALSE;
92 }
93 static void _free_tile(GisTile *tile, gpointer _self)
94 {
95         GisPluginBmng *self = _self;
96         g_debug("GisPluginBmng: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
97         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
98 }
99
100 static gpointer _update_tiles(gpointer _self)
101 {
102         g_debug("GisPluginBmng: _update_tiles");
103         GisPluginBmng *self = _self;
104         g_mutex_lock(self->mutex);
105         gdouble lat, lon, elev;
106         gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
107         gis_tile_update(self->tiles,
108                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
109                         lat, lon, elev,
110                         _load_tile, self);
111         gis_tile_gc(self->tiles, time(NULL)-10,
112                         _free_tile, self);
113         g_mutex_unlock(self->mutex);
114         return NULL;
115 }
116
117 /*************
118  * Callbacks *
119  *************/
120 static void _on_location_changed(GisViewer *viewer,
121                 gdouble lat, gdouble lon, gdouble elev, GisPluginBmng *self)
122 {
123         g_thread_create(_update_tiles, self, FALSE, NULL);
124 }
125
126 static gpointer _expose(GisCallback *callback, gpointer _self)
127 {
128         GisPluginBmng *self = GIS_PLUGIN_BMNG(_self);
129         g_debug("GisPluginBmng: expose viewer=%p tiles=%p,%p",
130                         self->viewer, self->tiles, self->tiles->data);
131         gis_viewer_render_tiles(self->viewer, self->tiles);
132         return NULL;
133 }
134
135 /***********
136  * Methods *
137  ***********/
138 GisPluginBmng *gis_plugin_bmng_new(GisViewer *viewer)
139 {
140         g_debug("GisPluginBmng: new");
141         GisPluginBmng *self = g_object_new(GIS_TYPE_PLUGIN_BMNG, NULL);
142         self->viewer = viewer;
143
144         /* Load initial tiles */
145         _load_tile(self->tiles, self);
146         g_thread_create(_update_tiles, self, FALSE, NULL);
147
148         /* Connect signals */
149         self->sigid = g_signal_connect(self->viewer, "location-changed",
150                         G_CALLBACK(_on_location_changed), self);
151
152         /* Add renderers */
153         GisCallback *callback = gis_callback_new(_expose, self);
154         gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
155
156         return self;
157 }
158
159
160 /****************
161  * GObject code *
162  ****************/
163 /* Plugin init */
164 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface);
165 G_DEFINE_TYPE_WITH_CODE(GisPluginBmng, gis_plugin_bmng, G_TYPE_OBJECT,
166                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
167                         gis_plugin_bmng_plugin_init));
168 static void gis_plugin_bmng_plugin_init(GisPluginInterface *iface)
169 {
170         g_debug("GisPluginBmng: plugin_init");
171         /* Add methods to the interface */
172 }
173 /* Class/Object init */
174 static void gis_plugin_bmng_init(GisPluginBmng *self)
175 {
176         g_debug("GisPluginBmng: init");
177         /* Set defaults */
178         self->mutex = g_mutex_new();
179         self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
180         self->wms   = gis_wms_new(
181                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
182                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
183 }
184 static void gis_plugin_bmng_dispose(GObject *gobject)
185 {
186         g_debug("GisPluginBmng: dispose");
187         GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
188         /* Drop references */
189         g_signal_handler_disconnect(self->viewer, self->sigid);
190         G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->dispose(gobject);
191 }
192 static void gis_plugin_bmng_finalize(GObject *gobject)
193 {
194         g_debug("GisPluginBmng: finalize");
195         GisPluginBmng *self = GIS_PLUGIN_BMNG(gobject);
196         /* Free data */
197         gis_tile_free(self->tiles, _free_tile, self);
198         gis_wms_free(self->wms);
199         g_mutex_free(self->mutex);
200         G_OBJECT_CLASS(gis_plugin_bmng_parent_class)->finalize(gobject);
201
202 }
203 static void gis_plugin_bmng_class_init(GisPluginBmngClass *klass)
204 {
205         g_debug("GisPluginBmng: class_init");
206         GObjectClass *gobject_class = (GObjectClass*)klass;
207         gobject_class->dispose  = gis_plugin_bmng_dispose;
208         gobject_class->finalize = gis_plugin_bmng_finalize;
209 }