// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/rain/editor_main.cpp
// Function: setup(int, char**)
void setup(int argc, char **argv) {
    // Commandline parsing, sets globalstate
    for (s32 i = 1; i < argc; i++) {
        char *val = argv[i];
        if (*val != '-') {
            if (path) {
                printf("Multiple files given replacing '%s' with '%s'.", path, argv[i]);
            }
            path = argv[i];
        } else {
            // Parse flags
            val++;
            if (*val == '-') val++;
            switch (*val) {
                default:
                    printf("Unkown argument '%s', ignoring.", argv[i]);
            };
        }
    }



    global_editor.sprites = std::vector<EditableSprite>();
    global_editor.cursor = fog_V2(0.5, 0.5);
    global_editor.current_sprite = 0;
    b8 no_path_passed = true;
    if (path != nullptr) {
        // NOTE(ed): Code bellow is copied from the asset loader. Might need to be a
        // unified function.
        FILE *sprite_file = fopen(path, "rb");
        while (sprite_file) {
            char *line = nullptr;
            size_t line_n = 0;
            getline(&line, &line_n, sprite_file);

            if (feof(sprite_file))
                break;

            char *ptr = line;
            u32 i = 0;
            char *name = ptr + i;
            for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
            char *res = ptr + i + 1;
            for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
            char *num = ptr + i + 1;
            for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }
            char *points_str = ptr + i + 1;
            for (; i < line_n; i++) if (ptr[i] == ':') { ptr[i] = '\0'; break; }

            // TODO(ed): Parse points, and stuff
#define CHECK_FOR_ERROR(ptr)                                  \
            do {                                                      \
                if (ptr == endptr) {                                  \
                    fprintf(stderr, "Failed to parse number in sprite file '%s'", \
                            path);                      \
                    fclose(sprite_file);                              \
                    return;                                           \
                }                                                     \
            } while (false);

            char *endptr;
            u32 num_points = strtol(num, &endptr, 10);
            CHECK_FOR_ERROR(num);
            std::vector<Vec2> points = std::vector<Vec2>(num_points);
            for (u32 p = 0; p < num_points; p++) {
                char *endptr;
                Vec4 point;
                while (isspace(*points_str)) points_str++;
                points_str += 1; // (
                for (u32 coord = 0; coord < 4; coord++) {
                    point._[coord] = strtod(points_str, &endptr);
                    CHECK_FOR_ERROR(points_str);
                    points_str = endptr;
                }
                points_str += 1; // )
                points[p] = fog_V2(point.z, point.w);
            }
#undef CHECK_FOR_ERROR

            u32 resource_hash = res[0] == '#' ? atoi(res + 1): fog_asset_asset_hash(res);
            // Reverse the hash
            for (u32 i = 0; i < fog_asset_num(); i++) {
                if (fog_asset_num() == resource_hash) {
                    resource_hash = i;
                }
            }

            EditableSprite sprite = {
                points,
                resource_hash,
            };
            memcpy(name, sprite.name, MIN(MAX_TEXT_LENGTH, strlen(name)));
            global_editor.sprites.push_back(sprite);
            no_path_passed = false;
            free(line);
        }
        if (sprite_file)
            fclose(sprite_file);
    } else {
        path = "default.spr";
    }

    if (no_path_passed) {
        create_new_sprite();

    }

    // Sprite editor
    fog_input_add(K(a), (u32) In::EDIT_PLACE, Player::P1);
    fog_input_add(K(s), (u32) In::EDIT_SELECT, Player::P1);
    fog_input_add(K(d), (u32) In::EDIT_REMOVE, Player::P1);
    fog_input_add(K(r), (u32) In::EDIT_RENAME, Player::P1);
    fog_input_add(K(u), (u32) In::EDIT_NEXT_SPRITE, Player::P1);
    fog_input_add(K(i), (u32) In::EDIT_PREV_SPRITE, Player::P1);
    fog_input_add(K(n), (u32) In::EDIT_NEXT_SPRITE_SHEET, Player::P1);
    fog_input_add(K(p), (u32) In::EDIT_PREV_SPRITE_SHEET, Player::P1);
    fog_input_add(K(j), (u32) In::EDIT_SNAP_SMALLER, Player::P1);
    fog_input_add(K(k), (u32) In::EDIT_SNAP_LARGER, Player::P1);
    fog_input_add(K(w), (u32) In::EDIT_SAVE, Player::P1);
    fog_input_add(K(c), (u32) In::EDIT_ADD_SPRITE, Player::P1);
    fog_input_add(K(r), (u32) In::EDIT_RENAME, Player::P1);
    fog_input_add(K(LSHIFT), (u32) In::TWEAK_STEP, Player::P1);
    fog_input_add(K(LCTRL), (u32) In::TWEAK_SMOOTH, Player::P1);

    fog_input_add(A(LEFTX, Player::P1), (u32) In::EDIT_MOVE_RIGHT_LEFT, Player::P1);
    fog_input_add(A(LEFTY, Player::P1), (u32) In::EDIT_MOVE_UP_DOWN, Player::P1);
    fog_input_add(A(RIGHTY, Player::P1), (u32) In::EDIT_ZOOM_IN_OUT, Player::P1);
    fog_input_add(B(B, Player::P1), (u32) In::EDIT_PLACE, Player::P1);
    fog_input_add(B(A, Player::P1), (u32) In::EDIT_SELECT, Player::P1);
    fog_input_add(B(Y, Player::P1), (u32) In::EDIT_REMOVE, Player::P1);
    fog_input_add(B(LEFTSHOULDER, Player::P1), (u32) In::TWEAK_STEP, Player::P1);
    fog_input_add(B(DPAD_DOWN, Player::P1), (u32) In::EDIT_SNAP_SMALLER, Player::P1);
    fog_input_add(B(DPAD_UP, Player::P1), (u32) In::EDIT_SNAP_LARGER, Player::P1);
    fog_input_add(A(TRIGGERRIGHT, Player::P1), (u32) In::EDIT_NEXT_SPRITE, Player::P1);
    fog_input_add(A(TRIGGERLEFT, Player::P1), (u32) In::EDIT_PREV_SPRITE, Player::P1);
    fog_input_add(B(DPAD_RIGHT, Player::P1), (u32) In::EDIT_NEXT_SPRITE_SHEET, Player::P1);
    fog_input_add(B(DPAD_LEFT, Player::P1), (u32) In::EDIT_PREV_SPRITE_SHEET, Player::P1);
    fog_input_add(B(START, Player::P1), (u32) In::EDIT_SAVE, Player::P1);
    fog_input_add(B(BACK, Player::P1), (u32) In::EDIT_ADD_SPRITE, Player::P1);

    fog_renderer_fetch_camera(0)->position = fog_V2(0.5, 0.5);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
cmpl $0x2, %edi
jl 0x2be6
movq %rsi, %rbx
movl %edi, %r12d
movl $0x1, %r13d
leaq 0x2d5a2(%rip), %r14     # 0x30140
leaq 0x2d56c(%rip), %r15     # 0x30111
movq (%rbx,%r13,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x2bbd
movq %r14, %rdi
movq %rdx, %rsi
xorl %eax, %eax
callq 0x2080
jmp 0x2bde
movq 0x4d1ec(%rip), %rsi     # 0x4fdb0
testq %rsi, %rsi
je 0x2bd7
movq %r15, %rdi
xorl %eax, %eax
callq 0x2080
movq (%rbx,%r13,8), %rdx
movq %rdx, 0x4d1d2(%rip)     # 0x4fdb0
incq %r13
cmpq %r13, %r12
jne 0x2ba5
movups 0x4d193(%rip), %xmm0    # 0x4fd80
movq 0x4d19c(%rip), %rax     # 0x4fd90
xorps %xmm1, %xmm1
movups %xmm1, 0x4d182(%rip)    # 0x4fd80
xorl %ecx, %ecx
movq %rcx, 0x4d189(%rip)     # 0x4fd90
leaq -0xa0(%rbp), %rdi
movups %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
leaq -0x70(%rbp), %rbx
movaps %xmm1, (%rbx)
movq %rcx, 0x10(%rbx)
callq 0x440c
movq %rbx, %rdi
callq 0x440c
movss 0x2d3d3(%rip), %xmm0    # 0x30008
movaps %xmm0, %xmm1
callq 0x28631
movlps %xmm0, 0x4d154(%rip)    # 0x4fd98
movl $0x0, 0x4d12a(%rip)     # 0x4fd78
movq 0x4d15b(%rip), %rdi     # 0x4fdb0
testq %rdi, %rdi
je 0x2fa1
leaq 0x2d4fb(%rip), %rsi     # 0x30160
callq 0x2520
movq %rax, %rbx
movb $0x1, %al
movl %eax, -0x34(%rbp)
leaq -0xc8(%rbp), %r12
movq %rbx, -0x40(%rbp)
testq %rbx, %rbx
je 0x2fbf
xorl %eax, %eax
movq %rax, -0x48(%rbp)
movq %rax, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movq %rbx, %rdx
callq 0x646c
movq %rbx, %rdi
callq 0x2040
testl %eax, %eax
jne 0x2fb1
movq -0x48(%rbp), %rbx
movq -0x30(%rbp), %rax
testq %rax, %rax
je 0x2cd6
xorl %edx, %edx
xorl %ecx, %ecx
cmpb $0x3a, (%rbx,%rdx)
je 0x2cea
incl %ecx
movq %rcx, %rdx
cmpq %rcx, %rax
ja 0x2cc1
movq %rcx, %rdx
jmp 0x2cf2
leaq 0x1(%rbx), %r13
movq %rbx, %r14
movq %rbx, -0x50(%rbp)
movq %r13, -0x58(%rbp)
jmp 0x2d87
movb $0x0, (%rbx,%rdx)
movq -0x30(%rbp), %rax
movl %ecx, %esi
cmpq %rsi, %rax
jbe 0x2d11
cmpb $0x3a, (%rbx,%rsi)
je 0x2d16
incl %ecx
movq %rcx, %rsi
cmpq %rcx, %rax
ja 0x2cf9
movq %rcx, %r14
movq %rcx, %rsi
jmp 0x2d21
movq %rsi, %r14
jmp 0x2d21
movb $0x0, (%rbx,%rsi)
movq -0x30(%rbp), %rax
movl %ecx, %r14d
cmpq %r14, %rax
jbe 0x2d3f
cmpb $0x3a, (%rbx,%r14)
je 0x2d44
incl %ecx
movq %rcx, %r14
cmpq %rcx, %rax
ja 0x2d26
movq %rcx, %rdi
movq %rcx, %r14
jmp 0x2d4f
movq %r14, %rdi
jmp 0x2d4f
movb $0x0, (%rbx,%r14)
movq -0x30(%rbp), %rax
movl %ecx, %edi
leaq (%rbx,%rdx), %r8
addq %rbx, %rdx
incq %rdx
leaq (%rbx,%rsi), %r13
incq %r13
addq %rbx, %r14
cmpq %rdi, %rax
movq %rdx, -0x58(%rbp)
movq %r8, -0x50(%rbp)
jbe 0x2d87
incl %ecx
cmpb $0x3a, (%rbx,%rdi)
je 0x2d83
movl %ecx, %edi
incl %ecx
cmpq %rdi, %rax
ja 0x2d72
jmp 0x2d87
movb $0x0, (%rbx,%rdi)
movq %r13, %rdi
leaq -0xd0(%rbp), %rsi
movl $0xa, %edx
callq 0x2430
cmpq -0xd0(%rbp), %r13
je 0x3340
movq %rax, %r15
movq %rbx, -0xb8(%rbp)
movq %rax, %rsi
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
andq %rbx, %rsi
leaq -0x70(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x430a
movq %r15, %rax
andq %rbx, %rax
je 0x2e63
movl %r15d, %eax
movq %rax, -0xc0(%rbp)
movb $0x1, %r15b
xorl %ebx, %ebx
incq %r14
movsbl (%r14), %edi
callq 0x2380
incq %r14
testl %eax, %eax
jne 0x2deb
movl %r15d, -0xb0(%rbp)
xorl %r13d, %r13d
movq %r14, %r15
movq %r14, %rdi
movq %r12, %rsi
callq 0x2540
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp,%r13,4)
movq -0xc8(%rbp), %r14
cmpq %r14, %r15
je 0x2e85
incq %r13
cmpq $0x4, %r13
jne 0x2e05
movss -0x98(%rbp), %xmm0
movss -0x94(%rbp), %xmm1
callq 0x28631
movq -0x70(%rbp), %rax
movlps %xmm0, (%rax,%rbx,8)
incq %rbx
cmpq -0xc0(%rbp), %rbx
setb %r15b
jne 0x2de8
movq -0x58(%rbp), %rdi
cmpb $0x23, (%rdi)
jne 0x2ebc
movq -0x50(%rbp), %rdi
addq $0x2, %rdi
callq 0x21e0
movl %eax, %r15d
leaq -0xa0(%rbp), %r14
jmp 0x2ecb
movq 0x4c144(%rip), %rax     # 0x4efd0
movq (%rax), %rdi
movq 0x4cf1a(%rip), %rdx     # 0x4fdb0
leaq 0x2d2c6(%rip), %rsi     # 0x30163
xorl %eax, %eax
callq 0x2170
movq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x2270
movl -0xb0(%rbp), %r14d
jmp 0x2f7d
callq 0x2817b
leaq -0xa0(%rbp), %r14
movq %rax, %r15
xorl %ebx, %ebx
callq 0x2820c
movl %ebx, %ecx
cmpq %rcx, %rax
jbe 0x2ef2
callq 0x2820c
movl %r15d, %edx
movl %ebx, %ecx
cmpq %rdx, %rax
je 0x2eeb
movl %r15d, %ecx
incl %ebx
movl %ecx, %r15d
jmp 0x2ecd
movq %r14, %rdi
leaq -0x70(%rbp), %rsi
callq 0x446e
movl %r15d, %eax
movq %rax, -0x88(%rbp)
xorps %xmm0, %xmm0
leaq -0x80(%rbp), %rbx
movups %xmm0, (%rbx)
movq -0xb8(%rbp), %r15
movq %r15, %rdi
callq 0x2250
cmpq $0x10, %rax
movl $0x10, %ecx
cmovaeq %rcx, %rax
movq %r15, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2090
leaq 0x4ce3d(%rip), %rdi     # 0x4fd80
movq %r14, %rsi
callq 0x43ae
movq -0x48(%rbp), %rdi
callq 0x2140
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
movq -0x40(%rbp), %rbx
je 0x2f73
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
xorl %r14d, %r14d
movl $0x0, -0x34(%rbp)
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x2f92
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
testb $0x1, %r14b
je 0x2c7d
jmp 0x332e
leaq 0x2d1e6(%rip), %rax     # 0x3018e
movq %rax, 0x4ce01(%rip)     # 0x4fdb0
jmp 0x2fbf
movq %rbx, %rdi
callq 0x2270
cmpb $0x0, -0x34(%rbp)
je 0x2fc4
callq 0x2ac0
movl $0x61, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x73, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x2, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x64, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x72, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x75, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x5, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x69, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x6, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x6e, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x7, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x70, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x8, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x6a, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x9, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x6b, %edi
callq 0x28f9b
movl %eax, %edi
movl $0xa, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x77, %edi
callq 0x28f9b
movl %eax, %edi
movl $0xb, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x63, %edi
callq 0x28f9b
movl %eax, %edi
movl $0xc, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x72, %edi
callq 0x28f9b
movl %eax, %edi
movl $0x4, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x400000e1, %edi       # imm = 0x400000E1
callq 0x28f9b
movl %eax, %edi
movl $0x10, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x400000e0, %edi       # imm = 0x400000E0
callq 0x28f9b
movl %eax, %edi
movl $0x11, %esi
movl $0x1, %edx
callq 0x28df8
xorl %edi, %edi
xorl %esi, %esi
callq 0x28fa7
movl %eax, %edi
movl $0xd, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x1, %edi
xorl %esi, %esi
callq 0x28fa7
movl %eax, %edi
movl $0xe, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x3, %edi
xorl %esi, %esi
callq 0x28fa7
movl %eax, %edi
movl $0xf, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x1, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x1, %esi
movl $0x1, %edx
callq 0x28df8
xorl %edi, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x2, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x3, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x3, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x9, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x10, %esi
movl $0x1, %edx
callq 0x28df8
movl $0xc, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x9, %esi
movl $0x1, %edx
callq 0x28df8
movl $0xb, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0xa, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x5, %edi
xorl %esi, %esi
callq 0x28fa7
movl %eax, %edi
movl $0x5, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x4, %edi
xorl %esi, %esi
callq 0x28fa7
movl %eax, %edi
movl $0x6, %esi
movl $0x1, %edx
callq 0x28df8
movl $0xe, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x7, %esi
movl $0x1, %edx
callq 0x28df8
movl $0xd, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0x8, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x6, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0xb, %esi
movl $0x1, %edx
callq 0x28df8
movl $0x4, %edi
xorl %esi, %esi
callq 0x28fe7
movl %eax, %edi
movl $0xc, %esi
movl $0x1, %edx
callq 0x28df8
movss 0x2ccfa(%rip), %xmm0    # 0x30008
movaps %xmm0, %xmm1
callq 0x28631
movaps %xmm0, -0xb0(%rbp)
xorl %edi, %edi
callq 0x29145
movaps -0xb0(%rbp), %xmm0
movlps %xmm0, (%rax)
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4bc89(%rip), %rax     # 0x4efd0
movq (%rax), %rdi
movq 0x4ca5f(%rip), %rdx     # 0x4fdb0
leaq 0x2ce0b(%rip), %rsi     # 0x30163
xorl %eax, %eax
callq 0x2170
movq -0x40(%rbp), %rdi
callq 0x2270
jmp 0x332e
jmp 0x3390
jmp 0x3390
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
testq %rdi, %rdi
je 0x3393
movq -0x90(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
jmp 0x3393
jmp 0x3390
movq %rax, %rbx
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x33a8
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x20e0
movq %rbx, %rdi
callq 0x2310