]> Pileus Git - aweather/blob - src/plugin-ridge.c
b099a08b6cfea32fcb7c3ca91e04fe95a68a5e85
[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 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 /***********
54  * Helpers *
55  ***********/
56 enum {
57         LAYER_TOPO,
58         LAYER_COUNTY,
59         LAYER_RIVERS,
60         LAYER_HIGHWAYS,
61         LAYER_CITY,
62         LAYER_COUNT
63 };
64
65 typedef struct {
66         gchar *name;
67         gchar *fmt;
68         gboolean enabled;
69         float z;
70         guint tex;
71 } layer_t;
72
73 static layer_t layers[] = {
74         [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
75         [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
76         [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
77         [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
78         [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
79 };
80
81 /* TODO: Remove this */
82 AWeatherGui *old_gui = NULL;
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(gchar *filename, gboolean updated, gpointer _layer)
90 {
91         layer_t *layer = _layer;
92         aweather_gui_gl_begin(old_gui);
93
94         /* Load image */
95         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
96         if (!pixbuf)
97                 g_warning("Failed to load texture: %s", filename);
98         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
99         int        width  = gdk_pixbuf_get_width(pixbuf);
100         int        height = gdk_pixbuf_get_height(pixbuf);
101         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
102
103         /* Create Texture */
104         glDeleteTextures(1, &layer->tex);
105         glGenTextures(1, &layer->tex);
106         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
107
108         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
109         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
110                         format, GL_UNSIGNED_BYTE, pixels);
111         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
112         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113
114         char *base = g_path_get_basename(filename);
115         g_message("loaded image:  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(old_gui);
121
122         g_object_unref(pixbuf);
123
124         /* Redraw */
125         aweather_gui_gl_redraw(old_gui);
126 }
127
128 static void set_site(AWeatherView *view, gchar *site, AWeatherRidge *ridge)
129 {
130         g_message("site changed to %s", site);
131         for (int i = 0; i < LAYER_COUNT; i++) {
132                 gchar *base = "http://radar.weather.gov/ridge/";
133                 gchar *path  = g_strdup_printf(layers[i].fmt, site);
134                 cache_file(base, path, AWEATHER_NEVER, load_texture, &layers[i]);
135                 g_free(path);
136         }
137 }
138
139 void toggle_layer(GtkToggleButton *check, gpointer _layer)
140 {
141         layer_t *layer = _layer;
142         layer->enabled = gtk_toggle_button_get_active(check);
143         aweather_gui_gl_redraw(old_gui);
144 }
145
146 /***********
147  * Methods *
148  ***********/
149 AWeatherRidge *aweather_ridge_new(AWeatherGui *gui)
150 {
151         AWeatherRidge *ridge = g_object_new(AWEATHER_TYPE_RIDGE, NULL);
152         ridge->gui = old_gui = gui;
153
154         AWeatherView *view    = aweather_gui_get_view(gui);
155         GtkWidget    *drawing = aweather_gui_get_widget(gui, "drawing");
156         GtkWidget    *config  = aweather_gui_get_widget(gui, "tabs");
157
158         /* Add configuration tab */
159         GtkWidget *tab  = gtk_label_new("Ridge");
160         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
161         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
162         for (int i = 0; i < LAYER_COUNT; i++) {
163                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
164                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
165                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
166                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), &layers[i]);
167         }
168         gtk_container_add(GTK_CONTAINER(body), hbox);
169         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
170
171         g_signal_connect(view,    "site-changed", G_CALLBACK(set_site),  ridge);
172
173         return ridge;
174 }
175
176 static void aweather_ridge_expose(AWeatherPlugin *_ridge)
177 {
178         AWeatherRidge *ridge = AWEATHER_RIDGE(_ridge);
179
180         g_message("ridge:expose");
181         glPushMatrix();
182         glEnable(GL_TEXTURE_2D);
183         glColor4f(1,1,1,1);
184
185         for (int i = 0; i < LAYER_COUNT; i++) {
186                 if (!layers[i].enabled)
187                         continue;
188                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
189                 glBegin(GL_POLYGON);
190                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
191                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
192                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
193                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
194                 glEnd();
195         }
196
197         glPopMatrix();
198 }