Pioneer
TextureBuilder.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _TEXTUREBUILDER_H
5 #define _TEXTUREBUILDER_H
6 
7 #include "Renderer.h"
8 #include "SDLWrappers.h"
9 #include "Texture.h"
10 #include <SDL.h>
11 #include <string>
12 
13 #include "PicoDDS/PicoDDS.h"
14 
15 namespace Graphics {
16 
18  public:
19  TextureBuilder(const SDLSurfacePtr &surface, TextureSampleMode sampleMode = LINEAR_CLAMP,
20  bool generateMipmaps = false, bool potExtend = false, bool forceRGBA = true, bool compressTextures = true, bool anisoFiltering = true);
21  TextureBuilder(const std::string &filename, TextureSampleMode sampleMode = LINEAR_CLAMP,
22  bool generateMipmaps = false, bool potExtend = false, bool forceRGBA = true, bool compressTextures = true, bool anisoFiltering = true,
23  TextureType textureType = TEXTURE_2D, const size_t layers = 1);
24  TextureBuilder(const std::vector<std::string> &filenames, TextureSampleMode sampleMode = LINEAR_CLAMP,
25  bool generateMipmaps = false, bool potExtend = false, bool forceRGBA = true, bool compressTextures = true, bool anisoFiltering = true,
26  TextureType textureType = TEXTURE_2D, const size_t layers = 1);
28 
29  static void Init();
30 
31  // convenience constructors for common texture types
32  static TextureBuilder Model(const std::string &filename)
33  {
34  return TextureBuilder(filename, LINEAR_REPEAT, true, false, false, true, true);
35  }
36  static TextureBuilder Normal(const std::string &filename)
37  {
38  return TextureBuilder(filename, LINEAR_REPEAT, true, false, false, false, true);
39  }
40  static TextureBuilder Billboard(const std::string &filename)
41  {
42  return TextureBuilder(filename, LINEAR_CLAMP, true, false, false, true, false);
43  }
44  static TextureBuilder UI(const std::string &filename)
45  {
46  return TextureBuilder(filename, LINEAR_CLAMP, false, true, true, false, false);
47  }
48  static TextureBuilder Decal(const std::string &filename)
49  {
50  return TextureBuilder(filename, LINEAR_CLAMP, true, true, false, true, true);
51  }
52  static TextureBuilder Raw(const std::string &filename)
53  {
54  return TextureBuilder(filename, NEAREST_REPEAT, false, false, false, false, false);
55  }
56  static TextureBuilder Cube(const std::string &filename)
57  {
58  return TextureBuilder(filename, LINEAR_CLAMP, true, true, false, true, false, TEXTURE_CUBE_MAP);
59  }
60  static TextureBuilder LookUpTable(const std::string &filename)
61  {
62  return TextureBuilder(filename, NEAREST_CLAMP, false, true, true, false, false);
63  }
64  static TextureBuilder Array(const std::vector<std::string> &filenames, const size_t layers)
65  {
66  return TextureBuilder(filenames, LINEAR_REPEAT, true, true, false, true, true, TEXTURE_2D_ARRAY, layers);
67  }
68 
70  {
71  PrepareSurface();
72  return m_descriptor;
73  }
74 
75  Texture *GetOrCreateTexture(Renderer *r, const std::string &type)
76  {
77  if (m_filenames.empty()) {
78  return CreateTexture(r);
79  }
80  SDL_LockMutex(m_textureLock);
81  Texture *t = r->GetCachedTexture(type, m_filenames.front());
82  if (t) {
83  SDL_UnlockMutex(m_textureLock);
84  return t;
85  }
86  t = CreateTexture(r);
87  r->AddCachedTexture(type, m_filenames.front(), t);
88  SDL_UnlockMutex(m_textureLock);
89  return t;
90  }
91 
92  //commonly used dummy textures
93  static Texture *GetWhiteTexture(Renderer *);
95 
96  private:
97  SDLSurfacePtr m_surface;
98  std::vector<SDLSurfacePtr> m_cubemap;
99  PicoDDS::DDSImage m_dds;
100  std::vector<PicoDDS::DDSImage> m_ddsarray;
101  std::vector<std::string> m_filenames;
102 
103  TextureSampleMode m_sampleMode;
104  bool m_generateMipmaps;
105 
106  bool m_potExtend;
107  bool m_forceRGBA;
108  bool m_compressTextures;
109  bool m_anisotropicFiltering;
110  TextureType m_textureType;
111  size_t m_layers;
112 
113  TextureDescriptor m_descriptor;
114 
115  Texture *CreateTexture(Renderer *r)
116  {
118  UpdateTexture(t);
119  return t;
120  }
121  void UpdateTexture(Texture *texture); // XXX pass src/dest rectangles
122  void PrepareSurface();
123  bool m_prepared;
124 
125  void LoadSurface();
126  void LoadDDS();
127 
128  static SDL_mutex *m_textureLock;
129  };
130 
131 } // namespace Graphics
132 
133 #endif
Definition: Renderer.h:44
Texture * GetCachedTexture(const std::string &type, const std::string &name)
Definition: Renderer.cpp:24
void AddCachedTexture(const std::string &type, const std::string &name, Texture *texture)
Definition: Renderer.cpp:31
virtual Texture * CreateTexture(const TextureDescriptor &descriptor)=0
Definition: TextureBuilder.h:17
static TextureBuilder Array(const std::vector< std::string > &filenames, const size_t layers)
Definition: TextureBuilder.h:64
static TextureBuilder Model(const std::string &filename)
Definition: TextureBuilder.h:32
TextureBuilder(const SDLSurfacePtr &surface, TextureSampleMode sampleMode=LINEAR_CLAMP, bool generateMipmaps=false, bool potExtend=false, bool forceRGBA=true, bool compressTextures=true, bool anisoFiltering=true)
Definition: TextureBuilder.cpp:18
static TextureBuilder LookUpTable(const std::string &filename)
Definition: TextureBuilder.h:60
static void Init()
Definition: TextureBuilder.cpp:65
static TextureBuilder Billboard(const std::string &filename)
Definition: TextureBuilder.h:40
static Texture * GetTransparentTexture(Renderer *)
Definition: TextureBuilder.cpp:374
~TextureBuilder()
Definition: TextureBuilder.cpp:61
static TextureBuilder UI(const std::string &filename)
Definition: TextureBuilder.h:44
static TextureBuilder Raw(const std::string &filename)
Definition: TextureBuilder.h:52
static TextureBuilder Cube(const std::string &filename)
Definition: TextureBuilder.h:56
Texture * GetOrCreateTexture(Renderer *r, const std::string &type)
Definition: TextureBuilder.h:75
static TextureBuilder Decal(const std::string &filename)
Definition: TextureBuilder.h:48
const TextureDescriptor & GetDescriptor()
Definition: TextureBuilder.h:69
static TextureBuilder Normal(const std::string &filename)
Definition: TextureBuilder.h:36
static Texture * GetWhiteTexture(Renderer *)
Definition: TextureBuilder.cpp:369
Definition: Texture.h:54
Definition: Texture.h:106
Definition: SDLWrappers.h:17
Definition: Background.h:14
TextureType
Definition: Texture.h:38
@ TEXTURE_2D
Definition: Texture.h:39
@ TEXTURE_CUBE_MAP
Definition: Texture.h:40
@ TEXTURE_2D_ARRAY
Definition: Texture.h:41
TextureSampleMode
Definition: Texture.h:31
@ NEAREST_REPEAT
Definition: Texture.h:35
@ NEAREST_CLAMP
Definition: Texture.h:33
@ LINEAR_CLAMP
Definition: Texture.h:32
@ LINEAR_REPEAT
Definition: Texture.h:34