// File: /workspace/github_cpp_makefile_star2_O1/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 $0x418, %rsp            # imm = 0x418
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1140
movq %rax, %rbx
movq %rax, %rdi
callq 0x1150
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rsp)
movl $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 0x1050
movq %rax, %r15
leaq 0x16d6(%rip), %rdx      # 0x3216
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1030
movl $0x44, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10b0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1120
movq %rbx, %rdi
movq %r15, 0x70(%rsp)
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1090
movq %rax, %r15
movq %rbx, %rdi
xorl %esi, %esi
callq 0x11b0
xorps %xmm0, %xmm0
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps 0x155f(%rip), %xmm0     # 0x3100
leaq 0x338(%rsp), %rax
movaps %xmm0, -0x48(%rax)
movaps 0x155c(%rip), %xmm0     # 0x3110
movaps %xmm0, -0x38(%rax)
movaps 0x1561(%rip), %xmm0     # 0x3120
movaps %xmm0, -0x28(%rax)
movabsq $0x406fe00000000000, %r12 # imm = 0x406FE00000000000
movq %r12, -0x18(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
movaps 0x154d(%rip), %xmm0     # 0x3130
leaq 0x2d8(%rsp), %rcx
movaps %xmm0, -0x48(%rcx)
movaps 0x154a(%rip), %xmm0     # 0x3140
movaps %xmm0, -0x38(%rcx)
movsd 0x1406(%rip), %xmm0     # 0x3008
movaps %xmm0, -0x28(%rcx)
movabsq $0x405b800000000000, %rax # imm = 0x405B800000000000
movq %rax, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq %r14, -0x8(%rcx)
movb %r14b, (%rcx)
movaps 0x152a(%rip), %xmm0     # 0x3150
leaq 0x278(%rsp), %rax
movaps %xmm0, -0x48(%rax)
movaps 0x1527(%rip), %xmm0     # 0x3160
movaps %xmm0, -0x38(%rax)
movapd 0x152b(%rip), %xmm0     # 0x3170
movapd %xmm0, -0x28(%rax)
movq %r14, -0x18(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
movaps 0x1520(%rip), %xmm1     # 0x3180
leaq 0x138(%rsp), %rax
movaps %xmm1, -0x48(%rax)
movapd 0x151c(%rip), %xmm1     # 0x3190
movapd %xmm1, -0x38(%rax)
movq %r14, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1570(%rip), %rsi      # 0x3210
leaq 0x156e(%rip), %rdx      # 0x3215
leaq 0x88(%rsp), %rdi
callq 0x241a
movaps 0x14e5(%rip), %xmm0     # 0x31a0
leaq 0x208(%rsp), %rdi
movaps %xmm0, -0x38(%rdi)
movaps 0x14e2(%rip), %xmm0     # 0x31b0
movaps %xmm0, -0x28(%rdi)
movsd 0x1396(%rip), %xmm0     # 0x3070
movapd %xmm0, -0x18(%rdi)
movabsq $0x4066c00000000000, %rax # imm = 0x4066C00000000000
movq %rax, -0x8(%rdi)
leaq 0x218(%rsp), %rax
movq %rax, -0x10(%rax)
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
addq %rsi, %rdx
callq 0x238a
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x1d2e
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x1110
movaps 0x148b(%rip), %xmm0     # 0x31c0
leaq 0x1b8(%rsp), %rax
movaps %xmm0, -0x48(%rax)
xorps %xmm0, %xmm0
movhps 0x12c5(%rip), %xmm0     # xmm0 = xmm0[0,1],mem[0,1]
movaps %xmm0, -0x38(%rax)
movapd 0x13c9(%rip), %xmm0     # 0x3120
movapd %xmm0, -0x28(%rax)
movq %r12, -0x18(%rax)
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1d0(%rsp), %rax
leaq 0x350(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x2f0(%rsp), %rax
movq %rax, 0x8(%rsi)
leaq 0x290(%rsp), %rax
movq %rax, 0x10(%rsi)
leaq 0x230(%rsp), %rax
movq %rax, 0x18(%rsi)
leaq 0xf0(%rsp), %rax
movq %rax, 0x20(%rsi)
leaq 0xa8(%rsp), %rdi
leaq 0xc0(%rsp), %rcx
movl $0x5, %edx
callq 0x2344
movq %rbx, 0x38(%rsp)
leaq 0x50(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
xorl %r13d, %r13d
movl $0xcccccccd, %ebp       # imm = 0xCCCCCCCD
leaq 0x350(%rsp), %r12
xorl %ebx, %ebx
leal (%rbx,%r13), %eax
imulq %rbp, %rax
shrq $0x22, %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x1264(%rip), %xmm1     # 0x3078
subsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movapd 0x13ac(%rip), %xmm2     # 0x31d0
subpd %xmm0, %xmm2
movapd %xmm2, 0x350(%rsp)
movsd %xmm1, 0x360(%rsp)
movq 0x58(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
je 0x1e67
movq 0x360(%rsp), %rax
movq %rax, 0x10(%rsi)
movapd 0x350(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x58(%rsp)
jmp 0x1e72
movq %r14, %rdi
movq %r12, %rdx
callq 0x2550
incl %ebx
cmpl $0x258, %ebx            # imm = 0x258
jne 0x1df9
incl %r13d
cmpl $0x258, %r13d           # imm = 0x258
jne 0x1df7
leaq 0x158(%rsp), %r12
xorpd %xmm0, %xmm0
leaq 0x350(%rsp), %r14
leaq 0xa8(%rsp), %r13
movq 0x38(%rsp), %rbx
movsd %xmm0, 0x18(%rsp)
movl $0x44, %esi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x1130
testl %eax, %eax
jne 0x1ed6
movl $0x0, 0x350(%rsp)
movq %r14, %rdi
leaq 0x170(%rsp), %rsi
leaq 0x1d0(%rsp), %rdx
callq 0x1a3e
xorl %ebp, %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %r14d, %r14d
movq 0x10(%r12), %rax
movq %rax, 0x30(%rsp)
movups (%r12), %xmm0
movaps %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movabsq $-0x3ff8000000000000, %rax # imm = 0xC008000000000000
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rax
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0xe8(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x170(%rsp), %rdx
movq %r13, %rcx
callq 0x17c2
movsd 0x20(%rsp), %xmm1
movsd 0x1075(%rip), %xmm0     # 0x3008
ucomisd %xmm1, %xmm0
jb 0x1fa7
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jb 0x1fa7
movapd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x28(%rsp), %xmm2
movsd 0x104d(%rip), %xmm1     # 0x3008
ucomisd %xmm2, %xmm1
jb 0x1fcf
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jb 0x1fcf
movapd %xmm2, %xmm1
movsd %xmm1, 0x28(%rsp)
movsd 0x30(%rsp), %xmm3
movsd 0x1025(%rip), %xmm2     # 0x3008
ucomisd %xmm3, %xmm2
jb 0x1ff7
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm3
jb 0x1ff7
movapd %xmm3, %xmm2
cvttsd2si %xmm0, %eax
cvttsd2si %xmm1, %ecx
movsd %xmm2, 0x30(%rsp)
cvttsd2si %xmm2, %edx
shll $0x8, %ecx
shll $0x10, %eax
addl %ecx, %eax
addl %edx, %eax
movslq %eax, %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1060
movq %rbx, %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movl %r14d, %ecx
movl %ebp, %r8d
callq 0x10e0
incl %r14d
cmpl $0x12c, %r14d           # imm = 0x12C
jne 0x1f00
incl %ebp
cmpl $0x12c, %ebp            # imm = 0x12C
jne 0x1ef0
movsd 0x18(%rsp), %xmm0
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x10a0
movsd 0x80(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0xa8(%rsp), %rbx
movq 0x8(%rbx), %r14
movsd 0x18(%rsp), %xmm0
mulsd 0xfab(%rip), %xmm0      # 0x3040
callq 0x11a0
movapd 0x40(%rsp), %xmm2
movapd %xmm2, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd 0x1130(%rip), %xmm1     # 0x31e0
addpd 0x1138(%rip), %xmm1     # 0x31f0
movupd %xmm1, (%r14)
movq 0x10(%rbx), %rax
movupd (%rax), %xmm0
movapd %xmm2, %xmm1
movhpd 0x78(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
addpd %xmm1, %xmm1
addpd %xmm1, %xmm0
movupd %xmm0, (%rax)
movq 0x18(%rbx), %rax
addsd 0x8(%rax), %xmm1
movsd %xmm1, 0x8(%rax)
movq 0x20(%rbx), %rax
movq 0x38(%rsp), %rbx
movsd 0x8(%rax), %xmm0
addsd %xmm2, %xmm2
subsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rax)
movq %rbx, %rdi
callq 0x1160
movsd 0x18(%rsp), %xmm0
leaq 0x350(%rsp), %r14
addsd 0xf5e(%rip), %xmm0      # 0x3080
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x11b0
movsd 0x18(%rsp), %xmm0
jmp 0x1eb1
jmp 0x2143
jmp 0x2143
jmp 0x2143
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x215d
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1110
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x217f
movq 0xb8(%rsp), %rsi
subq %rdi, %rsi
callq 0x1110
jmp 0x217f
movq %rax, %rbx
leaq 0x1b8(%rsp), %rax
movq 0x1a8(%rsp), %rdi
cmpq %rax, %rdi
je 0x21a4
movq 0x1b8(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x208(%rsp), %rdi
leaq 0x218(%rsp), %rax
cmpq %rax, %rdi
je 0x21e8
movq 0x218(%rsp), %rsi
jmp 0x21db
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x21e8
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x1110
jmp 0x21e8
movq %rax, %rbx
leaq 0x138(%rsp), %rax
movq 0x128(%rsp), %rdi
cmpq %rax, %rdi
je 0x220d
movq 0x138(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x268(%rsp), %rdi
leaq 0x278(%rsp), %rax
cmpq %rax, %rdi
je 0x2232
movq 0x278(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x2c8(%rsp), %rdi
leaq 0x2d8(%rsp), %rax
cmpq %rax, %rdi
je 0x2257
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x1110
movq 0x328(%rsp), %rdi
leaq 0x338(%rsp), %rax
cmpq %rax, %rdi
je 0x227c
movq 0x338(%rsp), %rsi
incq %rsi
callq 0x1110
movq %rbx, %rdi
callq 0x1180