diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0822db5 --- /dev/null +++ b/.clang-format @@ -0,0 +1,225 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/map.d/21x21.maze b/maps/21x21.maze similarity index 100% rename from map.d/21x21.maze rename to maps/21x21.maze diff --git a/map.d/33x33.maze b/maps/33x33.maze similarity index 100% rename from map.d/33x33.maze rename to maps/33x33.maze diff --git a/map.d/49x97.maze b/maps/49x97.maze similarity index 100% rename from map.d/49x97.maze rename to maps/49x97.maze diff --git a/map.d/65x65.maze b/maps/65x65.maze similarity index 100% rename from map.d/65x65.maze rename to maps/65x65.maze diff --git a/maze.c b/maze.c index 68ccccf..bfb0024 100644 --- a/maze.c +++ b/maze.c @@ -15,296 +15,314 @@ #define UP 3 typedef struct termios tstate_t; + typedef uint8_t dir_t; + +typedef struct entity_t { + size_t X; + size_t Y; +} entity_t; + typedef struct maze_t { - size_t X_SIZE; - size_t Y_SIZE; - size_t P_X_CORD; - size_t P_Y_CORD; - size_t S_X_CORD; - size_t S_Y_CORD; - size_t F_X_CORD; - size_t F_Y_CORD; - uint8_t **PNTR; + size_t SIZE_X; + size_t SIZE_Y; + entity_t *PLAYER; + entity_t *START; + entity_t *FINISH; + uint8_t **PNTR; } maze_t; tstate_t TSTATE; +size_t TERM_COL; +size_t TERM_ROW; maze_t *MAZE; bool QUIT = false; bool WINSTATE = false; -size_t TERM_COL; -size_t TERM_ROW; - void get_term_size() { - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - TERM_ROW = w.ws_row; - TERM_COL = w.ws_col; + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + TERM_ROW = w.ws_row; + TERM_COL = w.ws_col; } -void exit_on_inadequate_space() { - if (TERM_COL < 2 * MAZE->X_SIZE || TERM_ROW < MAZE->Y_SIZE) { - printf("Terminal is too small to display the whole maze.\n\ +void term_too_small_handler() { + if (TERM_COL < 2 * MAZE->SIZE_X || TERM_ROW < MAZE->SIZE_Y) { + printf("Terminal is too small to display the whole maze.\n\ Either make the terminal window bigger, or use a smaller maze map.\n"); - exit(0); - } + exit(0); + } } void get_maze_size(const char *path) { - FILE *file; - if (!(file = fopen(path, "r"))) { - printf("Couldn't find map file. Check file path.\n"); - exit(0); - } + FILE *file; + if (!(file = fopen(path, "r"))) { + printf("Couldn't find map file. Check file path.\n"); + exit(0); + } - char ch; - size_t x_cnt = 0; - size_t y_cnt = 0; - bool cnt_x = true; - while ((ch = getc(file)) != EOF) { - if (ch == '\n') { - ++y_cnt; - cnt_x = false; - } else if (cnt_x) { - ++x_cnt; - } + char ch; + size_t x_cnt = 0; + size_t y_cnt = 0; + bool cnt_x = true; + while ((ch = getc(file)) != EOF) { + if (ch == '\n') { + ++y_cnt; + cnt_x = false; + } else if (cnt_x) { + ++x_cnt; } + } - MAZE->X_SIZE = x_cnt; - MAZE->Y_SIZE = y_cnt; + MAZE->SIZE_X = x_cnt; + MAZE->SIZE_Y = y_cnt; } #define restore_cursor() printf("\033[u") #define save_cursor() printf("\033[s") -#define zero_cursor() printf("\033[0;0H"); fflush(stdout) +#define zero_cursor() \ + printf("\033[0;0H"); \ + fflush(stdout) void move(dir_t dir) { - switch (dir) { - case 1: - if (MAZE->P_X_CORD != 0 && MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD - 1] != 1) { - if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2; - } else { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0; - } - --MAZE->P_X_CORD; - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4; - } - break; - case 2: - if (MAZE->P_Y_CORD != MAZE->Y_SIZE - 1 && MAZE->PNTR[MAZE->P_Y_CORD + 1][MAZE->P_X_CORD] != 1) { - if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2; - } else { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0; - } - ++MAZE->P_Y_CORD; - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4; - } - break; - case 3: - if (MAZE->P_Y_CORD != 0 && MAZE->PNTR[MAZE->P_Y_CORD - 1][MAZE->P_X_CORD] != 1) { - if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2; - } else { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0; - } - --MAZE->P_Y_CORD; - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4; - } - break; - case 4: - if (MAZE->P_X_CORD != MAZE->X_SIZE - 1 && MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD + 1] != 1) { - if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2; - } else { - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0; - } - ++MAZE->P_X_CORD; - MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4; - } - break; + switch (dir) { + case 1: + if (MAZE->PLAYER->X != 0 && + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X - 1] != 1) { + if (MAZE->PLAYER->X == MAZE->START->X && + MAZE->PLAYER->Y == MAZE->START->Y) { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2; + } else { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0; + } + --MAZE->PLAYER->X; + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4; } + break; + case 2: + if (MAZE->PLAYER->Y != MAZE->SIZE_Y - 1 && + MAZE->PNTR[MAZE->PLAYER->Y + 1][MAZE->PLAYER->X] != 1) { + if (MAZE->PLAYER->X == MAZE->START->X && + MAZE->PLAYER->Y == MAZE->START->Y) { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2; + } else { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0; + } + ++MAZE->PLAYER->Y; + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4; + } + break; + case 3: + if (MAZE->PLAYER->Y != 0 && + MAZE->PNTR[MAZE->PLAYER->Y - 1][MAZE->PLAYER->X] != 1) { + if (MAZE->PLAYER->X == MAZE->START->X && + MAZE->PLAYER->Y == MAZE->START->Y) { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2; + } else { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0; + } + --MAZE->PLAYER->Y; + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4; + } + break; + case 4: + if (MAZE->PLAYER->X != MAZE->SIZE_X - 1 && + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X + 1] != 1) { + if (MAZE->PLAYER->X == MAZE->START->X && + MAZE->PLAYER->Y == MAZE->START->Y) { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2; + } else { + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0; + } + ++MAZE->PLAYER->X; + MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4; + } + break; + } } void free_map() { - for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) { - free(MAZE->PNTR[y_cord]); - } - free(MAZE->PNTR); - free(MAZE); + for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) { + free(MAZE->PNTR[y_cord]); + } + free(MAZE->PNTR); + free(MAZE); } void alloc_maze_buffer(const char *path) { - MAZE = (maze_t *)malloc(sizeof(maze_t)); - get_maze_size(path); - MAZE->PNTR = (uint8_t **)malloc(MAZE->Y_SIZE * sizeof(uint8_t *)); - for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) { - MAZE->PNTR[y_cord] = (uint8_t *)malloc(MAZE->X_SIZE * sizeof(uint8_t)); - } + MAZE = (maze_t *)malloc(sizeof(maze_t)); + MAZE->PLAYER = (entity_t *)malloc(sizeof(entity_t)); + MAZE->START = (entity_t *)malloc(sizeof(entity_t)); + MAZE->FINISH = (entity_t *)malloc(sizeof(entity_t)); + get_maze_size(path); + MAZE->PNTR = (uint8_t **)malloc(MAZE->SIZE_Y * sizeof(uint8_t *)); + for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) { + MAZE->PNTR[y_cord] = (uint8_t *)malloc(MAZE->SIZE_X * sizeof(uint8_t)); + } } void load_maze_to_buffer(const char *path) { - FILE *file; - if (!(file = fopen(path, "r"))) { - printf("Couldn't find map file. Check file path.\n"); - exit(0); - } + FILE *file; + if (!(file = fopen(path, "r"))) { + printf("Couldn't find map file. Check file path.\n"); + exit(0); + } - size_t y_cord = 0; - size_t x_cord = 0; + size_t y_cord = 0; + size_t x_cord = 0; - char cur; - while (EOF != (cur = getc(file))) { - if ('\n' == cur) { - x_cord = 0; - ++y_cord; - continue; - } else if ('4' == cur) { - MAZE->P_X_CORD = x_cord; - MAZE->P_Y_CORD = y_cord; - MAZE->S_X_CORD = x_cord; - MAZE->S_Y_CORD = y_cord; - } else if ('3' == cur) { - MAZE->F_X_CORD = x_cord; - MAZE->F_Y_CORD = y_cord; - } - MAZE->PNTR[y_cord][x_cord] = (uint8_t)(cur - (char)'0'); - ++x_cord; + char cur; + while (EOF != (cur = getc(file))) { + if ('\n' == cur) { + x_cord = 0; + ++y_cord; + continue; + } else if ('4' == cur) { + MAZE->PLAYER->X = x_cord; + MAZE->PLAYER->Y = y_cord; + MAZE->START->X = x_cord; + MAZE->START->Y = y_cord; + } else if ('3' == cur) { + MAZE->FINISH->X = x_cord; + MAZE->FINISH->Y = y_cord; } + MAZE->PNTR[y_cord][x_cord] = (uint8_t)(cur - (char)'0'); + ++x_cord; + } } void print() { - const char sprites[] = " H*XO"; - save_cursor(); - for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) { - for (size_t x_cord = 0; x_cord < MAZE->X_SIZE; ++x_cord) { - putchar(sprites[MAZE->PNTR[y_cord][x_cord]]); - if (x_cord < MAZE->X_SIZE - 1) { - putchar(' '); - } - } - if (y_cord < MAZE->Y_SIZE - 1) { - putchar('\n'); - } + const char sprites[] = " H*XO"; + save_cursor(); + for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) { + for (size_t x_cord = 0; x_cord < MAZE->SIZE_X; ++x_cord) { + putchar(sprites[MAZE->PNTR[y_cord][x_cord]]); + if (x_cord < MAZE->SIZE_X - 1) { + putchar(' '); + } } - restore_cursor(); + if (y_cord < MAZE->SIZE_Y - 1) { + putchar('\n'); + } + } + restore_cursor(); } void set_term_def(); void set_term_raw(); void clear() { - zero_cursor(); - save_cursor(); - for (size_t y_cord = 0; y_cord < TERM_ROW; ++y_cord) { - for (size_t x_cord = 0; x_cord < TERM_COL; ++x_cord) { - putchar(' '); - } - if (y_cord < MAZE->Y_SIZE - 1) { - putchar('\n'); - } + zero_cursor(); + save_cursor(); + for (size_t y_cord = 0; y_cord < TERM_ROW; ++y_cord) { + for (size_t x_cord = 0; x_cord < TERM_COL; ++x_cord) { + putchar(' '); } - restore_cursor(); + if (y_cord < MAZE->SIZE_Y - 1) { + putchar('\n'); + } + } + restore_cursor(); } void check_win() { - if (MAZE->P_Y_CORD == MAZE->F_Y_CORD && MAZE->P_X_CORD == MAZE->F_X_CORD) { - WINSTATE = true; - } + if (MAZE->PLAYER->Y == MAZE->FINISH->Y && + MAZE->PLAYER->X == MAZE->FINISH->X) { + WINSTATE = true; + } } // Reads keyboard input void *capture() { - set_term_raw(); // local function: Enable Raw Mode + set_term_raw(); // local function: Enable Raw Mode - char ch; - while ((ch = getchar()) != 'q') { - switch (ch) { - case 'w': - move(UP); - break; - case 'a': - move(LEFT); - break; - case 's': - move(DOWN); - break; - case 'd': - move(RIGHT); - break; - } - - check_win(); - if (WINSTATE) { - break; - } + char ch; + while ((ch = getchar()) != 'q') { + switch (ch) { + case 'w': + move(UP); + break; + case 'a': + move(LEFT); + break; + case 's': + move(DOWN); + break; + case 'd': + move(RIGHT); + break; } - QUIT = true; + check_win(); + if (WINSTATE) { + break; + } + } - return EXIT_SUCCESS; + QUIT = true; + + return EXIT_SUCCESS; } void *update() { - zero_cursor(); + zero_cursor(); + clear(); + while (!QUIT) { // When ESC is not pressed + print(); + usleep(41667); + } + + if (WINSTATE) { clear(); - while (!QUIT) { // When ESC is not pressed - print(); - usleep(41667); - } + printf("Congratulations! You have won the game.\n"); + } else { + clear(); + printf("Keyboard interrupt! Quitting now...\n"); + } - if (WINSTATE) { - clear(); - printf("Congratulations! You have won the game.\n"); - } else { - clear(); - printf("Keyboard interrupt! Quitting now...\n"); - } - - return EXIT_SUCCESS; + return EXIT_SUCCESS; } void set_term_raw() { - struct termios raw; - tcgetattr(STDIN_FILENO, &raw); // Save the state of the terminal to struct raw - // STDIN_FILENO is from - // tcgetattr() from + struct termios raw; + tcgetattr(STDIN_FILENO, &raw); // Save the state of the terminal to struct raw + // STDIN_FILENO is from + // tcgetattr() from - tcgetattr(STDIN_FILENO, &TSTATE); + tcgetattr(STDIN_FILENO, &TSTATE); - atexit(&set_term_def); // Revert to canonical mode when exiting the program - // atext() from - raw.c_lflag &= ~(ECHO | ICANON); // Turn off canonical mode - // Turn off ECHO mode so that keyboard is not - // printing to terminal - // ICANON and ECHO is bitflag. ~ is binary NOT operator + atexit(&set_term_def); // Revert to canonical mode when exiting the program + // atext() from + raw.c_lflag &= + ~(ECHO | ICANON); // Turn off canonical mode + // Turn off ECHO mode so that keyboard is not + // printing to terminal + // ICANON and ECHO is bitflag. ~ is binary NOT operator - tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); // Set the terminal to be in raw mode - // tcsetattr() from + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); // Set the terminal to be in raw + // mode tcsetattr() from } void set_term_def() { - tcsetattr(STDIN_FILENO, TCSAFLUSH, &TSTATE); // Set terminal to TSTATE state + tcsetattr(STDIN_FILENO, TCSAFLUSH, &TSTATE); // Set terminal to TSTATE state } int main(int argc, char *argv[]) { - if (argc > 1 && argc < 3) { - get_term_size(); - alloc_maze_buffer(argv[1]); - exit_on_inadequate_space(); - load_maze_to_buffer(argv[1]); + if (argc > 1 && argc < 3) { + get_term_size(); + alloc_maze_buffer(argv[1]); + term_too_small_handler(); + load_maze_to_buffer(argv[1]); - pthread_t update_pid, capture_pid; + pthread_t update_pid, capture_pid; - pthread_create(&update_pid, NULL, update, NULL); - pthread_create(&capture_pid, NULL, capture, NULL); + pthread_create(&update_pid, NULL, update, NULL); + pthread_create(&capture_pid, NULL, capture, NULL); - pthread_join(update_pid, NULL); - pthread_join(capture_pid, NULL); - } + pthread_join(update_pid, NULL); + pthread_join(capture_pid, NULL); + } - return 0; + return 0; }