~samhsmith/edt

ddce81ec491c3bb0eb546ad56076816d1ea2a081 — Sam H Smith 3 years ago f9704d5
line wrap
1 files changed, 63 insertions(+), 2 deletions(-)

M src/main.c
M src/main.c => src/main.c +63 -2
@@ 22,6 22,8 @@ u64 line_count;
u64* line_index_array;
u64* line_len_array;

u8 is_inserting = 0;

u64 more_chars(u64 count)
{
    character_buffer = realloc(character_buffer, character_buffer_len + count);


@@ 114,6 116,65 @@ void insert_text(u8* o_text)
    }
}

void re_line_heap(FC_Font* font, u64 screen_width)
{
    u8* new_char_buf = malloc(character_buffer_len);
    u64 cursor_position = 0;

    u64 new_index = 0;
    for(u64 i = 0; i < line_count; i++)
    {
        for(u64 j = 0; j < line_len_array[i]; j++)
        {
            if(i == current_line && j == current_column)
            { cursor_position = new_index; }
            new_char_buf[new_index] = character_buffer[line_index_array[i] + j];
            new_index++;
        }
        if(i == current_line && current_column >= line_len_array[i] && i + 1 < line_count)
        { cursor_position = new_index; }
    }
    free(character_buffer);
    u64 new_char_buf_len = new_index;
    new_char_buf = realloc(new_char_buf, new_char_buf_len);

    character_buffer = new_char_buf;
    character_buffer_len = new_char_buf_len;

    line_count = 1;
    line_index_array = realloc(line_index_array, sizeof(u64) * line_count);
    line_len_array = realloc(line_len_array, sizeof(u64) * line_count);
    line_index_array[0] = 0;
    line_len_array[0] = 0;

    u64 len = 0;
    u64 index = 0;
    for(u64 i = 0; i < new_char_buf_len; i++)
    {
        if(i == cursor_position && is_inserting)
        {
            if(index != 0) { current_line = line_count; }
            else { current_line = line_count-1; }
            current_column = len;
        }
        len++;
        if(new_char_buf[i] == '\n' ||
           FC_GetWidth(font, "     |%.*s_", len, new_char_buf + index) > screen_width)
        {
            if(index != 0)
            {
                line_count++;
                line_index_array = realloc(line_index_array, sizeof(u64) * line_count);
                line_len_array = realloc(line_len_array, sizeof(u64) * line_count);
            }
            line_index_array[line_count-1] = index;
            line_len_array[line_count-1] = len;
            index += len;
            len = 0;
        }
    }
}

void main(s32 argc, u8** argv)
{
    assert(SDL_Init(SDL_INIT_EVERYTHING) == 0);


@@ 186,8 247,6 @@ void main(s32 argc, u8** argv)

    u8 shift = 0;

    u8 is_inserting = 0;

    f64 camera_y = 0;
    u8 running = 1;
    while(running)


@@ 278,6 337,8 @@ void main(s32 argc, u8** argv)
            SDL_Rect viewport;
            SDL_RenderGetViewport(renderer, &viewport);

re_line_heap(font, viewport.w);

            f64 camera_target = (s64)(current_line*font_size);

            if(camera_target - camera_y > 0.5)