PlayStation Mobile Studio
 PlayStation(R)Mobile SDK 1.21.01

■Playstation Mobile テキストファイルの保存と読み込み Prev Top Next
関連ページ:なし


今回はテキストファイルの保存と読み込みをやります。
前回同様 C# 標準ライブラリを使用します。ファイル形式は扱いやすいXmlとします。
まあ実際のゲーム制作でどうしているかは知りませんが、少なくともこのままでは簡単に書き換えができてしまうので暗号化するとか必要かもしれません。

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.IO;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using Sce.PlayStation.Core.Input;

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

      private static DebugFont debugFont;

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

      // Xmlファイルのパス
      private const string SYSTEM_SETTING = "/Documents/SystemSetting.xml";

      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;

         // Xmlファイルが存在しない
         if( File.Exists( SYSTEM_SETTING ) == false )
         {
            // Xml書き込み時の各種設定
            XmlWriterSettings settings = new XmlWriterSettings();
#if DEBUG
            settings.Indent = true;
            settings.IndentChars = ("   ");
            settings.NewLineChars = "\r\n";
            settings.Encoding = Encoding.GetEncoding( "UTF-8" );
#else
            settings.Indent = false;
            settings.NewLineChars = "";
            settings.Encoding = Encoding.GetEncoding( "UTF-8" );
#endif
            // Xmlファイルを作成する
            using( XmlWriter xmlWriter = XmlWriter.Create( SYSTEM_SETTING, settings ) )
            {
               xmlWriter.WriteStartDocument();
               {
                  xmlWriter.WriteStartElement( "system" );
                  {
                     xmlWriter.WriteStartElement( "window" );
                     {
                        xmlWriter.WriteStartElement( "width" );
                        xmlWriter.WriteValue( 800 );
                     }xmlWriter.WriteEndElement();
                     {
                        xmlWriter.WriteStartElement( "height" );
                        xmlWriter.WriteValue( 480 );
                     }xmlWriter.WriteEndElement();
                  }
               }xmlWriter.WriteEndDocument();

               // Xmlファイル出力
               xmlWriter.Flush();
            }
         }

         // Xmlファイル読み込み
         XmlDocument doc = new XmlDocument();

         doc.Load( SYSTEM_SETTING );
         XmlNode node = doc.SelectNodes( "/system/window/width" )[0];
         int width = int.Parse( node.InnerText );

         node = doc.SelectNodes( "/system/window/height" )[0];
         int height = int.Parse( node.InnerText );

         // Xmlファイルから取得したスクリーンサイズ( ウィンドウサイズではない )を使用して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;

                        // Xml更新
                        XmlDocument doc = new XmlDocument();
                        doc.Load( SYSTEM_SETTING );
                        // InnerXmlを使用した場合、値に<>を入れるとエラーになるので、InnerTextにする
                        doc.SelectNodes( "/system/window/width" )[0].InnerText = width.ToString();
                        doc.SelectNodes( "/system/window/height" )[0].InnerText = height.ToString();
                        doc.Save(SYSTEM_SETTING);

                        // リソースをすべて解放
                        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();
      }
   }
}

前回同様ウィンドウ上でマウスクリックするとテキスト入力ダイアログボックスが表示されます。
width:heightの形式でスクリーンサイズを入力すると、Xmlファイルに設定内容を保存し、GraphicsContextを再作成します。

ところでスクリーンサイズを変更してもウィンドウ本体のサイズは常に一定のようです。ドキュメントによるとどうも
設定したスクリーンサイズのアスペクト比を維持したまま最大サイズにスケーリングされて表示されるようです。


Prev Top Next
inserted by FC2 system