]> Pileus Git - grits/blob - src/plugin-ridge.c
Converting a lot of stuff to GObject and adding gtk-doc support
[grits] / src / plugin-ridge.c
1 #include <config.h>
2 #include <gtk/gtk.h>
3 #include <curl/curl.h>
4 #include <GL/gl.h>
5
6 #include <stdio.h>
7
8 #include "aweather-gui.h"
9
10 enum {
11         LAYER_TOPO,
12         LAYER_COUNTY,
13         LAYER_RIVERS,
14         LAYER_HIGHWAYS,
15         LAYER_CITY,
16         LAYER_COUNT
17 };
18
19 static struct {
20         char *fmt;
21         float z;
22         guint tex;
23 } layers[] = {
24         [LAYER_TOPO]     = { "Overlays/" "Topo/"     "Short/" "%s_Topo_Short.jpg",     1, 0 },
25         [LAYER_COUNTY]   = { "Overlays/" "County/"   "Short/" "%s_County_Short.gif",   3, 0 },
26         [LAYER_RIVERS]   = { "Overlays/" "Rivers/"   "Short/" "%s_Rivers_Short.gif",   4, 0 },
27         [LAYER_HIGHWAYS] = { "Overlays/" "Highways/" "Short/" "%s_Highways_Short.gif", 5, 0 },
28         [LAYER_CITY]     = { "Overlays/" "Cities/"   "Short/" "%s_City_Short.gif",     6, 0 },
29 };
30
31 static CURL *curl_handle;
32
33
34 /**
35  * Cache a image from Ridge to the local disk
36  * \param  path  Path to the Ridge file, starting after /ridge/
37  * \return The local path to the cached image
38  */
39 char *cache_image(char *path)
40 {
41         gchar base[] = "http://radar.weather.gov/ridge/";
42         gchar *url   = g_strconcat(base, path, NULL);
43         gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL);
44         if (!g_file_test(local, G_FILE_TEST_EXISTS)) {
45                 if (!g_file_test(g_path_get_dirname(local), G_FILE_TEST_IS_DIR)) {
46                         //g_printerr("Making directory %s\n", g_path_get_dirname(local));
47                         g_mkdir_with_parents(g_path_get_dirname(local), 0755);
48                 }
49                 //g_printerr("Fetching image %s -> %s\n", url, local);
50                 long http_code;
51                 FILE *cached_image = fopen(local, "w+");
52                 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
53                 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, cached_image);
54                 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, NULL);
55                 curl_easy_perform(curl_handle);
56                 curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
57                 fflush(cached_image);
58                 if (http_code != 200) {
59                         g_message("http %ld while fetching %s", http_code, url);
60                         remove(local);
61                         return NULL;
62                 }
63         }
64         return local;
65 }
66
67 /**
68  * Load an image into an OpenGL texture
69  * \param  filename  Path to the image file
70  * \return The OpenGL identifier for the texture
71  */
72 guint load_texture(char *filename)
73 {
74         /* Load image */
75         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
76         guchar    *pixels = gdk_pixbuf_get_pixels(pixbuf);
77         int        width  = gdk_pixbuf_get_width(pixbuf);
78         int        height = gdk_pixbuf_get_height(pixbuf);
79         int        format = gdk_pixbuf_get_has_alpha(pixbuf) ? GL_RGBA : GL_RGB;
80
81         /* Create Texture */
82         guint id;
83         glGenTextures(1, &id);
84         glBindTexture(GL_TEXTURE_2D, id);   // 2d texture (x and y size)
85
86         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
87         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
88                         format, GL_UNSIGNED_BYTE, pixels);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91
92         g_message("loaded image:  w=%-3d  h=%-3d  fmt=%x  px=(%02x,%02x,%02x,%02x)  img=%s",
93                 width, height, format, pixels[0], pixels[1], pixels[2], pixels[3],
94                 g_path_get_basename(filename));
95         return id;
96 }
97
98 static gboolean expose(GtkWidget *da, GdkEventExpose *event, gpointer user_data)
99 {
100         g_message("ridge:expose");
101         glPushMatrix();
102         glEnable(GL_TEXTURE_2D);
103         glColor4f(1,1,1,1);
104
105         for (int i = 0; i < LAYER_COUNT; i++) {
106                 glBindTexture(GL_TEXTURE_2D, layers[i].tex);
107
108                 glBegin(GL_POLYGON);
109                 glTexCoord2f(0.0, 0.0); glVertex3f(500*1000*-1.0, 500*1000* 1.0, layers[i].z);
110                 glTexCoord2f(0.0, 1.0); glVertex3f(500*1000*-1.0, 500*1000*-1.0, layers[i].z);
111                 glTexCoord2f(1.0, 1.0); glVertex3f(500*1000* 1.0, 500*1000*-1.0, layers[i].z);
112                 glTexCoord2f(1.0, 0.0); glVertex3f(500*1000* 1.0, 500*1000* 1.0, layers[i].z);
113                 glEnd();
114         }
115
116         glPopMatrix();
117         return FALSE;
118 }
119
120 static gboolean configure(GtkWidget *da, GdkEventConfigure *event, gpointer user_data)
121 {
122         for (int i = 0; i < LAYER_COUNT; i++) {
123                 if (layers[i].tex != 0)
124                         continue;
125                 char *path  = g_strdup_printf(layers[i].fmt, "IND");
126                 char *local = cache_image(path);
127                 layers[i].tex = load_texture(local);
128                 g_free(local);
129                 g_free(path);
130         }
131         return FALSE;
132 }
133
134 gboolean ridge_init(AWeatherGui *gui)
135 {
136         GtkDrawingArea *drawing = aweather_gui_get_drawing(gui);
137
138         /* Set up OpenGL Stuff */
139         g_signal_connect(drawing, "expose-event",    G_CALLBACK(expose),    NULL);
140         g_signal_connect(drawing, "configure-event", G_CALLBACK(configure), NULL);
141
142         curl_global_init(CURL_GLOBAL_ALL);
143         curl_handle = curl_easy_init();
144
145         return TRUE;
146 }