TriEngine  v0.0.16
General-purpose engine in C#/OpenGL
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
TriDevs.TriEngine.Resources.ResourceManager Class Reference

Static class to manage resources. More...

Static Public Member Functions

static Font LoadFont (string name, string file, int size, bool dropShadow=false, FontType type=FontType.TTF)
 Loads a font file from the default resources path into the resources. More...
 
static Font LoadFont (string name, string file, int size, FontType type, FontConstructionConfig config)
 Loads a font file from the default resources path into the resources. More...
 
static Shader LoadShader (string name, string file, ShaderType type)
 Loads a shader file from the default resources path into the resources. More...
 
static ISound LoadSound (string name, string file, AudioFormat format=AudioFormat.Wav)
 Loads a sound file from the default resources path into the resources. More...
 
static ISong LoadSong (string name, string file, AudioFormat format=AudioFormat.Ogg)
 Loads a song file from the default resources path into the resources. More...
 
static bool Has (string name)
 Checks if the resource with the specified name has been added to the resource collection. More...
 
static bool Has< T > (string name)
 Checks if the resource with the specified name and type has been added to the resource collection. More...
 
static IResource Get (string name)
 Gets the resource with the specified name. More...
 
static T Get< T > (string name)
 Gets the resource with the specified name and casts it to the specified type. More...
 
static IEnumerable< T > GetAll< T > ()
 Gets all resources of the specified type. More...
 
static void Add (IResource resource)
 Adds a resource to the resource collection. More...
 

Static Public Attributes

static string BasePath = "Resources"
 Base path to the resources directory, relative to the current working directory. More...
 
static string FontPath = "Fonts"
 Path to the fonts directory, relative to BasePath. More...
 
static string ShaderPath = "Shaders"
 Path to the shaders directory, relative to BasePath. More...
 
static string SoundPath = "Sounds"
 Path to the sounds directory, relative to BasePath. More...
 
static string SongPath = "Songs"
 Path to the songs directory, relative to BasePath. More...
 

Detailed Description

Static class to manage resources.

Member Function Documentation

static void TriDevs.TriEngine.Resources.ResourceManager.Add ( IResource  resource)
static

Adds a resource to the resource collection.

Parameters
resourceResource to add.
Exceptions
ResourceExceptionThrown if the collection already contains a resource with the same name.
259  {
260  if (Has(resource.Name))
261  throw new ResourceException("Attempted to add resource that already exists: " + resource.Name);
262 
263  Resources.Add(resource.Name, resource);
264  }
static IResource TriDevs.TriEngine.Resources.ResourceManager.Get ( string  name)
static

Gets the resource with the specified name.

Parameters
nameName of resource to get.
Exceptions
ResourceExceptionThrown if the resource does not exist.
Returns
The IResource object with the specified name.
212  {
213  if (!Has(name))
214  throw new ResourceException("Attempted to get non-existing resource \"" + name + "\"!");
215 
216  return Resources[name];
217  }
static T TriDevs.TriEngine.Resources.ResourceManager.Get< T > ( string  name)
static

Gets the resource with the specified name and casts it to the specified type.

Template Parameters
TType to cast to.
Parameters
nameName of resource to get.
Exceptions
ResourceExceptionThrown if the resource could not be found or if the cast failed.
Returns
The resource object of type T with the specified name.
Type Constraints
T :class 
T :IResource 
227  : class, IResource
228  {
229  if (!Has(name))
230  throw new ResourceException("Attempted to get non-existing resource \"" + name + "\"!");
231 
232  T resource = Get(name) as T;
233 
234  if (resource == null)
235  throw new ResourceException("Resource with name \"" + name +
236  "\" is not of the requested type: " + typeof (T));
237 
238  return resource;
239  }
static IEnumerable<T> TriDevs.TriEngine.Resources.ResourceManager.GetAll< T > ( )
static

Gets all resources of the specified type.

Template Parameters
TType of resource to get.
Returns
An IEnumerable containing the relevant resources.

Returned collection will be empty if no matching resources were found.

Type Constraints
T :class 
T :IResource 
247  : class, IResource
248  {
249  return Resources.Values.Where(r => r is T).Cast<T>();
250  }
static bool TriDevs.TriEngine.Resources.ResourceManager.Has ( string  name)
static

Checks if the resource with the specified name has been added to the resource collection.

Parameters
nameName to search for.
Returns
True if the resource has been added, false otherwise.
190  {
191  return Resources.ContainsKey(name);
192  }
static bool TriDevs.TriEngine.Resources.ResourceManager.Has< T > ( string  name)
static

Checks if the resource with the specified name and type has been added to the resource collection.

Template Parameters
TType of resource to search for.
Parameters
nameName to search for.
Returns
True if the resource has been added, false otherwise.
Type Constraints
T :class 
T :IResource 
200  : class, IResource
201  {
202  return Has(name) && Resources[name].GetType() == typeof (T);
203  }
static Font TriDevs.TriEngine.Resources.ResourceManager.LoadFont ( string  name,
string  file,
int  size,
bool  dropShadow = false,
FontType  type = FontType.TTF 
)
static

Loads a font file from the default resources path into the resources.

Parameters
nameName to assign the font, or null to auto-generate one.
fileFont file to load.
sizeSize (in points) to use for the font.
dropShadowWhether or not the font should have shadows.
typeThe font filetype.
Returns
The newly loaded font object, or existing font object if one with matching name was found.
84  {
85  return LoadFont(name, file, size, type,
86  new FontConstructionConfig(new QFontBuilderConfiguration(dropShadow),
87  new QFontLoaderConfiguration(dropShadow)));
88  }
static Font TriDevs.TriEngine.Resources.ResourceManager.LoadFont ( string  name,
string  file,
int  size,
FontType  type,
FontConstructionConfig  config 
)
static

Loads a font file from the default resources path into the resources.

Parameters
nameName to assign the font, or null to auto-generate one.
fileFont file to load.
sizeSize (in points) to use for the font.
typeThe font filetype.
configThe relevant font construction configs.
Returns
The newly loaded font object, or existing font object if one with matching name was found.
100  {
101  if (Has<Font>(name))
102  return Get<Font>(name);
103 
104  file = Path.Combine(BasePath, FontPath, file);
105 
106  var font = new Font(name, file, size, type, config);
107  Add(font);
108  return font;
109  }
static Shader TriDevs.TriEngine.Resources.ResourceManager.LoadShader ( string  name,
string  file,
ShaderType  type 
)
static

Loads a shader file from the default resources path into the resources.

Parameters
nameName to assign the shader, or null to auto-generate one.
fileFile to load shader code from.
typeThe type of shader.
Returns
The newly loaded shader object, or existing shader object if one with matching name was found.
123  {
124  if (Has<Shader>(name))
125  return Get<Shader>(name);
126 
127  file = Path.Combine(BasePath, ShaderPath, file);
128 
129  var shader = new Shader(name, file, type);
130  Add(shader);
131  return shader;
132  }
static ISong TriDevs.TriEngine.Resources.ResourceManager.LoadSong ( string  name,
string  file,
AudioFormat  format = AudioFormat.Ogg 
)
static

Loads a song file from the default resources path into the resources.

Parameters
nameName to assign the song.
fileFile to load song from.
formatThe audio format of the song.
Returns
The newly loaded song object, or existing song object if one with matching name was found.
169  {
170  if (Has<ISong>(name))
171  return Get<ISong>(name);
172 
173  file = Path.Combine(BasePath, SongPath, file);
174 
175  var song = new Song(name, file, format);
176  Add(song);
177  return song;
178  }
static ISound TriDevs.TriEngine.Resources.ResourceManager.LoadSound ( string  name,
string  file,
AudioFormat  format = AudioFormat.Wav 
)
static

Loads a sound file from the default resources path into the resources.

Parameters
nameName to assign the sound.
fileFile to load sound from.
formatThe audio format of the sound.
Returns
The newly loaded sound object, or existing sound object if one with matching name was found.
146  {
147  if (Has<ISound>(name))
148  return Get<ISound>(name);
149 
150  file = Path.Combine(BasePath, SoundPath, file);
151 
152  var sound = new Sound(name, file, format);
153  Add(sound);
154  return sound;
155  }

Member Data Documentation

string TriDevs.TriEngine.Resources.ResourceManager.BasePath = "Resources"
static

Base path to the resources directory, relative to the current working directory.

string TriDevs.TriEngine.Resources.ResourceManager.FontPath = "Fonts"
static

Path to the fonts directory, relative to BasePath.

string TriDevs.TriEngine.Resources.ResourceManager.ShaderPath = "Shaders"
static

Path to the shaders directory, relative to BasePath.

string TriDevs.TriEngine.Resources.ResourceManager.SongPath = "Songs"
static

Path to the songs directory, relative to BasePath.

string TriDevs.TriEngine.Resources.ResourceManager.SoundPath = "Sounds"
static

Path to the sounds directory, relative to BasePath.


The documentation for this class was generated from the following file: