#include <stdio.h>
+#include "aweather-gui.h"
+#include "data.h"
+
enum {
LAYER_TOPO,
LAYER_COUNTY,
LAYER_COUNT
};
-static struct {
- char *fmt;
+typedef struct {
+ gchar *fmt;
float z;
guint tex;
-} layers[] = {
+} layer_t;
+
+static layer_t layers[] = {
[LAYER_TOPO] = { "Overlays/" "Topo/" "Short/" "%s_Topo_Short.jpg", 1, 0 },
[LAYER_COUNTY] = { "Overlays/" "County/" "Short/" "%s_County_Short.gif", 3, 0 },
[LAYER_RIVERS] = { "Overlays/" "Rivers/" "Short/" "%s_Rivers_Short.gif", 4, 0 },
[LAYER_CITY] = { "Overlays/" "Cities/" "Short/" "%s_City_Short.gif", 6, 0 },
};
-static CURL *curl_handle;
-
-
-/**
- * Cache a image from Ridge to the local disk
- * \param path Path to the Ridge file, starting after /ridge/
- * \return The local path to the cached image
- */
-char *cache_image(char *path)
-{
- gchar base[] = "http://radar.weather.gov/ridge/";
- gchar *url = g_strconcat(base, path, NULL);
- gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
- if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
- if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
- //g_printerr("Making directory %s\n", g_path_get_dirname(local));
- g_mkdir_with_parents(g_path_get_dirname(local), 0755);
- }
- //g_printerr("Fetching image %s -> %s\n", url, local);
- long http_code;
- FILE *cached_image = fopen(local, "w+");
- curl_easy_setopt(curl_handle, CURLOPT_URL, url);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
- curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
- curl_easy_perform(curl_handle);
- curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
- fflush(cached_image);
- if (http_code != 200) {
- g_message("http %ld while fetching %s", http_code, url);
- remove(local);
- return NULL;
- }
- }
- return local;
-}
+static AWeatherGui *gui = NULL;
/**
* Load an image into an OpenGL texture
* \param filename Path to the image file
* \return The OpenGL identifier for the texture
*/
-guint load_texture(char *filename)
+void load_texture(gchar *filename, gpointer _layer)
{
+ layer_t *layer = _layer;
+ aweather_gui_gl_begin(gui);
+
/* Load image */
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
+ if (!pixbuf)
+ g_warning("Failed to load texture: %s", filename);
guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
int width = gdk_pixbuf_get_width(pixbuf);
int height = gdk_pixbuf_get_height(pixbuf);
int format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
/* Create Texture */
- guint id;
- glGenTextures(1, &id);
- glBindTexture(GL_TEXTURE_2D, id); // 2d texture (x and y size)
+ glGenTextures(1, &layer->tex);
+ glBindTexture(GL_TEXTURE_2D, layer->tex); // 2d texture (x and y size)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
g_message("loaded image: w=%-3d h=%-3d fmt=%x px=(%02x,%02x,%02x,%02x) img=%s",
width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
g_path_get_basename(filename));
- return id;
+
+ aweather_gui_gl_end(gui);
+
+ /* Redraw */
+ gtk_widget_queue_draw(aweather_gui_get_widget(gui, "drawing"));
+}
+
+static void set_site(AWeatherView *view, gchar *site, gpointer user_data)
+{
+ g_message("location changed to %s", site);
+ for (int i = 0; i < LAYER_COUNT; i++) {
+ gchar *base = "http://radar.weather.gov/ridge/";
+ gchar *path = g_strdup_printf(layers[i].fmt, site);
+ cache_file(base, path, load_texture, &layers[i]);
+ g_free(path);
+ }
}
static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
glBindTexture(GL_TEXTURE_2D, layers[i].tex);
glBegin(GL_POLYGON);
- glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
- glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
- glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
- glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
+ glTexCoord2f(0.0, 0.0); glVertex3f(250*1000*-1.0, 250*1000* 1.0, layers[i].z);
+ glTexCoord2f(0.0, 1.0); glVertex3f(250*1000*-1.0, 250*1000*-1.0, layers[i].z);
+ glTexCoord2f(1.0, 1.0); glVertex3f(250*1000* 1.0, 250*1000*-1.0, layers[i].z);
+ glTexCoord2f(1.0, 0.0); glVertex3f(250*1000* 1.0, 250*1000* 1.0, layers[i].z);
glEnd();
}
return FALSE;
}
-static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
+gboolean ridge_init(AWeatherGui *_gui)
{
- for (int i = 0; i < LAYER_COUNT; i++) {
- if (layers[i].tex != 0)
- continue;
- char *path = g_strdup_printf(layers[i].fmt, "IND");
- char *local = cache_image(path);
- layers[i].tex = load_texture(local);
- g_free(local);
- g_free(path);
- }
- return FALSE;
-}
+ gui = _gui;
+ GtkDrawingArea *drawing = GTK_DRAWING_AREA(aweather_gui_get_widget(gui, "drawing"));
+ AWeatherView *view = aweather_gui_get_view(gui);
-gboolean ridge_init(GtkDrawingArea *drawing, GtkNotebook *config)
-{
/* Set up OpenGL Stuff */
- g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
- g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
-
- curl_global_init(CURL_GLOBAL_ALL);
- curl_handle = curl_easy_init();
+ g_signal_connect(drawing, "expose-event", G_CALLBACK(expose), NULL);
+ g_signal_connect(view, "location-changed", G_CALLBACK(set_site), NULL);
return TRUE;
}