+ if (DelayReached(&check_delay))
+ {
+ percent = (debug ? debug_value * 100 / XSN_DEBUG_STEPS : xsn_percent());
+
+ if (debug)
+ setup.debug.xsn_percent = percent;
+
+ if (setup.debug.xsn_mode != AUTO)
+ percent = setup.debug.xsn_percent;
+
+ setup.debug.xsn_percent = percent;
+
+ active = (percent > 0);
+
+ if ((active && !active_last) || setup.debug.xsn_mode != AUTO)
+ removeHideSetupEntry(&setup.debug.xsn_mode);
+ else if (!active && active_last)
+ setHideSetupEntry(&setup.debug.xsn_mode);
+
+ if (setup.debug.xsn_mode == FALSE)
+ active = FALSE;
+ }
+ else if (tile_cursor.xsn_debug)
+ {
+ debug_value = (active ? 0 : MIN(debug_value + 1, XSN_DEBUG_STEPS));
+ debug = TRUE;
+ active = FALSE;
+
+ ResetDelayCounter(&check_delay);
+
+ setup.debug.xsn_mode = (debug_value > 0);
+ tile_cursor.xsn_debug = FALSE;
+ }
+
+ if (!active)
+ return;
+
+ if (!initialized)
+ {
+ xsn.area_xsize = gfx.win_xsize;
+ xsn.area_ysize = gfx.win_ysize;
+
+ for (i = 0; i < num_xsn_data; i++)
+ {
+ int size = xsn_data[i].size;
+ byte *bits = xsn_data[i].bits;
+ Bitmap *bitmap = CreateBitmap(size, size, DEFAULT_DEPTH);
+
+ FillRectangle(bitmap, 0, 0, size, size, BLACK_PIXEL);
+
+ for (y = 0; y < size; y++)
+ for (x = 0; x < size; x++)
+ if ((bits[y] >> x) & 0x01)
+ SDLPutPixel(bitmap, x, y, WHITE_PIXEL);
+
+ SDL_Surface *surface = bitmap->surface;
+
+ if ((bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
+ Fail("SDLGetNativeSurface() failed");
+
+ SDL_Surface *surface_masked = bitmap->surface_masked;
+
+ SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
+
+ SDLSetAlpha(surface, TRUE, XSN_ALPHA_DEFAULT);
+ SDLSetAlpha(surface_masked, TRUE, XSN_ALPHA_DEFAULT);
+
+ xsn_data[i].bitmap = bitmap;
+ }
+
+ srand((unsigned int)time(NULL));
+
+ initialized = TRUE;
+ }
+
+ if (!active_last)
+ {
+ start_delay.value = (debug || setup.debug.xsn_mode == TRUE ? 0 :
+ (XSN_START_DELAY + XSN_RND(XSN_START_DELAY)) * 1000);
+ started = FALSE;
+
+ ResetDelayCounter(&start_delay);
+
+ reinitialize = TRUE;
+ }
+
+ if (reinitialize)
+ {
+ xsn.num_items = 0;
+ xsn.max_items = percent * XSN_MAX_ITEMS / 100;
+ xsn.max_height = percent * XSN_MAX_HEIGHT / 100;
+
+ xsn.max_dx = XSN_MAX_DX;
+ xsn.max_dy = XSN_MAX_DY;
+
+ xsn.change_delay = XSN_CHANGE_DELAY;
+ xsn.change_type = 0;
+ xsn.change_dir = 0;
+
+ xsn.alpha = XSN_ALPHA_DEFAULT;
+
+ for (i = 0; i < xsn.max_items; i++)
+ xsn_init_item(i);
+ }
+
+ if (xsn.area_xsize != gfx.win_xsize ||
+ xsn.area_ysize != gfx.win_ysize ||
+ reinitialize)
+ {
+ xsn.area_xsize = gfx.win_xsize;
+ xsn.area_ysize = gfx.win_ysize;
+
+ if (xsn.bitmap != NULL)
+ FreeBitmap(xsn.bitmap);
+
+ xsn.bitmap = CreateBitmap(xsn.area_xsize, xsn.max_height * 2,
+ DEFAULT_DEPTH);
+
+ FillRectangle(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
+ BLACK_PIXEL);
+
+ SDL_Surface *surface = xsn.bitmap->surface;
+
+ if ((xsn.bitmap->surface_masked = SDLGetNativeSurface(surface)) == NULL)
+ Fail("SDLGetNativeSurface() failed");
+
+ SDL_Surface *surface_masked = xsn.bitmap->surface_masked;
+
+ SDL_SetColorKey(surface_masked, SET_TRANSPARENT_PIXEL,
+ SDL_MapRGB(surface_masked->format, 0x00, 0x00, 0x00));
+
+ SDLSetAlpha(surface, TRUE, xsn.alpha);
+ SDLSetAlpha(surface_masked, TRUE, xsn.alpha);
+
+ SDLCreateBitmapTextures(xsn.bitmap);
+
+ for (i = 0; i < num_xsn_data; i++)
+ {
+ SDLFreeBitmapTextures(xsn_data[i].bitmap);
+ SDLCreateBitmapTextures(xsn_data[i].bitmap);
+ }
+
+ if (xsn.height != NULL)
+ checked_free(xsn.height);
+
+ xsn.height = checked_calloc(xsn.area_xsize * sizeof(int));
+
+ for (i = 0; i < xsn.area_xsize; i++)
+ xsn.height[i] = xsn.area_ysize - 1;
+ }
+
+ if (!started)
+ {
+ if (!DelayReached(&start_delay))
+ return;
+
+ update_delay.value = XSN_UPDATE_DELAY;
+ growth_delay.value = XSN_GROWTH_DELAY * 1000;
+ change_delay.value = XSN_CHANGE_DELAY * 1000;
+
+ ResetDelayCounter(&growth_delay);
+ ResetDelayCounter(&update_delay);
+ ResetDelayCounter(&change_delay);
+
+ started = TRUE;
+ }
+
+ if (xsn.num_items < xsn.max_items)
+ {
+ if (DelayReached(&growth_delay))
+ {
+ xsn.num_items += XSN_RND(XSN_GROWTH_RATE * 2);
+ xsn.num_items = MIN(xsn.num_items, xsn.max_items);
+ }
+ }
+
+ if (DelayReached(&update_delay))
+ {
+ for (i = 0; i < xsn.num_items; i++)
+ xsn_update_item(i);
+ }
+
+ if (DelayReached(&change_delay))
+ {
+ xsn_update_change();
+
+ change_delay.value = xsn.change_delay * 1000;
+ }
+
+ int xsn_alpha_dx = (gfx.mouse_y > xsn.area_ysize - xsn.max_height ?
+ (xsn.alpha > XSN_ALPHA_VISIBLE ? -1 : 0) :
+ (xsn.alpha < XSN_ALPHA_DEFAULT ? +1 : 0));
+
+ if (xsn_alpha_dx != 0)
+ {
+ xsn.alpha += xsn_alpha_dx;
+
+ SDLSetAlpha(xsn.bitmap->surface_masked, TRUE, xsn.alpha);
+
+ SDLFreeBitmapTextures(xsn.bitmap);
+ SDLCreateBitmapTextures(xsn.bitmap);
+ }
+
+ BlitToScreenMasked(xsn.bitmap, 0, 0, xsn.area_xsize, xsn.max_height,
+ 0, xsn.area_ysize - xsn.max_height);
+
+ for (i = 0; i < xsn.num_items; i++)
+ {
+ int dst_x = xsn.items[i].x;
+ int dst_y = xsn.items[i].y;
+ int type = xsn.items[i].type;
+ int size = xsn_data[type].size;
+ Bitmap *bitmap = xsn_data[type].bitmap;
+
+ BlitToScreenMasked(bitmap, 0, 0, size, size, dst_x, dst_y);
+ }