Jypeli  9
The simple game programming library
Content.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2013 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen, Mikko Röyskö.
28  */
29 
30 using System;
31 using System.IO;
33 using Microsoft.Xna.Framework.Graphics;
34 
35 using XnaSoundEffect = Microsoft.Xna.Framework.Audio.SoundEffect;
36 
37 #if NETFX_CORE
38 using Windows.ApplicationModel.Resources;
39 #else
40 #endif
41 
42 
43 namespace Jypeli
44 {
45  public partial class Game
46  {
50  public MediaPlayer MediaPlayer { get; private set; }
51 
52  // Need to find a way to get this working on Win8
53 
58  public static JypeliContentManager ResourceContent { get; private set; }
59 
60  private static string internalResourcePath = "Jypeli.Content.";
61 
62  private void InitXnaContent()
63  {
64  ResourceContent = new JypeliContentManager(this.Services);
65  Content.RootDirectory = "Content";
66  MediaPlayer = new MediaPlayer();
67  }
68 
74  public static Image LoadImage(string name)
75  {
76  return new Image("Content/" + name);
77  }
78 
84  public static Image LoadImageFromResources(string name)
85  {
86  name = internalResourcePath + "Images." + name;
87  return new Image(Texture2D.FromStream(Game.GraphicsDevice, ResourceContent.StreamInternalResource(name)));
88  }
89 
95  public static Image[] LoadImages(params string[] names)
96  {
97  Image[] result = new Image[names.Length];
98  for (int i = 0; i < names.Length; i++)
99  result[i] = LoadImage(names[i]);
100  return result;
101  }
102 
111  public static Image[] LoadImages(string baseName, int startIndex, int endIndex, bool zeroPad = false)
112  {
113  if (startIndex > endIndex) throw new ArgumentException("Starting index must be smaller than ending index.");
114 
115  Image[] result = new Image[endIndex - startIndex];
116  string format;
117 
118  if (zeroPad)
119  {
120  int digits = endIndex.ToString().Length;
121  format = "{0}{1:" + "0".Repeat(digits) + "}";
122  }
123  else
124  {
125  format = "{0}{1}";
126  }
127 
128  for (int i = startIndex; i < endIndex; i++)
129  {
130  string imgName = String.Format(format, baseName, i);
131  result[i - startIndex] = LoadImage(imgName);
132  }
133 
134  return result;
135  }
136 
141  public static void PlaySound(string name)
142  {
143  LoadSoundEffect("Content/" + name).Play();
144  }
145 
151  public static SoundEffect LoadSoundEffect(string name)
152  {
153  return new SoundEffect("Content/" + name);
154  }
155 
161  public static SoundEffect LoadSoundEffectFromResources(string name)
162  {
163  name = internalResourcePath + "Sounds." + name;
165  }
166 
172  public static SoundEffect[] LoadSoundEffects(params string[] names)
173  {
174  SoundEffect[] result = new SoundEffect[names.Length];
175  for (int i = 0; i < names.Length; i++)
176  result[i] = LoadSoundEffect(names[i]);
177  return result;
178  }
179 
184  public static Font LoadFont(string name)
185  {
186  return Font.FromContent(name);
187  }
188 
195  internal static string FileExtensionCheck(string file, string[] extensions)
196  {
197  if (!File.Exists(file))
198  {
199  foreach (string ex in extensions)
200  {
201  if (File.Exists(file + ex))
202  {
203  file += ex;
204  break;
205  }
206  }
207  }
208  return file;
209  }
210  }
211 }
Microsoft.Xna.Framework.Content.JypeliContentManager.StreamInternalResource
Stream StreamInternalResource(string assetName)
Definition: JypeliContentManager.cs:29
Jypeli.Game.MediaPlayer
MediaPlayer MediaPlayer
Mediasoitin. Voidaan käyttää musiikin soittamiseen.
Definition: Content.cs:50
Jypeli.MediaPlayer
Mediasoitin, jolla voi soittaa musiikkikappaleita.
Definition: MediaPlayer.cs:15
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Game.LoadImages
static Image[] LoadImages(string baseName, int startIndex, int endIndex, bool zeroPad=false)
Lataa taulukon kuvia contentista.
Definition: Content.cs:111
Jypeli.SoundEffect
Ääniefekti. Yhdestä efektistä voi luoda CreateSound-metodilla monta ääntä (Sound),...
Definition: SoundEffect.cs:17
Jypeli
Definition: Automobile.cs:5
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Game.LoadFont
static Font LoadFont(string name)
Lataa fontin. Fontin tulee olla lisätty content-hakemistoon.
Definition: Content.cs:184
Jypeli.Game.LoadSoundEffects
static SoundEffect[] LoadSoundEffects(params string[] names)
Lataa taulukon ääniefektejä contentista.
Definition: Content.cs:172
Jypeli.Game.FileExtensionCheck
static string FileExtensionCheck(string file, string[] extensions)
Etsii millä päätteellä annettu tiedosto löytyy
Definition: Content.cs:195
Jypeli.Game.LoadImages
static Image[] LoadImages(params string[] names)
Lataa taulukon kuvia contentista.
Definition: Content.cs:95
Jypeli.Font.FromContent
static Font FromContent(string name)
Lataa uuden fontin contentista.
Definition: Font.cs:143
Microsoft.Xna.Framework.Content.JypeliContentManager
Definition: JypeliContentManager.cs:8
Jypeli.Game.LoadImageFromResources
static Image LoadImageFromResources(string name)
Lataa kuvan Jypelin sisäisistä resursseista.
Definition: Content.cs:84
Jypeli.Game.PlaySound
static void PlaySound(string name)
Soittaa ääniefektin.
Definition: Content.cs:141
Microsoft.Xna.Framework.Content
Definition: JypeliContentManager.cs:6
Jypeli.Image
Kuva.
Definition: Image.cs:29
XnaSoundEffect
Microsoft.Xna.Framework.Audio.SoundEffect XnaSoundEffect
Definition: Content.cs:35
Jypeli.Game.LoadSoundEffectFromResources
static SoundEffect LoadSoundEffectFromResources(string name)
Lataa ääniefektin Jypelin sisäisistä resursseista.
Definition: Content.cs:161
Jypeli.Game.InitXnaContent
void InitXnaContent()
Definition: Content.cs:62
System
Definition: CFFauxAttributes.cs:29
Jypeli.Game.LoadSoundEffect
static SoundEffect LoadSoundEffect(string name)
Lataa ääniefektin contentista.
Definition: Content.cs:151
Jypeli.Game.ResourceContent
static JypeliContentManager ResourceContent
Kirjaston mukana tuleva sisältö. Voidaan käyttää esimerkiksi tekstuurien lataamiseen.
Definition: Content.cs:58
Jypeli.Font
Fontti.
Definition: Font.cs:23
Jypeli.Game.GraphicsDevice
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Jypeli.Game.LoadImage
static Image LoadImage(string name)
Lataa kuvan contentista.
Definition: Content.cs:74
Jypeli.Game
Definition: Content.cs:46
Jypeli.SoundEffect.Play
bool Play()
Soittaa äänen.
Definition: SoundEffect.cs:125
Jypeli.Game.internalResourcePath
static string internalResourcePath
Definition: Content.cs:60