]> Pileus Git - aweather/blob - src/plugin-ridge.c
8dcdac559a5da58b1570cfa99d7e046b5a79a433
[aweather] / src / plugin-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 "plugin-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         aweather_gui_gl_begin(self->gui);
114
115         /* Load image */
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);
120                 return;
121         }
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;
126
127         /* Create Texture */
128         glDeleteTextures(1, &layer->tex);
129         glGenTextures(1, &layer->tex);
130         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
131
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);
137
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],
142                 base);
143         g_free(base);
144
145         aweather_gui_gl_end(self->gui);
146
147         g_object_unref(pixbuf);
148
149         /* Redraw */
150         aweather_gui_gl_redraw(self->gui);
151 }
152
153
154 /*****************
155  * ASync helpers *
156  *****************/
157 typedef struct {
158         AWeatherRidge *self;
159         layer_t *layer;
160 } cached_t;
161 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
162 {
163         cached_t *udata = _udata;
164         load_texture(udata->self, udata->layer, filename);
165         g_free(udata);
166 }
167
168 /*************
169  * callbacks *
170  *************/
171 static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
172 {
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));
178                 udata->self  = self;
179                 udata->layer = &layers[i];
180                 cache_file(base, path, AWEATHER_ONCE, cached_cb, udata);
181                 //cache_file(base, path, AWEATHER_UPDATE, cached_cb, udata);
182                 g_free(path);
183         }
184 }
185
186 void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
187 {
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);
191 }
192
193 /***********
194  * Methods *
195  ***********/
196 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
197 {
198         AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
199         ridge->gui = gui;
200
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");
204
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);
215         }
216         gtk_container_add(GTK_CONTAINER(body), hbox);
217         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
218
219         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed),  ridge);
220
221         return ridge;
222 }
223
224 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
225 {
226         AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
227
228         g_debug("AWeatherRidge: expose");
229         glPushMatrix();
230         glEnable(GL_TEXTURE_2D);
231         glColor4f(1,1,1,1);
232
233         for (int i = 0; i < LAYER_COUNT; i++) {
234                 if (!layers[i].enabled)
235                         continue;
236                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
237                 glBegin(GL_POLYGON);
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);
242                 glEnd();
243         }
244
245         glPopMatrix();
246 }