]> Pileus Git - grits/blob - src/plugins/ridge.c
6f1e267d469ae66615099d5aa9ef181cc9ae3fef
[grits] / 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 <gtk/gtk.h>
20 #include <curl/curl.h>
21 #include <GL/gl.h>
22
23 #include <stdio.h>
24
25 #include "aweather-gui.h"
26 #include "ridge.h"
27 #include "data.h"
28
29 /****************
30  * GObject code *
31  ****************/
32 /* Plugin init */
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)
39 {
40         g_debug("AWeatherRidge: plugin_init");
41         /* Add methods to the interface */
42         iface->expose = aweather_ridge_expose;
43 }
44 /* Class/Object init */
45 static void aweather_ridge_init(AWeatherRidge *ridge)
46 {
47         g_debug("AWeatherRidge: init");
48         /* Set defaults */
49         ridge->gui = NULL;
50 }
51 static void aweather_ridge_dispose(GObject *gobject)
52 {
53         g_debug("AWeatherRidge: dispose");
54         AWeatherRidge *self = AWEATHER_RIDGE(gobject);
55         /* Drop references */
56         G_OBJECT_CLASS(aweather_ridge_parent_class)->dispose(gobject);
57 }
58 static void aweather_ridge_finalize(GObject *gobject)
59 {
60         g_debug("AWeatherRidge: finalize");
61         AWeatherRidge *self = AWEATHER_RIDGE(gobject);
62         /* Free data */
63         G_OBJECT_CLASS(aweather_ridge_parent_class)->finalize(gobject);
64
65 }
66 static void aweather_ridge_class_init(AWeatherRidgeClass *klass)
67 {
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;
72 }
73
74 /*********************
75  * Overlay constants *
76  *********************/
77 enum {
78         LAYER_TOPO,
79         LAYER_COUNTY,
80         LAYER_RIVERS,
81         LAYER_HIGHWAYS,
82         LAYER_CITY,
83         LAYER_COUNT
84 };
85
86 typedef struct {
87         gchar *name;
88         gchar *fmt;
89         gboolean enabled;
90         float z;
91         guint tex;
92 } layer_t;
93
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},
100 };
101
102
103 /***********
104  * Helpers *
105  ***********/
106 /*
107  * Load an image into an OpenGL texture
108  * \param  filename  Path to the image file
109  * \return The OpenGL identifier for the texture
110  */
111 void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
112 {
113         GisOpenGL *opengl = aweather_gui_get_opengl(self->gui);
114         gis_opengl_begin(opengl);
115
116         /* Load image */
117         GError *error = NULL;
118         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, &error);
119         if (!pixbuf || error) {
120                 g_warning("Failed to load texture: %s", filename);
121                 return;
122         }
123         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
124         int        width  = gdk_pixbuf_get_width(pixbuf);
125         int        height = gdk_pixbuf_get_height(pixbuf);
126         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
127
128         /* Create Texture */
129         glDeleteTextures(1, &layer->tex);
130         glGenTextures(1, &layer->tex);
131         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
132
133         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
134         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
135                         format, GL_UNSIGNED_BYTE, pixels);
136         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138
139         char *base = g_path_get_basename(filename);
140         g_debug("AWeatherRidge: load_texture - "
141                 "w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
142                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
143                 base);
144         g_free(base);
145
146         gis_opengl_end(opengl);
147
148         g_object_unref(pixbuf);
149
150         /* Redraw */
151         gis_opengl_redraw(opengl);
152 }
153
154
155 /*****************
156  * ASync helpers *
157  *****************/
158 typedef struct {
159         AWeatherRidge *self;
160         layer_t *layer;
161 } cached_t;
162 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
163 {
164         cached_t *udata = _udata;
165         load_texture(udata->self, udata->layer, filename);
166         g_free(udata);
167 }
168
169 /*************
170  * callbacks *
171  *************/
172 static void on_site_changed(GisView *view, gchar *site, AWeatherRidge *self)
173 {
174         g_debug("AWeatherRidge: on_site_changed - site=%s", site);
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, AWEATHER_ONCE, NULL, cached_cb, udata);
182                 //cache_file(base, path, AWEATHER_UPDATE, cached_cb, udata);
183                 g_free(path);
184         }
185 }
186
187 void toggle_layer(GtkToggleButton *check, AWeatherRidge *self)
188 {
189         layer_t *layer = g_object_get_data(G_OBJECT(check), "layer");
190         layer->enabled = gtk_toggle_button_get_active(check);
191         gis_opengl_redraw(aweather_gui_get_opengl(self->gui));
192 }
193
194 /***********
195  * Methods *
196  ***********/
197 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
198 {
199         AWeatherRidge *self = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
200         self->gui = gui;
201
202         GisView   *view    = aweather_gui_get_view(gui);
203         GtkWidget *drawing = aweather_gui_get_widget(gui, "drawing");
204         GtkWidget *config  = aweather_gui_get_widget(gui, "tabs");
205         g_debug("config = %p", aweather_gui_get_widget(gui, "tabs"));
206
207         /* Add configuration tab */
208         GtkWidget *tab  = gtk_label_new("Ridge");
209         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
210         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
211         for (int i = 0; i < LAYER_COUNT; i++) {
212                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
213                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
214                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
215                 g_object_set_data(G_OBJECT(check), "layer", &layers[i]);
216                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), self);
217         }
218         gtk_container_add(GTK_CONTAINER(body), hbox);
219         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
220
221         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed), self);
222         on_site_changed(view, gis_view_get_site(view), self);
223
224         return self;
225 }
226
227 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
228 {
229         AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
230
231         g_debug("AWeatherRidge: expose");
232         glPushMatrix();
233         glEnable(GL_TEXTURE_2D);
234         glColor4f(1,1,1,1);
235
236         for (int i = 0; i < LAYER_COUNT; i++) {
237                 if (!layers[i].enabled)
238                         continue;
239                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
240                 glBegin(GL_POLYGON);
241                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
242                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
243                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
244                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
245                 glEnd();
246         }
247
248         glPopMatrix();
249 }