#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["SampleVS01"]
#Text["SampleVS01: Basic Vertex Shaders"]

@Initialize {
	TSample();
}

@MainLoop {
	if (GetKeyState(KEY_Q) == KEY_PUSH)
		CloseStgScene();
	yield;
}

task TSample() {
	let dir = GetCurrentScriptDirectory();
	let path = dir ~ "../default_system/img/Default_Background_IceMountain_Spell01.png";

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	Obj_SetRenderPriorityI(obj, 23);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, path);
	
	//Please do not actually use 10000 vertices
	let VC = 5000;
	
	ObjPrim_SetVertexCount(obj, VC * 2);
	ObjRender_SetPosition(obj, 192, 224, 1);
	
	ascent (i in 0..VC) {
		let v_angle = 360 / (VC - 1) * i;
		let tex_v = 1 / (VC - 1) * i;
		ObjPrim_SetVertexPosition(obj, i * 2, 0, 0, 1);
		ObjPrim_SetVertexPosition(obj, i * 2 + 1, 240 * cos(v_angle), 240 * sin(v_angle), 1);
		ObjPrim_SetVertexUV(obj, i * 2, 0, tex_v);
		ObjPrim_SetVertexUV(obj, i * 2 + 1, 1, tex_v);
	}
	
	//Enables vertex shader rendering. 
	//If this is set to true and a vertex+pixel shader isn't used, rendering will always fail.
	ObjRender_SetVertexShaderRenderingMode(obj, true);
	
	ObjShader_SetShaderF(obj, dir ~ "SampleVS01_HLSL.txt");
	ObjShader_SetTechnique(obj, "Render");

	let t = 0;
	while (true) {
		ObjShader_SetFloat(obj, "frame_", t);
		/*
		//Equivalent to these
		ascent(i in 0..VC){
			let tex_v = 1 / (VC - 1) * i;
			ObjPrim_SetVertexUV(obj, i * 2, 0 - t, tex_v);
			ObjPrim_SetVertexUV(obj, i * 2 + 1, 1 - t, tex_v);
		}
		*/
	
		t += 0.006;
		t %= 1;
		yield;
	}
}

