PlayStation Mobile Studio
 PlayStation(R)Mobile SDK 1.21.01

■Playstation Mobile 永続的なメモリ領域を利用する Prev Top Next
関連ページ:なし


今回は永続的なメモリ領域を利用するためのサンプルを作成します。プログラムを再起動しても保存した内容は保持されます。
ドキュメント上ではメモリといってますが、たぶんバイナリファイルに保存していると思います。
これは /System/pm.dat というアプリ起動時に自動生成されるファイルを削除すると保存内容は破棄されることからの予想です。

さて今回のネタは前回のテキストファイルに設定内容を保存する方法と目的は同じです。
今回の方法はバイナリデータとしてデータを扱い、データ保存は固定長でメモリマップ上に保存することになります。
固定長となっているため拡張が難しくなります。 さらに最大使用可能バイト数が64KBとなっています。
このように制限の多い永続的なメモリ領域ですが、処理速度はテキストファイルを扱うより高速です。

以上からデータ保存量が常に一定であるシステム設定情報は永続的なメモリ領域に保存し、ゲームの進捗情報を保存するためのセーブデータのように
追加と削除が必要となるデータはテキストファイルに保存するのが一般的かと思います。たぶん。

次にアプリケーションについて説明すると、前回と同じくスクリーンサイズをメモリに保存し、読み込みます。


Common.cs 共通クラスとか
Sprite.vcg バーテックスシェーダーのcgファイル
Sprite.fcg フラグメントシェーダーのcgファイル
Sprite.cs スプライトシェーダークラス
SpriteMesh.cs スプライトメッシュクラス
DebugFont.cs デバッグフォントクラス
AppMain.cs main関数があるソースファイル

---AppMain.cs---  ↑


using System;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.Core.Graphics;
using System.Collections.Generic;
using Sce.PlayStation.Core.Input;
using System.Text;

namespace PSM_Samples
{
   public class AppMain
   {
      private static GraphicsContext graphics;

      private static DebugFont debugFont;

      // 入力ダイアログ
      private static TextInputDialog dialog;

      // 永続的なメモリ領域でスクリーンサイズの縦横サイズを保存するメモリ上のオフセット値
      const int OFFSET_ADDR_WIDTH = 0;
      const int OFFSET_ADDR_HEIGHT = OFFSET_ADDR_WIDTH + sizeof(int);
      // 永続的なメモリ領域で使用する全メモリバイト数
      const int SIZE_ADDR_ALL = OFFSET_ADDR_HEIGHT + sizeof(int);

      private static bool loop = true;

      public static void Main (string[] args)
      {
         Initialize ();

         while (loop) 
         {
            SystemEvents.CheckEvents ();
            Update ();
            Render ();
         }

         Destroy();
      }

      // 初期化
      public static void Initialize ()
      {
         dialog = null;

         // 永続的なメモリ領域からデータ取得
         byte[] persistentMemoryData = PersistentMemory.Read();

         byte[] byteArray = new byte[SIZE_ADDR_ALL];
         for(int i=0; i<byteArray.Length; i++ )
         {
            byteArray[i] = persistentMemoryData[i];
         }
         int width = BitConverter.ToInt32(byteArray, OFFSET_ADDR_WIDTH);
         int height = BitConverter.ToInt32(byteArray, OFFSET_ADDR_HEIGHT);

         // 永続的なメモリ領域から取得したスクリーンサイズ( ウィンドウサイズではない )を使用してGraphicsContextを作成する
         graphics = new GraphicsContext( width, height, PixelFormat.Rgba, PixelFormat.Depth16, MultiSampleMode.None );

         // デバッグフォントの作成
         debugFont = new DebugFont();
      }

      public static void Destroy()
      {
         if( dialog != null )
            dialog.Dispose();

         debugFont.Dispose();
         graphics.Dispose();
      }

      // フレーム更新
      public static void Update ()
      {
         List<TouchData> touchData = Touch.GetData(0);
         if( touchData.Count > 0 && touchData[0].Status == TouchStatus.Down )
         {
            // テキスト入力ダイアログ表示
            dialog = new TextInputDialog();
            dialog.Text = "";
            dialog.Open();
         }

         if (dialog != null )
         {
            // テキスト入力ダイアログが閉じられた
            if( dialog.State == CommonDialogState.Finished)
            {
               // OKが押された
               if (dialog.Result == CommonDialogResult.OK)
               {
                  string[] s = dialog.Text.Split(':');
                  if( s.Length == 2 )
                  {
                     int width, height = 0;
                     if( int.TryParse( s[0].Trim(), out width ) == true && int.TryParse( s[1].Trim(), out height ) == true )
                     {
                        if( width > 1280 )
                           width = 1280;
                        else if( width < 0 )
                           width = 0;

                        if( height > 800 )
                           height = 800;
                        else if( height < 0 )
                           height = 0;

                        // 永続的なメモリ領域からデータ取得
                        byte[] persistentMemoryData = PersistentMemory.Read();

                        byte[] byteArray0 = BitConverter.GetBytes(width);
                        byte[] byteArray1 = BitConverter.GetBytes(height);

                        for(int i=0; i<byteArray0.Length; i++ )
                        {
                           persistentMemoryData[OFFSET_ADDR_WIDTH + i] = byteArray0[i];
                        }

                        for(int i=0; i<byteArray1.Length; i++ )
                        {
                           persistentMemoryData[OFFSET_ADDR_HEIGHT + i] = byteArray1[i];
                        }

                        // 保存
                        PersistentMemory.Write(persistentMemoryData);

                        // リソースをすべて解放
                        Destroy();

                        // 初期化
                        Initialize ();

                        return;
                     }
                  }
               }

               // テキスト入力ダイアログを閉じる
               dialog.Dispose();
               dialog = null;
            }
         }
      }

      // レンダリング
      public static void Render()
      {
         // クリア
         graphics.SetClearColor (0.2f, 0.2f, 1, 1 );
         graphics.SetClearDepth( 1 );
         graphics.Clear ();

         // デバッグ情報出力
         debugFont.Draw( graphics, graphics.Screen.Width.ToString() + ":" + graphics.Screen.Height.ToString() );

         // スワップ
         graphics.SwapBuffers();
      }
   }
}


Prev Top Next
inserted by FC2 system