// File: /workspace/github_cpp_makefile_star2_O2/Lut99[P]RayTracer/src/lib/Renderer.cpp
// Function: RayTracer::Renderer::render_animation(RayTracer::RenderWorld*, RayTracer::Camera*, RayTracer::Frames&)
void Renderer::render_animation(RenderWorld* world, Camera* cam, Frames& out) {
    #ifdef THREADED
    ThreadPool pool(this->n_threads, this->batch_size);
    #endif

    for (std::size_t i = 0; i < out.n_frames; i++) {
        // Render this frame
        ProgressBar prgrs(0, cam->width * cam->height - 1, "(" + to_string(i + 1) + "/" + to_string(out.n_frames) + ")", "");

        #ifndef THREADED

        for (int y = cam->height-1; y >= 0; y--) {
            for (int x = 0; x < cam->width; x++) {
                // Render the pixel
                out[y][x] = world->render_pixel(x, y, *cam);

                if (this->show_progressbar) {
                    prgrs.update();
                }
            }
        }

        #else

        // Use the thread pool to render it
        unsigned long batch_index = 0;
        unsigned long to_do = cam->width * cam->height;
        while (batch_index < to_do) {
            // Update the progressbar in any case for smoothness
            if (this->show_progressbar) {
                prgrs.set(batch_index);
            }

            // If a the queue is full, continue
            if (pool.batch_queue_full()) {
                pool.wait();
            }

            // Create a new batch and append it
            PixelBatch batch = pool.get_batch(cam->width, cam->height, batch_index);
            batch.camera = cam;
            batch.world = world;
            batch.out = &out.get_current_frame();
            pool.add_batch(batch);
        }

        // Make sure the threads are complete
        pool.complete();

        #endif

        // Update the frames
        out.next();

        // Update the world & camera
        chrono::milliseconds time_passed(1000 / out.fps);
        world->update(time_passed);
        cam->update(time_passed);
    }

    #ifdef THREADED
    pool.stop();
    #endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, 0x30(%rsp)
xorl %esi, %esi
leaq 0x98(%rsp), %rbx
leaq 0xf8(%rsp), %rbp
movl 0x44(%r15), %eax
cmpq %rax, %rsi
jae 0x7236e
movslq 0x8(%r14), %rax
movq %rax, 0x20(%rsp)
movslq 0xc(%r14), %rax
movq %rax, 0x28(%rsp)
incq %rsi
leaq 0x78(%rsp), %r12
movq %r12, %rdi
movq %rsi, 0x18(%rsp)
callq 0x6eaa7
movq %rbx, %rdi
leaq 0x1e27a(%rip), %rsi     # 0x90449
movq %r12, %rdx
callq 0x6e761
leaq 0x38(%rsp), %rdi
movq %rbx, %r12
movq %rbx, %rsi
leaq 0x1e084(%rip), %rdx     # 0x9026d
callq 0x5a4df
movl 0x44(%r15), %esi
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x72430
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
movq %rbx, %rdx
callq 0x6aac0
leaq 0xb8(%rsp), %rdi
movq %rbp, %rsi
leaq 0x1c096(%rip), %rdx     # 0x8e2b7
callq 0x5a4df
leaq 0xd8(%rsp), %r13
movq %r13, %rdi
leaq 0x1a160(%rip), %rsi     # 0x8c398
leaq 0xf(%rsp), %rdx
callq 0x56548
movq 0x28(%rsp), %rdx
imulq 0x20(%rsp), %rdx
decq %rdx
leaq 0x118(%rsp), %rdi
xorl %esi, %esi
leaq 0xb8(%rsp), %rbx
movq %rbx, %rcx
movq %r13, %r8
callq 0x82ee0
movq %r13, %rdi
callq 0x52888
movq %rbx, %rdi
callq 0x52888
movq %rbp, %rdi
callq 0x52888
leaq 0x58(%rsp), %rdi
callq 0x52888
leaq 0x38(%rsp), %rdi
callq 0x52888
movq %r12, %rdi
callq 0x52888
leaq 0x78(%rsp), %rdi
callq 0x52888
movl 0xc(%r14), %r13d
testl %r13d, %r13d
jle 0x72326
decl %r13d
xorl %ebx, %ebx
cmpl 0x8(%r14), %ebx
jge 0x722af
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movl %ebx, %edx
movl %r13d, %ecx
movq %r14, %r8
callq 0x84620
movq %r15, %rdi
movl %r13d, %esi
callq 0x72a6c
movq %rax, 0x38(%rsp)
movl %edx, 0x40(%rsp)
leaq 0x38(%rsp), %rdi
movl %ebx, %esi
callq 0x73004
movq %rax, 0x98(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x72fd8
movq 0x30(%rsp), %rax
cmpb $0x1, 0x8(%rax)
jne 0x72322
leaq 0x118(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x8330a
incl %ebx
jmp 0x722b9
movq %r15, %rdi
callq 0x7290e
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
divl 0x48(%r15)
movl %eax, %ebx
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x84730
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x18(%rax)
leaq 0x118(%rsp), %rdi
callq 0x72416
movq 0x18(%rsp), %rsi
movq %r12, %rbx
jmp 0x72191
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x723fd
movq %rax, %rbx
jmp 0x723cf
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x52888
jmp 0x723a6
movq %rax, %rbx
jmp 0x723b3
movq %rax, %rbx
jmp 0x723d9
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x52888
leaq 0xf8(%rsp), %rdi
callq 0x52888
jmp 0x723c5
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x52888
leaq 0x38(%rsp), %rdi
callq 0x52888
leaq 0x98(%rsp), %rdi
callq 0x52888
leaq 0x78(%rsp), %rdi
callq 0x52888
jmp 0x7240d
movq %rax, %rbx
jmp 0x723e6
jmp 0x723fd
jmp 0x723fd
jmp 0x723fd
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x72416
movq %rbx, %rdi
callq 0x527a0
nop