// File: /workspace/github_c_makefile_star2_O3/TurkeyMcMac[P]ts3d/src/d3d.c
// Function: d3d_new_camera
d3d_camera *d3d_new_camera(
	d3d_scalar fovx,
	d3d_scalar fovy,
	size_t width,
	size_t height,
	d3d_pixel empty_pixel)
{
	size_t size;
	size_t pixels_size, txtr_offset, tans_offset, dists_offset;
	d3d_texture *empty_txtr;
	d3d_camera *cam;
	size = offsetof(d3d_camera, pixels);
	pixels_size = width * height * sizeof(d3d_pixel);
	if (width != 0 && pixels_size / sizeof(d3d_pixel) / width != height)
		return NULL;
	CHECKED_ADD(size, pixels_size);
	// This type probably has the alignment of a one-pixel texture:
	typedef struct { size_t width, height; d3d_pixel pixels[1]; } one_pix;
	ALIGN_SIZE(size, one_pix);
	txtr_offset = size;
	CHECKED_ADD(size, texture_size(1, 1));
	ALIGN_SIZE(size, d3d_scalar);
	tans_offset = size;
	if (height * sizeof(d3d_scalar) / sizeof(d3d_scalar) != height)
		return NULL;
	CHECKED_ADD(size, height * sizeof(d3d_scalar));
	dists_offset = size;
	if (width * sizeof(d3d_scalar) / sizeof(d3d_scalar) != width)
		return NULL;
	CHECKED_ADD(size, width * sizeof(d3d_scalar));
	cam = d3d_malloc(size);
	if (!cam) return NULL;
	// The members 'tans' and 'dists' are actually pointers to parts of the
	// same allocation. 'empty_txtr' is glued on before them, but is not a
	// member.
	empty_txtr = (void *)((char *)cam + txtr_offset);
	cam->tans = (void *)((char *)cam + tans_offset);
	cam->dists = (void *)((char *)cam + dists_offset);
	// Just do basic protection against non-positive FOVs as they might
	// cause issues. I could do something better than silently clamping, but
	// what do you expect a non-positive FOV to do anyway?
	cam->fov.x = fovx > (d3d_scalar)0.0 ? fovx : 0.001;
	cam->fov.y = fovy > (d3d_scalar)0.0 ? fovy : 0.001;
	cam->width = width;
	cam->height = height;
	empty_txtr->width = 1;
	empty_txtr->height = 1;
	empty_txtr->pixels[0] = empty_pixel;
	cam->blank_block.faces[D3D_DPOSX] =
	cam->blank_block.faces[D3D_DPOSY] =
	cam->blank_block.faces[D3D_DNEGX] =
	cam->blank_block.faces[D3D_DNEGY] =
	cam->blank_block.faces[D3D_DUP] =
	cam->blank_block.faces[D3D_DDOWN] = empty_txtr;
	cam->order = NULL;
	cam->order_buf_cap = 0;
	cam->last_sprites = NULL;
	cam->last_n_sprites = 0;
	empty_camera_pixels(cam);
	for (size_t y = 0; y < height; ++y) {
		d3d_scalar angle =
			cam->fov.y * ((d3d_scalar)0.5 - (d3d_scalar)y / height);
		cam->tans[y] = tan(angle);
	}
	return cam;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %r8d
movq %rsi, %rbx
movq %rdi, %r15
movq %rdi, %rax
mulq %rsi
movq %rax, %r12
seto %al
testq %rdi, %rdi
je 0x270f
cmpq $-0x80, %r12
setae %cl
orb %al, %cl
je 0x271c
jmp 0x28cf
cmpq $-0x81, %r12
ja 0x28cf
leaq 0x80(%r12), %rax
leaq 0x87(%r12), %r13
andq $-0x8, %r13
cmpq %rax, %r13
setb %al
cmpq $-0x11, %r13
setae %cl
orb %al, %cl
jne 0x28cf
leaq 0x11(%r13), %rax
addq $0x9f, %r12
andq $-0x8, %r12
cmpq %rax, %r12
setb %cl
cmovbeq %rax, %r12
movq %rbx, %rax
shrq $0x3d, %rax
setne %al
orb %cl, %al
jne 0x28cf
leaq (%r12,%rbx,8), %rax
cmpq %r12, %rax
movq %r12, %rbp
cmovaq %rax, %rbp
movabsq $0x2000000000000000, %rcx # imm = 0x2000000000000000
xorl %r14d, %r14d
cmpq %rcx, %r15
jae 0x28d2
cmpq %r12, %rax
jb 0x28d2
leaq (,%r15,8), %rdi
addq %rbp, %rdi
jb 0x28cf
movapd %xmm1, 0x10(%rsp)
movapd %xmm0, 0x20(%rsp)
movl %r8d, 0x8(%rsp)
callq 0x2150
testq %rax, %rax
je 0x28cf
movq %rax, %r14
addq %r13, %rax
addq %r14, %r12
movq %r12, 0x70(%r14)
addq %r14, %rbp
movq %rbp, 0x78(%r14)
movapd 0x20(%rsp), %xmm2
unpcklpd 0x10(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
cmpltpd %xmm2, %xmm1
andpd %xmm1, %xmm2
andnpd 0x981a(%rip), %xmm1     # 0xc020
orpd %xmm2, %xmm1
movupd %xmm1, (%r14)
movq %r15, 0x10(%r14)
movq %rbx, 0x18(%r14)
movl $0x1, %ecx
movq %rcx, (%r14,%r13)
movq %rcx, 0x8(%r14,%r13)
movl 0x8(%rsp), %ecx
movb %cl, 0x10(%r14,%r13)
movq %rax, 0x48(%r14)
movq %rax, 0x40(%r14)
movq %rax, 0x38(%r14)
movq %rax, 0x30(%r14)
movq %rax, 0x28(%r14)
movq %rax, 0x20(%r14)
movupd %xmm0, 0x50(%r14)
movupd %xmm0, 0x60(%r14)
movq 0x18(%r14), %rax
imulq 0x10(%r14), %rax
testq %rax, %rax
je 0x2880
movb 0x10(%r14,%r13), %al
xorl %ecx, %ecx
movb %al, 0x80(%r14,%rcx)
incq %rcx
movq 0x18(%r14), %rdx
imulq 0x10(%r14), %rdx
cmpq %rdx, %rcx
jb 0x2867
testq %rbx, %rbx
je 0x28d2
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r15d, %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
divsd 0x8(%rsp), %xmm1
movsd 0x9844(%rip), %xmm0     # 0xc0f0
subsd %xmm1, %xmm0
mulsd 0x8(%r14), %xmm0
callq 0x20f0
movq 0x70(%r14), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0x2896
jmp 0x28d2
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq