// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movq %rsi, %rbx
movslq %edi, %r12
pushq $0x1
popq %r13
leaq 0x28661(%rip), %r14     # 0x2b140
leaq 0x2862b(%rip), %r15     # 0x2b111
cmpq %r12, %r13
jge 0x2b29
movq (%rbx,%r13,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x2b03
movq %r14, %rdi
movq %rdx, %rsi
xorl %eax, %eax
callq 0x2080
jmp 0x2b24
movq 0x482b6(%rip), %rsi     # 0x4adc0
testq %rsi, %rsi
je 0x2b1d
movq %r15, %rdi
xorl %eax, %eax
callq 0x2080
movq (%rbx,%r13,8), %rdx
movq %rdx, 0x4829c(%rip)     # 0x4adc0
incq %r13
jmp 0x2ae6
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0x4824e(%rip), %rdi     # 0x4ad90
movq %rbx, %rsi
callq 0x44a4
movq %rbx, %rdi
callq 0x3f68
movss 0x284ae(%rip), %xmm0    # 0x2b008
movaps %xmm0, %xmm1
callq 0x2530a
movlps %xmm0, 0x4823f(%rip)    # 0x4ada8
andl $0x0, 0x48218(%rip)     # 0x4ad88
movq 0x48249(%rip), %rdi     # 0x4adc0
testq %rdi, %rdi
je 0x2e07
leaq 0x285d9(%rip), %rsi     # 0x2b160
callq 0x2520
movq %rax, %r14
xorl %ebx, %ebx
leaq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x68(%rbp), %r15
movq %rax, -0x40(%rbp)
testq %r14, %r14
je 0x2e15
andq $0x0, -0x38(%rbp)
andq $0x0, -0x30(%rbp)
movq %r14, %rdx
callq 0x5b2c
movq %r14, %rdi
callq 0x2040
testl %eax, %eax
jne 0x30e5
movq -0x38(%rbp), %r12
xorl %ecx, %ecx
movq -0x30(%rbp), %rax
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x2bf1
cmpb $0x3a, (%r12,%rdx)
je 0x2be8
incl %ecx
jmp 0x2bd6
movb $0x0, (%r12,%rdx)
movq -0x30(%rbp), %rax
leaq (%r12,%rdx), %rbx
addq %r12, %rdx
incq %rdx
movq %rdx, -0x60(%rbp)
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x2c1a
cmpb $0x3a, (%r12,%rdx)
je 0x2c11
incl %ecx
jmp 0x2bff
movb $0x0, (%r12,%rdx)
movq -0x30(%rbp), %rax
leaq (%r12,%rdx), %r14
incq %r14
movl %ecx, %r13d
cmpq %r13, %rax
jbe 0x2c3d
cmpb $0x3a, (%r12,%r13)
je 0x2c34
incl %ecx
jmp 0x2c21
movb $0x0, (%r12,%r13)
movq -0x30(%rbp), %rax
addq %r12, %r13
movl %ecx, %edx
cmpq %rdx, %rax
jbe 0x2c56
leal 0x1(%rdx), %ecx
cmpb $0x3a, (%r12,%rdx)
jne 0x2c40
movb $0x0, (%r12,%rdx)
movq %r14, %rdi
leaq -0x88(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x2430
cmpq -0x88(%rbp), %r14
je 0x30fb
movq %rbx, -0x58(%rbp)
movl %eax, %esi
leaq -0x80(%rbp), %rdi
movq %rsi, -0x50(%rbp)
leaq -0xc0(%rbp), %rdx
callq 0x3eda
xorl %ebx, %ebx
cmpq -0x50(%rbp), %rbx
je 0x2d03
incq %r13
movsbl (%r13), %edi
callq 0x2380
incq %r13
testl %eax, %eax
jne 0x2c9a
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x2ce1
movq %r13, %rdi
movq %r15, %rsi
callq 0x2540
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xc0(%rbp,%r14,4)
movq -0x68(%rbp), %rax
incq %r14
cmpq %rax, %r13
movq %rax, %r13
jne 0x2cae
jmp 0x2dd1
movss -0xb8(%rbp), %xmm0
movss -0xb4(%rbp), %xmm1
callq 0x2530a
movq -0x80(%rbp), %rax
movlps %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x2c91
movq -0x60(%rbp), %rdi
cmpb $0x23, (%rdi)
jne 0x2d1e
movq -0x58(%rbp), %rdi
addq $0x2, %rdi
callq 0x21e0
movl %eax, %r14d
jmp 0x2d26
callq 0x25184
movq %rax, %r14
xorl %ebx, %ebx
callq 0x251a2
movl %ebx, %ecx
movl %r14d, %r13d
cmpq %rcx, %rax
jbe 0x2d4d
callq 0x251a2
movl %ebx, %ecx
cmpq %r13, %rax
je 0x2d46
movl %r14d, %ecx
incl %ebx
movl %ecx, %r14d
jmp 0x2d28
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
leaq -0x80(%rbp), %rsi
callq 0x3f94
movq %r13, -0xa8(%rbp)
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %rbx
movups %xmm0, (%rbx)
movq %r12, %rdi
callq 0x2250
cmpq $0x10, %rax
pushq $0x10
popq %rcx
cmovaeq %rcx, %rax
movq %r12, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2090
leaq 0x47ff4(%rip), %rdi     # 0x4ad90
movq %r14, %rsi
callq 0x3f2c
movq -0x38(%rbp), %rdi
callq 0x2140
movq %r14, %rdi
callq 0x416e
leaq -0x80(%rbp), %rdi
callq 0x416e
movb $0x1, %bl
movq -0x40(%rbp), %r14
leaq -0x38(%rbp), %rdi
leaq -0x30(%rbp), %rsi
jmp 0x2ba1
movq 0x471f8(%rip), %rax     # 0x49fd0
movq (%rax), %rdi
movq 0x47fde(%rip), %rdx     # 0x4adc0
leaq 0x2837a(%rip), %rsi     # 0x2b163
xorl %eax, %eax
callq 0x2170
movq -0x40(%rbp), %rdi
callq 0x2270
leaq -0x80(%rbp), %rdi
callq 0x416e
jmp 0x30d3
leaq 0x28380(%rip), %rax     # 0x2b18e
movq %rax, 0x47fab(%rip)     # 0x4adc0
callq 0x2a22
pushq $0x61
popq %rdi
callq 0x2584c
pushq $0x1
popq %rbx
movl %eax, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x25709
pushq $0x73
popq %rdi
callq 0x2584c
pushq $0x2
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x64
popq %rdi
callq 0x2584c
pushq $0x3
popq %r13
movl %eax, %edi
movl %r13d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0x72
popq %r15
movl %r15d, %edi
callq 0x2584c
pushq $0x4
popq %r12
movl %eax, %edi
movl %r12d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0x75
popq %rdi
callq 0x2584c
pushq $0x5
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x69
popq %rdi
callq 0x2584c
pushq $0x6
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x6e
popq %rdi
callq 0x2584c
pushq $0x7
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x70
popq %rdi
callq 0x2584c
pushq $0x8
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x6a
popq %rdi
callq 0x2584c
pushq $0x9
popq %r14
movl %eax, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0x6b
popq %rdi
callq 0x2584c
pushq $0xa
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x77
popq %rdi
callq 0x2584c
pushq $0xb
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
pushq $0x63
popq %rdi
callq 0x2584c
pushq $0xc
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
movl %r15d, %edi
callq 0x2584c
movl %eax, %edi
movl %r12d, %esi
movl %ebx, %edx
callq 0x25709
movl $0x400000e1, %edi       # imm = 0x400000E1
callq 0x2584c
pushq $0x10
popq %r15
movl %eax, %edi
movl %r15d, %esi
movl %ebx, %edx
callq 0x25709
movl $0x400000e0, %edi       # imm = 0x400000E0
callq 0x2584c
pushq $0x11
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
xorl %edi, %edi
xorl %esi, %esi
callq 0x25858
pushq $0xd
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
movl %ebx, %edi
xorl %esi, %esi
callq 0x25858
pushq $0xe
popq %r12
movl %eax, %edi
movl %r12d, %esi
movl %ebx, %edx
callq 0x25709
movl %r13d, %edi
xorl %esi, %esi
callq 0x25858
pushq $0xf
popq %rsi
movl %eax, %edi
movl %ebx, %edx
callq 0x25709
movl %ebx, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x25709
xorl %edi, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0x2
popq %rsi
movl %ebx, %edx
callq 0x25709
movl %r13d, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
movl %r13d, %esi
movl %ebx, %edx
callq 0x25709
movl %r14d, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
movl %r15d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0xc
popq %rdi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0xb
popq %rdi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0xa
popq %rsi
movl %ebx, %edx
callq 0x25709
pushq $0x5
popq %r14
movl %r14d, %edi
xorl %esi, %esi
callq 0x25858
movl %eax, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x25709
pushq $0x4
popq %rdi
xorl %esi, %esi
callq 0x25858
movl %eax, %edi
pushq $0x6
popq %r14
movl %r14d, %esi
movl %ebx, %edx
callq 0x25709
movl %r12d, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0x7
popq %rsi
movl %ebx, %edx
callq 0x25709
pushq $0xd
popq %rdi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0x8
popq %rsi
movl %ebx, %edx
callq 0x25709
movl %r14d, %edi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0xb
popq %rsi
movl %ebx, %edx
callq 0x25709
pushq $0x4
popq %rdi
xorl %esi, %esi
callq 0x25862
movl %eax, %edi
pushq $0xc
popq %rsi
movl %ebx, %edx
callq 0x25709
movss 0x27f4f(%rip), %xmm0    # 0x2b008
movaps %xmm0, %xmm1
callq 0x2530a
movaps %xmm0, -0x50(%rbp)
xorl %edi, %edi
callq 0x258d0
movaps -0x50(%rbp), %xmm0
movlps %xmm0, (%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x2270
testb $0x1, %bl
je 0x2e15
jmp 0x2e1a
movq 0x46ece(%rip), %rax     # 0x49fd0
movq (%rax), %rdi
movq 0x47cb4(%rip), %rdx     # 0x4adc0
leaq 0x28050(%rip), %rsi     # 0x2b163
xorl %eax, %eax
callq 0x2170
movq -0x40(%rbp), %rdi
callq 0x2270
jmp 0x30d3
jmp 0x313c
jmp 0x313c
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x416e
jmp 0x313f
jmp 0x313c
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x416e
movq %rbx, %rdi
callq 0x2310