FPS :: 60; MS_PER_FRAME :: 1000 / FPS; buffer: string; lines: [..]String_Builder; line_height: int; my_font: *Simp.Dynamic_Font; window_width := 800; window_height := 600; first_line := 0; cursor_x := 0; cursor_y := 0; White :: Vector4.{1, 1, 1, 1}; main :: () { args := get_command_line_arguments(); assert(args.count == 2, "The file name must be passed as a CLI argument"); window_name := ifx args.count == 1 then "edit" else args[1]; window := create_window(window_width, window_height, window_name); my_init_fonts(); Simp.set_render_target(window); should_quit := false; prev := get_time(); assert(read_file_lines(args[1]), "Must be able to read file!"); while !should_quit { reset_temporary_storage(); now := get_time(); dt := now - prev; prev = now; Input.update_window_events(); handle_window_resizes(window); for event : Input.events_this_frame { if event.type == .QUIT { should_quit = true; break; } if event.type == .KEYBOARD && event.key_pressed { if event.key_code == { case .ESCAPE; should_quit = true; case .ARROW_UP; #through; case .ARROW_DOWN; #through; case .ARROW_LEFT; #through; case .ARROW_RIGHT; handle_arrow(event.key_code); } } } Simp.clear_render_target(.0, .0, .0, 1); render_edit_buffer(); Simp.swap_buffers(window); now = get_time(); if now - prev < MS_PER_FRAME { sleep_milliseconds(xx (now - prev)); } } } render_edit_buffer :: () { visible_lines := get_visible_lines(); for line_buffer: visible_lines { Simp.set_shader_for_text(); line_str := tbuilder_to_string(*line_buffer); prefix: string; suffix: string; has_cursor_on_line := it_index == cursor_y; if has_cursor_on_line { prefix, suffix = string_to_line(line_str, cursor_x); } else { prefix, suffix = string_to_line(line_str); } prefix_width := Simp.prepare_text(my_font, prefix); top_of_line := window_height - (it_index + 1) * line_height; Left_Max :: 5; Simp.draw_prepared_text(my_font, Left_Max, top_of_line, White); if has_cursor_on_line { Simp.set_shader_for_color(); start := ifx cursor_x == 0 then Left_Max else prefix_width + Left_Max; Simp.immediate_quad( cast(float) start, cast(float) top_of_line + line_height, cast(float) start + 1, cast(float) top_of_line, White); Simp.set_shader_for_text(); Simp.prepare_text(my_font, suffix); Simp.draw_prepared_text(my_font, start, top_of_line, White); } } } handle_window_resizes :: (window: Window_Type) { for Input.get_window_resizes() { Simp.update_window(it.window); if it.window == window { should_reinit := (it.width != window_width) || (it.height != window_height); window_width = it.width; window_height = it.height; if should_reinit { my_init_fonts(); } } } } handle_arrow :: (key_code: Input.Key_Code) { visible_lines := get_visible_lines(); if key_code == { case .ARROW_UP; cursor_y = ifx cursor_y == 0 then 0 else cursor_y - 1; case .ARROW_DOWN; cursor_y = ifx cursor_y == visible_lines.count - 1 then visible_lines.count - 1 else cursor_y + 1; case .ARROW_LEFT; if cursor_x == 0 && cursor_y != 0 { prev_line := visible_lines[cursor_y - 1]; len := builder_string_length(*prev_line); cursor_y = max(0, cursor_y - 1); cursor_x = len; } else if cursor_x != 0 { cursor_x -= 1; } case .ARROW_RIGHT; line := visible_lines[cursor_y]; len := builder_string_length(*line); if cursor_x == len { cursor_x = 0; cursor_y += 1; } else { cursor_x += 1; } } } get_visible_lines :: () -> []String_Builder { num_lines_in_screen := window_height / line_height - 1; return array_view(lines, first_line, num_lines_in_screen); } string_to_line :: (str: string, cursor_char_index := -1) -> string, string { prefix_builder: String_Builder; prefix_builder.allocator = temporary_allocator; suffix_builder: String_Builder; suffix_builder.allocator = temporary_allocator; init_string_builder(*prefix_builder); init_string_builder(*suffix_builder); start := 0; char_index := 0; cursor_index := 0; current_builder := *prefix_builder; while char_index < str.count { defer char_index += 1; c := str.data[char_index]; if c == #char "\t" { // s := string.{data=str.data + start, count=(char_index + 1) - start}; // append(current_builder, s); // append(current_builder, " "); // start = char_index + 1; // cursor_index += 4; } else { cursor_index += 1; } if cursor_index == cursor_char_index { s := string.{data=str.data + start, count=(char_index + 1) - start}; append(current_builder, s); current_builder = *suffix_builder; start = char_index + 1; } } s := string.{data=str.data + start, count=str.count - start}; append(current_builder, s); prefix := builder_to_string(*prefix_builder,, temp); suffix := builder_to_string(*suffix_builder,, temp); return prefix, suffix; } get_time :: () -> s64 { val, ok := to_milliseconds(current_time_monotonic()); assert(ok, "Must be able to convert from apollo to milliseconds"); return val; } read_file_lines :: (file_path: string) -> bool { file_contents, ok := read_entire_file(file_path); if !ok return ok; for lines reset(*it); free(lines.data); lines = [..]String_Builder.{}; lines_temp := split(file_contents, "\n"); defer free(lines_temp.data); for line: lines_temp { builder: String_Builder; init_string_builder(*builder); append(*builder, line); array_add(*lines, builder); } return ok; } my_init_fonts :: () { line_height = 16; base_path := path_strip_filename(get_path_of_running_executable()); my_font = Simp.get_font_at_size("/home/grant/.local/share/fonts/otf/BerkeleyMono/", "BerkeleyMono-Regular.otf", line_height); // my_font = Simp.get_font_at_size(base_path, "Anonymous Pro.ttf", line_height); assert(my_font != null); } tbuilder_to_string :: (builder: *String_Builder) -> string { s := builder_to_string(builder, do_reset = false,, temp); return s; } #import "Basic"; #import "File"; #import "Math"; #import "String"; #import "System"; #import "Window_Creation"; Input :: #import "Input"; Simp :: #import "Simp";