diff options
-rw-r--r-- | buffer.c | 15 | ||||
-rw-r--r-- | main.c | 45 | ||||
-rw-r--r-- | node.c | 2 | ||||
-rw-r--r-- | text.c | 37 | ||||
-rw-r--r-- | text.h | 2 | ||||
-rw-r--r-- | ui.c | 22 |
6 files changed, 48 insertions, 75 deletions
@@ -1962,8 +1962,11 @@ void buffer_render(TextBuffer *buffer, Rect r) { char str[32] = {0}; strbuf_printf(str, U32_FMT, line + 1); // convert line number to string float x = x1 + line_number_width - (float)strlen(str) * char_width; // right justify - gl_color_rgba(colors[line == cursor_line ? COLOR_CURSOR_LINE_NUMBER : COLOR_LINE_NUMBERS]); - text_render_with_state(font, &text_state, str, x, y); + // set color + rgba_u32_to_floats(colors[line == cursor_line ? COLOR_CURSOR_LINE_NUMBER : COLOR_LINE_NUMBERS], + text_state.color); + text_state.x = x; text_state.y = y; + text_render_chars_utf8(font, &text_state, str); y += char_height; if (y > y2) break; } @@ -2053,9 +2056,6 @@ void buffer_render(TextBuffer *buffer, Rect r) { // dynamic array of character types, to be filled by syntax_highlight SyntaxCharType *char_types = NULL; bool syntax_highlighting = language && settings->syntax_highlighting; - if (!syntax_highlighting) { - gl_color_rgba(colors[COLOR_TEXT]); - } if (buffer->frame_latest_line_modified >= buffer->frame_earliest_line_modified && syntax_highlighting) { @@ -2091,7 +2091,8 @@ void buffer_render(TextBuffer *buffer, Rect r) { text_state.min_y = y1; text_state.max_x = x2; text_state.max_y = y2; - + if (!syntax_highlighting) + rgba_u32_to_floats(colors[COLOR_TEXT], text_state.color); for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) { Line *line = &lines[line_idx]; buffer->longest_line_on_screen = max_u32(buffer->longest_line_on_screen, line->len); @@ -2106,7 +2107,7 @@ void buffer_render(TextBuffer *buffer, Rect r) { char32_t c = line->str[i]; if (syntax_highlighting) { SyntaxCharType type = char_types[i]; - gl_color_rgba(colors[syntax_char_type_to_color(type)]); + rgba_u32_to_floats(colors[syntax_char_type_to_color(type)], text_state.color); } switch (c) { case '\n': assert(0); break; @@ -286,36 +286,7 @@ int main(int argc, char **argv) { text_init(); SDL_GL_SetSwapInterval(1); // vsync - Font *font = text_font_load("assets/font.ttf", 16); - if (!font) { - die("%s", text_get_err()); - } - - while (1) { - int w, h; - SDL_GetWindowSize(window, &w, &h); - ted->window_width = (float)w; - ted->window_height = (float)h; - gl_window_width = (float)w, gl_window_height = (float)h; - - SDL_Event event; - while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) return 0; - - (void)settings; - glViewport(0, 0, w, h); - glClearColor(0,0,0,1); - glClear(GL_COLOR_BUFFER_BIT); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - text_render(font, "Hello how are you?", 5, 5, 0xff00aaff); - SDL_GL_SwapWindow(window); - } - - return 0; - -#if 0 ted_load_fonts(ted); if (ted_haserr(ted)) die("Error loading font: %s", ted_geterr(ted)); @@ -410,9 +381,6 @@ int main(int argc, char **argv) { case SDL_QUIT: command_execute(ted, CMD_QUIT, 1); break; - int w, h; - SDL_GetWindowSize(window, &w, &h); - case SDL_MOUSEWHEEL: { // scroll with mouse wheel Sint32 dx = event.wheel.x, dy = -event.wheel.y; @@ -588,10 +556,6 @@ int main(int argc, char **argv) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glViewport(0, 0, (GLsizei)window_width, (GLsizei)window_height); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - // pixel coordinates; down is positive y - glOrtho(0, window_width, window_height, 0, -1, +1); { // clear (background) float bg_color[4]; rgba_u32_to_floats(settings->colors[COLOR_BG], bg_color); @@ -608,9 +572,8 @@ int main(int argc, char **argv) { Node *node = ted->root; node_frame(ted, node, rect4(x1, y1, x2, y2)); } else { - gl_color_rgba(colors[COLOR_TEXT_SECONDARY]); text_render_anchored(font, "Press Ctrl+O to open a file or Ctrl+N to create a new one.", - window_width * 0.5f, window_height * 0.5f, ANCHOR_MIDDLE); + window_width * 0.5f, window_height * 0.5f, colors[COLOR_TEXT_SECONDARY], ANCHOR_MIDDLE); } Menu menu = ted->menu; @@ -624,9 +587,6 @@ int main(int argc, char **argv) { } for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) { TextBuffer *buffer = &ted->buffers[i]; - int w, h; - SDL_GetWindowSize(window, &w, &h); - if (buffer_haserr(buffer)) { ted_seterr_to_buferr(ted, buffer); buffer_clearerr(buffer); @@ -668,7 +628,6 @@ int main(int argc, char **argv) { gl_color_rgba(colors[COLOR_ERROR_BORDER]); rect_render_border(r, settings->border_thickness); glEnd(); - gl_color_rgba(colors[COLOR_ERROR_TEXT]); float text_x1 = rect_x1(r) + padding, text_x2 = rect_x2(r) - padding; @@ -679,6 +638,7 @@ int main(int argc, char **argv) { text_state.min_x = text_x1; text_state.max_x = text_x2; text_state.wrap = true; + rgba_u32_to_floats(colors[COLOR_ERROR_TEXT], text_state.color); text_render_with_state(font, &text_state, ted->error_shown, text_x1, text_y1); } } @@ -730,5 +690,4 @@ int main(int argc, char **argv) { #endif return 0; -#endif } @@ -147,9 +147,9 @@ static void node_frame(Ted *ted, Node *node, Rect r) { char const *surround = buffer_unsaved_changes(buffer) ? "*" : ""; strbuf_printf(tab_title, "%s%s%s", surround, filename, surround); } - gl_color_rgba(colors[COLOR_TEXT]); 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); } @@ -323,28 +323,29 @@ top: } } +void text_render_chars_utf8(Font *font, TextRenderState *state, char const *str) { + char const *end = str + strlen(str); + while (str != end) { + char32_t c = 0; + size_t ret = unicode_utf8_to_utf32(&c, str, (size_t)(end - str)); + if (ret == 0) { + break; + } else if (ret == (size_t)-1) { + // invalid UTF-8 + text_render_char(font, state, '?'); + ++str; + } else { + str += ret; + text_render_char(font, state, c); + } + } +} + void text_render_with_state(Font *font, TextRenderState *render_state, char const *text, float x, float y) { if (render_state->render) text_chars_begin(font); render_state->x = x; render_state->y = y; - char32_t c = 0; - char const *end = text + strlen(text); - while (text != end) { - size_t ret = unicode_utf8_to_utf32(&c, text, (size_t)(end - text)); - if (ret == 0) break; - if (ret == (size_t)(-1)) { - // invalid UTF-8; skip this byte - text_render_char(font, render_state, '?'); - ++text; - } else { - text += ret; // character consists of `ret` bytes - switch (c) { - default: - text_render_char(font, render_state, c); - break; - } - } - } + text_render_chars_utf8(font, render_state, text); if (render_state->render) text_chars_end(font); } @@ -60,6 +60,8 @@ extern void text_chars_begin(Font *font); extern void text_chars_end(Font *font); // Render a single character. extern void text_render_char(Font *font, TextRenderState *state, char32_t c); +// Render a null-terminated UTF-8 string (must be within text_chars_begin/end). +extern void text_render_chars_utf8(Font *font, TextRenderState *state, char const *str); // Free memory used by font. extern void text_font_free(Font *font); @@ -449,26 +449,36 @@ static void file_selector_render(Ted *ted, FileSelector *fs) { } } - TextRenderState text_render_state = {.min_x = x1, .max_x = x2, .min_y = y1, .max_y = y2, .render = true}; + TextRenderState text_state = text_render_state_default; + text_state.min_x = x1; + text_state.max_x = x2; + text_state.min_y = y1; + text_state.max_y = y2; + text_state.render = true; + text_chars_begin(font); // render file names themselves for (u32 i = 0; i < n_entries; ++i) { Rect r; if (file_selector_entry_pos(ted, fs, i, &r)) { float x = r.pos.x, y = r.pos.y; + ColorSetting color = 0; switch (entries[i].type) { case FS_FILE: - gl_color_rgba(colors[COLOR_TEXT]); + color = COLOR_TEXT; break; case FS_DIRECTORY: - gl_color_rgba(colors[COLOR_TEXT_FOLDER]); + color = COLOR_TEXT_FOLDER; break; default: - gl_color_rgba(colors[COLOR_TEXT_OTHER]); + color = COLOR_TEXT_OTHER; break; } - text_render_with_state(font, &text_render_state, entries[i].name, x, y); + text_state.x = x; text_state.y = y; + rgba_u32_to_floats(colors[color], text_state.color); + text_render_chars_utf8(font, &text_state, entries[i].name); } } + text_chars_end(font); } static void button_render(Ted *ted, Rect button, char const *text, u32 color) { @@ -569,7 +579,6 @@ static void popup_render(Ted *ted, char const *title, char const *body) { y += padding; // body text - gl_color_rgba(colors[COLOR_TEXT]); float text_x1 = rect_x1(r) + padding; float text_x2 = rect_x2(r) - padding; @@ -577,6 +586,7 @@ static void popup_render(Ted *ted, char const *title, char const *body) { state.min_x = text_x1; 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); button_render(ted, button_yes, "Yes", colors[COLOR_YES]); |