// File: /workspace/github_cpp_makefile_star2_O2/dmarto[P]crrs-xlib/main.cpp
// Function: main
int main()
{
	const int W = 300;
	const int H = 300;

	//
	Display *display = XOpenDisplay(NULL);
	Window rWin = XDefaultRootWindow(display);

	Window win = XCreateSimpleWindow(display, rWin, 0, 0, W, H, 0, 0, 0);

	XStoreName(display, win, "Ray Tracer");

	unsigned long eventmask = PointerMotionMask | ButtonPressMask;
	XSelectInput(display, win, eventmask);

	XMapWindow(display, win);

	// TODO: render style
	GC render = XCreateGC(display, win, 0, NULL);
	XSync(display, False);

	// WORLD

	Colors color;

	Sphere sphere(Vec3(W * 0.5, H * 0.5, 40), 30, color.white);
	Sphere sphere1(Vec3(W * 0.3, H * 0.5, 20), 15, color.red);
	Sphere sphere2(Vec3(W * 0.25, H * 0.35, 30), 20, color.green);
	Sphere sphere3(Vec3(W * 0.85, H * 0.65, 40), 35, color.blue);

	Vec3 worldColor = Vec3(94, 0, 182);
	Sphere world(Vec3(W * 0.5, H * 3.8, 650), 1100, worldColor, "world");

	Sphere light(Vec3(W * 0.1, H * 0.5, 0), 40, color.white);

	std::vector<Sphere *> objList = {&world, &sphere, &sphere1, &sphere2, &sphere3};

	double theta = 0.0;

	std::vector<Vec3> background;
	for (int y = 0; y < H * 2; ++y)
		for (int x = 0; x < W * 2; ++x)
			background.emplace_back(Vec3(122, 11, 133) - (x + y) / 5);

	// WORLD LOOP

	XEvent event;
	while (1)
	{
		if (!XCheckMaskEvent(display, eventmask, &event))
			event.type = 0;

		HandleEvents(event, light, world);

		for (int y = 0; y < H; ++y)
			for (int x = 0; x < W; ++x)
			{
				ScreenData scrnData = {color, x, y, light, objList};
				Vec3 pixclr = RenderPixel(scrnData);
				ColorBoundary(pixclr);

				XSetForeground(display, render, _RGB((int)pixclr.x, (int)pixclr.y, (int)pixclr.z));
				XDrawPoint(display, win, render, x, y);
			}

		int startIdx = 1;
		objList[startIdx]->center.x = (W >> 1) + (W / 6) * sin(theta);
		objList[startIdx]->center.y = H / 2.8 + (H / 6) * cos(theta / 2);
		objList[startIdx + 1]->center.x = objList[startIdx + 1]->center.x + 2 * sin(theta);
		objList[startIdx + 1]->center.y = objList[startIdx + 1]->center.y + 2 * cos(theta);
		objList[startIdx + 2]->center.y = objList[startIdx + 2]->center.y + 2 * sin(theta);
		objList[startIdx + 3]->center.y = objList[startIdx + 3]->center.y + 2 * -sin(theta);
		// objList[startIdx + 3]->center.z = 50 + 10 * sin(theta);

		theta += 0.1;

		XFlush(display);
		XSync(display, False);
	}

	XCloseDisplay(display);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1180
movq %rax, %rbx
movq %rax, %rdi
callq 0x11a0
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
andl $0x0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movl $0x12c, %r8d            # imm = 0x12C
movl $0x12c, %r9d            # imm = 0x12C
callq 0x1060
movq %rax, %r15
leaq 0x177f(%rip), %rdx      # 0x3236
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1030
pushq $0x44
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10f0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1160
movq %rbx, %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x10d0
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1200
movaps 0x160b(%rip), %xmm0     # 0x3110
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0xa0(%rsp)
movabsq $0x406fe00000000000, %rax # imm = 0x406FE00000000000
movq %rax, 0xb0(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0xb8(%rsp)
movups %xmm0, 0xc8(%rsp)
xorpd %xmm1, %xmm1
movsd 0x14c2(%rip), %xmm0     # 0x3008
movups %xmm0, 0xd8(%rsp)
movaps 0x15cb(%rip), %xmm0     # 0x3120
movups %xmm0, 0xe8(%rsp)
movq %rax, 0xf8(%rsp)
movapd %xmm1, 0x100(%rsp)
movaps 0x15bb(%rip), %xmm0     # 0x3130
movaps %xmm0, 0x110(%rsp)
movaps 0x15bc(%rip), %xmm0     # 0x3140
leaq 0x378(%rsp), %rcx
movaps %xmm0, -0x38(%rcx)
movaps 0x15b9(%rip), %xmm0     # 0x3150
movaps %xmm0, -0x28(%rcx)
movaps 0x90(%rsp), %xmm0
movaps %xmm0, -0x18(%rcx)
movq 0xa0(%rsp), %rax
movq %rax, -0x8(%rcx)
leaq 0x388(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps 0x1595(%rip), %xmm0     # 0x3160
leaq 0x318(%rsp), %rdx
movaps %xmm0, -0x38(%rdx)
movaps 0x1592(%rip), %xmm0     # 0x3170
movaps %xmm0, -0x28(%rdx)
movups 0xd8(%rsp), %xmm0
movaps %xmm0, -0x18(%rdx)
movq 0xe8(%rsp), %rcx
movq %rcx, -0x8(%rdx)
leaq 0x328(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %r14b, (%rax)
movb %r14b, (%rcx)
movaps 0x1568(%rip), %xmm0     # 0x3180
leaq 0x2b8(%rsp), %rcx
movaps %xmm0, -0x38(%rcx)
movaps 0x1565(%rip), %xmm0     # 0x3190
movaps %xmm0, -0x28(%rcx)
movaps 0xf0(%rsp), %xmm0
movaps %xmm0, -0x18(%rcx)
movq 0x100(%rsp), %rax
movq %rax, -0x8(%rcx)
leaq 0x2c8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %r14b, (%rax)
movaps 0x153e(%rip), %xmm0     # 0x31a0
leaq 0x258(%rsp), %rcx
movaps %xmm0, -0x38(%rcx)
movaps 0x153b(%rip), %xmm0     # 0x31b0
movaps %xmm0, -0x28(%rcx)
movupd 0x108(%rsp), %xmm0
movapd %xmm0, -0x18(%rcx)
movq 0x118(%rsp), %rax
movq %rax, -0x8(%rcx)
leaq 0x268(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %r14b, (%rax)
leaq 0x1582(%rip), %rsi      # 0x3230
leaq 0x120(%rsp), %rdi
leaq 0x3a0(%rsp), %rdx
callq 0x2204
movaps 0x14f6(%rip), %xmm0     # 0x31c0
leaq 0x1f8(%rsp), %rdi
movaps %xmm0, -0x38(%rdi)
movaps 0x14f3(%rip), %xmm0     # 0x31d0
movaps %xmm0, -0x28(%rdi)
movsd 0x1387(%rip), %xmm0     # 0x3070
movapd %xmm0, -0x18(%rdi)
movabsq $0x4066c00000000000, %rax # imm = 0x4066C00000000000
movq %rax, -0x8(%rdi)
leaq 0x120(%rsp), %rsi
callq 0x1190
leaq 0x120(%rsp), %rdi
callq 0x10b0
movaps 0x14c3(%rip), %xmm0     # 0x31e0
leaq 0x198(%rsp), %rcx
movaps %xmm0, -0x38(%rcx)
xorps %xmm0, %xmm0
movhps 0x12dd(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, -0x28(%rcx)
movapd 0x90(%rsp), %xmm0
movapd %xmm0, -0x18(%rcx)
movq 0xa0(%rsp), %rax
movq %rax, -0x8(%rcx)
leaq 0x1a8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1c0(%rsp), %rax
leaq 0x3a0(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x340(%rsp), %rax
movq %rax, 0x8(%rsi)
leaq 0x2e0(%rsp), %rax
movq %rax, 0x10(%rsi)
leaq 0x280(%rsp), %rax
movq %rax, 0x18(%rsi)
leaq 0x220(%rsp), %rax
movq %rax, 0x20(%rsi)
leaq 0x78(%rsp), %rdi
pushq $0x5
popq %rdx
leaq 0x40(%rsp), %rcx
callq 0x2240
leaq 0x140(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
andq $0x0, 0x10(%r14)
xorl %r13d, %r13d
pushq $0x5
popq %rbp
leaq 0x3a0(%rsp), %r12
cmpl $0x258, %r13d           # imm = 0x258
je 0x1e42
xorl %r15d, %r15d
cmpl $0x258, %r15d           # imm = 0x258
je 0x1e3d
leal (%r15,%r13), %eax
xorl %edx, %edx
divl %ebp
cvtsi2sd %eax, %xmm0
movsd 0x1271(%rip), %xmm1     # 0x3078
subsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x13d9(%rip), %xmm2     # 0x31f0
subpd %xmm0, %xmm2
movapd %xmm2, 0x3a0(%rsp)
movsd %xmm1, 0x3b0(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2276
incl %r15d
jmp 0x1dea
incl %r13d
jmp 0x1dde
xorpd %xmm0, %xmm0
leaq 0x3a0(%rsp), %r13
leaq 0x60(%rsp), %r12
leaq 0x40(%rsp), %r14
movq %rbx, %r15
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
pushq $0x44
popq %rsi
movq %r13, %rdx
callq 0x1170
testl %eax, %eax
jne 0x1e7b
andl $0x0, 0x3a0(%rsp)
movq %r13, %rdi
leaq 0x160(%rsp), %rsi
leaq 0x1c0(%rsp), %rdx
callq 0x19a8
xorl %r13d, %r13d
cmpl $0x12c, %r13d           # imm = 0x12C
je 0x1f43
xorl %ebp, %ebp
cmpl $0x12c, %ebp            # imm = 0x12C
je 0x1f3b
leaq 0x90(%rsp), %rax
movq %rax, 0x40(%rsp)
movl %ebp, 0x48(%rsp)
movl %r13d, 0x4c(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x1947
movq %r12, %rdi
callq 0x130c
cvttsd2si 0x60(%rsp), %eax
cvttsd2si 0x68(%rsp), %ecx
cvttsd2si 0x70(%rsp), %edx
shll $0x8, %ecx
shll $0x10, %eax
addl %edx, %eax
addl %ecx, %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x1070
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl %ebp, %ecx
movl %r13d, %r8d
callq 0x1120
incl %ebp
jmp 0x1ea5
incl %r13d
jmp 0x1e96
movsd 0x18(%rsp), %xmm0
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x10e0
movq 0x78(%rsp), %r15
movq 0x8(%r15), %r13
movsd 0x18(%rsp), %xmm0
mulsd 0x10d1(%rip), %xmm0     # 0x3040
callq 0x11f0
movsd 0x38(%rsp), %xmm1
movapd %xmm1, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd 0x1276(%rip), %xmm2     # 0x3200
addpd 0x127e(%rip), %xmm2     # 0x3210
movupd %xmm2, (%r13)
movq 0x10(%r15), %rax
movhpd 0x30(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
movupd (%rax), %xmm0
addpd %xmm1, %xmm1
addpd %xmm1, %xmm0
movupd %xmm0, (%rax)
movq 0x18(%r15), %rax
movsd 0x8(%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq 0x20(%r15), %rax
movsd 0x8(%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rax)
movq %rbx, %r15
movq %rbx, %rdi
callq 0x11b0
movsd 0x18(%rsp), %xmm0
leaq 0x3a0(%rsp), %r13
addsd 0x1089(%rip), %xmm0     # 0x3080
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1200
movsd 0x18(%rsp), %xmm0
jmp 0x1e5b
jmp 0x2018
jmp 0x2018
jmp 0x2018
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x243c
leaq 0x78(%rsp), %rdi
callq 0x23b2
jmp 0x2037
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x10b0
leaq 0x1f8(%rsp), %rdi
jmp 0x2059
movq %rax, %rbx
leaq 0x120(%rsp), %rdi
callq 0x10b0
jmp 0x2063
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x10b0
leaq 0x2b8(%rsp), %rdi
callq 0x10b0
leaq 0x318(%rsp), %rdi
callq 0x10b0
leaq 0x378(%rsp), %rdi
callq 0x10b0
movq %rbx, %rdi
callq 0x11d0
nop