]> Pileus Git - grits/blob - src/plugin-ridge.c
* Fixing some async issue (w/ gthread_init)
[grits] / 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 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)
38 {
39         GObjectClass *object_class = (GObjectClass*)klass;
40 }
41 static void aweather_ridge_plugin_init(AWeatherPluginInterface *iface)
42 {
43         /* Add methods to the interface */
44         iface->expose = aweather_ridge_expose;
45 }
46 static void aweather_ridge_init(AWeatherRidge *ridge)
47 {
48         /* Set defaults */
49         ridge->gui = NULL;
50 }
51
52 /*********************
53  * Overlay constants *
54  *********************/
55 enum {
56         LAYER_TOPO,
57         LAYER_COUNTY,
58         LAYER_RIVERS,
59         LAYER_HIGHWAYS,
60         LAYER_CITY,
61         LAYER_COUNT
62 };
63
64 typedef struct {
65         gchar *name;
66         gchar *fmt;
67         gboolean enabled;
68         float z;
69         guint tex;
70 } layer_t;
71
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},
78 };
79
80
81 /***********
82  * Helpers *
83  ***********/
84 /*
85  * Load an image into an OpenGL texture
86  * \param  filename  Path to the image file
87  * \return The OpenGL identifier for the texture
88  */
89 void load_texture(AWeatherRidge *self, layer_t *layer, gchar *filename)
90 {
91         aweather_gui_gl_begin(self->gui);
92
93         /* Load image */
94         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
95         if (!pixbuf)
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;
101
102         /* Create Texture */
103         glDeleteTextures(1, &layer->tex);
104         glGenTextures(1, &layer->tex);
105         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
106
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);
112
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],
117                 base);
118         g_free(base);
119
120         aweather_gui_gl_end(self->gui);
121
122         g_object_unref(pixbuf);
123
124         /* Redraw */
125         aweather_gui_gl_redraw(self->gui);
126 }
127
128
129 /*****************
130  * ASync helpers *
131  *****************/
132 typedef struct {
133         AWeatherRidge *self;
134         layer_t *layer;
135 } cached_t;
136 void cached_cb(gchar *filename, gboolean updated, gpointer _udata)
137 {
138         cached_t *udata = _udata;
139         load_texture(udata->self, udata->layer, filename);
140         g_free(udata);
141 }
142
143 /*************
144  * callbacks *
145  *************/
146 static void on_site_changed(AWeatherView *view, gchar *site, AWeatherRidge *self)
147 {
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));
153                 udata->self  = self;
154                 udata->layer = &layers[i];
155                 cache_file(base, path, AWEATHER_NEVER, cached_cb, udata);
156                 g_free(path);
157         }
158 }
159
160 void toggle_layer(GtkToggleButton *check, AWeatherGui *gui)
161 {
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);
165 }
166
167 /***********
168  * Methods *
169  ***********/
170 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
171 {
172         AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
173         ridge->gui = gui;
174
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");
178
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);
189         }
190         gtk_container_add(GTK_CONTAINER(body), hbox);
191         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
192
193         g_signal_connect(view, "site-changed", G_CALLBACK(on_site_changed),  ridge);
194
195         return ridge;
196 }
197
198 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
199 {
200         AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
201
202         g_debug("AWeatherRidge: expose");
203         glPushMatrix();
204         glEnable(GL_TEXTURE_2D);
205         glColor4f(1,1,1,1);
206
207         for (int i = 0; i < LAYER_COUNT; i++) {
208                 if (!layers[i].enabled)
209                         continue;
210                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
211                 glBegin(GL_POLYGON);
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);
216                 glEnd();
217         }
218
219         glPopMatrix();
220 }