/*!

	\file テクスチャヘルパー関数

*/

#if defined HELPER_TEXTURE_INCLUDED___
  #endinput
#endif
#define HELPER_TEXTURE_INCLUDED___

#include <all.inc>

#include "./common/helper_xml.inc"

//////////////////////////////////////////////////////////////////////////////////
/*
	テクスチャ同期読み込み
*/
stock texture: TextureLoad(const url[], bool:is_keep = false) {

	if (strlen(url) == 0) {
		return texture:0;
	}
	new filename[256];
	sprintf( filename, url );
	// デコード起動
	imgDecStart(filename);

	// デコード待ち
	new result = imgDecWait();
	if (result != IMGDEC_OK) {
		printf("Failured  Texture : %s ....", filename);
		return texture:0;
	}
	if (is_keep) {
		new texture:tex = imgTexID();
		imgKeepTex(tex);
		return tex;
	}
	return imgTexID();
}

//////////////////////////////////////////////////////////////////////////////////
/*!

	texture_arrayにtagとtagname以下に設定されているテクスチャを全部読み込む
	offsetを指定すると、指定した配列のインデックスから処理される。

	\param xmltag:tag
	\param const tagname[]
	\param const id[]
	\param texture:texture_array[]
	\param offset = 0

	↓こんな構造のxml対応
	<TEXTURE id = "baseparts" comment = "共通BGテクスチャ">
		<PARTS path = "texture/block_start.png" />
		<PARTS path = "texture/block_goal.png" />
	</TEXTURE>

*/

stock TextureMultiLoadFromXml(
								xmltag:tag,
								const tagname[],
								const id[],
								texture:texture_array[],
								const offset = 0
							) {

	//テクスチャを取り込み
	new xmltag:parent = domFindTag( tag, tagname, id);
	new i = offset;
	for (new xmltag: child = domFirstChild(parent) ; child ; child = domNext(child)) {
		//座標とサイズを設定
		new tmp_value[64];
		domGetData(child, "path", tmp_value);

		texture_array[i] = TextureLoad(tmp_value, true);
		++i;
	}

}

//////////////////////////////////////////////////////////////////////////////////
/*!

	tagとtagname以下に設定されているテクスチャを読み込みtexture idを返す

	\param xmltag:tag
	\param const tagname[]
	\param const id[]
	\param index = 0

*/

stock texture: TextureSingleLoadFromXml(
													xmltag:tag,
													const tagname[],
													const id[],
													index = 0
											) {

	new xmltag:parent = domFindTag( tag, tagname, id);
	new xmltag:child = domFirstChild(parent);
	for (new i = 0; i < index;i++) child = domNext(child);

	new tmp_value[128];
	domGetData(child, "path", tmp_value);

	return TextureLoad(tmp_value, true);

}

//////////////////////////////////////////////////////////////////////////////////
/*!

	指定されたtagとidからindex番目のテクスチャを
	texture_array[array_index]に読み込む

	\param xmltag:tag
	\param const tagname[]
	\param const id[]
	\parama texture:texture_array[]
	\param array_index = 0
	\parama tex_index = 0

	↓こんな構造のxml対応
	<TEXTURE id = "alphabet" comment = "文字テクスチャ">
		<PARTS x =   "0" y = "32" w = "12" h = "16" char = "A"/>
		<PARTS x =  "12" y = "32" w = "12" h = "16" char = "B"/>
		<PARTS x =  "24" y = "32" w = "12" h = "16" char = "C"/>
	</TEXTURE>

*/
stock TrimingTextureFromXml(
								xmltag:tag,
								const tagname[],
								const id[],
								texture:texture_array[],
								const offset = 0,
								const x_offset = 0,
								const y_offset = 0,
								array_size = sizeof texture_array
							) {

	new xmltag:parent = domFindTag(tag, tagname, id);
	new i = offset;

	new array_count = 0;

	for (new xmltag: child = domFirstChild(parent) ; child ; child = domNext(child)) {
		//座標とサイズを設定
		new x, y, w, h;
		x = GetNumericData(child, "x");
		y = GetNumericData(child, "y");
		w = GetNumericData(child, "w");
		h = GetNumericData(child, "h");

		imgTrimStart(x + x_offset, y + y_offset, w, h);

		if(imgTrimWait() == IMGTRIM_OK) {
			new texture:tex_id = imgTexID();
			texture_array[i] = tex_id;
			imgKeepTex(tex_id);
		}
		++i;
		++array_count;
		if (array_count > array_size) {
			printf("array size over.\n");
		}
	}
}

