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 */
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);
79 glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache);
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);
88 g_object_unref(loader);
93 on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info)
95 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
97 g_mutex_lock(glk_data->resource_lock);
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);
106 on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data)
110 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
112 g_mutex_lock(glk_data->resource_lock);
114 g_cond_broadcast(glk_data->resource_loaded);
115 g_mutex_unlock(glk_data->resource_lock);
122 clear_image_cache(struct image_info *data, gpointer user_data)
124 gdk_pixbuf_unref(data->pixbuf);
128 static struct image_info*
129 image_cache_find(struct image_info* to_find)
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;
137 printf("Cache is empty\n");
141 /* Iterate over the cache to find the correct image and size */
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 */
153 printf("Cache hit\n");
154 return info; /* Found a match */
158 } while( (link = g_slist_next(link)) );
160 return NULL; /* No match found */
164 glk_image_get_info(glui32 image, glui32 *width, glui32 *height)
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 */
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);
178 printf("Cache hit for %d\n", image);
182 *width = found->width;
184 *height = found->height;
189 glk_image_draw(winid_t win, glui32 image, glsi32 val1, glsi32 val2)
191 printf("image_draw(%d)\n", image);
192 VALID_WINDOW(win, return FALSE);
193 g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
195 struct image_info *to_find = g_new0(struct image_info, 1);
196 struct image_info *info;
199 /* Lookup the proper resource */
200 to_find->resource_number = image;
201 to_find->scaled = FALSE; /* we want the original image size */
203 if( !(info = image_cache_find(to_find)) ) {
204 info = load_image_in_cache(image, 0, 0);
211 gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
213 WARNING("Could not get pixmap");
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 );
220 /* Update the screen */
221 gtk_widget_queue_draw(win->widget);
230 glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui32 width, glui32 height)
232 VALID_WINDOW(win, return FALSE);
233 g_return_val_if_fail(win->type == wintype_Graphics, FALSE);
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;
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 */
245 if( !(info = image_cache_find(to_find)) ) {
246 info = load_image_in_cache(image, width, height);
253 gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL );
255 WARNING("Could not get pixmap");
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);
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);
272 /* Continue working with the scaled version */
276 gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, info->pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 );
278 /* Update the screen */
279 gtk_widget_queue_draw(win->widget);
287 glk_window_set_background_color(winid_t win, glui32 color) {
288 win->background_color = color;
292 glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 width, glui32 height)
294 VALID_WINDOW(win, return);
295 g_return_if_fail(win->type == wintype_Graphics);
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);
309 glk_window_erase_rect(winid_t win, glsi32 left, glsi32 top, glui32 width, glui32 height)
311 glk_window_fill_rect(win, win->background_color, left, top, width, height);
314 void glk_window_flow_break(winid_t win)
318 /*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/
320 on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win)
323 gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL );
327 /* Determine whether a pixmap exists with the correct size */
328 gboolean needs_resize = FALSE;
332 gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight );
333 if(oldwidth != allocation->width || oldheight != allocation->height)
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);
342 /* Copy the contents of the old pixmap */
344 gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight);
346 /* Use the new pixmap */
347 gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL );
348 g_object_unref(newmap);