private void LoadAllTextures() |
{ |
//Check to make sure the directory assigned exists. |
if (!Directory.Exists(pathToContentFolder)) |
{ |
//If the path to the Content folder doesn't exist, let the User know. |
MessageBox.Show(String.Format("The {0} directory does not exist. Please check that 'pathToContentFolder' is correctly assigned.", pathToContentFolder)); |
|
//There's no need to continue if we have the wrong folder, so just close the application. |
Close(); |
} |
|
//For all tiles we have in the game, let's load their textures! |
foreach (char c in CharactersDictionary.Keys) |
{ |
//Make sure the image exists before we load it! |
if (!File.Exists(pathToContentFolder + CharactersDictionary[c])) |
{ |
//Blank tiles (.) are null because they don't have a texture, so skip them |
if (CharactersDictionary[c] != null) |
{ |
//If it doesn't exist, let the User know so they can change it! |
MessageBox.Show(String.Format( |
"The file path for tile '{0}' does not exist. You tried to load {1}. Make sure 'pathToContentFolder' and the path for tile '{0}' in CharactersDictionary are correctly assigned.", |
c, |
pathToContentFolder + CharactersDictionary[c])); |
|
//Close the application so they can change change it to the correct value. |
Close(); |
} |
} |
tileDictionary.Add(c, GetTileTexture(c)); |
|
//Load an image so we can show a preview of it in our texture list view |
Image image = GetTileImage(c); |
|
if (image != null) |
{ |
//Here we have to work some magic since _most_ of our players/enemies are in the |
//form of spritesheets instead of a single image. What we want to do is get the |
//first frame of the image and use that for our image. We use our List of PlayerAndEnemyChars |
//in the Tile.cs class to find out if we're dealing with a player/enemy image. |
if (AnimatedSpriteChars.Contains(c)) |
{ |
System.Drawing.Bitmap original = new System.Drawing.Bitmap(image); |
System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(0, 0, image.Height, image.Height); |
image = (Image)original.Clone(srcRect, original.PixelFormat); |
} |
|
//Add it to our image list which we use to draw each texture in the texture list view |
imageList.Images.Add(c.ToString(), image); |
} |
|
ListViewItem item = new ListViewItem(c.ToString(), imageList.Images.IndexOfKey(c.ToString())); |
|
//And finally, add it to our list of textures to show up in the texture list view |
textureListView.Items.Add(item); |
} |
} |
|
private Image GetTileImage(char tileType) |
{ |
Image image = null; |
string filePathFromContentDirectory = CharactersDictionary[tileType]; |
|
if (!string.IsNullOrEmpty(filePathFromContentDirectory)) |
{ |
//Open a new stream so we can load the image |
using (Stream stream = File.OpenRead(pathToContentFolder + filePathFromContentDirectory)) |
image = Image.FromStream(stream); |
} |
|
return image; |
} |
private Texture2D GetTileTexture(char tileType) |
{ |
Texture2D texture = null; |
string filePathFromContentDirectory = CharactersDictionary[tileType]; |
|
if (!string.IsNullOrEmpty(filePathFromContentDirectory)) |
{ |
//Open a new stream so we can load the texture |
using (Stream stream = File.OpenRead(pathToContentFolder + filePathFromContentDirectory)) |
texture = Texture2D.FromStream(GraphicsDevice, stream); |
} |
|
return texture; |
} |