Umorna -- Tiny game written to test 'chads' library, it uses assets from itch.io...
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.6KB

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