1 /***************************************************************************
2  *                 (C) Copyright 2003-2014 - Faiumoni e.V.                 *
3  ***************************************************************************
4  ***************************************************************************
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  ***************************************************************************/
12 package games.stendhal.client;
13 
14 import java.awt.Color;
15 import java.awt.Composite;
16 import java.awt.Graphics;
17 
18 import org.apache.log4j.Logger;
19 
20 import games.stendhal.client.sprite.EmptySprite;
21 import games.stendhal.client.sprite.Sprite;
22 import games.stendhal.client.sprite.SpriteStore;
23 import games.stendhal.client.sprite.SpriteTileset;
24 import games.stendhal.client.sprite.Tileset;
25 import games.stendhal.client.sprite.TilesetGroupAnimationMap;
26 
27 /**
28  * Weather renderer. The weather sprites are looked up from
29  * "data/sprites/weather". Animation can be specified normally in animation.seq.
30  * The sprite height is taken as the tile size for both directions, the rest
31  * can be used for animation.
32  */
33 public class WeatherLayerRenderer extends LayerRenderer {
34 	private final Sprite weather;
35 
36 	/**
37 	 * Create a new weather layer.
38 	 *
39 	 * @param weather weather description
40 	 * @param color zone coloring data
41 	 * @param blend zone blending mode
42 	 */
WeatherLayerRenderer(String weather, Color color, Composite blend)43 	public WeatherLayerRenderer(String weather, Color color, Composite blend) {
44 		SpriteStore sr = SpriteStore.get();
45 
46 		String name = "data/sprites/weather/" + weather + ".png";
47 		Sprite template;
48 		/*
49 		 * The failsafe sprite must not be used here, under any circumstances.
50 		 * It would cover all of the map, so revert to an empty sprite instead
51 		 */
52 		if (sr.getSprite(name) == sr.getFailsafe()) {
53 			template = new EmptySprite(1024, 1024, null);
54 			Logger.getLogger(WeatherLayerRenderer.class).warn("Weather sprite not found:" + name);
55 		} else {
56 			template = sr.getModifiedSprite(name, color, blend);
57 		}
58 		Tileset ts = new SpriteTileset(sr, template, template.getHeight());
59 		TilesetGroupAnimationMap animationMap = TileStore.getAnimationMap();
60 
61 		if (animationMap != null && animationMap.get(name) != null) {
62 			this.weather = animationMap.get(name).getSprite(ts, 0);
63 		} else {
64 			this.weather = ts.getSprite(0);
65 		}
66 	}
67 
68 	@Override
draw(Graphics g, int x, int y, int w, int h)69 	public void draw(Graphics g, int x, int y, int w, int h) {
70 		int myX = (x * IGameScreen.SIZE_UNIT_PIXELS) / weather.getWidth();
71 		int myY = (y * IGameScreen.SIZE_UNIT_PIXELS) / weather.getHeight();
72 		int myW = (w * IGameScreen.SIZE_UNIT_PIXELS) / weather.getWidth() + 1;
73 		int myH = (h * IGameScreen.SIZE_UNIT_PIXELS) / weather.getHeight() + 1;
74 
75 		int sy = myY * weather.getHeight();
76 		for (int j = myY; j < myY + myH; j++) {
77 			int sx = myX * weather.getWidth();
78 
79 			for (int i = myX; i < myX + myW; i++) {
80 				weather.draw(g, sx, sy);
81 				sx += weather.getWidth();
82 			}
83 			sy += weather.getHeight();
84 		}
85 	}
86 
87 	@Override
setTileset(Tileset tileset)88 	public void setTileset(Tileset tileset) {
89 		throw new UnsupportedOperationException("Adding tilesets not supported");
90 	}
91 }
92