1 /* GemRB - Infinity Engine Emulator
2  * Copyright (C) 2003 The GemRB Project
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  */
20 
21 #ifndef SPRITESHEET_H
22 #define SPRITESHEET_H
23 
24 #include "Sprite2D.h"
25 #include "Video.h"
26 
27 #include <map>
28 
29 namespace GemRB {
30 
31 template <typename KeyType>
32 class SpriteSheet {
33 protected:
34 	Region SheetRegion; // FIXME: this is only needed because of a subclass
35 	std::map<KeyType, Region> RegionMap;
36 
SpriteSheet(Holder<Video> video)37 	SpriteSheet(Holder<Video> video)
38 	: video(video) {};
39 
40 public:
41 	Holder<Sprite2D> Sheet;
42 	Holder<Video> video;
43 
44 public:
SpriteSheet(Holder<Video> video,Holder<Sprite2D> sheet)45 	SpriteSheet(Holder<Video> video, Holder<Sprite2D> sheet)
46 	: Sheet(sheet), video(video) {
47 		SheetRegion = Sheet->Frame;
48 	};
49 
50 	virtual ~SpriteSheet() = default;
51 
52 	const Region& operator[](KeyType key) {
53 		return RegionMap[key];
54 	}
55 
56 	// return the passed in region, clipped to the sprite dimensions or a region with -1 w/h if outside the sprite bounds
MapSheetSegment(KeyType key,Region rgn)57 	const Region& MapSheetSegment(KeyType key, Region rgn) {
58 		Region intersection = rgn.Intersect(SheetRegion);
59 		if (!intersection.Dimensions().IsEmpty()) {
60 			if (RegionMap.insert(std::make_pair(key, intersection)).second) {
61 				return RegionMap[key];
62 			}
63 			// FIXME: should we return something like Region(0,0,0,0) here?
64 			// maybe we should just use Atlas[key] = intersection too.
65 		}
66 		const static Region nullRgn(0,0, -1,-1);
67 		return nullRgn;
68 	}
69 
Draw(KeyType key,const Region & dest,BlitFlags flags,const Color & tint)70 	void Draw(KeyType key, const Region& dest, BlitFlags flags, const Color& tint) const {
71 		const auto& i = RegionMap.find(key);
72 		if (i != RegionMap.end()) {
73 			video->BlitSprite(Sheet, i->second, dest, flags, tint);
74 		}
75 	}
76 };
77 
78 }
79 
80 #endif  // ! SPRITESHEET_H
81