|
@@ -9,6 +9,8 @@
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "./stb_image.h"
|
|
#include "./stb_image.h"
|
|
|
|
|
|
|
|
+#include "./editor.h"
|
|
|
|
+
|
|
#define FONT_WIDTH 128
|
|
#define FONT_WIDTH 128
|
|
#define FONT_HEIGHT 64
|
|
#define FONT_HEIGHT 64
|
|
#define FONT_COLS 18
|
|
#define FONT_COLS 18
|
|
@@ -136,47 +138,7 @@ void render_text_sized(SDL_Renderer *renderer, Font *font, const char *text, siz
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#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;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+Editor editor = {0};
|
|
|
|
|
|
#define UNHEX(color) \
|
|
#define UNHEX(color) \
|
|
((color) >> (8 * 0)) & 0xFF, \
|
|
((color) >> (8 * 0)) & 0xFF, \
|
|
@@ -186,7 +148,9 @@ void buffer_delete(void)
|
|
|
|
|
|
void render_cursor(SDL_Renderer *renderer, const Font *font)
|
|
void render_cursor(SDL_Renderer *renderer, const Font *font)
|
|
{
|
|
{
|
|
- const Vec2f pos = vec2f((float) buffer_cursor * FONT_CHAR_WIDTH * FONT_SCALE, 0.0f);
|
|
|
|
|
|
+ const Vec2f pos =
|
|
|
|
+ vec2f((float) editor.cursor_col * FONT_CHAR_WIDTH * FONT_SCALE,
|
|
|
|
+ (float) editor.cursor_row * FONT_CHAR_HEIGHT * FONT_SCALE);
|
|
|
|
|
|
const SDL_Rect rect = {
|
|
const SDL_Rect rect = {
|
|
.x = (int) floorf(pos.x),
|
|
.x = (int) floorf(pos.x),
|
|
@@ -198,26 +162,25 @@ void render_cursor(SDL_Renderer *renderer, const Font *font)
|
|
scc(SDL_SetRenderDrawColor(renderer, UNHEX(0xFFFFFFFF)));
|
|
scc(SDL_SetRenderDrawColor(renderer, UNHEX(0xFFFFFFFF)));
|
|
scc(SDL_RenderFillRect(renderer, &rect));
|
|
scc(SDL_RenderFillRect(renderer, &rect));
|
|
|
|
|
|
- set_texture_color(font->spritesheet, 0xFF000000);
|
|
|
|
- if (buffer_cursor < buffer_size) {
|
|
|
|
- render_char(renderer, font, buffer[buffer_cursor], pos, FONT_SCALE);
|
|
|
|
|
|
+ const char *c = editor_char_under_cursor(&editor);
|
|
|
|
+ if (c) {
|
|
|
|
+ set_texture_color(font->spritesheet, 0xFF000000);
|
|
|
|
+ render_char(renderer, font, *c, pos, FONT_SCALE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+// TODO: Save/Load file
|
|
// TODO: Jump forward/backward by a word
|
|
// TODO: Jump forward/backward by a word
|
|
// TODO: Delete a word
|
|
// TODO: Delete a word
|
|
// TODO: Blinking cursor
|
|
// TODO: Blinking cursor
|
|
-// TODO: Multiple lines
|
|
|
|
-// TODO: Save/Load file
|
|
|
|
|
|
+// TODO: Delete line
|
|
|
|
+// TODO: Split the line on Enter
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
int main(int argc, char **argv)
|
|
{
|
|
{
|
|
(void) argc;
|
|
(void) argc;
|
|
(void) argv;
|
|
(void) argv;
|
|
|
|
|
|
- buffer_insert_text_before_cursor("Hello, World");
|
|
|
|
-
|
|
|
|
scc(SDL_Init(SDL_INIT_VIDEO));
|
|
scc(SDL_Init(SDL_INIT_VIDEO));
|
|
|
|
|
|
SDL_Window *window =
|
|
SDL_Window *window =
|
|
@@ -243,25 +206,42 @@ int main(int argc, char **argv)
|
|
case SDL_KEYDOWN: {
|
|
case SDL_KEYDOWN: {
|
|
switch (event.key.keysym.sym) {
|
|
switch (event.key.keysym.sym) {
|
|
case SDLK_BACKSPACE: {
|
|
case SDLK_BACKSPACE: {
|
|
- buffer_backspace();
|
|
|
|
|
|
+ editor_backspace(&editor);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
+ case SDLK_F2: {
|
|
|
|
+ editor_save_to_file(&editor, "output");
|
|
|
|
+ } break;
|
|
|
|
+
|
|
|
|
+ case SDLK_RETURN: {
|
|
|
|
+ editor_insert_new_line(&editor);
|
|
|
|
+ } break;
|
|
|
|
+
|
|
case SDLK_DELETE: {
|
|
case SDLK_DELETE: {
|
|
- buffer_delete();
|
|
|
|
|
|
+ editor_delete(&editor);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SDLK_UP: {
|
|
|
|
+ if (editor.cursor_row > 0) {
|
|
|
|
+ editor.cursor_row -= 1;
|
|
|
|
+ }
|
|
|
|
+ } break;
|
|
|
|
+
|
|
|
|
+ case SDLK_DOWN: {
|
|
|
|
+ editor.cursor_row += 1;
|
|
} break;
|
|
} break;
|
|
|
|
|
|
case SDLK_LEFT: {
|
|
case SDLK_LEFT: {
|
|
- if (buffer_cursor > 0) {
|
|
|
|
- buffer_cursor -= 1;
|
|
|
|
|
|
+ if (editor.cursor_col > 0) {
|
|
|
|
+ editor.cursor_col -= 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
case SDLK_RIGHT: {
|
|
case SDLK_RIGHT: {
|
|
- if (buffer_cursor < buffer_size) {
|
|
|
|
- buffer_cursor += 1;
|
|
|
|
- }
|
|
|
|
|
|
+ editor.cursor_col += 1;
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -269,7 +249,7 @@ int main(int argc, char **argv)
|
|
break;
|
|
break;
|
|
|
|
|
|
case SDL_TEXTINPUT: {
|
|
case SDL_TEXTINPUT: {
|
|
- buffer_insert_text_before_cursor(event.text.text);
|
|
|
|
|
|
+ editor_insert_text_before_cursor(&editor, event.text.text);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -278,7 +258,10 @@ int main(int argc, char **argv)
|
|
scc(SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0));
|
|
scc(SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0));
|
|
scc(SDL_RenderClear(renderer));
|
|
scc(SDL_RenderClear(renderer));
|
|
|
|
|
|
- render_text_sized(renderer, &font, buffer, buffer_size, vec2f(0.0, 0.0), 0xFFFFFFFF, FONT_SCALE);
|
|
|
|
|
|
+ for (size_t row = 0; row < editor.size; ++row) {
|
|
|
|
+ const Line *line = editor.lines + row;
|
|
|
|
+ render_text_sized(renderer, &font, line->chars, line->size, vec2f(0.0f, (float) row * FONT_CHAR_HEIGHT * FONT_SCALE), 0xFFFFFFFF, FONT_SCALE);
|
|
|
|
+ }
|
|
render_cursor(renderer, &font);
|
|
render_cursor(renderer, &font);
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
SDL_RenderPresent(renderer);
|