]> Pileus Git - grits/blob - src/plugin-ridge.c
* Road plan (HACKING)
[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 "data.h"
27
28 enum {
29         LAYER_TOPO,
30         LAYER_COUNTY,
31         LAYER_RIVERS,
32         LAYER_HIGHWAYS,
33         LAYER_CITY,
34         LAYER_COUNT
35 };
36
37 typedef struct {
38         gchar *name;
39         gchar *fmt;
40         gboolean enabled;
41         float z;
42         guint tex;
43 } layer_t;
44
45 static layer_t layers[] = {
46         [LAYER_TOPO]     = {"Topo",     "Overlays/Topo/Short/%s_Topo_Short.jpg",         TRUE,  1, 0},
47         [LAYER_COUNTY]   = {"Counties", "Overlays/County/Short/%s_County_Short.gif",     TRUE,  3, 0},
48         [LAYER_RIVERS]   = {"Rivers",   "Overlays/Rivers/Short/%s_Rivers_Short.gif",     FALSE, 4, 0},
49         [LAYER_HIGHWAYS] = {"Highways", "Overlays/Highways/Short/%s_Highways_Short.gif", FALSE, 5, 0},
50         [LAYER_CITY]     = {"Cities",   "Overlays/Cities/Short/%s_City_Short.gif",       TRUE,  6, 0},
51 };
52
53 static AWeatherGui *gui = NULL;
54
55 /**
56  * Load an image into an OpenGL texture
57  * \param  filename  Path to the image file
58  * \return The OpenGL identifier for the texture
59  */
60 void load_texture(gchar *filename, gboolean updated, gpointer _layer)
61 {
62         layer_t *layer = _layer;
63         aweather_gui_gl_begin(gui);
64
65         /* Load image */
66         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
67         if (!pixbuf)
68                 g_warning("Failed to load texture: %s", filename);
69         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
70         int        width  = gdk_pixbuf_get_width(pixbuf);
71         int        height = gdk_pixbuf_get_height(pixbuf);
72         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
73
74         /* Create Texture */
75         glDeleteTextures(1, &layer->tex);
76         glGenTextures(1, &layer->tex);
77         glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
78
79         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
80         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
81                         format, GL_UNSIGNED_BYTE, pixels);
82         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
83         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
84
85         char *base = g_path_get_basename(filename);
86         g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
87                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
88                 base);
89         g_free(base);
90
91         aweather_gui_gl_end(gui);
92
93         g_object_unref(pixbuf);
94
95         /* Redraw */
96         gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
97 }
98
99 static void set_site(AWeatherView *view, gchar *site, gpointer user_data)
100 {
101         g_message("location changed to %s", site);
102         for (int i = 0; i < LAYER_COUNT; i++) {
103                 gchar *base = "http://radar.weather.gov/ridge/";
104                 gchar *path  = g_strdup_printf(layers[i].fmt, site);
105                 cache_file(base, path, AWEATHER_NEVER, load_texture, &layers[i]);
106                 g_free(path);
107         }
108 }
109
110 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
111 {
112         g_message("ridge:expose");
113         glPushMatrix();
114         glEnable(GL_TEXTURE_2D);
115         glColor4f(1,1,1,1);
116
117         for (int i = 0; i < LAYER_COUNT; i++) {
118                 if (!layers[i].enabled)
119                         continue;
120                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
121                 glBegin(GL_POLYGON);
122                 glTexCoord2f(0.0, 0.0); glVertex3f(240*1000*-1.0, 282*1000* 1.0, layers[i].z);
123                 glTexCoord2f(0.0, 1.0); glVertex3f(240*1000*-1.0, 282*1000*-1.0, layers[i].z);
124                 glTexCoord2f(1.0, 1.0); glVertex3f(240*1000* 1.0, 282*1000*-1.0, layers[i].z);
125                 glTexCoord2f(1.0, 0.0); glVertex3f(240*1000* 1.0, 282*1000* 1.0, layers[i].z);
126                 glEnd();
127         }
128
129         glPopMatrix();
130         return FALSE;
131 }
132
133 void toggle_layer(GtkToggleButton *check, gpointer _layer)
134 {
135         layer_t *layer = _layer;
136         layer->enabled = gtk_toggle_button_get_active(check);
137         gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
138 }
139
140 gboolean ridge_init(AWeatherGui *_gui)
141 {
142         gui = _gui;
143         AWeatherView *view    = aweather_gui_get_view(gui);
144         GtkWidget    *drawing = aweather_gui_get_widget(gui, "drawing");
145         GtkWidget    *config  = aweather_gui_get_widget(gui, "tabs");
146
147         /* Add configuration tab */
148         GtkWidget *tab  = gtk_label_new("Ridge");
149         GtkWidget *body = gtk_alignment_new(0.5, 0, 0, 0);
150         GtkWidget *hbox = gtk_hbox_new(FALSE, 10);
151         for (int i = 0; i < LAYER_COUNT; i++) {
152                 GtkWidget *check = gtk_check_button_new_with_label(layers[i].name);
153                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), layers[i].enabled);
154                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, TRUE, 0);
155                 g_signal_connect(check, "toggled", G_CALLBACK(toggle_layer), &layers[i]);
156         }
157         gtk_container_add(GTK_CONTAINER(body), hbox);
158         gtk_notebook_append_page(GTK_NOTEBOOK(config), body, tab);
159
160         /* Set up OpenGL Stuff */
161         g_signal_connect(drawing, "expose-event",     G_CALLBACK(expose),    NULL);
162         g_signal_connect(view,    "location-changed", G_CALLBACK(set_site),  NULL);
163
164         return TRUE;
165 }