Graphics caching not operational.
[rodin/chimara.git] / libchimara / graphics.c
1 #include "graphics.h"
2 #include "chimara-glk-private.h"
3 #include "magic.h"
4
5 #define BUFFER_SIZE (1024)
6
7 extern GPrivate *glk_data_key;
8 void on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info);
9 void on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data);
10
11 static gboolean image_loaded;
12 static gboolean size_determined;
13
14 static struct image_info*
15 load_image_in_cache(glui32 image, gint width, gint height)
16 {
17         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
18         giblorb_err_t blorb_error = 0;
19         giblorb_result_t resource;
20         GError *pixbuf_error = NULL;
21         guchar *buffer;
22
23         /* Lookup the proper resource */
24         blorb_error = giblorb_load_resource(glk_data->resource_map, giblorb_method_FilePos, &resource, giblorb_ID_Pict, image);
25         if(blorb_error != giblorb_err_None) {
26                 WARNING_S( "Error loading resource", giblorb_get_error_message(blorb_error) );
27                 return NULL;
28         }
29
30         struct image_info *info = g_new0(struct image_info, 1);
31         info->resource_number = image;
32
33         /* Load the resource */
34         GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
35         g_signal_connect( loader, "size-prepared", G_CALLBACK(on_size_prepared), info ); 
36         g_signal_connect( loader, "closed", G_CALLBACK(on_pixbuf_closed), NULL ); 
37
38         /* Scale image if necessary */
39         if(width > 0 && height > 0) {
40                 gdk_pixbuf_loader_set_size(loader, width, height);
41                 info->scaled = TRUE;
42         }
43
44         glk_stream_set_position(glk_data->resource_file, resource.data.startpos, seekmode_Start);
45         buffer = g_malloc( BUFFER_SIZE * sizeof(guchar) );
46
47         guint32 total_read = 0;
48         image_loaded = FALSE;
49         while(total_read < resource.length && !image_loaded) {
50                 guint32 num_read = glk_get_buffer_stream(glk_data->resource_file, (char *) buffer, BUFFER_SIZE);
51
52                 if( !gdk_pixbuf_loader_write(loader, buffer, MIN(BUFFER_SIZE, num_read), &pixbuf_error) ) {
53                         WARNING_S("Cannot read image", pixbuf_error->message);
54                         giblorb_unload_chunk(glk_data->resource_map, image);
55                         gdk_pixbuf_loader_close(loader, &pixbuf_error);
56                         g_free(buffer);
57                         return NULL;
58                 }
59
60                 total_read += num_read;
61         }
62         gdk_pixbuf_loader_close(loader, &pixbuf_error);
63         giblorb_unload_chunk(glk_data->resource_map, resource.chunknum);
64         g_free(buffer);
65
66         /* Wait for the PixbufLoader to finish loading the image */
67         g_mutex_lock(glk_data->resource_lock);
68         while(!image_loaded) {
69                 g_cond_wait(glk_data->resource_loaded, glk_data->resource_lock);
70         }
71         g_mutex_unlock(glk_data->resource_lock);
72
73         /* Store the image in the cache */
74         gdk_threads_enter();
75
76         if( g_slist_length(glk_data->image_cache) >= IMAGE_CACHE_MAX_NUM ) {
77                 struct image_info *head = (struct image_info*) glk_data->image_cache->data;
78                 gdk_pixbuf_unref(head->pixbuf);
79                 g_free(head);
80                 glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache);
81         }
82         info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
83         gdk_pixbuf_ref(info->pixbuf);
84         info->width = gdk_pixbuf_get_width(info->pixbuf);
85         info->height = gdk_pixbuf_get_height(info->pixbuf);
86         glk_data->image_cache = g_slist_prepend(glk_data->image_cache, info);
87
88         gdk_threads_leave();
89
90         g_object_unref(loader);
91         return info;
92 }
93
94 void
95 on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info)
96 {
97         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
98
99         g_mutex_lock(glk_data->resource_lock);
100         info->width = width;
101         info->height = height;
102         size_determined = TRUE;
103         g_cond_broadcast(glk_data->resource_info_available);
104         g_mutex_unlock(glk_data->resource_lock);
105 }
106
107 void
108 on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data)
109 {
110         gdk_threads_enter();
111
112         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
113
114         g_mutex_lock(glk_data->resource_lock);
115         image_loaded = TRUE;
116         g_cond_broadcast(glk_data->resource_loaded);
117         g_mutex_unlock(glk_data->resource_lock);
118
119         gdk_threads_leave();
120 }
121
122
123 void
124 clear_image_cache(struct image_info *data, gpointer user_data)
125 {
126         gdk_pixbuf_unref(data->pixbuf);
127         g_free(data);
128 }
129
130 static struct image_info*
131 image_cache_find(struct image_info* to_find)
132 {
133         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
134         GSList *link = glk_data->image_cache;
135
136         gdk_threads_enter();
137
138         /* Empty cache */
139         if(link == NULL) {
140                 gdk_threads_leave();
141                 printf("Cache miss for image %d\n", to_find->resource_number);
142                 return NULL;
143         }
144
145         /* Iterate over the cache to find the correct image and size */
146         do {
147                 struct image_info *info = (struct image_info*) link->data;
148                 if(info->resource_number == to_find->resource_number) {
149                         /* Check size: are we looking for a scaled version or the original one? */
150                         if(to_find->scaled) {
151                                 if(info->width >= to_find->width && info->height >= to_find->height) {
152                                         gdk_threads_leave();
153                                         printf("Cache hit for image %d\n", to_find->resource_number);
154                                         return info; /* Found a good enough match */
155                                 }
156                         } else {
157                                 if(!info->scaled) {
158                                         gdk_threads_leave();
159                                         printf("Cache hit for image %d\n", to_find->resource_number);
160                                         return info; /* Found a match */
161                                 }
162                         }
163                 }
164         } while( (link = g_slist_next(link)) );
165
166         gdk_threads_leave();
167
168         printf("Cache miss for image %d\n", to_find->resource_number);
169         return NULL; /* No match found */
170 }
171
172 glui32
173 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
174 {
175         struct image_info *to_find = g_new0(struct image_info, 1);
176         struct image_info *found;
177         to_find->resource_number = image;
178         to_find->scaled = FALSE; /* we want the original image size */
179
180         if( !(found = image_cache_find(to_find)) ) {
181                 found = load_image_in_cache(image, 0, 0);
182                 if(found == NULL)
183                         return FALSE;
184         }
185
186         if(width != NULL)
187                 *width = found->width;
188         if(width != NULL)
189                 *height = found->height;
190         return TRUE;
191 }
192
193 glui32
194 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
195 {
196         VALID_WINDOW(win, return FALSE);
197         g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
198
199         struct image_info *to_find = g_new0(struct image_info, 1);
200         struct image_info *info;
201         GdkPixmap *canvas;
202
203         /* Lookup the proper resource */
204         to_find->resource_number = image;
205         to_find->scaled = FALSE; /* we want the original image size */
206
207         if( !(info = image_cache_find(to_find)) ) {
208                 info = load_image_in_cache(image, 0, 0);
209                 if(info == NULL)
210                         return FALSE;
211         }
212
213         gdk_threads_enter();
214
215         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
216         if(canvas == NULL) {
217                 WARNING("Could not get pixmap");
218                 return FALSE;
219         }
220
221         gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, GDK_PIXBUF((GdkPixbuf*)info->pixbuf), 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
222
223         /* Update the screen */
224         gtk_widget_queue_draw(win->widget);
225
226         gdk_threads_leave();
227
228         return TRUE;
229 }
230
231
232 glui32
233 glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height)
234 {
235         VALID_WINDOW(win, return FALSE);
236         g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
237
238         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
239         struct image_info *to_find = g_new0(struct image_info, 1);
240         struct image_info *info;
241         struct image_info *scaled_info;
242         GdkPixmap *canvas;
243
244         /* Lookup the proper resource */
245         to_find->resource_number = image;
246         to_find->scaled = TRUE; /* any image size equal or larger than requested will do */
247
248         if( !(info = image_cache_find(to_find)) ) {
249                 info = load_image_in_cache(image, width, height);
250                 if(info == NULL)
251                         return FALSE;
252         }
253
254         gdk_threads_enter();
255
256         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
257         if(canvas == NULL) {
258                 WARNING("Could not get pixmap");
259                 return FALSE;
260         }
261
262         /* Scale the image if necessary */
263         if(info->width != width || info->height != height) {
264                 GdkPixbuf *scaled = gdk_pixbuf_scale_simple(info->pixbuf, width, height, GDK_INTERP_BILINEAR);
265
266                 /* Add the scaled image into the image cache */
267                 scaled_info = g_new0(struct image_info, 1);
268                 scaled_info->resource_number = info->resource_number;
269                 scaled_info->width = gdk_pixbuf_get_width(scaled);
270                 scaled_info->height = gdk_pixbuf_get_width(scaled);
271                 scaled_info->pixbuf = scaled;
272                 scaled_info->scaled = TRUE;
273                 glk_data->image_cache = g_slist_prepend(glk_data->image_cache, scaled_info);
274
275                 /* Continue working with the scaled version */
276                 info = scaled_info;
277         }
278
279         gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
280
281         /* Update the screen */
282         gtk_widget_queue_draw(win->widget);
283
284         gdk_threads_leave();
285
286         return TRUE;
287 }
288
289 void
290 glk_window_set_background_color(winid_t win, glui32 color) {
291         win->background_color = color;
292 }
293
294 void
295 glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 width, glui32 height)
296 {
297         VALID_WINDOW(win, return);
298         g_return_if_fail(win->type == wintype_Graphics);
299
300
301         gdk_threads_enter();
302
303         GdkPixmap *map;
304         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &map, NULL );
305         gdk_draw_rectangle( GDK_DRAWABLE(map), win->widget->style->white_gc, TRUE, left, top, width, height);
306         gtk_widget_queue_draw(win->widget);
307
308         gdk_threads_leave();
309 }
310
311 void
312 glk_window_erase_rect(winid_t win, glsi32 left, glsi32 top, glui32 width, glui32 height)
313 {
314         glk_window_fill_rect(win, win->background_color, left, top, width, height);
315 }
316
317 void glk_window_flow_break(winid_t win)
318 {
319 }
320
321 /*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/
322 void
323 on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win)
324
325         GdkPixmap *oldmap;
326         gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL );
327         gint oldwidth = 0;
328         gint oldheight = 0;
329  
330         /* Determine whether a pixmap exists with the correct size */
331         gboolean needs_resize = FALSE;
332         if(oldmap == NULL)
333                 needs_resize = TRUE;
334         else {
335                 gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
336                 if(oldwidth != allocation->width || oldheight != allocation->height)
337                         needs_resize = TRUE;
338         }
339
340         if(needs_resize) {
341                 /* Create a new pixmap */
342                 GdkPixmap *newmap = gdk_pixmap_new(widget->window, allocation->width, allocation->height, -1);
343                 gdk_draw_rectangle( GDK_DRAWABLE(newmap), widget->style->white_gc, TRUE, 0, 0, allocation->width, allocation->height);
344
345                 /* Copy the contents of the old pixmap */
346                 if(oldmap != NULL)
347                         gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight);
348                 
349                 /* Use the new pixmap */
350                 gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL );
351                 g_object_unref(newmap);
352         }
353 }
354