/* * Gnome Set game - bgrnd.c * Copyright 2007 Kirill Gorelov * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "bgrnd.h" #include "ui.h" #include "cfg.h" #include "debug.h" /* Global background variable */ bgrnd_t bg; /* Loads the background from a file */ void bgrnd_load(gchar *filename) { GError *err = NULL; DBG(BGRND_DEBUG, "Loading background %s\n", filename); bg.orig = gdk_pixbuf_new_from_file(filename, &err); if (bg.orig == NULL) { printf("Error: %s\n", err->message); g_assert_not_reached(); } bgrnd_flush(TRUE); } void bgrnd_flush(gboolean flag) { DBG(BGRND_DEBUG, "Flushing background\n"); /* Do we need to recreate the background? */ if (flag) { if (bg.gc) { g_object_unref(bg.gc); bg.gc = NULL; } if (bg.scaled) { g_object_unref(bg.scaled); bg.scaled = NULL; } } /* Create a gc to paint a tiled background */ if (cfg.bgrnd_tiled) { if (bg.gc) return; if (bg.scaled) { g_object_unref(bg.scaled); bg.scaled = NULL; } GdkPixmap *tile; GtkWidget *gsetdrawing = glade_xml_get_widget (ui.xml, "gsetdrawing"); gdk_pixbuf_render_pixmap_and_mask_for_colormap (bg.orig, gdk_colormap_get_system(), &tile, NULL, 127); bg.gc = gdk_gc_new(gsetdrawing->window); gdk_gc_set_tile(bg.gc, tile); gdk_gc_set_fill(bg.gc, GDK_TILED); g_object_unref(tile); return; } /* Draw a scaled background */ if (bg.gc) { g_object_unref(bg.gc); bg.gc = NULL; } /* rescale the background if necessary */ GtkWidget *widget = glade_xml_get_widget(ui.xml, "gsetdrawing"); if (bg.scaled == NULL || widget->allocation.width != gdk_pixbuf_get_width(bg.scaled) || widget->allocation.height != gdk_pixbuf_get_height(bg.scaled)) { if (bg.scaled) g_object_unref(bg.scaled); bg.scaled = gdk_pixbuf_scale_simple(bg.orig, widget->allocation.width, widget->allocation.height, GDK_INTERP_BILINEAR); } } /* Draw to the pixmap */ void bgrnd_draw(GdkPixmap *canvas, gint x, gint y, gint width, gint height) { DBG(BGRND_DEBUG, "Drawing background\n"); if (bg.scaled) { gdk_draw_pixbuf(canvas, bg.gc, bg.scaled, x, y, x, y, width, height, GDK_RGB_DITHER_NONE, 0, 0); return; } gdk_draw_rectangle(canvas, bg.gc, TRUE, x, y, width, height); } /* Free resources */ void bgrnd_free(void) { DBG(BGRND_DEBUG, "Freeing background\n"); if (bg.orig) g_object_unref(bg.orig); if (bg.scaled) g_object_unref(bg.scaled); if (bg.gc) g_object_unref(bg.gc); }