銀色瓦版

絵が趣味の管理人がだらだら書く日記。

なんとなくゲームプログラミング#2

このプログラムカテゴリ、なんと1年ぶりである。

1年放置している間に、C#を勉強してXNAを使う方針に変更した。
がんばってC#を勉強しているけど、そもそもクラスがわかってないので先は遠い。

制作目標はスパロボのパクリゲーなので、とりあえずスパロボが2Dゲーということから平面の画像処理を充実させる事に。
ただ一部に3Dは使うだろうから、いちおう3Dベースで作る。

ソース1
public class GameMain : Microsoft.Xna.Framework.Game
{
private GraphicsDeviceManager graphics;
private VertexDeclaration dec;
private BasicEffect effect;

private Graphics2D graph;

public GameMain()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
// TODO: Add your initialization logic here

graph = new Graphics2D(Content, "test");
effect = new BasicEffect(GraphicsDevice, null);
effect.TextureEnabled = true;

dec = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);

base.Initialize();
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.VertexDeclaration = dec;

GraphicsDevice.Clear(Color.CornflowerBlue);

effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
texture.Draw(GraphicDevice, effect);
pass.End();
}
effect.End();

base.Draw(gameTime);
}

static void Main(string[] args)
{
using (Game game = new GameMain()) game.Run();
}
}

public class Graphics2D
{
private Texture2D texture;
private VertexPosition[] texture;

public Graphics2D(ContentManager content, string name)
{
texture = content.Load(name);

vertex = new VertexPositionTexture[] {
new VertexPositionTexture(new Vector3(-0.5F, 0.5F, 0), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3( 0.5F, 0.5F, 0), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3( 0.5F, -0.5F, 0), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5F, 0.5F, 0), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3( 0.5F, 0.5F, 0), new Vector2(1, 0))
new VertexPositionTexture(new Vector3(-0.5F, -0.5F, 0), new Vector2(0, 1))
};
}

public void Draw(GraphicsDevice graphics, BasicEffect effect)
{
effect.Texture = texture;
graphics.DrawUserPrimitives(PrimitiveType.TriangleList, vertex, 0, 2);
}
}



最初はこんな感じで作った。
なんて言うか初期化がうざい。実にうざい。でもDirectXをそのまま使うよりはだいぶんまし。

XNAに依存する処理をなるべく分離させようという素人の思いつきでGraphics2Dクラスなんか作ってみた。他のクラスはテクスチャとかを持ったGraphics2Dクラスを持つことで、テクスチャを間接的に持つ。
しかし、結局ContentManagerとかGraphicsDeviceを渡さないといけないので、まったく意味がなかった。

そこでコードを書き換えてみた。

ソース2
public class GameMain : Microsoft.Xna.Framework.Game
{
private GraphicsDeviceManager graphics;
private VertexDeclaration dec;
private BasicEffect effect;

private VertexBuffer squareVertex;
private IndexBuffer indexBuffer;
private short[] indices;
private Graphics2D graph;

public GameMain()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
// TODO: Add your initialization logic here

var vertex = new VertexPositionTexture[] {
new VertexPositionTexture(new Vector3(-0.5F, 0.5F, 0), new Vector2(0, 0)),
new VertexPositionTexture(new Vector3( 0.5F, 0.5F, 0), new Vector2(1, 0)),
new VertexPositionTexture(new Vector3( 0.5F, -0.5F, 0), new Vector2(1, 1)),
new VertexPositionTexture(new Vector3(-0.5F, -0.5F, 0), new Vector2(0, 1))
};
indices = new short[] {0, 1, 2, 0, 2, 3};
squareVertex = new VertexBuffer(GraphicsDevice, VertexPositionTexture.SizeInBytes * vertex.Length, BufferUsage.None);
squareVertex.SetData(vertex);
indexBuffer = new IndexBuffer(GraphicsDevice, sizeof(short) * indices.Length, BufferUsage.None, IndexElementSize.SixteenBits);
indexBuffer.SetData(indices);

graph = new Graphics2D(Content, "test");
effect = new BasicEffect(GraphicsDevice, null);
effect.TextureEnabled = true;

dec = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);

base.Initialize();
}

protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.VertexDeclaration = dec;
GraphicsDevice.Vertices[0].SetSource(squareVertex, 0, VertexPositionTexture.SizeInBytes);
GraphicsDevice.Indices = indexBuffer;

GraphicsDevice.Clear(Color.CornflowerBlue);

effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
effect.Texture = graph.Texture;
effect.CommitChanges();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);
pass.End();
}
effect.End();

base.Draw(gameTime);
}

static void Main(string[] args)
{
using (Game game = new GameMain()) game.Run();
}
}

public class Graphics2D
{
private Texture2D texture;

public Graphics2D(ContentManager content, string name)
{
texture = content.Load(name);
}

public Texture2D Texture
{
get { return texture; }
}
}


全然関係ないところのコードも増えたりしたけど、Graphics2DにGraphicsDeviceを渡さなくてもよくなった。
だがここまでくるとTexture2Dをそのまま持った方がはやい。

結論:素人ができもしないことをするな
  1. 2009/06/14(日) 23:47:18|
  2. プログラム
  3. | トラックバック:0
  4. | コメント:0

Lunascapeを5にあげてみた
せっかくjavascriptの実行速度が速いので、標準のレンダリングエンジンをGecko(firefoxのあれ)にしてみることに
だがこれがまた微妙に使いづらかったりする

右クリックメニューがTridentとおおきく違うのでやりづらい
あとキャッシュの保存場所が通常の方法では変更できない
仕方がないので高度な設定(firefoxでアドレスバーにabout:configと入力すると使えるあれ。ルナのオプションから起動できるのはまずくないだろうか)をいじることに
バグりそう

あとキャッシュファイルに拡張子が付かないのでニコ動のデータひっぱり出すのがめんどい
  1. 2009/05/17(日) 15:29:04|
  2. 日記
  3. | トラックバック:0
  4. | コメント:0



GWからSAN値を減らしてがんばった成果物
手首がなかったり面の閉じ忘れがあったりそもそもドラグーンが跡形もなかったりと色々酷いが、しばらくShadeはさわりたくないのでここまで
  1. 2009/05/13(水) 01:43:49|
  2. 日記
  3. | トラックバック:0
  4. | コメント:0

例によってあまりにも放置がすぎたので慌てて更新
かといって書く事はあんまりない

とりあえず最近衝動買いがないなぁ、と思っていたので
C#の参考書を買うついでに
「スクリプトエンジンプログラミング」という本を買った
やる気がいつまで保つか

授業でcgiプログラムを作る事になったが、なぜかCで作るらしい
普通cgiはCでなんかつくんねーよプギャーとか思っていたが
家の本棚を眺めていると「C言語でつくるCGI入門」という本が
中学の時分に買ったもののようだ

アイマスSPは4人クリア
  1. 2009/04/30(木) 22:14:40|
  2. 日記
  3. | トラックバック:0
  4. | コメント:0

二ヶ月ぶりとなりました

さすがに三月ももう終わりなので、これはいけないと慌てて更新をすることに
これだから物事が継続してできない



生存報告だけではなんなので作業途中の画像
前にも掲載した事のある3Dエタフリ
ほとんど変わってません
  1. 2009/03/30(月) 01:31:21|
  2. 日記
  3. | トラックバック:0
  4. | コメント:0

あ…ありのまま今起こったことを話すぜ!
『おれは27時間テレビを見ていたと思ったらいつのまにか28時間テレビを見ていた』
な…何を言ってるのかかわからねーと思うが、おれも何をされたのかわからなかった
頭がどうにかなりそうだった…
放送事故だとか寝落ちだとかそんなチャチなもんじゃあ断じてねえ
もっと恐ろしい運営の罠を味わったぜ…

動画ピックアップなどは後日……
  1. 2009/01/18(日) 22:05:36|
  2. 日記
  3. | トラックバック:0
  4. | コメント:0

アイマス新年会 27時間無礼講SP スタート

はじまったよ


プレミアでも重いとかなにこの……なに?
  1. 2009/01/17(土) 18:01:56|
  2. ニコニコ
  3. | トラックバック:0
  4. | コメント:0
次のページ

プロフィール

銀之助

Author:銀之助
画像はTWにおける自キャラ
とりあえずメッセ
glow_of_the_sky@hotmail.com

FC2カウンター

テイルズ時計

最近の記事

最近のコメント

最近のトラックバック

月別アーカイブ

カテゴリー

ブロとも申請フォーム

この人とブロともになる

リンク

このブログをリンクに追加する

ブログ内検索

RSSフィード

Powered By FC2ブログ

Powered By FC2ブログ
ブログやるならFC2ブログ