// File: /workspace/github_cpp_makefile_star2_O2/IsseW[P]bytepusher/src/main.cpp
// Function: main
int main(){
    Renderer renderer{"some title", GRID_WIDTH, GRID_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT};

    Grid grid = Grid(GRID_WIDTH, GRID_HEIGHT);
    grid.set_tile(4, 4, Tile::Alive);
    grid.set_tile(4, 5, Tile::Alive);
    grid.set_tile(4, 6, Tile::Alive);
    grid.set_tile(4, 7, Tile::Alive);
    grid.set_tile(4, 8, Tile::Alive);
    grid.set_tile(4, 9, Tile::Alive);
    grid.set_tile(4, 10, Tile::Alive);

    uint64_t now, last;
    double delta_time = 0;

    double next_tick = TICK_TIME;

    auto callback = [&](const SDL_Event& e) {
        switch (e.type) {
            case SDL_MOUSEBUTTONDOWN: {
                uint32_t x = e.button.x * GRID_WIDTH / WINDOW_WIDTH;
                uint32_t y = e.button.y * GRID_HEIGHT / WINDOW_HEIGHT;
                switch (e.button.button) {
                    case SDL_BUTTON_LEFT:
                        grid.set_tile(x, y, Tile::Alive);
                        break;
                    
                }
                break;
            }
        }
    };
    renderer.set_manual_event_handler(callback);

    while(renderer.poll_events()) {
        last = now;
        now = SDL_GetPerformanceCounter();
        delta_time = (now - last) / (double)SDL_GetPerformanceFrequency();

        next_tick -= delta_time;
        if (next_tick < 0.0) {
            next_tick = TICK_TIME;
            grid.update_with_rule(game_of_life_rule);
        }

        renderer.bulk_send_data(grid.get_texture());
        renderer.finalize();
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb0, %rsp
leaq 0xccd(%rip), %rsi       # 0x3040
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x263c
leaq 0x58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
pushq $0x20
popq %rcx
movl %ecx, %edx
movl $0x140, %r8d            # imm = 0x140
movl $0x140, %r9d            # imm = 0x140
callq 0x2b54
leaq 0x18(%rsp), %rdi
callq 0x20c0
leaq 0x18(%rsp), %rdi
pushq $0x20
popq %rdx
movl %edx, %esi
callq 0x2778
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rdx
movq %rdx, %rsi
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x5
popq %rdx
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x6
popq %rdx
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x7
popq %rdx
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x8
popq %rdx
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x9
popq %rdx
movl $0xff, %ecx
callq 0x2892
leaq 0x18(%rsp), %rbx
pushq $0x4
popq %rsi
pushq $0xa
popq %rdx
movq %rbx, %rdi
movl $0xff, %ecx
callq 0x2892
leaq 0x38(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movq %rbx, (%rsi)
leaq 0x171(%rip), %rax       # 0x25d6
movq %rax, 0x18(%rsi)
leaq 0x1a6(%rip), %rax       # 0x2616
movq %rax, 0x10(%rsi)
leaq 0x58(%rsp), %rdi
callq 0x2b4a
leaq 0x38(%rsp), %rdi
callq 0x2678
movsd 0xb78(%rip), %xmm0      # 0x3008
leaq 0x58(%rsp), %rbx
leaq 0x564(%rip), %r14       # 0x2a00
leaq 0x18(%rsp), %r15
movsd %xmm0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2c9e
testb %al, %al
je 0x2566
callq 0x21c0
movq %rax, %r12
callq 0x2150
movq %r12, %rcx
subq %r13, %rcx
movq %rcx, %xmm0
movq 0xb49(%rip), %xmm2      # 0x3020
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0xb4d(%rip), %xmm3      # 0x3030
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movq %rax, %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm2
subsd %xmm1, %xmm2
xorpd %xmm0, %xmm0
movsd %xmm2, 0x10(%rsp)
ucomisd %xmm2, %xmm0
jbe 0x2543
movq %r15, %rdi
movq %r14, %rsi
callq 0x2838
movsd 0xacb(%rip), %xmm0      # 0x3008
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
callq 0x29e6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2c26
movq %rbx, %rdi
callq 0x2c5a
movq %r12, %r13
jmp 0x24a7
leaq 0x18(%rsp), %rdi
callq 0x2694
leaq 0x58(%rsp), %rdi
callq 0x2be8
xorl %eax, %eax
addq $0xb0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x2678
jmp 0x25ba
movq %rax, %rbx
jmp 0x25c4
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x20c0
jmp 0x25ce
movq %rax, %rbx
jmp 0x25ce
jmp 0x25b7
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2694
leaq 0x58(%rsp), %rdi
callq 0x2be8
movq %rbx, %rdi
callq 0x2200