X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=libchimara%2Fgraphics.c;h=ab06d8f6683fde919d14ab737777d4136fbc83c5;hb=1e0dc5378f314f555e3b923c6d95f5017abd528b;hp=a8a0318bd86ff19838c80a07a0849f284c851862;hpb=f92585f76bce0b8d1efc11d2b2020a0f516234c5;p=projects%2Fchimara%2Fchimara.git diff --git a/libchimara/graphics.c b/libchimara/graphics.c index a8a0318..ab06d8f 100644 --- a/libchimara/graphics.c +++ b/libchimara/graphics.c @@ -4,7 +4,7 @@ #define BUFFER_SIZE (1024) -extern GPrivate *glk_data_key; +extern GPrivate glk_data_key; void on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info); void on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data); glui32 draw_image_common(winid_t win, GdkPixbuf *pixbuf, glsi32 val1, glsi32 val2); @@ -13,25 +13,12 @@ static gboolean image_loaded; static gboolean size_determined; static struct image_info* -load_image_in_cache(glui32 image, gint width, gint height) +load_image_from_blorb(giblorb_result_t resource, glui32 image, gint width, gint height) { - ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); - giblorb_err_t blorb_error = 0; - giblorb_result_t resource; + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); GError *pixbuf_error = NULL; guchar *buffer; - /* Lookup the proper resource */ - if(!glk_data->resource_map) { - WARNING("No resource map has been loaded yet."); - return NULL; - } - blorb_error = giblorb_load_resource(glk_data->resource_map, giblorb_method_FilePos, &resource, giblorb_ID_Pict, image); - if(blorb_error != giblorb_err_None) { - WARNING_S( "Error loading resource", giblorb_get_error_message(blorb_error) ); - return NULL; - } - struct image_info *info = g_new0(struct image_info, 1); info->resource_number = image; @@ -69,44 +56,104 @@ load_image_in_cache(glui32 image, gint width, gint height) g_free(buffer); /* Wait for the PixbufLoader to finish loading the image */ - g_mutex_lock(glk_data->resource_lock); + g_mutex_lock(&glk_data->resource_lock); while(!image_loaded) { - g_cond_wait(glk_data->resource_loaded, glk_data->resource_lock); + g_cond_wait(&glk_data->resource_loaded, &glk_data->resource_lock); + } + g_mutex_unlock(&glk_data->resource_lock); + + info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); + g_object_ref(info->pixbuf); + + g_object_unref(loader); + return info; +} + +static struct image_info * +load_image_from_file(const gchar *filename, glui32 image, gint width, gint height) +{ + GError *err = NULL; + + struct image_info *info = g_new0(struct image_info, 1); + info->resource_number = image; + + if(width > 0 && height > 0) { + info->scaled = TRUE; + info->pixbuf = gdk_pixbuf_new_from_file_at_size(filename, width, height, &err); + } else { + info->pixbuf = gdk_pixbuf_new_from_file(filename, &err); + } + if(!info->pixbuf) { + IO_WARNING("Error loading resource from alternative location", filename, err->message); + g_error_free(err); + g_free(info); + return NULL; + } + g_object_ref(info->pixbuf); + + return info; +} + +static struct image_info* +load_image_in_cache(glui32 image, gint width, gint height) +{ + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); + struct image_info *info = NULL; + + /* Lookup the proper resource */ + if(!glk_data->resource_map) { + if(!glk_data->resource_load_callback) { + WARNING("No resource map has been loaded yet."); + return NULL; + } + gchar *filename = glk_data->resource_load_callback(CHIMARA_RESOURCE_IMAGE, image, glk_data->resource_load_callback_data); + if(!filename) { + WARNING("Error loading resource from alternative location"); + return NULL; + } + info = load_image_from_file(filename, image, width, height); + g_free(filename); + } else { + giblorb_result_t resource; + giblorb_err_t blorb_error = giblorb_load_resource(glk_data->resource_map, giblorb_method_FilePos, &resource, giblorb_ID_Pict, image); + if(blorb_error != giblorb_err_None) { + WARNING_S( "Error loading resource", giblorb_get_error_message(blorb_error) ); + return NULL; + } + info = load_image_from_blorb(resource, image, width, height); } - g_mutex_unlock(glk_data->resource_lock); + + if(info == NULL) + return NULL; /* Store the image in the cache */ gdk_threads_enter(); if( g_slist_length(glk_data->image_cache) >= IMAGE_CACHE_MAX_NUM ) { struct image_info *head = (struct image_info*) glk_data->image_cache->data; - gdk_pixbuf_unref(head->pixbuf); + g_object_unref(head->pixbuf); g_free(head); glk_data->image_cache = g_slist_remove_link(glk_data->image_cache, glk_data->image_cache); } - info->pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); - gdk_pixbuf_ref(info->pixbuf); info->width = gdk_pixbuf_get_width(info->pixbuf); info->height = gdk_pixbuf_get_height(info->pixbuf); glk_data->image_cache = g_slist_prepend(glk_data->image_cache, info); gdk_threads_leave(); - - g_object_unref(loader); return info; } void on_size_prepared(GdkPixbufLoader *loader, gint width, gint height, struct image_info *info) { - ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); - g_mutex_lock(glk_data->resource_lock); + g_mutex_lock(&glk_data->resource_lock); info->width = width; info->height = height; size_determined = TRUE; - g_cond_broadcast(glk_data->resource_info_available); - g_mutex_unlock(glk_data->resource_lock); + g_cond_broadcast(&glk_data->resource_info_available); + g_mutex_unlock(&glk_data->resource_lock); } void @@ -114,12 +161,12 @@ on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data) { gdk_threads_enter(); - ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); - g_mutex_lock(glk_data->resource_lock); + g_mutex_lock(&glk_data->resource_lock); image_loaded = TRUE; - g_cond_broadcast(glk_data->resource_loaded); - g_mutex_unlock(glk_data->resource_lock); + g_cond_broadcast(&glk_data->resource_loaded); + g_mutex_unlock(&glk_data->resource_lock); gdk_threads_leave(); } @@ -128,14 +175,14 @@ on_pixbuf_closed(GdkPixbufLoader *loader, gpointer data) void clear_image_cache(struct image_info *data, gpointer user_data) { - gdk_pixbuf_unref(data->pixbuf); + g_object_unref(data->pixbuf); g_free(data); } static struct image_info* image_cache_find(struct image_info* to_find) { - ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); GSList *link = glk_data->image_cache; gdk_threads_enter(); @@ -185,11 +232,11 @@ image_cache_find(struct image_info* to_find) * @height: Pointer to a location at which to store the image's height. * * This gets information about the image resource with the given identifier. It - * returns %TRUE if there is such an image, and %FALSE if not. You can also pass - * pointers to width and height variables; if the image exists, the variables - * will be filled in with the width and height of the image, in pixels. (You can - * pass %NULL for either width or height if you don't care about that - * information.) + * returns %TRUE (1) if there is such an image, and %FALSE (0) if not. You can + * also pass pointers to width and height variables; if the image exists, the + * variables will be filled in with the width and height of the image, in + * pixels. (You can pass %NULL for either width or height if you don't care + * about that information.) * * * You should always use this function to measure the size of images when you @@ -304,7 +351,7 @@ glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui3 VALID_WINDOW(win, return FALSE); g_return_val_if_fail(win->type == wintype_Graphics || win->type == wintype_TextBuffer, FALSE); - ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key); + ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key); struct image_info *to_find = g_new0(struct image_info, 1); struct image_info *info; struct image_info *scaled_info; @@ -339,38 +386,39 @@ glk_image_draw_scaled(winid_t win, glui32 image, glsi32 val1, glsi32 val2, glui3 return draw_image_common(win, info->pixbuf, val1, val2); } -/* Internal function: draws a pixbuf to a graphics window of text buffer */ +/* Internal function: draws a pixbuf to a graphics window or text buffer */ glui32 draw_image_common(winid_t win, GdkPixbuf *pixbuf, glsi32 val1, glsi32 val2) { - GdkPixmap *canvas; - gdk_threads_enter(); - switch(win->type) { case wintype_Graphics: { - gtk_image_get_pixmap( GTK_IMAGE(win->widget), &canvas, NULL ); - if(canvas == NULL) { - WARNING("Could not get pixmap"); - return FALSE; - } + gdk_threads_enter(); - gdk_draw_pixbuf( GDK_DRAWABLE(canvas), NULL, pixbuf, 0, 0, val1, val2, -1, -1, GDK_RGB_DITHER_NONE, 0, 0 ); + cairo_t *cr = cairo_create(win->backing_store); + gdk_cairo_set_source_pixbuf(cr, pixbuf, val1, val2); + cairo_paint(cr); + cairo_destroy(cr); /* Update the screen */ gtk_widget_queue_draw(win->widget); + + gdk_threads_leave(); } break; case wintype_TextBuffer: { + flush_window_buffer(win); + + gdk_threads_enter(); + GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) ); GtkTextIter end, start; gtk_text_buffer_get_end_iter(buffer, &end); - start = end; - flush_window_buffer(win); gtk_text_buffer_insert_pixbuf(buffer, &end, pixbuf); + start = end; gtk_text_iter_forward_char(&end); gint height = 0; @@ -390,12 +438,11 @@ draw_image_common(winid_t win, GdkPixbuf *pixbuf, glsi32 val1, glsi32 val2) GtkTextTag *tag = gtk_text_buffer_create_tag(buffer, NULL, "rise", PANGO_SCALE * (-height), NULL); gtk_text_buffer_apply_tag(buffer, tag, &start, &end); } + + gdk_threads_leave(); } break; - } - - gdk_threads_leave(); return TRUE; } @@ -431,6 +478,16 @@ glk_window_set_background_color(winid_t win, glui32 color) win->background_color = color; } +static void +glkcairo_set_source_glkcolor(cairo_t *cr, glui32 val) +{ + double r, g, b; + r = ((val & 0xff0000) >> 16) / 256.0; + g = ((val & 0x00ff00) >> 8) / 256.0; + b = (val & 0x0000ff) / 256.0; + cairo_set_source_rgb(cr, r, g, b); +} + /** * glk_window_fill_rect: * @win: A graphics window. @@ -452,16 +509,12 @@ glk_window_fill_rect(winid_t win, glui32 color, glsi32 left, glsi32 top, glui32 gdk_threads_enter(); - GdkPixmap *map; - gtk_image_get_pixmap( GTK_IMAGE(win->widget), &map, NULL ); - - GdkGC *gc = gdk_gc_new(map); - GdkColor gdkcolor; - glkcolor_to_gdkcolor(color, &gdkcolor); - gdk_gc_set_rgb_fg_color(gc, &gdkcolor); - gdk_draw_rectangle( GDK_DRAWABLE(map), gc, TRUE, left, top, width, height); + cairo_t *cr = cairo_create(win->backing_store); + glkcairo_set_source_glkcolor(cr, color); + cairo_rectangle(cr, (double)left, (double)top, (double)width, (double)height); + cairo_fill(cr); gtk_widget_queue_draw(win->widget); - g_object_unref(gc); + cairo_destroy(cr); gdk_threads_leave(); } @@ -527,37 +580,54 @@ void glk_window_flow_break(winid_t win) VALID_WINDOW(win, return); } -/*** Called when the graphics window is resized. Resize the backing pixmap if necessary ***/ -void -on_graphics_size_allocate(GtkWidget *widget, GtkAllocation *allocation, winid_t win) -{ - GdkPixmap *oldmap; - gtk_image_get_pixmap( GTK_IMAGE(widget), &oldmap, NULL ); - gint oldwidth = 0; - gint oldheight = 0; - - /* Determine whether a pixmap exists with the correct size */ +/* Called when the graphics window is resized, restacked, or moved. Resize the +backing store if necessary. */ +gboolean +on_graphics_configure(GtkWidget *widget, GdkEventConfigure *event, winid_t win) +{ + int oldwidth = 0, oldheight = 0; + + /* Determine whether the backing store can stay the same size */ gboolean needs_resize = FALSE; - if(oldmap == NULL) + if(win->backing_store == NULL) needs_resize = TRUE; else { - gdk_drawable_get_size( GDK_DRAWABLE(oldmap), &oldwidth, &oldheight ); - if(oldwidth != allocation->width || oldheight != allocation->height) + oldwidth = cairo_image_surface_get_width(win->backing_store); + oldheight = cairo_image_surface_get_height(win->backing_store); + if(oldwidth != event->width || oldheight != event->height) needs_resize = TRUE; } if(needs_resize) { - /* Create a new pixmap */ - GdkPixmap *newmap = gdk_pixmap_new(widget->window, allocation->width, allocation->height, -1); - gdk_draw_rectangle( GDK_DRAWABLE(newmap), widget->style->white_gc, TRUE, 0, 0, allocation->width, allocation->height); - - /* Copy the contents of the old pixmap */ - if(oldmap != NULL) - gdk_draw_drawable( GDK_DRAWABLE(newmap), widget->style->white_gc, GDK_DRAWABLE(oldmap), 0, 0, 0, 0, oldwidth, oldheight); - - /* Use the new pixmap */ - gtk_image_set_from_pixmap( GTK_IMAGE(widget), newmap, NULL ); - g_object_unref(newmap); + /* Create a new backing store */ + cairo_surface_t *new_backing_store = gdk_window_create_similar_surface( gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, gtk_widget_get_allocated_width(widget), gtk_widget_get_allocated_height(widget) ); + cairo_t *cr = cairo_create(new_backing_store); + + /* Clear to background color */ + glkcairo_set_source_glkcolor(cr, win->background_color); + cairo_paint(cr); + + if(win->backing_store != NULL) { + /* Copy the contents of the old backing store */ + cairo_set_source_surface(cr, win->backing_store, 0, 0); + cairo_paint(cr); + cairo_surface_destroy(win->backing_store); + } + + cairo_destroy(cr); + /* Use the new backing store */ + win->backing_store = new_backing_store; } + + return TRUE; /* Event handled, stop processing */ } +/* Draw the backing store to the screen. Called whenever the drawing area is +exposed. */ +gboolean +on_graphics_draw(GtkWidget *widget, cairo_t *cr, winid_t win) +{ + cairo_set_source_surface(cr, win->backing_store, 0, 0); + cairo_paint(cr); + return FALSE; +}