9 #include <gdk-pixbuf/gdk-pixbuf.h>
12 unsigned char r,g,b,alpha;
15 void gai_display_error_continue(const char *);
16 GdkPixbuf *gai_load_image(const char *);
22 #include "grabscreen.h"
23 #include "background.h"
26 static Background_settings background_settings;
27 static AquariumData *ad;
29 Background_settings *background_get_settings_ptr(void)
31 return &background_settings;
34 static void background_prepare_solid(unsigned char r, unsigned char g, unsigned b)
38 for (i = 0; i < ad->ymax; i++) {
39 for (j = 0; j < ad->xmax; j++) {
40 t = (i * ad->xmax * 3) + j * 3;
49 static void background_prepare_shaded(unsigned char r1, unsigned char g1, unsigned b1,
50 unsigned char r2, unsigned char g2, unsigned b2)
55 d1 = ((float)r2 - (float)r1) / (float) ad->ymax;
56 d2 = ((float)g2 - (float)g1) / (float) ad->ymax;
57 d3 = ((float)b2 - (float)b1) / (float) ad->ymax;
59 for (i = 0; i < ad->ymax; i++) {
60 for (j = 0; j < ad->xmax; j++) {
61 t = (i * ad->xmax * 3) + j * 3;
62 ad->bgr[t + 0] = (unsigned char)((float)r1 + ((float) i * d1));
63 ad->bgr[t + 1] = (unsigned char)((float)g1 + ((float) i * d2));
64 ad->bgr[t + 2] = (unsigned char)((float)b1 + ((float) i * d3));
71 static void background_prepare_image(char *image_file, int user_choise)
73 GdkPixbuf *water=NULL, *water2=NULL;
76 int i, j, k=0, y, rowstride, alpha;
79 water = gdk_pixbuf_new_from_file(image_file, &imerr);
81 gai_display_error_continue("Can't load image!\nChanging to default water image background.");
82 water = gai_load_image("water.png");
85 water = gai_load_image("water.png");
88 water2 = gdk_pixbuf_scale_simple(water, ad->xmax, ad->ymax,
90 buff = gdk_pixbuf_get_pixels(water2);
92 rowstride = gdk_pixbuf_get_rowstride(water2);
93 alpha = (int)gdk_pixbuf_get_has_alpha(water2);
95 for (i = 0; i < ad->ymax; i++) {
97 for (j = 0; j < ((ad->xmax * (3+alpha))); j += (3+alpha)) {
98 ad->bgr[k + 0] = buff[y + j + 0];
99 ad->bgr[k + 1] = buff[y + j + 1];
100 ad->bgr[k + 2] = buff[y + j + 2];
105 g_object_unref(water);
106 g_object_unref(water2);
110 static void background_prepare_desktop(void)
112 int rowstride, alpha, i, j, k=0, y;
113 unsigned char *buff, *str;
114 GdkPixbuf *desktop, *tmp_desktop;
119 win = gdk_get_default_root_window();
120 str = getenv("SDL_WINDOWID");
123 xwin = GDK_WINDOW_XWINDOW(win);
126 xwin = (Window)atoi(str);
127 display = XOpenDisplay(NULL);
128 grab_screen_image(ScreenOfDisplay(display, DefaultScreen(display)), xwin);
132 desktop = gdk_pixbuf_get_from_drawable(NULL,
135 gdk_screen_width(), gdk_screen_height());
137 if(gdk_screen_width() != ad->xmax || gdk_screen_height() != ad->ymax){
138 tmp_desktop = gdk_pixbuf_scale_simple(desktop, ad->xmax, ad->ymax,
139 GDK_INTERP_BILINEAR);
140 g_object_unref(desktop);
141 desktop = tmp_desktop;
144 buff = gdk_pixbuf_get_pixels(desktop);
146 rowstride = gdk_pixbuf_get_rowstride(desktop);
147 alpha = (int)gdk_pixbuf_get_has_alpha(desktop);
149 for (i = 0; i < ad->ymax; i++) {
151 for (j = 0; j < ((ad->xmax * (3+alpha))); j += (3+alpha)) {
152 ad->bgr[k + 0] = buff[y + j + 0];
153 ad->bgr[k + 1] = buff[y + j + 1];
154 ad->bgr[k + 2] = buff[y + j + 2];
159 g_object_unref(desktop);
162 void background_exit(void)
167 void background_init(void)
169 ad = aquarium_get_settings_ptr();
171 if(background_settings.desktop){
172 background_prepare_desktop();
177 if(background_settings.type == BG_SOLID)
178 background_prepare_solid(background_settings.solid_c.r,
179 background_settings.solid_c.g,
180 background_settings.solid_c.b);
182 if(background_settings.type == BG_SHADED)
183 background_prepare_shaded(background_settings.shaded_top_c.r,
184 background_settings.shaded_top_c.g,
185 background_settings.shaded_top_c.b,
186 background_settings.shaded_bot_c.r,
187 background_settings.shaded_bot_c.g,
188 background_settings.shaded_bot_c.b);
190 if(background_settings.type == BG_IMAGE)
191 background_prepare_image(background_settings.imagename, 1);
193 if(background_settings.type == BG_WATER)
194 background_prepare_image(NULL,0);