2009/05/27(Wed)StaticResourceLoader.cs

はてブ数 2009/05/27 2:48 計算機な日記::ソフト作り つーさ

GSDK on C# で。

これで

namespace Tsukikage.GameSDK.Base
{
  public interface IDXResourceLoader<ResourceType>
  {
    ResourceType LoadResource(ResourceType target);
  }
}

こうして

namespace Tsukikage.GameSDK.Direct3D
{
  public class D3DDevice : DXResource,
    IDXResourceLoader<D3DTexture>, IDXResourceLoader<D3DFont>
  {
    public D3DTexture LoadResource(D3DTexture target)
    {
      LoadTexture(target);
      return target;
    }
  }
}

こうやって


using Ref = System.Reflection;
namespace Tsukikage.GameSDK.Base
{
  /// <summary>
  /// staticリソース読み込み支援クラス
  /// </summary>
  public class StaticResourceLoader
  {
    static Dictionary<Type, StaticResourceLoader> loaders = new Dictionary<Type, StaticResourceLoader>();
    public static void Load<TypeToLoad>(Type target, IDXResourceLoader<TypeToLoad> loader)
    {
      lock (loaders)
      {
        if (loaders.ContainsKey(target) ) 
          return;
        
        loaders[target] = new StaticResourceLoader();

        foreach (Ref.FieldInfo fi in target.GetFields(Ref.BindingFlags.Public | Ref.BindingFlags.Static))
        {
          if (fi.FieldType == typeof(TypeToLoad) && fi.GetValue(null) != null)
            loader.LoadResource((TypeToLoad)fi.GetValue(null));
        }
      }
    }
  }
}

で、こう。


namespace WindowsGame1.Scenes
{
  public class Textures
  {
    public static D3DTexture Circle = new D3DTexture("circle.png");
  }

  class Scene1 : Scene
  {
    public override void Initialize()
    {
      StaticResourceLoader.Load<D3DTexture>(typeof(Textures), GSDK.D3D);
    }
  }
}

……。

まず口から出てくるのは「キメェ」の一言かもしれない。

ただ、大富豪的プログラミング時代においては、中規模程度のゲームまではこういうリソースの持ち方もありかなぁと思わないでもない。

(自分も含めて)ゲーム作り初心者が多いうちのサークルでは、この前もテクスチャリソースがリークして大変なことになってたけど。

ここを読んでくださっているみなさんはどう思われますか、なんつて。

ここを見てる人はきっとリソース管理なんて朝飯前で、こんなことしないのかもしれない……。

あー、ゲーム作りてぇなぁ……。