// File: /workspace/github_c_makefile_star2_O2/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 $0xb88, %rsp            # imm = 0xB88
pushq $0x20
popq %rdi
callq 0x1160
leaq 0x1ad8(%rip), %rdi      # 0x305f
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 0x11a0
testq %rbx, %rbx
je 0x1a68
pushq $-0x1
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1030
testq %rax, %rax
je 0x1a68
movq %rax, %r14
movq %rbx, %rdi
callq 0x132c
movq %rax, %r12
movq %rax, %r13
shrq $0x20, %r13
leaq 0x3b3e(%rip), %rdi      # 0x5130
movq %r14, %rsi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x134c
xorl %ebp, %ebp
xorl %edi, %edi
callq 0x1170
callq 0x11d0
testq %rax, %rax
js 0x161a
cvtsi2ss %rax, %xmm0
jmp 0x162f
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x19cd(%rip), %xmm0     # 0x3004
movaps %xmm0, (%rsp)
callq 0x1150
testq %rax, %rax
movq %rbx, 0x18(%rsp)
movq %r14, 0x10(%rsp)
js 0x1656
cvtsi2ss %rax, %xmm0
jmp 0x166b
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movaps (%rsp), %xmm1
divss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x1981(%rip), %xmm1     # 0x3008
cvttss2si %xmm1, %r14
andq %rcx, %r14
orq %rax, %r14
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x1a84
movb $0x1, %r13b
leaq 0x38(%rsp), %r12
testb $0x1, %r13b
je 0x1a5e
callq 0x11d0
testq %rax, %rax
js 0x16c8
cvtsi2ss %rax, %xmm0
jmp 0x16dd
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
mulss 0x191f(%rip), %xmm0     # 0x3004
movaps %xmm0, (%rsp)
callq 0x1150
movq %r14, %rdx
testq %rax, %rax
js 0x16fd
cvtsi2ss %rax, %xmm0
jmp 0x1712
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movaps (%rsp), %xmm1
divss %xmm0, %xmm1
cvttss2si %xmm1, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x18da(%rip), %xmm1     # 0x3008
cvttss2si %xmm1, %r14
andq %rcx, %r14
orq %rax, %r14
movq %r14, %rbx
subq %rdx, %rbx
movb $0x1, %r13b
movq %r12, %rdi
callq 0x10c0
testl %eax, %eax
je 0x175f
movq %r12, %rdi
callq 0x1406
movl %eax, %r13d
xorb $0x1, %r13b
jmp 0x1742
callq 0x1140
movq 0x18(%rsp), %rdi
callq 0x132c
xorl %edi, %edi
callq 0x1220
cmpb $0x0, 0x4(%rax)
jne 0x1781
cmpb $0x0, 0xb(%rax)
je 0x17a4
movss 0x1883(%rip), %xmm0     # 0x300c
divss 0x8c(%rsp), %xmm0
addss 0x84(%rsp), %xmm0
movss %xmm0, 0x84(%rsp)
cmpb $0x0, 0x7(%rax)
jne 0x17b0
cmpb $0x0, 0xf(%rax)
je 0x17d3
movss 0x1858(%rip), %xmm0     # 0x3010
divss 0x8c(%rsp), %xmm0
addss 0x84(%rsp), %xmm0
movss %xmm0, 0x84(%rsp)
cmpb $0x0, 0x1a(%rax)
jne 0x17df
cmpb $0x0, 0xe(%rax)
je 0x1802
movss 0x1825(%rip), %xmm0     # 0x300c
divss 0x8c(%rsp), %xmm0
addss 0x88(%rsp), %xmm0
movss %xmm0, 0x88(%rsp)
cmpb $0x0, 0x16(%rax)
jne 0x180e
cmpb $0x0, 0xd(%rax)
je 0x1831
movss 0x17fa(%rip), %xmm0     # 0x3010
divss 0x8c(%rsp), %xmm0
addss 0x88(%rsp), %xmm0
movss %xmm0, 0x88(%rsp)
cmpb $0x0, 0x50(%rax)
je 0x185a
movss 0x17d5(%rip), %xmm0     # 0x3014
divss 0x8c(%rsp), %xmm0
addss 0x84(%rsp), %xmm0
movss %xmm0, 0x84(%rsp)
cmpb $0x0, 0x4f(%rax)
je 0x1883
movss 0x17b0(%rip), %xmm0     # 0x3018
divss 0x8c(%rsp), %xmm0
addss 0x84(%rsp), %xmm0
movss %xmm0, 0x84(%rsp)
cmpb $0x0, 0x52(%rax)
je 0x18ac
movss 0x1783(%rip), %xmm0     # 0x3014
divss 0x8c(%rsp), %xmm0
addss 0x88(%rsp), %xmm0
movss %xmm0, 0x88(%rsp)
cmpb $0x0, 0x51(%rax)
je 0x18d5
movss 0x175e(%rip), %xmm0     # 0x3018
divss 0x8c(%rsp), %xmm0
addss 0x88(%rsp), %xmm0
movss %xmm0, 0x88(%rsp)
cmpb $0x0, 0x2e(%rax)
je 0x18f9
cvtss2sd 0x8c(%rsp), %xmm0
mulsd 0x173c(%rip), %xmm0     # 0x3028
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8c(%rsp)
cmpb $0x0, 0x2d(%rax)
je 0x191d
cvtss2sd 0x8c(%rsp), %xmm0
mulsd 0x1720(%rip), %xmm0     # 0x3030
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x8c(%rsp)
cmpb $0x0, 0x27(%rax)
je 0x192e
movl $0x3f800000, 0x8c(%rsp) # imm = 0x3F800000
cmpb $0x0, 0x4a(%rax)
je 0x1948
andq $0x0, 0x84(%rsp)
movl $0x3f800000, 0x8c(%rsp) # imm = 0x3F800000
movups 0x37e9(%rip), %xmm0     # 0x5138
movups %xmm0, 0x20(%rsp)
movl 0x37ee(%rip), %eax      # 0x5148
movl %eax, 0x30(%rsp)
leaq 0x37eb(%rip), %rax      # 0x5150
cmpb $0x0, (%rax)
je 0x19ef
xorl %ebp, %ebp
leaq 0x37dd(%rip), %rax      # 0x5154
cmpb $0x0, (%rax)
jne 0x199a
movq 0x37b5(%rip), %rdi      # 0x5138
movl 0x37bb(%rip), %edx      # 0x5144
imull 0x37b0(%rip), %edx      # 0x5140
shll $0x2, %edx
xorl %esi, %esi
callq 0x10b0
cvtsi2ss %rbp, %xmm0
divss 0x165d(%rip), %xmm0     # 0x3004
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x1ece
movq 0x10(%rsp), %rdi
leaq 0x3770(%rip), %rsi      # 0x5130
callq 0x13b7
cmpq $0x10, %rbx
ja 0x16ad
cvtsi2ss %rbx, %xmm0
movss 0x1644(%rip), %xmm1     # 0x3020
subss %xmm0, %xmm1
cvttss2si %xmm1, %rdi
callq 0x10e0
jmp 0x16ad
addq %rbx, %rbp
cmpq $0x9, %rbp
jb 0x1970
testq %rbp, %rbp
js 0x1a08
cvtsi2ss %rbp, %xmm0
jmp 0x1a1d
movq %rbp, %rax
shrq %rax
andl $0x1, %ebp
orq %rax, %rbp
cvtsi2ss %rbp, %xmm0
addss %xmm0, %xmm0
movaps %xmm0, (%rsp)
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
movq %r15, %rdi
callq 0x1caa
movaps (%rsp), %xmm0
addss 0x15df(%rip), %xmm0     # 0x301c
cvttss2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subss 0x15b7(%rip), %xmm0     # 0x3008
cvttss2si %xmm0, %rbp
andq %rcx, %rbp
orq %rax, %rbp
jmp 0x19f2
leaq 0x70(%rsp), %rdi
callq 0x261e
callq 0x1060
xorl %eax, %eax
addq $0xb88, %rsp            # imm = 0xB88
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)