authored by
Yuki Izumi
<yuki@kivikakk.ee>
10 years ago
main.c
| 24 ++++++---
qb.c
| 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
qb.h
| 14 +++++-
renderer.c
| 2 +-
text.c
| 134 +------------------------------------------------
text.h
| 5 +-
6 files changed, 208 insertions(+), 142 deletions(-)
@@ -1,4 +1,5 @@
#include "renderer.h"
+#include "qb.h"
#include "text.h"
static void loop(void);
@@ -9,6 +10,8 @@ int main(int argc, char **argv) {
return r;
}
+ qb_init();
+
loop();
renderer_quit();
@@ -35,17 +38,23 @@ void loop(void) {
case SDL_KEYDOWN:
case SDL_KEYUP:
- printf(
- "%s %s %06d\n",
- event.key.state == SDL_PRESSED ? "down" : "up ",
- SDL_GetKeyName(event.key.keysym.sym),
- event.key.keysym.mod);
+ if (event.key.state == SDL_PRESSED) {
+ if (event.key.keysym.sym == SDLK_DOWN && cursor_y < total_lines - 1) {
+ ++cursor_y;
+ } else if (event.key.keysym.sym == SDLK_UP && cursor_y > 0) {
+ --cursor_y;
+ } else if (event.key.keysym.sym == SDLK_LEFT && cursor_x > 0) {
+ --cursor_x;
+ } else if (event.key.keysym.sym == SDLK_RIGHT) {
+ ++cursor_x;
+ }
+ qb_render();
+ text_refresh();
+ }
break;
}
}
- text_refresh();
-
Uint32 next_tick = last_tick + TICK_LENGTH;
last_tick = SDL_GetTicks();
@@ -58,6 +67,7 @@ void loop(void) {
if (until_flip == 0) {
until_flip = FLIP_CURSOR;
text_cursor_toggle();
+ text_refresh();
}
}
}
@@ -1,7 +1,172 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
#include "qb.h"
+#include "text.h"
+
+doc_line_t *active_doc;
+int cursor_x = 0;
+int cursor_y = 0;
+int total_lines = 1;
+
+static doc_line_t *create_doc_line(void) {
+ doc_line_t *doc_line = malloc(sizeof(*doc_line));
+ memset(doc_line, 0, sizeof(*doc_line));
+ doc_line->line = malloc(8);
+ doc_line->allocated = 8;
+ return doc_line;
+}
+
+void qb_init(void) {
+ active_doc = create_doc_line();
+ active_doc->line = strdup("10 PRINT \"LOL\"");
+ active_doc->stored = strlen(active_doc->line);
+ active_doc->allocated = active_doc->stored + 1;
+
+ active_doc->next = create_doc_line();
+ active_doc->next->line = strdup("20 GOTO 10");
+ active_doc->next->stored = strlen(active_doc->next->line);
+ active_doc->next->allocated = active_doc->next->stored + 1;
+
+ total_lines = 2;
+
+ qb_render();
+}
+
+void qb_render(void) {
+ for (int i = 0; i < 80 * 25; ++i) {
+ screen[i] = 0x1700;
+ }
+
+ for (int x = 0; x < 80; ++x) {
+ screen[x] = 0x7000;
+ }
+
+ const char *menu_options[] = {
+ "File",
+ "Edit",
+ "View",
+ "Search",
+ "Run",
+ "Debug",
+ "Calls",
+ "Options",
+ NULL,
+ };
+
+ int offset = 2;
+ for (int i = 0; ; ++i) {
+ if (!menu_options[i]) {
+ break;
+ }
+
+ int len = strlen(menu_options[i]);
+ for (int j = 0; j < len; ++j) {
+ screen[0 * 80 + offset + 1 + j] = 0x7000 | (menu_options[i][j]);
+ }
+
+ offset += len + 2;
+ }
+
+ screen[0 * 80 + 74] = 0x7000 + 'H';
+ screen[0 * 80 + 75] = 0x7000 + 'e';
+ screen[0 * 80 + 76] = 0x7000 + 'l';
+ screen[0 * 80 + 77] = 0x7000 + 'p';
+
+ screen[1 * 80 + 0] = 0x17da;
+ for (int x = 1; x < 79; ++x) {
+ screen[1 * 80 + x] = 0x17c4;
+ }
+
+ const char *file = "Untitled";
+ int flen = strlen(file);
+ int start = 40 - flen / 2;
+ screen[1 * 80 + start - 1] = 0x7000;
+
+ int j;
+ for (j = 0; j < flen; ++j) {
+ screen[1 * 80 + start + j] = 0x7000 | file[j];
+ }
+
+ screen[1 * 80 + start + j] = 0x7000;
+
+ screen[1 * 80 + 75] = 0x17b4;
+ screen[1 * 80 + 76] = 0x7112;
+ screen[1 * 80 + 77] = 0x17c3;
+
+ screen[1 * 80 + 79] = 0x17bf;
+
+ for (int y = 2; y < 24; ++y) {
+ screen[y * 80 + 0] = screen[y * 80 + 79] = 0x17b3;
+ for (int x = 1; x < 79; ++x) {
+ screen[y * 80 + x] = 0x1700;
+ }
+ }
+
+ doc_line_t *line = active_doc;
+ for (int y = 0; y < 22 && line; ++y, line = line->next) {
+ for (int x = 0; x < line->stored; ++x) {
+ screen[(y + 2) * 80 + 1 + x] += line->line[x];
+ }
+ }
+
+ screen[2 * 80 + 79] = 0x7018;
+ screen[3 * 80 + 79] = 0x0000;
+ for (int y = 4; y < 22; ++y) {
+ screen[y * 80 + 79] = 0x70b0;
+ }
+ screen[22 * 80 + 79] = 0x7019;
+
+ screen[23 * 80 + 1] = 0x701b;
+ screen[23 * 80 + 2] = 0x0000;
+ for (int x = 3; x < 78; ++x) {
+ screen[23 * 80 + x] = 0x70b0;
+ }
+ screen[23 * 80 + 78] = 0x701a;
+
+ const char *footer[] = {
+ "Shift+F1=Help",
+ "F6=Window",
+ "F2=Subs",
+ "F5=Run",
+ "F8=Step",
+ NULL,
+ };
+
+ for (int x = 0; x < 80; ++x) {
+ screen[24 * 80 + x] = 0x3000;
+ }
+
+ offset = 1;
+ for (int i = 0; ; ++i) {
+ if (!footer[i]) {
+ break;
+ }
+
+ int len = strlen(footer[i]);
+ screen[24 * 80 + offset] += '<';
+ int j;
+ for (j = 0; j < len; ++j) {
+ screen[24 * 80 + offset + 1 + j] += footer[i][j];
+ }
+ screen[24 * 80 + offset + 1 + j] += '>';
+
+ offset += len + 3;
+ }
+
+ screen[24 * 80 + 62] += 0xb3;
+
+ char *counter;
+ asprintf(&counter, "%05d:%03d", cursor_y + 1, cursor_x + 1);
+ int len = strlen(counter);
+ for (int i = 0; i < len; ++i) {
+ screen[24 * 80 + 70 + i] += counter[i];
+ }
+ free(counter);
-typedef struct doc_line {
-
-} doc_line_t;
+ screen_cursor_x = cursor_x + 1;
+ screen_cursor_y = cursor_y + 2;
+}
/* vim: set sw=4 et: */
@@ -1,6 +1,20 @@
#ifndef QB_H
#define QB_H
+typedef struct doc_line {
+ char *line;
+ int allocated, stored;
+ struct doc_line *next;
+} doc_line_t;
+
+extern doc_line_t *active_doc;
+extern int cursor_x;
+extern int cursor_y;
+extern int total_lines;
+
+void qb_init(void);
+void qb_render(void);
+
#endif
/* vim: set sw=4 et: */
@@ -38,8 +38,6 @@ int renderer_init(void) {
sfont = read_raw_sdlfont("cp437.vga");
- text_init();
-
return 0;
}
@@ -1,135 +1,11 @@
#include "text.h"
#include "renderer.h"
-static unsigned short screen[80 * 25];
-static int cursor_on = 1;
-static int cursor_x = 1;
-static int cursor_y = 2;
-
-void text_init(void) {
- for (int i = 0; i < 80 * 25; ++i) {
- screen[i] = 0x1700;
- }
-
- for (int x = 0; x < 80; ++x) {
- screen[x] = 0x7000;
- }
-
- const char *menu_options[] = {
- "File",
- "Edit",
- "View",
- "Search",
- "Run",
- "Debug",
- "Calls",
- "Options",
- NULL,
- };
-
- int offset = 2;
- for (int i = 0; ; ++i) {
- if (!menu_options[i]) {
- break;
- }
-
- int len = strlen(menu_options[i]);
- for (int j = 0; j < len; ++j) {
- screen[0 * 80 + offset + 1 + j] = 0x7000 | (menu_options[i][j]);
- }
-
- offset += len + 2;
- }
-
- screen[0 * 80 + 74] = 0x7000 + 'H';
- screen[0 * 80 + 75] = 0x7000 + 'e';
- screen[0 * 80 + 76] = 0x7000 + 'l';
- screen[0 * 80 + 77] = 0x7000 + 'p';
-
- screen[1 * 80 + 0] = 0x17da;
- for (int x = 1; x < 79; ++x) {
- screen[1 * 80 + x] = 0x17c4;
- }
-
- const char *file = "Untitled";
- int flen = strlen(file);
- int start = 40 - flen / 2;
- screen[1 * 80 + start - 1] = 0x7000;
-
- int j;
- for (j = 0; j < flen; ++j) {
- screen[1 * 80 + start + j] = 0x7000 | file[j];
- }
-
- screen[1 * 80 + start + j] = 0x7000;
-
- screen[1 * 80 + 75] = 0x17b4;
- screen[1 * 80 + 76] = 0x7112;
- screen[1 * 80 + 77] = 0x17c3;
-
- screen[1 * 80 + 79] = 0x17bf;
+unsigned short screen[80 * 25];
+int screen_cursor_x;
+int screen_cursor_y;
- for (int y = 2; y < 24; ++y) {
- screen[y * 80 + 0] = screen[y * 80 + 79] = 0x17b3;
- for (int x = 1; x < 79; ++x) {
- screen[y * 80 + x] = 0x1700;
- }
- }
-
- screen[2 * 80 + 1] += 'a';
-
- screen[2 * 80 + 79] = 0x7018;
- screen[3 * 80 + 79] = 0x0000;
- for (int y = 4; y < 22; ++y) {
- screen[y * 80 + 79] = 0x70b0;
- }
- screen[22 * 80 + 79] = 0x7019;
-
- screen[23 * 80 + 1] = 0x701b;
- screen[23 * 80 + 2] = 0x0000;
- for (int x = 3; x < 78; ++x) {
- screen[23 * 80 + x] = 0x70b0;
- }
- screen[23 * 80 + 78] = 0x701a;
-
- const char *footer[] = {
- "Shift+F1=Help",
- "F6=Window",
- "F2=Subs",
- "F5=Run",
- "F8=Step",
- NULL,
- };
-
- for (int x = 0; x < 80; ++x) {
- screen[24 * 80 + x] = 0x3000;
- }
-
- offset = 1;
- for (int i = 0; ; ++i) {
- if (!footer[i]) {
- break;
- }
-
- int len = strlen(footer[i]);
- screen[24 * 80 + offset] += '<';
- int j;
- for (j = 0; j < len; ++j) {
- screen[24 * 80 + offset + 1 + j] += footer[i][j];
- }
- screen[24 * 80 + offset + 1 + j] += '>';
-
- offset += len + 3;
- }
-
- screen[24 * 80 + 62] += 0xb3;
-
- const char *counter = "00001:001";
- int len = strlen(counter);
- for (int i = 0; i < len; ++i) {
- screen[24 * 80 + 70 + i] += counter[i];
- }
-}
+static int cursor_on = 1;
void text_refresh(void) {
SDL_RenderClear(renderer);
@@ -141,7 +17,7 @@ void text_refresh(void) {
}
if (cursor_on) {
- text_draw_cursor(cursor_x, cursor_y);
+ text_draw_cursor(screen_cursor_x, screen_cursor_y);
}
SDL_RenderPresent(renderer);
@@ -1,12 +1,15 @@
#ifndef TEXT_H
#define TEXT_H
-void text_init(void);
void text_refresh(void);
void text_refresh_at(int x, int y);
void text_draw_cursor(int x, int y);
void text_cursor_toggle(void);
+extern unsigned short screen[80 * 25];
+extern int screen_cursor_x;
+extern int screen_cursor_y;
+
#endif
/* vim: set sw=4 et: */