2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Siv3DでのJSONファイル使い方サンプル

Last updated at Posted at 2017-11-26

Siv3DでのJSONファイル使い方サンプル

JSON,INI,CSVの特徴

INI

余り使ったことが無いけど構成ファイルに使える。
JSONと違ってコメントを入れられる。

config.iniファイルを作って、ウィンドウサイズやキー設定を入れておくと便利。

CSV

複数ユニットの挙動を列挙するのに便利。
簡単に扱えるので初心者向け?
JSONの方が出来ることが多いので自分はもう使っていません。

JSON

一番便利。
ユニットのデータをまとめるのによく使います。
例外的なユニットも記述できる汎用性の高さが魅力的。
コメント機能がないのが残念(裏技的な方法は検索すれば出てきますが、、、)

具体例

data.json
[
    {
        "name":"ザリガニ",
        "color":"(255, 0, 0)",
        "size":100,
        "speed":1.0
    },
    {
        "name":"カタツムリ",
        "color":"(255, 128, 0)",
        "size":50,
        "speed":0.1
    },
]

Main.cpp
# include<Siv3D.hpp>

struct UData
{
	String	name;
	Color	color;
	int		size;
	double	speed;

	UData(JSONValue j)
		: name(j[L"name"].get<String>())
		, color(j[L"color"].get<Color>())
		, size(j[L"size"].get<int>())
		, speed(j[L"speed"].get<double>())
	{}
};

struct Unit
{
	UData*	uData;
	Vec2	pos;
	
	Unit(UData* _uData)
		: uData(_uData)
		, pos(RandomVec2(Window::ClientRect()))
	{}

	void	update()
	{
		Circle(pos, uData->size).draw(uData->color);
	}
};

void Main()
{
	Array<UData> uData;
	JSONReader json(L"data.json");
	for (auto j : json.root().getArray()) uData.emplace_back(j);

	Array<Unit>	units;
	for (auto& ud : uData) units.emplace_back(&ud);

	while (System::Update())
	{
		for (auto& u : units) u.update();
	}
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?