Add On screen display and help screen
[mercenary-reloaded.git] / src / libtext / text.c
diff --git a/src/libtext/text.c b/src/libtext/text.c
new file mode 100644 (file)
index 0000000..6af9f21
--- /dev/null
@@ -0,0 +1,124 @@
+/* Text screens and OSD generator
+ *
+ * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
+ * All Rights Reserved
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "topaz.h"
+
+#define COLOR(c)       { *rgba++ = palette_red[c]; *rgba++ = palette_green[c]; *rgba++ = palette_blue[c]; *rgba++ = palette_alpha[c]; }
+#define COLOR_BLUE     0
+#define COLOR_WHITE    1
+#define COLOR_BLACK    2
+#define COLOR_YELLOW   3
+#define COLOR_RED      4
+static uint8_t palette_red[5] = { 0x00, 0xff, 0x00, 0xff, 0xdd };
+static uint8_t palette_green[5] = { 0x55, 0xff, 0x00, 0x88, 0x22 };
+static uint8_t palette_blue[5] = { 0xaa, 0xff, 0x22, 0x00, 0x22 };
+static uint8_t palette_alpha[5] = { 0xff, 0xff, 0xff, 0xff, 0xff };
+
+uint8_t *text_alloc(int image_width, int image_height, uint8_t alpha)
+{
+       uint8_t *buffer, *rgba;
+       int x, y;
+
+       palette_alpha[COLOR_BLUE] = alpha;
+
+       buffer = calloc(image_width * image_height, 4);
+       if (!buffer)
+               return NULL;
+
+       /* clear screen */
+       rgba = buffer;
+       for (y = 0; y < image_height; y++) {
+               for (x = 0; x < image_width; x++)
+                       COLOR(COLOR_BLUE)
+       }
+
+       return buffer;
+}
+
+/* render given text to an RGBA image */
+void text_render(uint8_t *buffer, int image_width, int image_height, const char *text, uint8_t alpha, int color, double start_column, double start_line, int double_height)
+{
+       uint8_t *rgba;
+       char *font;
+       int x, y, i, j;
+
+       palette_alpha[COLOR_BLUE] = alpha;
+
+       x = start_column * 8.0;
+       y = start_line * ((double_height) ? 16.0 : 8.0);
+       if (x < 0 || y < 0)
+               return;
+
+       /* render text */
+       while (*text) {
+               /* newline */
+               if (*text == '\n') {
+                       y += (double_height) ? 16 : 8;
+                       x = start_column * 8.0;
+                       text++;
+                       continue;
+               }
+               /* if no space to the bottom */
+               if (y + ((double_height) ? 16 : 8) > image_height)
+                       break;
+               /* if no space to the right */
+               if (x + 8 > image_width) {
+                       text++;
+                       continue;
+               }
+               /* if non visible characters */
+               if (*text < 32 || *text > 126) {
+                       text++;
+                       continue;
+               }
+               /* set pointers */
+               rgba = buffer + (y * image_width + x) * 4;
+               font = topaz_data + (*text - 32) * 64 + 7;
+               for (i = 0; i < 8; i++) {
+                       /* render one line */
+                       for (j = 0; j < 8; j++) {
+                               if (*font)
+                                       COLOR(color)
+                               else
+                                       COLOR(COLOR_BLUE)
+                               font += 8;
+                       }
+                       if (double_height) {
+                               font -= 64;
+                               rgba += (image_width - 8) * 4;
+                               /* render line again */
+                               for (j = 0; j < 8; j++) {
+                                       if (*font)
+                                               COLOR(color)
+                                       else
+                                               COLOR(COLOR_BLUE)
+                                       font += 8;
+                               }
+                       }
+                       rgba += (image_width - 8) * 4;
+                       font -= 65;
+               }
+               x += 8;
+               text++;
+       }
+}
+