Start of image cache implementation... breaks image functionality.
[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         if( g_slist_length(glk_data->image_cache) >= IMAGE_CACHE_MAX_NUM ) {
75                 printf("Cache size exceeded\n");
76                 struct image_info *head = (struct image_info*) glk_data->image_cache->data;
77                 gdk_pixbuf_unref(head->pixbuf);
78                 g_free(head);
79                 glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache);
80         }
81         printf("Loading pixbuf\n");
82         info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
83         info->width = gdk_pixbuf_get_width(info->pixbuf);
84         info->height = gdk_pixbuf_get_height(info->pixbuf);
85         printf("Caching pixbuf\n");
86         glk_data->image_cache = g_slist_prepend(glk_data->image_cache, info);
87
88         g_object_unref(loader);
89         return info;
90 }
91
92 void
93 on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info)
94 {
95         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
96
97         g_mutex_lock(glk_data->resource_lock);
98         info->width = width;
99         info->height = height;
100         size_determined = TRUE;
101         g_cond_broadcast(glk_data->resource_info_available);
102         g_mutex_unlock(glk_data->resource_lock);
103 }
104
105 void
106 on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data)
107 {
108         gdk_threads_enter();
109
110         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
111
112         g_mutex_lock(glk_data->resource_lock);
113         image_loaded = TRUE;
114         g_cond_broadcast(glk_data->resource_loaded);
115         g_mutex_unlock(glk_data->resource_lock);
116
117         gdk_threads_leave();
118 }
119
120
121 void
122 clear_image_cache(struct image_info *data, gpointer user_data)
123 {
124         gdk_pixbuf_unref(data->pixbuf);
125         g_free(data);
126 }
127
128 static struct image_info*
129 image_cache_find(struct image_info* to_find)
130 {
131         printf("Finding image %d\n", to_find->resource_number);
132         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
133         GSList *link = glk_data->image_cache;
134
135         /* Empty cache */
136         if(link == NULL) {
137                 printf("Cache is empty\n");
138                 return NULL;
139         }
140
141         /* Iterate over the cache to find the correct image and size */
142         do {
143                 struct image_info *info = (struct image_info*) link->data;
144                 printf("Examining cache entry %d\n", info->resource_number);
145                 if(info->resource_number == to_find->resource_number) {
146                         /* Check size: are we looking for a scaled version or the original one? */
147                         if(to_find->scaled) {
148                                 if(info->width >= to_find->width && info->height >= to_find->height) {
149                                         return info; /* Found a good enough match */
150                                 }
151                         } else {
152                                 if(!info->scaled) {
153                                         printf("Cache hit\n");
154                                         return info; /* Found a match */
155                                 }
156                         }
157                 }
158         } while( (link = g_slist_next(link)) );
159
160         return NULL; /* No match found */
161 }
162
163 glui32
164 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
165 {
166         printf("get_info(%d)\n", image);
167         struct image_info *to_find = g_new0(struct image_info, 1);
168         struct image_info *found;
169         to_find->resource_number = image;
170         to_find->scaled = FALSE; /* we want the original image size */
171
172         if( !(found = image_cache_find(to_find)) ) {
173                 printf("Cache miss for %d\n", image);
174                 found = load_image_in_cache(image, 0, 0);
175                 if(found == NULL)
176                         return FALSE;
177         } else {
178                 printf("Cache hit for %d\n", image);
179         }
180
181         if(width != NULL)
182                 *width = found->width;
183         if(width != NULL)
184                 *height = found->height;
185         return TRUE;
186 }
187
188 glui32
189 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
190 {
191         printf("image_draw(%d)\n", image);
192         VALID_WINDOW(win, return FALSE);
193         g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
194
195         struct image_info *to_find = g_new0(struct image_info, 1);
196         struct image_info *info;
197         GdkPixmap *canvas;
198
199         /* Lookup the proper resource */
200         to_find->resource_number = image;
201         to_find->scaled = FALSE; /* we want the original image size */
202
203         if( !(info = image_cache_find(to_find)) ) {
204                 info = load_image_in_cache(image, 0, 0);
205                 if(info == NULL)
206                         return FALSE;
207         }
208
209         gdk_threads_enter();
210
211         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
212         if(canvas == NULL) {
213                 WARNING("Could not get pixmap");
214                 return FALSE;
215         }
216
217         printf("image info: %d x %d, scaled=%d, pixbufaddr=%d\n", info->width, info->height, (int)info->scaled, (int)info->pixbuf);
218         gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
219
220         /* Update the screen */
221         gtk_widget_queue_draw(win->widget);
222
223         gdk_threads_leave();
224
225         return TRUE;
226 }
227
228
229 glui32
230 glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height)
231 {
232         VALID_WINDOW(win, return FALSE);
233         g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
234
235         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
236         struct image_info *to_find = g_new0(struct image_info, 1);
237         struct image_info *info;
238         struct image_info *scaled_info;
239         GdkPixmap *canvas;
240
241         /* Lookup the proper resource */
242         to_find->resource_number = image;
243         to_find->scaled = TRUE; /* any image size equal or larger than requested will do */
244
245         if( !(info = image_cache_find(to_find)) ) {
246                 info = load_image_in_cache(image, width, height);
247                 if(info == NULL)
248                         return FALSE;
249         }
250
251         gdk_threads_enter();
252
253         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
254         if(canvas == NULL) {
255                 WARNING("Could not get pixmap");
256                 return FALSE;
257         }
258
259         /* Scale the image if necessary */
260         if(info->width != width || info->height != height) {
261                 GdkPixbuf *scaled = gdk_pixbuf_scale_simple(info->pixbuf, width, height, GDK_INTERP_BILINEAR);
262
263                 /* Add the scaled image into the image cache */
264                 scaled_info = g_new0(struct image_info, 1);
265                 scaled_info->resource_number = info->resource_number;
266                 scaled_info->width = gdk_pixbuf_get_width(scaled);
267                 scaled_info->height = gdk_pixbuf_get_width(scaled);
268                 scaled_info->pixbuf = scaled;
269                 scaled_info->scaled = TRUE;
270                 glk_data->image_cache = g_slist_prepend(glk_data->image_cache, scaled_info);
271
272                 /* Continue working with the scaled version */
273                 info = scaled_info;
274         }
275
276         gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
277
278         /* Update the screen */
279         gtk_widget_queue_draw(win->widget);
280
281         gdk_threads_leave();
282
283         return TRUE;
284 }
285
286 void
287 glk_window_set_background_color(winid_t win, glui32 color) {
288         win->background_color = color;
289 }
290
291 void
292 glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 width, glui32 height)
293 {
294         VALID_WINDOW(win, return);
295         g_return_if_fail(win->type == wintype_Graphics);
296
297
298         gdk_threads_enter();
299
300         GdkPixmap *map;
301         gtk_image_get_pixmap( GTK_IMAGE(win->widget), &map, NULL );
302         gdk_draw_rectangle( GDK_DRAWABLE(map), win->widget->style->white_gc, TRUE, left, top, width, height);
303         gtk_widget_queue_draw(win->widget);
304
305         gdk_threads_leave();
306 }
307
308 void
309 glk_window_erase_rect(winid_t win, glsi32 left, glsi32 top, glui32 width, glui32 height)
310 {
311         glk_window_fill_rect(win, win->background_color, left, top, width, height);
312 }
313
314 void glk_window_flow_break(winid_t win)
315 {
316 }
317
318 /*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/
319 void
320 on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win)
321
322         GdkPixmap *oldmap;
323         gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL );
324         gint oldwidth = 0;
325         gint oldheight = 0;
326  
327         /* Determine whether a pixmap exists with the correct size */
328         gboolean needs_resize = FALSE;
329         if(oldmap == NULL)
330                 needs_resize = TRUE;
331         else {
332                 gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
333                 if(oldwidth != allocation->width || oldheight != allocation->height)
334                         needs_resize = TRUE;
335         }
336
337         if(needs_resize) {
338                 /* Create a new pixmap */
339                 GdkPixmap *newmap = gdk_pixmap_new(widget->window, allocation->width, allocation->height, -1);
340                 gdk_draw_rectangle( GDK_DRAWABLE(newmap), widget->style->white_gc, TRUE, 0, 0, allocation->width, allocation->height);
341
342                 /* Copy the contents of the old pixmap */
343                 if(oldmap != NULL)
344                         gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight);
345                 
346                 /* Use the new pixmap */
347                 gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL );
348                 g_object_unref(newmap);
349         }
350 }
351