diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 77 |
1 files changed, 41 insertions, 36 deletions
@@ -74,10 +74,11 @@ int main(void) { bool quit = false; TextBuffer text_buffer; - buffer_create(&text_buffer, font); + TextBuffer *buffer = &text_buffer; + buffer_create(buffer, font); - if (!buffer_load_file(&text_buffer, "buffer.c")) - die("Error loading file: %s", buffer_geterr(&text_buffer)); + if (!buffer_load_file(buffer, "buffer.c")) + die("Error loading file: %s", buffer_geterr(buffer)); Uint32 time_at_last_frame = SDL_GetTicks(); @@ -102,74 +103,78 @@ int main(void) { // scroll with mouse wheel Sint32 dx = event.wheel.x, dy = -event.wheel.y; double scroll_speed = 2.5; - buffer_scroll(&text_buffer, dx * scroll_speed, dy * scroll_speed); + buffer_scroll(buffer, dx * scroll_speed, dy * scroll_speed); } break; case SDL_MOUSEBUTTONDOWN: switch (event.button.button) { case SDL_BUTTON_LEFT: { BufferPos pos; - if (buffer_pixels_to_pos(&text_buffer, (float)event.button.x, (float)event.button.y, &pos)) - buffer_cursor_move_to_pos(&text_buffer, pos); + if (buffer_pixels_to_pos(buffer, V2((float)event.button.x, (float)event.button.y), &pos)) + buffer_cursor_move_to_pos(buffer, pos); } break; } break; case SDL_KEYDOWN: { switch (event.key.keysym.sym) { case SDLK_PAGEUP: - buffer_scroll(&text_buffer, 0, -buffer_display_lines(&text_buffer)); + buffer_scroll(buffer, 0, -buffer_display_lines(buffer)); break; case SDLK_PAGEDOWN: - buffer_scroll(&text_buffer, 0, +buffer_display_lines(&text_buffer)); + buffer_scroll(buffer, 0, +buffer_display_lines(buffer)); break; case SDLK_RIGHT: - if (ctrl) - buffer_cursor_move_right_words(&text_buffer, 1); - else - buffer_cursor_move_right(&text_buffer, 1); + if (shift) { + buffer_select_right(buffer, 1); + } else { + if (ctrl) + buffer_cursor_move_right_words(buffer, 1); + else + buffer_cursor_move_right(buffer, 1); + } break; case SDLK_LEFT: if (ctrl) - buffer_cursor_move_left_words(&text_buffer, 1); + buffer_cursor_move_left_words(buffer, 1); else - buffer_cursor_move_left(&text_buffer, 1); + buffer_cursor_move_left(buffer, 1); break; case SDLK_UP: - buffer_cursor_move_up(&text_buffer, 1); + buffer_cursor_move_up(buffer, 1); break; case SDLK_DOWN: - buffer_cursor_move_down(&text_buffer, 1); + buffer_cursor_move_down(buffer, 1); break; case SDLK_RETURN: - buffer_insert_char_at_cursor(&text_buffer, U'\n'); + buffer_insert_char_at_cursor(buffer, U'\n'); break; case SDLK_TAB: - buffer_insert_char_at_cursor(&text_buffer, U'\t'); + buffer_insert_char_at_cursor(buffer, U'\t'); break; case SDLK_DELETE: if (ctrl) - buffer_delete_words_at_cursor(&text_buffer, 1); + buffer_delete_words_at_cursor(buffer, 1); else - buffer_delete_chars_at_cursor(&text_buffer, 1); + buffer_delete_chars_at_cursor(buffer, 1); break; case SDLK_BACKSPACE: if (ctrl) - buffer_backspace_words_at_cursor(&text_buffer, 1); + buffer_backspace_words_at_cursor(buffer, 1); else - buffer_backspace_at_cursor(&text_buffer, 1); + buffer_backspace_at_cursor(buffer, 1); break; case SDLK_s: if (ctrl) { - if (!buffer_save(&text_buffer)) { - printf("Error saving: %s.", buffer_geterr(&text_buffer)); + if (!buffer_save(buffer)) { + printf("Error saving: %s.", buffer_geterr(buffer)); } } break; case SDLK_z: if (ctrl) { if (shift) { - buffer_redo(&text_buffer, 1); + buffer_redo(buffer, 1); } else { - buffer_undo(&text_buffer, 1); + buffer_undo(buffer, 1); } } break; @@ -177,7 +182,7 @@ int main(void) { } break; case SDL_TEXTINPUT: { char *text = event.text.text; - buffer_insert_utf8_at_cursor(&text_buffer, text); + buffer_insert_utf8_at_cursor(buffer, text); } break; } } @@ -195,13 +200,13 @@ int main(void) { double scroll_amount_x = scroll_speed * frame_dt * 1.5; // characters are taller than they are wide double scroll_amount_y = scroll_speed * frame_dt; if (keyboard_state[SDL_SCANCODE_UP]) - buffer_scroll(&text_buffer, 0, -scroll_amount_y); + buffer_scroll(buffer, 0, -scroll_amount_y); if (keyboard_state[SDL_SCANCODE_DOWN]) - buffer_scroll(&text_buffer, 0, +scroll_amount_y); + buffer_scroll(buffer, 0, +scroll_amount_y); if (keyboard_state[SDL_SCANCODE_LEFT]) - buffer_scroll(&text_buffer, -scroll_amount_x, 0); + buffer_scroll(buffer, -scroll_amount_x, 0); if (keyboard_state[SDL_SCANCODE_RIGHT]) - buffer_scroll(&text_buffer, +scroll_amount_x, 0); + buffer_scroll(buffer, +scroll_amount_x, 0); } @@ -222,7 +227,7 @@ int main(void) { { float x1 = 50, y1 = 50, x2 = window_widthf-50, y2 = window_heightf-50; - buffer_render(&text_buffer, x1, y1, x2, y2); + buffer_render(buffer, x1, y1, x2, y2); if (text_has_err()) { debug_println("Text error: %s\n", text_get_err()); break; @@ -230,9 +235,9 @@ int main(void) { } #if DEBUG - //buffer_print_debug(&text_buffer); - buffer_check_valid(&text_buffer); - buffer_print_undo_history(&text_buffer); + //buffer_print_debug(buffer); + buffer_check_valid(buffer); + buffer_print_undo_history(buffer); #endif SDL_GL_SwapWindow(window); @@ -241,7 +246,7 @@ int main(void) { SDL_GL_DeleteContext(glctx); SDL_DestroyWindow(window); SDL_Quit(); - buffer_free(&text_buffer); + buffer_free(buffer); text_font_free(font); return 0; |