1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsPrerequisites.h"
6#include "Utility/BsModule.h"
7#include "2D/BsSpriteMaterial.h"
8
9namespace bs
10{
11 /** @addtogroup 2D-Internal
12 * @{
13 */
14
15 /** Contains materials used for sprite rendering. */
16 class BS_EXPORT SpriteManager : public Module<SpriteManager>
17 {
18 /** Types of sprite materials accessible by default. */
19 enum class BuiltinSpriteMaterialType
20 {
21 ImageTransparent,
22 ImageOpaque,
23 ImageTransparentAnimated,
24 ImageOpaqueAnimated,
25 Text,
26 Line,
27 Count // Keep at end
28 };
29
30 public:
31 SpriteManager();
32 ~SpriteManager();
33
34 /** Returns the material used for rendering image sprites.
35 *
36 * @param[in] transparent True if the material should be able to render transparecy.
37 * @param[in] animation True if the material should be able to perform sprite sheet animation.
38 * @return Requested sprite material.
39 */
40 SpriteMaterial* getImageMaterial(bool transparent, bool animation = false) const
41 {
42 if(!animation)
43 {
44 if(transparent)
45 return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::ImageTransparent]);
46
47 return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::ImageOpaque]);
48 }
49 else
50 {
51 if(transparent)
52 return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::ImageTransparentAnimated]);
53
54 return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::ImageOpaqueAnimated]);
55 }
56 }
57
58 /** Returns the material used for rendering text sprites. */
59 SpriteMaterial* getTextMaterial() const
60 { return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::Text]); }
61
62 /** Returns the material used for rendering antialiased lines. */
63 SpriteMaterial* getLineMaterial() const
64 { return getMaterial(builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::Line]); }
65
66 /** Returns a sprite material with the specified ID. Returns null if one cannot be found. */
67 SpriteMaterial* getMaterial(UINT32 id) const;
68
69 /**
70 * Registers a new material in the sprite manager. Caller must ensure the material has a unique ID that doesn't
71 * already exist in the sprite manager, otherwise the call will be ignored.
72 *
73 * @return Newly created material, or at existing one if one already exists.
74 */
75 template <class T, class... Args>
76 SpriteMaterial* registerMaterial(Args &&...args)
77 {
78 SpriteMaterial* newMaterial = bs_new<T>(std::forward<Args>(args)...);
79
80 UINT32 id = newMaterial->getId();
81 auto iterFind = mMaterials.find(id);
82 if (iterFind != mMaterials.end())
83 {
84 // Already exists
85 LOGWRN("Attempting to register a sprite material that already exists, ignoring request.");
86 bs_delete(newMaterial);
87 return iterFind->second;
88 }
89
90 mMaterials[id] = newMaterial;
91 return newMaterial;
92 }
93 private:
94 UnorderedMap<UINT32, SpriteMaterial*> mMaterials;
95 UINT32 builtinMaterialIds[(UINT32)BuiltinSpriteMaterialType::Count];
96 };
97
98 /** @} */
99}