2 #include "chimara-glk-private.h"
5 #define BUFFER_SIZE (1024)
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);
11 static gboolean image_loaded;
12 static gboolean size_determined;
14 static struct image_info*
15 load_image_in_cache(glui32 image, gint width, gint height)
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;
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) );
30 struct image_info *info = g_new0(struct image_info, 1);
31 info->resource_number = image;
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 );
38 /* Scale image if necessary */
39 if(width > 0 && height > 0) {
40 gdk_pixbuf_loader_set_size(loader, width, height);
44 glk_stream_set_position(glk_data->resource_file, resource.data.startpos, seekmode_Start);
45 buffer = g_malloc( BUFFER_SIZE * sizeof(guchar) );
47 guint32 total_read = 0;
49 while(total_read < resource.length && !image_loaded) {
50 guint32 num_read = glk_get_buffer_stream(glk_data->resource_file, (char *) buffer, BUFFER_SIZE);
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);
60 total_read += num_read;
62 gdk_pixbuf_loader_close(loader, &pixbuf_error);
63 giblorb_unload_chunk(glk_data->resource_map, resource.chunknum);
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);
71 g_mutex_unlock(glk_data->resource_lock);
73 /* Store the image in the cache */
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);
80 glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache);
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);
90 g_object_unref(loader);
95 on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info)
97 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
99 g_mutex_lock(glk_data->resource_lock);
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);
108 on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data)
112 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
114 g_mutex_lock(glk_data->resource_lock);
116 g_cond_broadcast(glk_data->resource_loaded);
117 g_mutex_unlock(glk_data->resource_lock);
124 clear_image_cache(struct image_info *data, gpointer user_data)
126 gdk_pixbuf_unref(data->pixbuf);
130 static struct image_info*
131 image_cache_find(struct image_info* to_find)
133 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
134 GSList *link = glk_data->image_cache;
141 printf("Cache miss for image %d\n", to_find->resource_number);
145 /* Iterate over the cache to find the correct image and size */
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) {
153 printf("Cache hit for image %d\n", to_find->resource_number);
154 return info; /* Found a good enough match */
159 printf("Cache hit for image %d\n", to_find->resource_number);
160 return info; /* Found a match */
164 } while( (link = g_slist_next(link)) );
168 printf("Cache miss for image %d\n", to_find->resource_number);
169 return NULL; /* No match found */
173 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
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 */
180 if( !(found = image_cache_find(to_find)) ) {
181 found = load_image_in_cache(image, 0, 0);
187 *width = found->width;
189 *height = found->height;
194 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
196 VALID_WINDOW(win, return FALSE);
197 g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
199 struct image_info *to_find = g_new0(struct image_info, 1);
200 struct image_info *info;
203 /* Lookup the proper resource */
204 to_find->resource_number = image;
205 to_find->scaled = FALSE; /* we want the original image size */
207 if( !(info = image_cache_find(to_find)) ) {
208 info = load_image_in_cache(image, 0, 0);
215 gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
217 WARNING("Could not get pixmap");
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 );
223 /* Update the screen */
224 gtk_widget_queue_draw(win->widget);
233 glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height)
235 VALID_WINDOW(win, return FALSE);
236 g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
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;
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 */
248 if( !(info = image_cache_find(to_find)) ) {
249 info = load_image_in_cache(image, width, height);
256 gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
258 WARNING("Could not get pixmap");
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);
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);
275 /* Continue working with the scaled version */
279 gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
281 /* Update the screen */
282 gtk_widget_queue_draw(win->widget);
290 glk_window_set_background_color(winid_t win, glui32 color) {
291 win->background_color = color;
295 glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 width, glui32 height)
297 VALID_WINDOW(win, return);
298 g_return_if_fail(win->type == wintype_Graphics);
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);
312 glk_window_erase_rect(winid_t win, glsi32 left, glsi32 top, glui32 width, glui32 height)
314 glk_window_fill_rect(win, win->background_color, left, top, width, height);
317 void glk_window_flow_break(winid_t win)
321 /*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/
323 on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win)
326 gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL );
330 /* Determine whether a pixmap exists with the correct size */
331 gboolean needs_resize = FALSE;
335 gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
336 if(oldwidth != allocation->width || oldheight != allocation->height)
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);
345 /* Copy the contents of the old pixmap */
347 gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight);
349 /* Use the new pixmap */
350 gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL );
351 g_object_unref(newmap);