2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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.
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.
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/>.
20 #include <curl/curl.h>
25 #include "aweather-gui.h"
26 #include "plugin-ridge.h"
32 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface);
33 static void aweather_ridge_expose(AWeatherPlugin *_ridge);
34 G_DEFINE_TYPE_WITH_CODE(AWeatherRidge, aweather_ridge, G_TYPE_OBJECT,
35 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
36 aweather_ridge_plugin_init));
37 static void aweather_ridge_class_init(AWeatherRidgeClass *klass)
39 GObjectClass *object_class = (GObjectClass*)klass;
41 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface)
43 /* Add methods to the interface */
44 iface->expose = aweather_ridge_expose;
46 static void aweather_ridge_init(AWeatherRidge *ridge)
52 /*********************
54 *********************/
72 static layer_t layers[] = {
73 [LAYER_TOPO] = {"Topo", "Overlays/Topo/Short/%s_Topo_Short.jpg", TRUE, 1, 0},
74 [LAYER_COUNTY] = {"Counties", "Overlays/County/Short/%s_County_Short.gif", TRUE, 3, 0},
75 [LAYER_RIVERS] = {"Rivers", "Overlays/Rivers/Short/%s_Rivers_Short.gif", FALSE, 4, 0},
76 [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
77 [LAYER_CITY] = {"Cities", "Overlays/Cities/Short/%s_City_Short.gif", TRUE, 6, 0},
85 * Load an image into an OpenGL texture
86 * \param filename Path to the image file
87 * \return The OpenGL identifier for the texture
89 void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
91 aweather_gui_gl_begin(self->gui);
94 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
96 g_warning("Failed to load texture: %s", filename);
97 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
98 int width = gdk_pixbuf_get_width(pixbuf);
99 int height = gdk_pixbuf_get_height(pixbuf);
100 int format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
103 glDeleteTextures(1, &layer->tex);
104 glGenTextures(1, &layer->tex);
105 glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
107 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
109 format, GL_UNSIGNED_BYTE, pixels);
110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113 char *base = g_path_get_basename(filename);
114 g_debug("AWeatherRidge: load_texture - "
115 "w=%-3d h=%-3d fmt=%x px=(%02x,%02x,%02x,%02x) img=%s",
116 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
120 aweather_gui_gl_end(self->gui);
122 g_object_unref(pixbuf);
125 aweather_gui_gl_redraw(self->gui);
136 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
138 cached_t *udata = _udata;
139 load_texture(udata->self, udata->layer, filename);
146 static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
148 g_debug("AWeatherRidge: on_site_changed - site=%s", site);
149 for (int i = 0; i < LAYER_COUNT; i++) {
150 gchar *base = "http://radar.weather.gov/ridge/";
151 gchar *path = g_strdup_printf(layers[i].fmt, site);
152 cached_t *udata = g_malloc(sizeof(cached_t));
154 udata->layer = &layers[i];
155 cache_file(base, path, AWEATHER_NEVER, cached_cb, udata);
160 void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
162 layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
163 layer->enabled = gtk_toggle_button_get_active(check);
164 aweather_gui_gl_redraw(gui);
170 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
172 AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
175 AWeatherView *view = aweather_gui_get_view(gui);
176 GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
177 GtkWidget *config = aweather_gui_get_widget(gui, "tabs");
179 /* Add configuration tab */
180 GtkWidget *tab = gtk_label_new("Ridge");
181 GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
182 GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
183 for (int i = 0; i < LAYER_COUNT; i++) {
184 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
185 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
186 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
187 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
188 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), gui);
190 gtk_container_add(GTK_CONTAINER(body), hbox);
191 gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
193 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), ridge);
198 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
200 AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
202 g_debug("AWeatherRidge: expose");
204 glEnable(GL_TEXTURE_2D);
207 for (int i = 0; i < LAYER_COUNT; i++) {
208 if (!layers[i].enabled)
210 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
212 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
213 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
214 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
215 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);