Optimize cursor rendering

This commit is contained in:
Krzosa Karol
2024-07-26 09:57:15 +02:00
parent 672a0b3561
commit a74409c3ec
2 changed files with 38 additions and 56 deletions

View File

@@ -28,7 +28,7 @@ void _BeginProfileScope(const char *name, int len) {
get_time_in_micros() // timestamp in microseconds -- start of your timing block 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() { void EndProfileScope() {
spall_buffer_end(&spall_ctx, &spall_buffer, spall_buffer_end(&spall_ctx, &spall_buffer,

View File

@@ -91,50 +91,39 @@ void DrawCaret(Window &window, XY xy, float size, Color color) {
DrawRectangleRec(ToRectangle(rect), color); DrawRectangleRec(ToRectangle(rect), color);
} }
void DrawLineHighlight(Window &window, XY fxy, Color color) { void DrawWindow(Window &window) {
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();
View &view = *GetActiveView(&window); View &view = *GetActiveView(&window);
Buffer *buffer = GetBuffer(view.active_buffer); Buffer *buffer = GetBuffer(view.active_buffer);
Int front = GetFront(it); DrawRectangleRec(ToRectangle(window.total_rect), ColorBackground);
Int back = GetBack(it); bool is_active = IsActive(&window) || window.id.id == GetLastActiveWindow().id;
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;
XY bxy = PosToXY(*buffer, back); 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);
Color color = ColorSelection; BeginProfileScope(draw_caret_selection);
for (Int line = visible.min.y; line <= visible.max.y && line >= 0 && line < buffer->line_starts.len; line += 1) { Rect2I visible = GetVisibleCells(window);
String16 line_string = GetLineString(*buffer, line); 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) { if (GetSize(it.range)) {
bool a = line > min.line && line < max.line; //
bool b = min.line != max.line && (line == min.line && col >= min.col); // Draw selection
bool c = min.line != max.line && (line == max.line && col < max.col); for (Int line = visible.min.y; line <= visible.max.y && line >= 0 && line < buffer->line_starts.len; line += 1) {
bool d = min.line == max.line && line == max.line && col >= min.col && col < max.col; 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}; Vec2I pos = {col * FontCharSpacing, line * FontLineSpacing};
pos -= view.scroll; pos -= view.scroll;
pos += window.document_rect.min; pos += window.document_rect.min;
Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)FontCharSpacing, (float)FontLineSpacing}; Rectangle rectangle = {(float)pos.x, (float)pos.y, (float)FontCharSpacing, (float)FontLineSpacing};
DrawRectangleRec(rectangle, ColorSelection);
DrawRectangleRec(rectangle, color);
if (line_string[col] == ' ' || line_string[col] == '\t') { 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); 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 { } 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(); EndProfileScope();
DrawVisibleText(window); DrawVisibleText(window);
BeginProfileScope("draw_carets"); BeginProfileScope(draw_carets);
Rect2I visible = GetVisibleCells(window);
For(view.carets) { For(view.carets) {
Int front = GetFront(it); Int front = GetFront(it);
XY fxy = PosToXY(*buffer, front); XY fxy = PosToXY(*buffer, front);