// File: /workspace/github_c_makefile_star2_O1/mesbahamin[P]star-sim/src/platform_sdl.c
// Function: main
int main(int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_VIDEO))
    {
        // TODO(amin): log SDL_Init error
    }

    SDL_Window *window = SDL_CreateWindow(
            TITLE,
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            SCREEN_WIDTH,
            SCREEN_HEIGHT,
            SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);

#ifdef FULLSCREEN
    SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
#endif

    if (window)
    {
        SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

        if (renderer)
        {
            struct SDLWindowDimension dimension = sdl_get_window_dimension(window);
            sdl_resize_texture(&global_back_buffer, renderer, dimension.width, dimension.height);

            bool running = true;

#ifdef USE_TEST_SEED
            srand((uint32_t)0);
#else
            srand((uint32_t)time(NULL));
#endif

            uint64_t lag = 0;
            uint64_t previous_ms = (SDL_GetPerformanceCounter() * SECOND) / SDL_GetPerformanceFrequency();

            struct SimState sim_state;
            sim_init(&sim_state, dimension.width, dimension.height);

            while (running)
            {
                uint64_t current_ms = (SDL_GetPerformanceCounter() * SECOND) / SDL_GetPerformanceFrequency();
                uint64_t elapsed_ms = current_ms - previous_ms;
                previous_ms = current_ms;
                lag += elapsed_ms;
                //printf("%" PRIu64 ", %" PRIu64 ", %f\n", elapsed_ms, lag, MS_PER_UPDATE);

                SDL_Event event;

                while (SDL_PollEvent(&event))
                {
                    running = !handle_event(&event);
                }

                SDL_PumpEvents();

                dimension = sdl_get_window_dimension(window);

                const uint8_t *keystate = SDL_GetKeyboardState(0);

                // TODO: move this to a function
                if (keystate[SDL_SCANCODE_A] || keystate[SDL_SCANCODE_H])
                {
                    sim_state.view.dx += 5 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_D] || keystate[SDL_SCANCODE_L])
                {
                    sim_state.view.dx -= 5 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_W] || keystate[SDL_SCANCODE_K])
                {
                    sim_state.view.dy += 5 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_S] || keystate[SDL_SCANCODE_J])
                {
                    sim_state.view.dy -= 5 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_LEFT])
                {
                    sim_state.view.dx += 1 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_RIGHT])
                {
                    sim_state.view.dx -= 1 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_UP])
                {
                    sim_state.view.dy += 1 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_DOWN])
                {
                    sim_state.view.dy -= 1 / sim_state.view.zoom;
                }
                if (keystate[SDL_SCANCODE_EQUALS])
                {
                    sim_state.view.zoom *= 1.01;
                }
                if (keystate[SDL_SCANCODE_MINUS])
                {
                    sim_state.view.zoom *= 0.99;
                }
                if (keystate[SDL_SCANCODE_0])
                {
                    sim_state.view.zoom = 1;
                }
                if (keystate[SDL_SCANCODE_HOME])
                {
                    sim_state.view.dx = 0;
                    sim_state.view.dy = 0;
                    sim_state.view.zoom = 1;
                }

                struct OffscreenBuffer buffer;
                // WARNING: these pointers are aliased until the end of the
                // loop
                buffer.memory = global_back_buffer.memory;
                buffer.width = global_back_buffer.width;
                buffer.height = global_back_buffer.height;
                buffer.pitch = global_back_buffer.pitch;

                if (PAUSED)
                {
                    lag = 0;
                }
                else
                {
                    while (lag >= MS_PER_UPDATE)
                    {
                        sim_update(&sim_state, buffer.width, buffer.height);
                        //printf("\t%" PRIu64 ", %f\n", lag, MS_PER_UPDATE);
                        lag -= MS_PER_UPDATE;
                    }
                }

                if (!RENDER_TRAILS)
                {
                    clear_screen(&global_back_buffer, COLOR_BACKGROUND);
                }

                sim_render(&buffer, lag/SECOND, &sim_state);
                sdl_update_window(renderer, &global_back_buffer);
                if (elapsed_ms <= MS_PER_FRAME)
                {
                    SDL_Delay(MS_PER_FRAME - elapsed_ms);
                }
            }

            sim_cleanup(&sim_state);
        }
        else
        {
            // TODO(amin): log SDL_Renderer error
        }
    }
    else
    {
        // TODO(amin): log SDL_Window error
    }

    SDL_Quit();
    return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xba8, %rsp            # imm = 0xBA8
movl $0x20, %edi
callq 0x1160
leaq 0x2ad8(%rip), %rdi      # 0x405f
movl $0x1fff0000, %esi       # imm = 0x1FFF0000
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
movl $0x280, %ecx            # imm = 0x280
movl $0x1e0, %r8d            # imm = 0x1E0
movl $0x2020, %r9d           # imm = 0x2020
callq 0x1050
movq %rax, %rbx
movq %rax, %rdi
movl $0x1001, %esi           # imm = 0x1001
callq 0x1190
testq %rbx, %rbx
je 0x1af1
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1030
testq %rax, %rax
je 0x1af1
movq %rax, %r14
leaq 0x94(%rsp), %rdx
leaq 0x90(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x10a0
movq (%r15), %r12
movq %r12, %r13
shrq $0x20, %r13
leaq 0x4b21(%rip), %rdi      # 0x6128
movq %r14, 0x30(%rsp)
movq %r14, %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x133c
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1170
callq 0x11c0
testq %rax, %rax
js 0x1635
cvtsi2ss %rax, %xmm0
jmp 0x164a
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x29b2(%rip), %xmm0     # 0x4004
movaps %xmm0, (%rsp)
callq 0x1150
testq %rax, %rax
js 0x166a
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x1682
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movaps (%rsp), %xmm1
divss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x296a(%rip), %xmm1     # 0x4008
cvttss2si %xmm1, %r15
andq %rcx, %r15
orq %rax, %r15
leaq 0x90(%rsp), %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x1b0c
movb $0x1, %r12b
leaq 0x58(%rsp), %r13
movq %rbx, 0x38(%rsp)
callq 0x11c0
testq %rax, %rax
js 0x16dd
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x16f5
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x2907(%rip), %xmm0     # 0x4004
movaps %xmm0, (%rsp)
callq 0x1150
testq %rax, %rax
js 0x1715
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x172d
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movaps (%rsp), %xmm1
divss %xmm0, %xmm1
movaps %xmm1, (%rsp)
subss 0x28c7(%rip), %xmm1     # 0x4008
movaps %xmm1, 0x40(%rsp)
movq %r13, %rdi
callq 0x10c0
testl %eax, %eax
je 0x176d
movq %r13, %rdi
callq 0x13fd
movl %eax, %r12d
movq %r13, %rdi
callq 0x10c0
testl %eax, %eax
jne 0x1752
xorb $0x1, %r12b
callq 0x1140
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
leaq 0x1c(%rsp), %rdx
callq 0x10a0
xorl %edi, %edi
callq 0x1210
cmpb $0x0, 0x4(%rax)
jne 0x1797
cmpb $0x0, 0xb(%rax)
je 0x17ba
movss 0x286d(%rip), %xmm0     # 0x400c
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x7(%rax)
jne 0x17c6
cmpb $0x0, 0xf(%rax)
je 0x17e9
movss 0x2842(%rip), %xmm0     # 0x4010
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x1a(%rax)
jne 0x17f5
cmpb $0x0, 0xe(%rax)
je 0x1818
movss 0x280f(%rip), %xmm0     # 0x400c
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x16(%rax)
jne 0x1824
cmpb $0x0, 0xd(%rax)
je 0x1847
movss 0x27e4(%rip), %xmm0     # 0x4010
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x50(%rax)
je 0x1870
movss 0x27bf(%rip), %xmm0     # 0x4014
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x4f(%rax)
je 0x1899
movss 0x279a(%rip), %xmm0     # 0x4018
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x52(%rax)
je 0x18c2
movss 0x276d(%rip), %xmm0     # 0x4014
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x51(%rax)
je 0x18eb
movss 0x2748(%rip), %xmm0     # 0x4018
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x2e(%rax)
je 0x1912
xorps %xmm0, %xmm0
cvtss2sd 0xac(%rsp), %xmm0
mulsd 0x2723(%rip), %xmm0     # 0x4028
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xac(%rsp)
cmpb $0x0, 0x2d(%rax)
je 0x1939
xorps %xmm0, %xmm0
cvtss2sd 0xac(%rsp), %xmm0
mulsd 0x2704(%rip), %xmm0     # 0x4030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xac(%rsp)
cmpb $0x0, 0x27(%rax)
je 0x194a
movl $0x3f800000, 0xac(%rsp) # imm = 0x3F800000
cmpb $0x0, 0x4a(%rax)
movaps (%rsp), %xmm0
je 0x196b
movq $0x0, 0xa4(%rsp)
movl $0x3f800000, 0xac(%rsp) # imm = 0x3F800000
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
cvttss2si 0x40(%rsp), %rbx
andq %rcx, %rbx
orq %rax, %rbx
movq %rbx, %rbp
subq %r15, %rbp
movups 0x479f(%rip), %xmm0     # 0x6130
movups %xmm0, 0x18(%rsp)
movl 0x47a4(%rip), %eax      # 0x6140
movl %eax, 0x28(%rsp)
leaq 0x47a1(%rip), %rax      # 0x6148
cmpb $0x0, (%rax)
je 0x19b9
xorl %r14d, %r14d
leaq 0x90(%rsp), %r15
jmp 0x1a37
addq %rbp, %r14
cmpq $0x9, %r14
leaq 0x90(%rsp), %r15
jb 0x1a37
testq %r14, %r14
js 0x19d9
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
jmp 0x19f2
movq %r14, %rax
shrq %rax
andl $0x1, %r14d
orq %rax, %r14
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
addss %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edx
movq %r15, %rdi
callq 0x1d2e
movaps (%rsp), %xmm0
addss 0x260a(%rip), %xmm0     # 0x401c
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x25e6(%rip), %xmm0     # 0x4008
cvttss2si %xmm0, %r14
sarq $0x3f, %rcx
andq %rcx, %r14
orq %rax, %r14
cmpq $0x8, %r14
ja 0x19ca
leaq 0x470e(%rip), %rax      # 0x614c
cmpb $0x0, (%rax)
jne 0x1a61
movq 0x46e6(%rip), %rdi      # 0x6130
movl 0x46ec(%rip), %edx      # 0x613c
imull 0x46e1(%rip), %edx      # 0x6138
shll $0x2, %edx
xorl %esi, %esi
callq 0x10b0
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
divss 0x2593(%rip), %xmm0     # 0x4004
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x1f8b
movq 0x30(%rsp), %rdi
leaq 0x469e(%rip), %rsi      # 0x6128
callq 0x13ae
cmpq $0x10, %rbp
ja 0x1ad2
testq %rbp, %rbp
js 0x1aa4
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
jmp 0x1abc
movq %rbp, %rax
shrq %rax
andl $0x1, %ebp
orq %rax, %rbp
xorps %xmm0, %xmm0
cvtsi2ss %rbp, %xmm0
addss %xmm0, %xmm0
movss 0x255c(%rip), %xmm1     # 0x4020
subss %xmm0, %xmm1
cvttss2si %xmm1, %rdi
callq 0x10e0
movq %rbx, %r15
testb $0x1, %r12b
movq 0x38(%rsp), %rbx
jne 0x16c9
leaq 0x90(%rsp), %rdi
callq 0x27e4
callq 0x1060
xorl %eax, %eax
addq $0xba8, %rsp            # imm = 0xBA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop