From a74409c3ec2567b56e4368ebe8cbabbcb5d304fe Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 26 Jul 2024 09:57:15 +0200 Subject: [PATCH] Optimize cursor rendering --- src/profiler/profiler.cpp | 2 +- src/text_editor/window_draw.cpp | 92 +++++++++++++-------------------- 2 files changed, 38 insertions(+), 56 deletions(-) diff --git a/src/profiler/profiler.cpp b/src/profiler/profiler.cpp index 8e1044b..797dbb6 100644 --- a/src/profiler/profiler.cpp +++ b/src/profiler/profiler.cpp @@ -28,7 +28,7 @@ void _BeginProfileScope(const char *name, int len) { get_time_in_micros() // timestamp in microseconds -- start of your timing block ); } - #define BeginProfileScope(name) _BeginProfileScope(name, sizeof(name) - 1) + #define BeginProfileScope(name) _BeginProfileScope(#name, sizeof(#name) - 1) void EndProfileScope() { spall_buffer_end(&spall_ctx, &spall_buffer, diff --git a/src/text_editor/window_draw.cpp b/src/text_editor/window_draw.cpp index 869648f..bf55b6a 100644 --- a/src/text_editor/window_draw.cpp +++ b/src/text_editor/window_draw.cpp @@ -91,50 +91,39 @@ void DrawCaret(Window &window, XY xy, float size, Color color) { DrawRectangleRec(ToRectangle(rect), color); } -void DrawLineHighlight(Window &window, XY fxy, Color color) { - ProfileFunction(); - View &view = *GetActiveView(&window); - Vec2I w = XYToWorldPos(view, XYLine(fxy.line)); - w -= view.scroll; - w += window.document_rect.min; - Rect2I rect = { - { 0, w.y}, - {GetRenderWidth(), w.y + FontLineSpacing} - }; - DrawRectangleRec(ToRectangle(rect), color); -} - -void DrawSelection(Window &window, Caret &it) { - ProfileFunction(); +void DrawWindow(Window &window) { View &view = *GetActiveView(&window); Buffer *buffer = GetBuffer(view.active_buffer); - Int front = GetFront(it); - Int back = GetBack(it); - if (front != back) { - Rect2I visible = GetVisibleCells(window); - XY min = PosToXY(*buffer, it.range.min); - XY max = PosToXY(*buffer, it.range.max); - if (visible.min.y > max.line) return; - if (visible.max.y < min.line) return; + DrawRectangleRec(ToRectangle(window.total_rect), ColorBackground); + bool is_active = IsActive(&window) || window.id.id == GetLastActiveWindow().id; - XY bxy = PosToXY(*buffer, back); - Color color = ColorSelection; - for (Int line = visible.min.y; line <= visible.max.y && line >= 0 && line < buffer->line_starts.len; line += 1) { - String16 line_string = GetLineString(*buffer, line); + BeginScissorMode((int)window.document_rect.min.x, (int)window.document_rect.min.y, (int)window.document_rect.max.x - (int)window.document_rect.min.x, (int)window.document_rect.max.y - (int)window.document_rect.min.y); + BeginProfileScope(draw_caret_selection); + Rect2I visible = GetVisibleCells(window); + For(view.carets) { + XY min = PosToXY(*buffer, it.range.min); + XY max = PosToXY(*buffer, it.range.max); + if (visible.min.y > max.line) continue; + if (visible.max.y < min.line) continue; - for (Int col = visible.min.x; col < visible.max.x && col >= 0 && col < line_string.len; col += 1) { - bool a = line > min.line && line < max.line; - bool b = min.line != max.line && (line == min.line && col >= min.col); - bool c = min.line != max.line && (line == max.line && col < max.col); - bool d = min.line == max.line && line == max.line && col >= min.col && col < max.col; + if (GetSize(it.range)) { + // + // Draw selection + for (Int line = visible.min.y; line <= visible.max.y && line >= 0 && line < buffer->line_starts.len; line += 1) { + String16 line_string = GetLineString(*buffer, line); + + for (Int col = visible.min.x; col < visible.max.x && col >= 0 && col < line_string.len; col += 1) { + bool a = line > min.line && line < max.line; + bool b = min.line != max.line && (line == min.line && col >= min.col); + bool c = min.line != max.line && (line == max.line && col < max.col); + bool d = min.line == max.line && line == max.line && col >= min.col && col < max.col; + if (!(a || b || c || d)) continue; - if (a || b || c || d) { Vec2I pos = {col * FontCharSpacing, line * FontLineSpacing}; pos -= view.scroll; pos += window.document_rect.min; Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)FontCharSpacing, (float)FontLineSpacing}; - - DrawRectangleRec(rectangle, color); + DrawRectangleRec(rectangle, ColorSelection); if (line_string[col] == ' ' || line_string[col] == '\t') { DrawCircle((int)pos.x + (int)FontCharSpacing / 2, (int)pos.y + (int)FontLineSpacing / 2, (float)FontSize / 10.f, ColorWhitespaceDuringSelection); @@ -145,33 +134,26 @@ void DrawSelection(Window &window, Caret &it) { } } } - } - } -} - -void DrawWindow(Window &window) { - View &view = *GetActiveView(&window); - Buffer *buffer = GetBuffer(view.active_buffer); - DrawRectangleRec(ToRectangle(window.total_rect), ColorBackground); - bool is_active = IsActive(&window) || window.id.id == GetLastActiveWindow().id; - - BeginScissorMode((int)window.document_rect.min.x, (int)window.document_rect.min.y, (int)window.document_rect.max.x - (int)window.document_rect.min.x, (int)window.document_rect.max.y - (int)window.document_rect.min.y); - BeginProfileScope("draw_caret_selection"); - For(view.carets) { - Int front = GetFront(it); - XY fxy = PosToXY(*buffer, front); - if (GetSize(it.range)) { - DrawSelection(window, it); } else { - DrawLineHighlight(window, fxy, ColorLineHighlight); + // + // Draw highlight + Int front = GetFront(it); + XY fxy = PosToXY(*buffer, front); + Vec2I w = XYToWorldPos(view, XYLine(fxy.line)); + w -= view.scroll; + w += window.document_rect.min; + Rect2I rect = { + { 0, w.y}, + {GetRenderWidth(), w.y + FontLineSpacing} + }; + DrawRectangleRec(ToRectangle(rect), ColorLineHighlight); } } EndProfileScope(); DrawVisibleText(window); - BeginProfileScope("draw_carets"); - Rect2I visible = GetVisibleCells(window); + BeginProfileScope(draw_carets); For(view.carets) { Int front = GetFront(it); XY fxy = PosToXY(*buffer, front);