import dlib.aliases;
import dlib.platform;
import editor;
import std.stdio;
import dlib.alloc;
import dlib.util;
import dlib.math;

void main(string[] argv)
{
	u8[] buffer = null;
	u8[] buffer_name = null;
	if(argv.length > 1)
	{
		buffer_name = (cast(u8*)argv[1].ptr)[0 .. argv[1].length];
		File f = File(argv[1], "rb");
		buffer = Alloc!(u8)(f.size());
		f.rawRead(buffer);
		f.close();
	}

	PlatformWindow window;
	if(!CreateWindow(&window, "Editor", 1920, 1080))
	{
		Errf("Unable to create window: %s", WindowError());
		return;
	}

	StartPlatformThread(&window);

	InitCtx(&window);

	import ui;
	import vulkan;

	u64 no_ev_count;
	while (true)
	{
		Inputs* inputs = GetEvents(&window);
		if(window.close) 
		{
			break;
		}

		Vec2 current_res = Vec2(window.w, window.h);
		if(g_ctx.pc.resolution != current_res)
		{
			SetExtent(window.w, window.h);
			g_ctx.pc.resolution = current_res;
			Ortho(&g_ctx.pc.projection, 0.0, 0.0, current_res.x, current_res.y, -10.0, 10.0);
		}

		if(inputs.first == null)
		{
			no_ev_count += 1;
			Pause();
			
			if(no_ev_count >= 5)
			{
				no_ev_count = 0;
				Yield();
			}
		}

		Cycle(inputs);
	}
}
