commit a5a9d72dfbd3303db821b26dbb7efc278e0a056f
parent 07a927715ac68f83522da4e8dd0c894f1087c896
Author: rexim <reximkut@gmail.com>
Date: Fri, 2 Jul 2021 19:39:45 +0700
Implement text manipulations under the cursor
- Insert before the cursor
- Delete character before and after the cursor
Diffstat:
| M | main.c | | | 78 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
1 file changed, 55 insertions(+), 23 deletions(-)
diff --git a/main.c b/main.c
@@ -106,9 +106,11 @@ void render_char(SDL_Renderer *renderer, const Font *font, char c, Vec2f pos, fl
.h = (int) floorf(FONT_CHAR_HEIGHT * scale),
};
- assert(c >= ASCII_DISPLAY_LOW);
- assert(c <= ASCII_DISPLAY_HIGH);
- const size_t index = c - ASCII_DISPLAY_LOW;
+ size_t index = '?' - ASCII_DISPLAY_LOW;
+ if (ASCII_DISPLAY_LOW <= c && c <= ASCII_DISPLAY_HIGH) {
+ index = c - ASCII_DISPLAY_LOW;
+ }
+
scc(SDL_RenderCopy(renderer, font->spritesheet, &font->glyph_table[index], &dst));
}
@@ -134,17 +136,48 @@ void render_text_sized(SDL_Renderer *renderer, Font *font, const char *text, siz
}
}
-void render_text(SDL_Renderer *renderer, Font *font, const char *text, Vec2f pos, Uint32 color, float scale)
-{
- render_text_sized(renderer, font, text, strlen(text), pos, color, scale);
-}
-
#define BUFFER_CAPACITY 1024
char buffer[BUFFER_CAPACITY];
size_t buffer_cursor = 0;
size_t buffer_size = 0;
+void buffer_insert_text_before_cursor(const char *text)
+{
+ size_t text_size = strlen(text);
+ const size_t free_space = BUFFER_CAPACITY - buffer_size;
+ if (text_size > free_space) {
+ text_size = free_space;
+ }
+ memmove(buffer + buffer_cursor + text_size,
+ buffer + buffer_cursor,
+ buffer_size - buffer_cursor);
+ memcpy(buffer + buffer_cursor, text, text_size);
+ buffer_size += text_size;
+ buffer_cursor += text_size;
+}
+
+void buffer_backspace(void)
+{
+ if (buffer_cursor > 0 && buffer_size > 0) {
+ memmove(buffer + buffer_cursor - 1,
+ buffer + buffer_cursor,
+ buffer_size - buffer_cursor);
+ buffer_size -= 1;
+ buffer_cursor -= 1;
+ }
+}
+
+void buffer_delete(void)
+{
+ if (buffer_cursor < buffer_size && buffer_size > 0) {
+ memmove(buffer + buffer_cursor,
+ buffer + buffer_cursor + 1,
+ buffer_size - buffer_cursor);
+ buffer_size -= 1;
+ }
+}
+
#define UNHEX(color) \
((color) >> (8 * 0)) & 0xFF, \
((color) >> (8 * 1)) & 0xFF, \
@@ -172,7 +205,8 @@ void render_cursor(SDL_Renderer *renderer, const Font *font)
}
-// TODO: move the cursor around
+// TODO: Jump forward/backward by a word
+// TODO: Delete a word
// TODO: Blinking cursor
// TODO: Multiple lines
// TODO: Save/Load file
@@ -182,6 +216,8 @@ int main(int argc, char **argv)
(void) argc;
(void) argv;
+ buffer_insert_text_before_cursor("Hello, World");
+
scc(SDL_Init(SDL_INIT_VIDEO));
SDL_Window *window =
@@ -207,37 +243,33 @@ int main(int argc, char **argv)
case SDL_KEYDOWN: {
switch (event.key.keysym.sym) {
case SDLK_BACKSPACE: {
- if (buffer_size > 0) {
- buffer_size -= 1;
- buffer_cursor = buffer_size;
- }
+ buffer_backspace();
}
break;
+ case SDLK_DELETE: {
+ buffer_delete();
+ } break;
+
case SDLK_LEFT: {
if (buffer_cursor > 0) {
buffer_cursor -= 1;
}
- } break;
+ }
+ break;
case SDLK_RIGHT: {
if (buffer_cursor < buffer_size) {
buffer_cursor += 1;
}
- } break;
+ }
+ break;
}
}
break;
case SDL_TEXTINPUT: {
- size_t text_size = strlen(event.text.text);
- const size_t free_space = BUFFER_CAPACITY - buffer_size;
- if (text_size > free_space) {
- text_size = free_space;
- }
- memcpy(buffer + buffer_size, event.text.text, text_size);
- buffer_size += text_size;
- buffer_cursor = buffer_size;
+ buffer_insert_text_before_cursor(event.text.text);
}
break;
}