Umorna -- Tiny game written to test 'chads' library, it uses assets from itch.io...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.6KB

  1. #include <fcntl.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #define SYNTAX_LIMIT (64)
  5. static int syntax_count = 0;
  6. static int syntax_enrange [SYNTAX_LIMIT];
  7. static int syntax_derange [SYNTAX_LIMIT];
  8. static char syntax_begin [SYNTAX_LIMIT] [96];
  9. static char syntax_end [SYNTAX_LIMIT] [96];
  10. static char syntax_escape [SYNTAX_LIMIT];
  11. static int syntax_colour [SYNTAX_LIMIT];
  12. static int character_compare_array (char character, char * character_array) {
  13. int i = 0;
  14. do {
  15. if (character == character_array [i]) {
  16. return (1);
  17. }
  18. } while (++i != (int) strlen (character_array));
  19. return (0);
  20. }
  21. static void syntax_rule (int enrange,
  22. int derange,
  23. char * begin,
  24. char * end,
  25. char escape,
  26. int colour) {
  27. if (syntax_count >= SYNTAX_LIMIT) {
  28. return;
  29. }
  30. strncpy (syntax_begin [syntax_count], begin, 96);
  31. strncpy (syntax_end [syntax_count], end, 96);
  32. syntax_enrange [syntax_count] = enrange;
  33. syntax_derange [syntax_count] = derange;
  34. syntax_escape [syntax_count] = escape;
  35. syntax_colour [syntax_count] = colour;
  36. ++syntax_count;
  37. }
  38. static int syntax_loop (char * string,
  39. int * length) {
  40. int offset, subset, select;
  41. for (select = offset = 0; select != syntax_count; ++select) {
  42. if (syntax_enrange [select] == 0) {
  43. if (syntax_derange [select] == 0) {
  44. if (strncmp (string, syntax_begin [select], strlen (syntax_begin [select])) == 0) {
  45. break;
  46. }
  47. } else {
  48. if ((strncmp (string, syntax_begin [select], strlen (syntax_begin [select])) == 0)
  49. && (character_compare_array (string [offset + (int) strlen (syntax_begin [select])], syntax_end [select]) == 1)) {
  50. break;
  51. }
  52. }
  53. } else {
  54. for (subset = 0; subset != (int) strlen (syntax_begin [select]); ++subset) {
  55. if (string [offset] == syntax_begin [select] [subset]) {
  56. goto selected;
  57. }
  58. }
  59. }
  60. }
  61. selected:
  62. if (select >= syntax_count) {
  63. * length = 1;
  64. return (select);
  65. }
  66. for (offset = 1; string [offset - 1] != '\0'; ++offset) {
  67. if (string [offset] == syntax_escape [select]) {
  68. ++offset;
  69. continue;
  70. }
  71. if (syntax_derange [select] == 0) {
  72. if (strncmp (& string [offset], syntax_end [select], strlen (syntax_end [select])) == 0) {
  73. * length = offset + (int) strlen (syntax_end [select]);
  74. goto finished;
  75. }
  76. } else {
  77. subset = 0;
  78. if (strcmp (syntax_end [select], "") == 0) {
  79. break;
  80. } do {
  81. if (string [offset] == syntax_end [select] [subset]) {
  82. * length = offset;
  83. goto finished;
  84. }
  85. } while (++subset != (int) strlen (syntax_end [select]));
  86. }
  87. }
  88. finished:
  89. return (select);
  90. }
  91. #define COLOUR_GREY (0)
  92. #define COLOUR_WHITE (1)
  93. #define COLOUR_BLUE (2)
  94. #define COLOUR_YELLOW (3)
  95. #define COLOUR_CYAN (4)
  96. #define COLOUR_PINK (5)
  97. #define COLOUR_LMAO (7)
  98. static void syntax_c (void) {
  99. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  100. char * keywords [] = {
  101. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  102. "do", "while", "for", "continue", "switch", "case", "default", "break",
  103. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  104. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  105. };
  106. int word;
  107. syntax_rule (0, 0, "/*", "*/", '\0', COLOUR_GREY);
  108. syntax_rule (0, 0, "//", "\n", '\0', COLOUR_GREY);
  109. syntax_rule (0, 0, "#", "\n", '\\', COLOUR_LMAO);
  110. syntax_rule (0, 0, "'", "'", '\\', COLOUR_PINK);
  111. syntax_rule (0, 0, "\"", "\"", '\\', COLOUR_PINK);
  112. for (word = 0; word != sizeof (keywords) / sizeof (keywords [0]); ++word) {
  113. syntax_rule (0, 1, keywords [word], separators, '\0', COLOUR_YELLOW);
  114. }
  115. syntax_rule (1, 0, "()[]{}", "", '\0', COLOUR_BLUE);
  116. syntax_rule (1, 0, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN);
  117. syntax_rule (1, 1, "0123456789", separators, '\0', COLOUR_PINK);
  118. syntax_rule (1, 1, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE);
  119. syntax_rule (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE);
  120. syntax_rule (1, 1, "_", separators, '\0', COLOUR_WHITE);
  121. }
  122. static void render_c_code (char * string,
  123. int x,
  124. int y) {
  125. int select, length = 0, offset, reset_x;
  126. reset_x = x;
  127. for (offset = 0; offset < (int) strlen (string); offset += length) {
  128. char substring [1024] = "";
  129. int i = 0, square = 16;
  130. Color colour = RED;
  131. select = syntax_loop (& string [offset], & length);
  132. strncpy (substring, & string [offset], (unsigned long int) length);
  133. switch (syntax_colour [select]) {
  134. case COLOUR_GREY: colour = GRAY; break;
  135. case COLOUR_WHITE: colour = WHITE; break;
  136. case COLOUR_BLUE: colour = BLUE; break;
  137. case COLOUR_YELLOW: colour = YELLOW; break;
  138. case COLOUR_CYAN: colour = PURPLE; break;
  139. case COLOUR_PINK: colour = RED; break;
  140. case COLOUR_LMAO: colour = GREEN; break;
  141. default: colour = GREEN; break;
  142. }
  143. for (i = 0; i < (int) strlen (substring); ++i) {
  144. if (substring [i] == '\n') {
  145. y += square;
  146. x = reset_x;
  147. } else if (substring [i] == '\t') {
  148. x += square * 4;
  149. } else {
  150. DrawTextCodepoint (GetFontDefault (), substring [i], (Vector2) { x, y }, square, colour);
  151. x += square;
  152. }
  153. }
  154. if (y > 900) {
  155. return;
  156. }
  157. }
  158. }