summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-12-27 15:48:59 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2020-12-27 15:48:59 -0500
commitc38e418286c5cc2e8af7f68de9483d9cdcf95dce (patch)
tree461a7ce91748ac506fa31e7c918ab5ba6f6d8b14
parent0fcc0682080da33edc81fa98c450a904165d5b5a (diff)
only render visible lines
-rw-r--r--base.h7
-rw-r--r--buffer.c15
-rw-r--r--main.c9
3 files changed, 23 insertions, 8 deletions
diff --git a/base.h b/base.h
index c9306a3..9b0c805 100644
--- a/base.h
+++ b/base.h
@@ -77,19 +77,20 @@ typedef unsigned long long ullong;
#if DEBUG
#if __unix__
-#define debug_println(...) printf(__VA_ARGS__), printf("\n")
+#define debug_print(...) printf(__VA_ARGS__)
#else // __unix__
-static void debug_println(char const *fmt, ...) {
+static void debug_print(char const *fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsprintf_s(buf, sizeof buf, fmt, args);
va_end(args);
OutputDebugStringA(buf);
- OutputDebugStringA("\n");
}
#endif // __unix__
+#define debug_println(...) debug_print(__VA_ARGS__), debug_print("\n")
#else // DEBUG
+#define debug_print(...)
#define debug_println(...)
#endif
diff --git a/buffer.c b/buffer.c
index fac72af..7f9d810 100644
--- a/buffer.c
+++ b/buffer.c
@@ -787,10 +787,12 @@ void buffer_render(TextBuffer *buffer, float x1, float y1, float x2, float y2) {
u32 column = 0;
- // @TODO: make this better (we should figure out where to start rendering, etc.)
- text_state.y -= (float)buffer->scroll_y * char_height;
+ u32 start_line = (u32)buffer->scroll_y; // line to start rendering from
+ text_state.y -= (float)(buffer->scroll_y - start_line) * char_height;
- for (u32 line_idx = 0; line_idx < nlines; ++line_idx) {
+ //debug_print("Rendering from " U32_FMT, start_line);
+
+ for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) {
Line *line = &lines[line_idx];
for (char32_t *p = line->str, *end = p + line->len; p != end; ++p) {
char32_t c = *p;
@@ -813,9 +815,16 @@ void buffer_render(TextBuffer *buffer, float x1, float y1, float x2, float y2) {
// next line
text_state.x = render_start_x;
+ if (text_state.y > text_state.max_y) {
+ // made it to the bottom of the buffer view.
+ //debug_println(" to " U32_FMT ".", line_idx);
+ break;
+ }
text_state.y += text_font_char_height(font);
column = 0;
}
+
+
text_chars_end(font);
{ // render cursor
diff --git a/main.c b/main.c
index ae197ac..763decd 100644
--- a/main.c
+++ b/main.c
@@ -76,13 +76,17 @@ int main(void) {
TextBuffer text_buffer;
buffer_create(&text_buffer, font);
- if (!buffer_load_file(&text_buffer, "test.txt"))
+ if (!buffer_load_file(&text_buffer, "buffer.c"))
die("Error loading file: %s", buffer_geterr(&text_buffer));
Uint32 time_at_last_frame = SDL_GetTicks();
while (!quit) {
+ #if DEBUG
+ printf("\033[H\033[2J"); fflush(stdout);
+ #endif
+
SDL_Event event;
Uint8 const *keyboard_state = SDL_GetKeyboardState(NULL);
bool ctrl = keyboard_state[SDL_SCANCODE_LCTRL] || keyboard_state[SDL_SCANCODE_RCTRL];
@@ -216,10 +220,11 @@ int main(void) {
}
}
+ #if DEBUG
//buffer_print_debug(&text_buffer);
buffer_check_valid(&text_buffer);
- printf("\033[H\033[2J"); fflush(stdout);
buffer_print_undo_history(&text_buffer);
+ #endif
SDL_GL_SwapWindow(window);
}