// File: /workspace/github_c_makefile_star2_O3/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 0x1ad8
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edx, %edx
callq 0x1030
testq %rax, %rax
je 0x1ad8
movq %rax, %rbp
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 %rbp, %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x133c
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x1170
callq 0x11c0
testq %rax, %rax
js 0x1630
cvtsi2ss %rax, %xmm0
jmp 0x1645
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x29b7(%rip), %xmm0     # 0x4004
movaps %xmm0, (%rsp)
callq 0x1150
testq %rax, %rax
movq %rbp, 0x18(%rsp)
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, %rbp
andq %rcx, %rbp
orq %rax, %rbp
leaq 0x90(%rsp), %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x1af4
leaq 0x58(%rsp), %r13
movq %rbx, 0x38(%rsp)
callq 0x11c0
testq %rax, %rax
js 0x16da
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x16f2
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x290a(%rip), %xmm0     # 0x4004
movaps %xmm0, (%rsp)
callq 0x1150
testq %rax, %rax
js 0x1712
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x172a
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 0x28ca(%rip), %xmm1     # 0x4008
movaps %xmm1, 0x40(%rsp)
movq %r13, %rdi
callq 0x10c0
testl %eax, %eax
je 0x176c
movq %r13, %rdi
callq 0x13fd
movl %eax, %r12d
movq %r13, %rdi
callq 0x10c0
testl %eax, %eax
jne 0x174f
xorb $0x1, %r12b
jmp 0x176f
movb $0x1, %r12b
callq 0x1140
movq %rbx, %rdi
leaq 0x20(%rsp), %rsi
leaq 0x24(%rsp), %rdx
callq 0x10a0
xorl %edi, %edi
callq 0x1210
cmpb $0x0, 0x4(%rax)
jne 0x1799
cmpb $0x0, 0xb(%rax)
je 0x17bc
movss 0x286b(%rip), %xmm0     # 0x400c
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x7(%rax)
jne 0x17c8
cmpb $0x0, 0xf(%rax)
je 0x17eb
movss 0x2840(%rip), %xmm0     # 0x4010
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x1a(%rax)
jne 0x17f7
cmpb $0x0, 0xe(%rax)
je 0x181a
movss 0x280d(%rip), %xmm0     # 0x400c
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x16(%rax)
jne 0x1826
cmpb $0x0, 0xd(%rax)
je 0x1849
movss 0x27e2(%rip), %xmm0     # 0x4010
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x50(%rax)
je 0x1872
movss 0x27bd(%rip), %xmm0     # 0x4014
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x4f(%rax)
je 0x189b
movss 0x2798(%rip), %xmm0     # 0x4018
divss 0xac(%rsp), %xmm0
addss 0xa4(%rsp), %xmm0
movss %xmm0, 0xa4(%rsp)
cmpb $0x0, 0x52(%rax)
je 0x18c4
movss 0x276b(%rip), %xmm0     # 0x4014
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x51(%rax)
je 0x18ed
movss 0x2746(%rip), %xmm0     # 0x4018
divss 0xac(%rsp), %xmm0
addss 0xa8(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
cmpb $0x0, 0x2e(%rax)
je 0x1914
xorps %xmm0, %xmm0
cvtss2sd 0xac(%rsp), %xmm0
mulsd 0x2721(%rip), %xmm0     # 0x4028
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xac(%rsp)
cmpb $0x0, 0x2d(%rax)
je 0x193b
xorps %xmm0, %xmm0
cvtss2sd 0xac(%rsp), %xmm0
mulsd 0x2702(%rip), %xmm0     # 0x4030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xac(%rsp)
cmpb $0x0, 0x27(%rax)
je 0x194c
movl $0x3f800000, 0xac(%rsp) # imm = 0x3F800000
cmpb $0x0, 0x4a(%rax)
movaps (%rsp), %xmm0
je 0x196d
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, %r15
subq %rbp, %r15
movups 0x479d(%rip), %xmm0     # 0x6130
movups %xmm0, 0x20(%rsp)
movl 0x47a2(%rip), %eax      # 0x6140
movl %eax, 0x30(%rsp)
leaq 0x479f(%rip), %rax      # 0x6148
cmpb $0x0, (%rax)
je 0x19bb
xorl %r14d, %r14d
movq 0x18(%rsp), %rbp
jmp 0x1a3b
addq %r15, %r14
cmpq $0x9, %r14
movq 0x18(%rsp), %rbp
jb 0x1a3b
testq %r14, %r14
js 0x19d8
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
jmp 0x19f1
movq %r14, %rax
shrq %rax
andl $0x1, %r14d
orq %rax, %r14
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
addss %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
leaq 0x90(%rsp), %rdi
callq 0x1d17
movaps (%rsp), %xmm0
addss 0x2606(%rip), %xmm0     # 0x401c
cvttss2si %xmm0, %rax
movq %rax, %rcx
subss 0x25e2(%rip), %xmm0     # 0x4008
cvttss2si %xmm0, %r14
sarq $0x3f, %rcx
andq %rcx, %r14
orq %rax, %r14
cmpq $0x8, %r14
ja 0x19c9
leaq 0x470a(%rip), %rax      # 0x614c
cmpb $0x0, (%rax)
jne 0x1a65
movq 0x46e2(%rip), %rdi      # 0x6130
movl 0x46e8(%rip), %edx      # 0x613c
imull 0x46dd(%rip), %edx      # 0x6138
shll $0x2, %edx
xorl %esi, %esi
callq 0x10b0
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
divss 0x258f(%rip), %xmm0     # 0x4004
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1f95
movq %rbp, %rdi
leaq 0x4697(%rip), %rsi      # 0x6128
callq 0x13ae
cmpq $0x10, %r15
ja 0x1aba
xorps %xmm0, %xmm0
cvtsi2ss %r15, %xmm0
movss 0x2574(%rip), %xmm1     # 0x4020
subss %xmm0, %xmm1
cvttss2si %xmm1, %rdi
callq 0x10e0
movq %rbx, %rbp
testb %r12b, %r12b
movq 0x38(%rsp), %rbx
jne 0x16c6
leaq 0x90(%rsp), %rdi
callq 0x2805
callq 0x1060
xorl %eax, %eax
addq $0xba8, %rsp            # imm = 0xBA8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)