]> Pileus Git - aweather/blob - src/plugins/ridge.c
Add desktop files and pixmaps
[aweather] / src / plugins / ridge.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 <stdio.h>
20 #include <gtk/gtk.h>
21 #include <GL/gl.h>
22
23 #include <gis/gis.h>
24
25 #include "ridge.h"
26
27 /****************
28  * GObject code *
29  ****************/
30 /* Plugin init */
31 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface);
32 static void gis_plugin_ridge_expose(GisPlugin *_self);
33 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self);
34 G_DEFINE_TYPE_WITH_CODE(GisPluginRidge, gis_plugin_ridge, G_TYPE_OBJECT,
35                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
36                         gis_plugin_ridge_plugin_init));
37 static void gis_plugin_ridge_plugin_init(GisPluginInterface *iface)
38 {
39         g_debug("GisPluginRidge: plugin_init");
40         /* Add methods to the interface */
41         iface->expose     = gis_plugin_ridge_expose;
42         iface->get_config = gis_plugin_ridge_get_config;
43 }
44 /* Class/Object init */
45 static void gis_plugin_ridge_init(GisPluginRidge *self)
46 {
47         g_debug("GisPluginRidge: init");
48         /* Set defaults */
49 }
50 static void gis_plugin_ridge_dispose(GObject *gobject)
51 {
52         g_debug("GisPluginRidge: dispose");
53         GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
54         /* Drop references */
55         G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->dispose(gobject);
56 }
57 static void gis_plugin_ridge_finalize(GObject *gobject)
58 {
59         g_debug("GisPluginRidge: finalize");
60         GisPluginRidge *self = GIS_PLUGIN_RIDGE(gobject);
61         /* Free data */
62         G_OBJECT_CLASS(gis_plugin_ridge_parent_class)->finalize(gobject);
63
64 }
65 static void gis_plugin_ridge_class_init(GisPluginRidgeClass *klass)
66 {
67         g_debug("GisPluginRidge: class_init");
68         GObjectClass *gobject_class = (GObjectClass*)klass;
69         gobject_class->dispose  = gis_plugin_ridge_dispose;
70         gobject_class->finalize = gis_plugin_ridge_finalize;
71 }
72
73 /*********************
74  * Overlay constants *
75  *********************/
76 enum {
77         LAYER_TOPO,
78         LAYER_COUNTY,
79         LAYER_RIVERS,
80         LAYER_HIGHWAYS,
81         LAYER_CITY,
82         LAYER_COUNT
83 };
84
85 typedef struct {
86         gchar *name;
87         gchar *fmt;
88         gboolean enabled;
89         float z;
90         guint tex;
91 } layer_t;
92
93 static layer_t layers[] = {
94         [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
95         [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
96         [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
97         [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
98         [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
99 };
100
101
102 /***********
103  * Helpers *
104  ***********/
105 /*
106  * Load an image into an OpenGL texture
107  * \param  filename  Path to the image file
108  * \return The OpenGL identifier for the texture
109  */
110 void load_texture(GisPluginRidge *self, layer_t *layer, gchar *filename)
111 {
112         gis_opengl_begin(self->opengl);
113
114         /* Load image */
115         GError *error = NULL;
116         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
117         if (!pixbuf || error) {
118                 g_warning("Failed to load texture: %s", filename);
119                 return;
120         }
121         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
122         int        width  = gdk_pixbuf_get_width(pixbuf);
123         int        height = gdk_pixbuf_get_height(pixbuf);
124         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
125
126         /* Create Texture */
127         glDeleteTextures(1, &layer->tex);
128         glGenTextures(1, &layer->tex);
129         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
130
131         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
132         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
133                         format, GL_UNSIGNED_BYTE, pixels);
134         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
135         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
136
137         char *base = g_path_get_basename(filename);
138         g_debug("GisPluginRidge: load_texture - "
139                 "w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
140                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
141                 base);
142         g_free(base);
143
144         gis_opengl_end(self->opengl);
145
146         g_object_unref(pixbuf);
147
148         /* Redraw */
149         gis_opengl_redraw(self->opengl);
150 }
151
152
153 /*****************
154  * ASync helpers *
155  *****************/
156 typedef struct {
157         GisPluginRidge *self;
158         layer_t *layer;
159 } cached_t;
160 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
161 {
162         cached_t *udata = _udata;
163         load_texture(udata->self, udata->layer, filename);
164         g_free(udata);
165 }
166
167 /*************
168  * callbacks *
169  *************/
170 static void on_site_changed(GisView *view, gchar *site, GisPluginRidge *self)
171 {
172         g_debug("GisPluginRidge: on_site_changed - site=%s", site);
173         if (site == NULL || site[0] == '\0')
174                 return;
175         for (int i = 0; i < LAYER_COUNT; i++) {
176                 gchar *base = "http://radar.weather.gov/ridge/";
177                 gchar *path  = g_strdup_printf(layers[i].fmt, site+1);
178                 cached_t *udata = g_malloc(sizeof(cached_t));
179                 udata->self  = self;
180                 udata->layer = &layers[i];
181                 cache_file(base, path, GIS_ONCE, NULL, cached_cb, udata);
182                 g_free(path);
183         }
184 }
185
186 void toggle_layer(GtkToggleButton *check, GisPluginRidge *self)
187 {
188         layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
189         layer->enabled = gtk_toggle_button_get_active(check);
190         gis_opengl_redraw(self->opengl);
191 }
192
193 /***********
194  * Methods *
195  ***********/
196 GisPluginRidge *gis_plugin_ridge_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
197 {
198         GisPluginRidge *self = g_object_new(GIS_TYPE_PLUGIN_RIDGE, NULL);
199         self->world  = world;
200         self->view   = view;
201         self->opengl = opengl;
202
203         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
204         on_site_changed(view, gis_view_get_site(view), self);
205
206         return self;
207 }
208
209 static GtkWidget *gis_plugin_ridge_get_config(GisPlugin *_self)
210 {
211         GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
212         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
213         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
214         for (int i = 0; i < LAYER_COUNT; i++) {
215                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
216                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
217                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
218                 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
219                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), self);
220         }
221         gtk_container_add(GTK_CONTAINER(body), hbox);
222         return body;
223 }
224
225 static void gis_plugin_ridge_expose(GisPlugin *_self)
226 {
227         GisPluginRidge *self = GIS_PLUGIN_RIDGE(_self);
228
229         g_debug("GisPluginRidge: expose");
230         glPushMatrix();
231         glEnable(GL_TEXTURE_2D);
232         glColor4f(1,1,1,1);
233
234         for (int i = 0; i < LAYER_COUNT; i++) {
235                 if (!layers[i].enabled)
236                         continue;
237                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
238                 glBegin(GL_POLYGON);
239                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
240                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
241                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
242                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
243                 glEnd();
244         }
245
246         glPopMatrix();
247 }