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"
33 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface);
34 static void aweather_ridge_expose(AWeatherPlugin *_ridge);
35 G_DEFINE_TYPE_WITH_CODE(AWeatherRidge, aweather_ridge, G_TYPE_OBJECT,
36 G_IMPLEMENT_INTERFACE(AWEATHER_TYPE_PLUGIN,
37 aweather_ridge_plugin_init));
38 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface)
40 g_debug("AWeatherRidge: plugin_init");
41 /* Add methods to the interface */
42 iface->expose = aweather_ridge_expose;
44 /* Class/Object init */
45 static void aweather_ridge_init(AWeatherRidge *ridge)
47 g_debug("AWeatherRidge: init");
51 static void aweather_ridge_dispose(GObject *gobject)
53 g_debug("AWeatherRidge: dispose");
54 AWeatherRidge *self = AWEATHER_RIDGE(gobject);
56 G_OBJECT_CLASS(aweather_ridge_parent_class)->dispose(gobject);
58 static void aweather_ridge_finalize(GObject *gobject)
60 g_debug("AWeatherRidge: finalize");
61 AWeatherRidge *self = AWEATHER_RIDGE(gobject);
63 G_OBJECT_CLASS(aweather_ridge_parent_class)->finalize(gobject);
66 static void aweather_ridge_class_init(AWeatherRidgeClass *klass)
68 g_debug("AWeatherRidge: class_init");
69 GObjectClass *gobject_class = (GObjectClass*)klass;
70 gobject_class->dispose = aweather_ridge_dispose;
71 gobject_class->finalize = aweather_ridge_finalize;
74 /*********************
76 *********************/
94 static layer_t layers[] = {
95 [LAYER_TOPO] = {"Topo", "Overlays/Topo/Short/%s_Topo_Short.jpg", TRUE, 1, 0},
96 [LAYER_COUNTY] = {"Counties", "Overlays/County/Short/%s_County_Short.gif", TRUE, 3, 0},
97 [LAYER_RIVERS] = {"Rivers", "Overlays/Rivers/Short/%s_Rivers_Short.gif", FALSE, 4, 0},
98 [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
99 [LAYER_CITY] = {"Cities", "Overlays/Cities/Short/%s_City_Short.gif", TRUE, 6, 0},
107 * Load an image into an OpenGL texture
108 * \param filename Path to the image file
109 * \return The OpenGL identifier for the texture
111 void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
113 aweather_gui_gl_begin(self->gui);
116 GError *error = NULL;
117 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
118 if (!pixbuf || error) {
119 g_warning("Failed to load texture: %s", filename);
122 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
123 int width = gdk_pixbuf_get_width(pixbuf);
124 int height = gdk_pixbuf_get_height(pixbuf);
125 int format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
128 glDeleteTextures(1, &layer->tex);
129 glGenTextures(1, &layer->tex);
130 glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
132 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
134 format, GL_UNSIGNED_BYTE, pixels);
135 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
136 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138 char *base = g_path_get_basename(filename);
139 g_debug("AWeatherRidge: load_texture - "
140 "w=%-3d h=%-3d fmt=%x px=(%02x,%02x,%02x,%02x) img=%s",
141 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
145 aweather_gui_gl_end(self->gui);
147 g_object_unref(pixbuf);
150 aweather_gui_gl_redraw(self->gui);
161 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
163 cached_t *udata = _udata;
164 load_texture(udata->self, udata->layer, filename);
171 static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
173 g_debug("AWeatherRidge: on_site_changed - site=%s", site);
174 for (int i = 0; i < LAYER_COUNT; i++) {
175 gchar *base = "http://radar.weather.gov/ridge/";
176 gchar *path = g_strdup_printf(layers[i].fmt, site+1);
177 cached_t *udata = g_malloc(sizeof(cached_t));
179 udata->layer = &layers[i];
180 cache_file(base, path, AWEATHER_ONCE, NULL, cached_cb, udata);
181 //cache_file(base, path, AWEATHER_UPDATE, cached_cb, udata);
186 void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
188 layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
189 layer->enabled = gtk_toggle_button_get_active(check);
190 aweather_gui_gl_redraw(gui);
196 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
198 AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
201 AWeatherView *view = aweather_gui_get_view(gui);
202 GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
203 GtkWidget *config = aweather_gui_get_widget(gui, "tabs");
205 /* Add configuration tab */
206 GtkWidget *tab = gtk_label_new("Ridge");
207 GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
208 GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
209 for (int i = 0; i < LAYER_COUNT; i++) {
210 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
211 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
212 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
213 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
214 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), gui);
216 gtk_container_add(GTK_CONTAINER(body), hbox);
217 gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
219 g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), ridge);
224 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
226 AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
228 g_debug("AWeatherRidge: expose");
230 glEnable(GL_TEXTURE_2D);
233 for (int i = 0; i < LAYER_COUNT; i++) {
234 if (!layers[i].enabled)
236 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
238 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
239 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
240 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
241 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);