From cefdff9a6a449506ddaeae56a2e038a1a2106846 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 5 Feb 2021 11:42:01 -0500 Subject: converted everything to modern opengl for some reason SDL_GL_SwapWindow seems to be in a busy loop? --- base.h | 19 +++++++++++-------- buffer.c | 47 +++++++++++++---------------------------------- gl.c | 10 ++++++++++ main.c | 24 +++++++++++++----------- make.bat | 1 + math.c | 4 +++- menu.c | 15 +++++---------- node.c | 22 ++++++++++++---------- ui.c | 59 +++++++++++++++++++++++++---------------------------------- 9 files changed, 93 insertions(+), 108 deletions(-) diff --git a/base.h b/base.h index a526c6b..c2cd80d 100644 --- a/base.h +++ b/base.h @@ -112,11 +112,8 @@ typedef unsigned long long ullong; #define no_warn_end #endif -#if DEBUG -#if __unix__ -#define debug_print(...) printf(__VA_ARGS__) -#else // __unix__ -static void debug_print(char const *fmt, ...) { +#if _WIN32 +static void print(char const *fmt, ...) { char buf[256]; va_list args; va_start(args, fmt); @@ -124,9 +121,15 @@ static void debug_print(char const *fmt, ...) { va_end(args); OutputDebugStringA(buf); } -#endif // __unix__ -#define debug_println(...) debug_print(__VA_ARGS__), debug_print("\n") -#else // DEBUG +#else +#define print printf +#endif +#define println(...) print(__VA_ARGS__), print("\n") + +#if DEBUG +#define debug_print print +#define debug_println println +#else #define debug_print(...) #define debug_println(...) #endif diff --git a/buffer.c b/buffer.c index 6bc52a2..6216545 100644 --- a/buffer.c +++ b/buffer.c @@ -1931,20 +1931,9 @@ void buffer_render(TextBuffer *buffer, Rect r) { float const padding = settings->padding; float const border_thickness = settings->border_thickness; - - u32 border_color = colors[COLOR_BORDER]; // color of border around buffer - - // bounding box around buffer - glBegin(GL_QUADS); - gl_color_rgba(border_color); - rect_render_border(rect4(x1, y1, x2, y2), border_thickness); - glEnd(); - x1 += border_thickness * 0.5f; - y1 += border_thickness * 0.5f; - x2 -= border_thickness * 0.5f; - y2 -= border_thickness * 0.5f; - u32 start_line = (u32)buffer->scroll_y; // line to start rendering from + Rect bounding_box = rect4(x1, y1, x2, y2); + float render_start_y = y1 - (float)(buffer->scroll_y - start_line) * char_height; // where the 1st line is rendered // line numbering @@ -1975,12 +1964,8 @@ void buffer_render(TextBuffer *buffer, Rect r) { x1 += line_number_width; x1 += 2; // a little bit of padding // line separating line numbers from text - glBegin(GL_LINES); - gl_color_rgba(colors[COLOR_LINE_NUMBERS_SEPARATOR]); - glVertex2f(x1, y1); - glVertex2f(x1, y2); - glEnd(); - x1 += padding; + gl_geometry_rect(rect(V2(x1, y1), V2(border_thickness, y2 - y1)), colors[COLOR_LINE_NUMBERS_SEPARATOR]); + x1 += padding + border_thickness; } // get screen coordinates of cursor @@ -1993,23 +1978,21 @@ void buffer_render(TextBuffer *buffer, Rect r) { // highlight line cursor is on { - gl_color_rgba(colors[COLOR_CURSOR_LINE_BG]); - glBegin(GL_QUADS); Rect hl_rect = rect(V2(x1, cursor_display_pos.y), V2(x2-x1-1, char_height)); buffer_clip_rect(buffer, &hl_rect); - rect_render(hl_rect); - glEnd(); + gl_geometry_rect(hl_rect, colors[COLOR_CURSOR_LINE_BG]); } + u32 border_color = colors[COLOR_BORDER]; // color of border around buffer + // bounding box around buffer + gl_geometry_rect_border(bounding_box, border_thickness, border_color); + // what x coordinate to start rendering the text from float render_start_x = x1 - (float)buffer->scroll_x * char_width; u32 column = 0; - if (buffer->selection) { // draw selection - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_SELECTION_BG]); BufferPos sel_start = {0}, sel_end = {0}; int cmp = buffer_pos_cmp(buffer->cursor_pos, buffer->selection_pos); if (cmp < 0) { @@ -2043,14 +2026,12 @@ void buffer_render(TextBuffer *buffer, Rect r) { V2((float)n_columns_highlighted * char_width, char_height) ); buffer_clip_rect(buffer, &hl_rect); - rect_render(hl_rect); + gl_geometry_rect(hl_rect, colors[COLOR_SELECTION_BG]); } index1 = 0; } - - glEnd(); } - + gl_geometry_draw(); Language language = buffer_language(buffer); // dynamic array of character types, to be filled by syntax_highlight @@ -2156,10 +2137,8 @@ void buffer_render(TextBuffer *buffer, Rect r) { } if (is_on) { if (buffer_clip_rect(buffer, &cursor_rect)) { - gl_color_rgba(colors[COLOR_CURSOR]); - glBegin(GL_QUADS); - rect_render(cursor_rect); - glEnd(); + gl_geometry_rect(cursor_rect, colors[COLOR_CURSOR]); + gl_geometry_draw(); } } } diff --git a/gl.c b/gl.c index 738a2e8..9873bfa 100644 --- a/gl.c +++ b/gl.c @@ -194,6 +194,16 @@ static void gl_geometry_rect(Rect r, u32 color_rgba) { arr_add(gl_geometry_triangles, triangle); } +static void gl_geometry_rect_border(Rect r, float border_thickness, u32 color) { + float border_radius = border_thickness * 0.5f; + float x1 = r.pos.x, y1 = r.pos.y, x2 = x1 + r.size.x, y2 = y1 + r.size.y; + + gl_geometry_rect(rect4(x1+border_radius, y1-border_radius, x2+border_radius, y1+border_radius), color); + gl_geometry_rect(rect4(x1-border_radius, y2-border_radius, x2-border_radius, y2+border_radius), color); + gl_geometry_rect(rect4(x1-border_radius, y1-border_radius, x1+border_radius, y2-border_radius), color); + gl_geometry_rect(rect4(x2-border_radius, y1+border_radius, x2+border_radius, y2+border_radius), color); +} + static void gl_geometry_draw(void) { size_t ntriangles = arr_len(gl_geometry_triangles); diff --git a/main.c b/main.c index 3aa62e1..5d3d37a 100644 --- a/main.c +++ b/main.c @@ -286,7 +286,6 @@ int main(int argc, char **argv) { text_init(); SDL_GL_SetSwapInterval(1); // vsync - ted_load_fonts(ted); if (ted_haserr(ted)) die("Error loading font: %s", ted_geterr(ted)); @@ -343,6 +342,7 @@ int main(int argc, char **argv) { #if DEBUG //printf("\033[H\033[2J"); #endif + PROFILE_TIME(frame_start); SDL_Event event; @@ -563,7 +563,6 @@ int main(int argc, char **argv) { } glClear(GL_COLOR_BUFFER_BIT); - PROFILE_TIME(frame_start); Font *font = ted->font; @@ -622,13 +621,8 @@ int main(int argc, char **argv) { Rect r = error_box_rect(ted); float padding = settings->padding; - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_ERROR_BG]); - rect_render(r); - gl_color_rgba(colors[COLOR_ERROR_BORDER]); - rect_render_border(r, settings->border_thickness); - glEnd(); - + gl_geometry_rect(r, colors[COLOR_ERROR_BG]); + gl_geometry_rect_border(r, settings->border_thickness, colors[COLOR_ERROR_BORDER]); float text_x1 = rect_x1(r) + padding, text_x2 = rect_x2(r) - padding; float text_y1 = rect_y1(r) + padding; @@ -651,16 +645,24 @@ int main(int argc, char **argv) { buffer_check_valid(&ted->line_buffer); #endif + PROFILE_TIME(frame_end_noswap); + + #if PROFILE + { + print("Frame (noswap): %.1f ms\n", (frame_end_noswap - frame_start) * 1000); + } + #endif + + SDL_GL_SwapWindow(window); PROFILE_TIME(frame_end); #if PROFILE { - printf("Frame: %.1f ms\n", (frame_end - frame_start) * 1000); + print("Frame: %.1f ms\n", (frame_end - frame_start) * 1000); } #endif - SDL_GL_SwapWindow(window); } diff --git a/make.bat b/make.bat index ac9269a..991d023 100644 --- a/make.bat +++ b/make.bat @@ -10,4 +10,5 @@ if _%1 == _ ( cl main.c ted.res /DDEBUG /DEBUG /Zi %CFLAGS% /Fe:ted ) if _%1 == _release cl main.c ted.res /O2 %CFLAGS% /Fe:ted +if _%1 == _release_with_debug_info cl main.c ted.res /DEBUG /Zi /O2 %CFLAGS% /Fe:ted if _%1 == _profile cl main.c ted.res /O2 /DPROFILE %CFLAGS% /Fe:ted diff --git a/math.c b/math.c index 00735cf..2b01730 100644 --- a/math.c +++ b/math.c @@ -720,6 +720,8 @@ static void rect_print(Rect r) { } #if MATH_GL + +#if 0 // must be rendering GL_QUADS to use these functions! static void rect_render(Rect r) { @@ -754,7 +756,7 @@ static void rect_render_border(Rect r, float border_thickness) { glVertex2f(x2+border_radius, y2+border_radius); glVertex2f(x2-border_radius, y2+border_radius); } - +#endif /* gl grayscale color diff --git a/menu.c b/menu.c index 70aaf9d..b9d3e66 100644 --- a/menu.c +++ b/menu.c @@ -167,10 +167,8 @@ static void menu_render(Ted *ted, Menu menu) { float char_height_bold = text_font_char_height(font_bold); // render backdrop - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_MENU_BACKDROP]); - rect_render(rect(V2(0, 0), V2(window_width, window_height))); - glEnd(); + gl_geometry_rect(rect(V2(0, 0), V2(window_width, window_height)), colors[COLOR_MENU_BACKDROP]); + gl_geometry_draw(); if (*ted->warn_overwrite) { char const *path = ted->warn_overwrite; @@ -196,12 +194,9 @@ static void menu_render(Ted *ted, Menu menu) { rect_coords(rect, &menu_x1, &menu_y1, &menu_x2, &menu_y2); // menu rectangle & border - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_MENU_BG]); - rect_render(rect); - gl_color_rgba(colors[COLOR_BORDER]); - rect_render_border(rect, settings->border_thickness); - glEnd(); + gl_geometry_rect(rect, colors[COLOR_MENU_BG]); + gl_geometry_rect_border(rect, settings->border_thickness, colors[COLOR_BORDER]); + gl_geometry_draw(); menu_x1 += padding; menu_y1 += padding; diff --git a/node.c b/node.c index d9c2cad..c61f04d 100644 --- a/node.c +++ b/node.c @@ -85,6 +85,7 @@ static void node_frame(Ted *ted, Node *node, Rect r) { Settings const *settings = &ted->settings; u32 const *colors = settings->colors; Font *font = ted->font; + float const border_thickness = settings->border_thickness; float tab_bar_height = 20; @@ -124,35 +125,36 @@ static void node_frame(Ted *ted, Node *node, Rect r) { } } + TextRenderState text_state = text_render_state_default; + text_chars_begin(font); for (u16 i = 0; i < ntabs; ++i) { TextBuffer *buffer = &ted->buffers[node->tabs[i]]; char tab_title[256]; char const *path = buffer_get_filename(buffer); char const *filename = path_filename(path); Rect tab_rect = rect(V2(r.pos.x + tab_width * i, r.pos.y), V2(tab_width, tab_bar_height)); - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_BORDER]); - + // tab border - rect_render_border(tab_rect, 1); + gl_geometry_rect_border(tab_rect, border_thickness, colors[COLOR_BORDER]); if (i == node->active_tab) { // highlight active tab - gl_color_rgba(colors[is_active ? COLOR_ACTIVE_TAB_HL : COLOR_HL]); - rect_render(tab_rect); + gl_geometry_rect(tab_rect, colors[is_active ? COLOR_ACTIVE_TAB_HL : COLOR_HL]); } - glEnd(); - + // tab title { char const *surround = buffer_unsaved_changes(buffer) ? "*" : ""; strbuf_printf(tab_title, "%s%s%s", surround, filename, surround); } - TextRenderState text_state = text_render_state_default; text_state.max_x = rect_x2(tab_rect); rgba_u32_to_floats(colors[COLOR_TEXT], text_state.color); - text_render_with_state(font, &text_state, tab_title, tab_rect.pos.x, tab_rect.pos.y); + text_state.x = tab_rect.pos.x; + text_state.y = tab_rect.pos.y; + text_render_chars_utf8(font, &text_state, tab_title); } + gl_geometry_draw(); + text_chars_end(font); } u16 buffer_index = node->tabs[node->active_tab]; diff --git a/ui.c b/ui.c index 27121fc..68e464c 100644 --- a/ui.c +++ b/ui.c @@ -441,13 +441,11 @@ static void file_selector_render(Ted *ted, FileSelector *fs) { if (rect_contains_point(r, ted->mouse_pos) || ((!fs->create_menu || buffer_empty(&ted->line_buffer)) // only highlight selected for create menus if there is no search term (because that will be the name of the file) && fs->selected == i)) { - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_MENU_HL]); - rect_render(r); - glEnd(); + gl_geometry_rect(r, colors[COLOR_MENU_HL]); } } } + gl_geometry_draw(); TextRenderState text_state = text_render_state_default; text_state.min_x = x1; @@ -481,18 +479,18 @@ static void file_selector_render(Ted *ted, FileSelector *fs) { text_chars_end(font); } +// make sure you call gl_geometry_draw when you are ready to draw all the buttons that have been rendered! static void button_render(Ted *ted, Rect button, char const *text, u32 color) { u32 const *colors = ted->settings.colors; - - glBegin(GL_QUADS); + if (rect_contains_point(button, ted->mouse_pos)) { // highlight button when hovering over it - gl_color_rgba((color & 0xffffff00) | ((color & 0xff) / 3)); - rect_render(button); + u32 new_color = (color & 0xffffff00) | ((color & 0xff) / 3); + gl_geometry_rect(button, new_color); } - gl_color_rgba(colors[COLOR_BORDER]); - rect_render_border(button, 1); - glEnd(); + + gl_geometry_rect_border(button, ted->settings.border_thickness, colors[COLOR_BORDER]); + gl_geometry_draw(); v2 pos = rect_center(button); text_render_anchored(ted->font, text, pos.x, pos.y, color, ANCHOR_MIDDLE); @@ -551,33 +549,29 @@ static void popup_render(Ted *ted, char const *title, char const *body) { u32 const *colors = settings->colors; float const char_height_bold = text_font_char_height(font_bold); float const padding = settings->padding; - + float const border_thickness = settings->border_thickness; + popup_get_rects(ted, &r, &button_yes, &button_no, &button_cancel); - - glBegin(GL_QUADS); - gl_color_rgba(colors[COLOR_MENU_BG]); - rect_render(r); - gl_color_rgba(colors[COLOR_BORDER]); - rect_render_border(r, 1); - glEnd(); - + + float y = r.pos.y; + + // popup rectangle + gl_geometry_rect(r, colors[COLOR_MENU_BG]); + gl_geometry_rect_border(r, border_thickness, colors[COLOR_BORDER]); + // line separating text from body + gl_geometry_rect(rect(V2(r.pos.x, y + char_height_bold), V2(r.size.x, border_thickness)), colors[COLOR_BORDER]); + + button_render(ted, button_yes, "Yes", colors[COLOR_YES]); + button_render(ted, button_no, "No", colors[COLOR_NO]); + button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]); + // title text v2 title_size = {0}; text_get_size(font_bold, title, &title_size.x, &title_size.y); v2 title_pos = v2_sub(V2(window_width * 0.5f, y), V2(title_size.x * 0.5f, 0)); text_render(font_bold, title, title_pos.x, title_pos.y, colors[COLOR_TEXT]); - y += char_height_bold; - // line separating text from body - glBegin(GL_LINES); - gl_color_rgba(colors[COLOR_BORDER]); - glVertex2f(rect_x1(r), y); - glVertex2f(rect_x2(r), y); - glEnd(); - - y += padding; - // body text float text_x1 = rect_x1(r) + padding; float text_x2 = rect_x2(r) - padding; @@ -587,9 +581,6 @@ static void popup_render(Ted *ted, char const *title, char const *body) { state.max_x = text_x2; state.wrap = true; rgba_u32_to_floats(colors[COLOR_TEXT], state.color); - text_render_with_state(font, &state, body, text_x1, y); + text_render_with_state(font, &state, body, text_x1, y + char_height_bold + padding); - button_render(ted, button_yes, "Yes", colors[COLOR_YES]); - button_render(ted, button_no, "No", colors[COLOR_NO]); - button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]); } -- cgit v1.2.3