More cleaning up.

- Brought remaining files in line with style guides, and improved comments.
This commit is contained in:
Barry Kane 2023-02-15 22:09:21 +00:00
parent f31f0c79a5
commit f5cb3ad16e
7 changed files with 105 additions and 90 deletions

View File

@ -124,7 +124,7 @@ void * outputThreadHandler(void * parameters)
{ {
break; break;
} }
if (&connectedPlayers[index] == message->recipients[targetIndex]) if (&connectedPlayers[index] == message->recipients[sentCount])
{ {
sentCount++; sentCount++;
messageSend(tlssessions[index], message->content); messageSend(tlssessions[index], message->content);
@ -156,7 +156,7 @@ void userInputSanatize(char * inputString, int length)
inputString[length - 1] = '\0'; inputString[length - 1] = '\0';
} }
// Sanatize user names so they display correctly; // Sanatize user names so they display correctly:
void userNameSanatize(char * inputString, int length) void userNameSanatize(char * inputString, int length)
{ {
for(int index = 0; index <= length; index++) for(int index = 0; index <= length; index++)

View File

@ -11,7 +11,7 @@
static inline void deallocateListNode(listNode * node, listDataType type) static inline void deallocateListNode(listNode * node, listDataType type)
{ {
// Delete the node: // Delete the node:
switch(type) switch (type)
{ {
case PLAYER: case PLAYER:
{ {
@ -51,7 +51,7 @@ list * createList(listDataType type)
newList->itemCount = 0; newList->itemCount = 0;
newList->head = NULL; newList->head = NULL;
newList->tail = NULL; newList->tail = NULL;
// Return the new list: // Return the new list:
return newList; return newList;
} }
@ -60,7 +60,7 @@ list * createList(listDataType type)
int destroyList(list ** list) int destroyList(list ** list)
{ {
// Check if the list is empty: // Check if the list is empty:
if((*list)->itemCount == 0) if ((*list)->itemCount == 0)
{ {
free(*list); free(*list);
list = NULL; list = NULL;
@ -68,7 +68,7 @@ int destroyList(list ** list)
} }
else else
{ {
while((*list)->itemCount > 0) while ((*list)->itemCount > 0)
{ {
removeFromList((*list), (*list)->type, (*list)->itemCount - 1); removeFromList((*list), (*list)->type, (*list)->itemCount - 1);
} }
@ -82,13 +82,13 @@ int destroyList(list ** list)
listData * getFromList(list * list, size_t listIndex) listData * getFromList(list * list, size_t listIndex)
{ {
// Check that we were given a valid index: // Check that we were given a valid index:
if(listIndex > (list->itemCount - 1)) if (listIndex > (list->itemCount - 1))
{ {
perror("Invalid index specified.\n"); perror("Invalid index specified.\n");
return NULL; return NULL;
} }
// Return the head if index is 0: // Return the head if index is 0:
else if(listIndex == 0) else if (listIndex == 0)
{ {
return &(list->head->data); return &(list->head->data);
} }
@ -96,7 +96,7 @@ listData * getFromList(list * list, size_t listIndex)
else else
{ {
listNode * currentNode = list->head; listNode * currentNode = list->head;
while(listIndex-- > 0) while (listIndex-- > 0)
{ {
currentNode = currentNode->next; currentNode = currentNode->next;
} }
@ -108,23 +108,23 @@ listData * getFromList(list * list, size_t listIndex)
listNode * getNodeFromList(list * list, size_t listIndex) listNode * getNodeFromList(list * list, size_t listIndex)
{ {
// Check that we were given a valid index: // Check that we were given a valid index:
if(listIndex > (list->itemCount - 1)) if (listIndex > (list->itemCount - 1))
{ {
perror("Invalid index specified.\n"); perror("Invalid index specified.\n");
return NULL; return NULL;
} }
// Return the head if index is 0: // Return the head if index is 0:
else if(listIndex == 0) else if (listIndex == 0)
{ {
return list->head; return list->head;
} }
// Loop through the entries in the list until we get to the right one: // Loop through the entries in the list until we get to the right one:
else else
{ {
if((list->itemCount / 2) < listIndex) if ((list->itemCount / 2) < listIndex)
{ {
listNode * currentNode = list->tail; listNode * currentNode = list->tail;
while(listIndex-- > 0) while (listIndex-- > 0)
{ {
currentNode = currentNode->previous; currentNode = currentNode->previous;
} }
@ -133,7 +133,7 @@ listNode * getNodeFromList(list * list, size_t listIndex)
else else
{ {
listNode * currentNode = list->head; listNode * currentNode = list->head;
while(listIndex-- > 0) while (listIndex-- > 0)
{ {
currentNode = currentNode->next; currentNode = currentNode->next;
} }
@ -146,14 +146,14 @@ listNode * getNodeFromList(list * list, size_t listIndex)
listNode * addToList(list * list, void * data, listDataType type) listNode * addToList(list * list, void * data, listDataType type)
{ {
// Check the type: // Check the type:
if(type != list->type) if (type != list->type)
{ {
fprintf(stderr, "Not the correct type for this list.\n"); fprintf(stderr, "Not the correct type for this list.\n");
return NULL; return NULL;
} }
// If this is the first item in the list: // If this is the first item in the list:
if(list->itemCount == 0) if (list->itemCount == 0)
{ {
// Allocate the new node for the list: // Allocate the new node for the list:
list->head = calloc(1, sizeof(listNode)); list->head = calloc(1, sizeof(listNode));
@ -164,7 +164,7 @@ listNode * addToList(list * list, void * data, listDataType type)
list->tail = list->head; list->tail = list->head;
// Add the data to the new node: // Add the data to the new node:
switch(type) switch (type)
{ {
case PATH: case PATH:
{ {
@ -194,7 +194,7 @@ listNode * addToList(list * list, void * data, listDataType type)
list->tail->next = calloc(1, sizeof(listNode)); list->tail->next = calloc(1, sizeof(listNode));
// Add the data to the new node: // Add the data to the new node:
switch(type) switch (type)
{ {
case PATH: case PATH:
{ {
@ -235,26 +235,26 @@ listNode * addToList(list * list, void * data, listDataType type)
listNode * insertIntoList(list * list, void * data, listDataType type, size_t listIndex) listNode * insertIntoList(list * list, void * data, listDataType type, size_t listIndex)
{ {
// Check that the types are correct: // Check that the types are correct:
if(list->type != type) if (list->type != type)
{ {
fprintf(stderr, "Types do not match.\n"); fprintf(stderr, "Types do not match.\n");
return NULL; return NULL;
} }
// Handle the special case of adding to the end of the list: // Handle the special case of adding to the end of the list:
if(listIndex == (list->itemCount - 1)) if (listIndex == (list->itemCount - 1))
{ {
return addToList(list, data, type); return addToList(list, data, type);
} }
// Handle the special case of adding to the beginning of the list: // Handle the special case of adding to the beginning of the list:
if(listIndex == 0) if (listIndex == 0)
{ {
// Create the new node: // Create the new node:
listNode * newNode = calloc(1, sizeof(listNode)); listNode * newNode = calloc(1, sizeof(listNode));
// Add the data to the node: // Add the data to the node:
switch(type) switch (type)
{ {
case PATH: case PATH:
{ {
@ -290,7 +290,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
} }
// Check that the index is valid: // Check that the index is valid:
if(listIndex > (list->itemCount - 1)) if (listIndex > (list->itemCount - 1))
{ {
fprintf(stderr, "Index is invalid for the list.\n"); fprintf(stderr, "Index is invalid for the list.\n");
return NULL; return NULL;
@ -313,7 +313,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
previousNode->next->previous = previousNode; previousNode->next->previous = previousNode;
// Add the data to the node: // Add the data to the node:
switch(type) switch (type)
{ {
case PATH: case PATH:
{ {
@ -344,7 +344,7 @@ listNode * insertIntoList(list * list, void * data, listDataType type, size_t li
bool deleteFromList(list * list, void * data, listDataType type) bool deleteFromList(list * list, void * data, listDataType type)
{ {
size_t index = 0; size_t index = 0;
if(getIndexFromList(list, data, type, &index) == false) if (getIndexFromList(list, data, type, &index) == false)
{ {
return false; return false;
} }
@ -359,19 +359,19 @@ bool deleteFromList(list * list, void * data, listDataType type)
int removeFromList(list * list, listDataType type, size_t listIndex) int removeFromList(list * list, listDataType type, size_t listIndex)
{ {
// Check that we're removing the correct type: // Check that we're removing the correct type:
if(list->type != type) if (list->type != type)
{ {
return -1; return -1;
} }
// Check the list index is valid: // Check the list index is valid:
if(listIndex > list->itemCount - 1) if (listIndex > list->itemCount - 1)
{ {
return -2; return -2;
} }
// The first node in the list: // The first node in the list:
if(listIndex == 0) if (listIndex == 0)
{ {
// Get the current head and move the list's head on: // Get the current head and move the list's head on:
listNode * oldHead = list->head; listNode * oldHead = list->head;
@ -379,7 +379,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
// If we haven't removed the last item, set the previous pointer // If we haven't removed the last item, set the previous pointer
// in the new head to null. // in the new head to null.
if(list->head != NULL) if (list->head != NULL)
{ {
list->head->previous = NULL; list->head->previous = NULL;
} }
@ -392,7 +392,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
return list->itemCount; return list->itemCount;
} }
// The last node in the list: // The last node in the list:
else if(listIndex == (list->itemCount - 1)) else if (listIndex == (list->itemCount - 1))
{ {
// Move the tail up by one: // Move the tail up by one:
list->tail = list->tail->previous; list->tail = list->tail->previous;
@ -430,7 +430,7 @@ int removeFromList(list * list, listDataType type, size_t listIndex)
bool getIndexFromList(list * list, void * data, listDataType type, size_t * index) bool getIndexFromList(list * list, void * data, listDataType type, size_t * index)
{ {
// Check the list types are the same: // Check the list types are the same:
if(list->type == type) if (list->type == type)
{ {
fprintf(stderr, "List types do not match.\n"); fprintf(stderr, "List types do not match.\n");
return false; return false;
@ -438,11 +438,11 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
for(*index = 0; *index < list->itemCount; *index += 1) for(*index = 0; *index < list->itemCount; *index += 1)
{ {
switch(type) switch (type)
{ {
case AREA: case AREA:
{ {
if(getFromList(list, *index)->area == data) if (getFromList(list, *index)->area == data)
{ {
return true; return true;
} }
@ -450,7 +450,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
} }
case PLAYER: case PLAYER:
{ {
if(getFromList(list, *index)->player == data) if (getFromList(list, *index)->player == data)
{ {
return true; return true;
} }
@ -458,7 +458,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
} }
case PATH: case PATH:
{ {
if(getFromList(list, *index)->path == data) if (getFromList(list, *index)->path == data)
{ {
return true; return true;
} }
@ -466,7 +466,7 @@ bool getIndexFromList(list * list, void * data, listDataType type, size_t * inde
} }
case SKILL: case SKILL:
{ {
if(getFromList(list, *index)->skill == data) if (getFromList(list, *index)->skill == data)
{ {
return true; return true;
} }

View File

@ -15,6 +15,7 @@ typedef struct playerSkill playerSkill;
// -=[ Data Structures ]=-: // -=[ Data Structures ]=-:
// ======================== // ========================
// An enum of the possible data types that can be stored in a list:
typedef enum listDataType typedef enum listDataType
{ {
PATH, PATH,
@ -23,6 +24,7 @@ typedef enum listDataType
SKILL SKILL
} listDataType; } listDataType;
// A union containing a pointers to all data types that can be stored in a list:
typedef union listData typedef union listData
{ {
playerPath * path; playerPath * path;
@ -30,7 +32,8 @@ typedef union listData
playerInfo * player; playerInfo * player;
playerSkill * skill; playerSkill * skill;
} listData; } listData;
// A doubly linked node for the linked list type:
typedef struct listNode listNode; typedef struct listNode listNode;
typedef struct listNode typedef struct listNode
{ {
@ -39,6 +42,7 @@ typedef struct listNode
listNode * previous; listNode * previous;
} listNode; } listNode;
// A header structure for the list containing the length, head, tail, and type of the list.
typedef struct list typedef struct list
{ {
listDataType type; listDataType type;

View File

@ -11,7 +11,7 @@
// Create a new skill and add it to the global skill list: // Create a new skill and add it to the global skill list:
listNode * createSkill(list * globalSkillList, char * skillName, int skillNameLength, bool trainedSkill) listNode * createSkill(list * globalSkillList, char * skillName, int skillNameLength, bool trainedSkill)
{ {
if(skillNameLength >= 32) if (skillNameLength >= 32)
{ {
fprintf(stderr, "Skill name is too long. Please shorten the name and try again.\n"); fprintf(stderr, "Skill name is too long. Please shorten the name and try again.\n");
return NULL; return NULL;
@ -35,9 +35,9 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
// Check if the skill exists in the game: // Check if the skill exists in the game:
size_t globalIndex = 0; size_t globalIndex = 0;
bool skillExists = false; bool skillExists = false;
while(globalIndex < globalSkillList->itemCount) while (globalIndex < globalSkillList->itemCount)
{ {
if(strncmp(skillName, getFromList(globalSkillList, globalIndex)->skill->skillName, skillNameLength) == 0) if (strncmp(skillName, getFromList(globalSkillList, globalIndex)->skill->skillName, skillNameLength) == 0)
{ {
skillExists = true; skillExists = true;
break; break;
@ -45,7 +45,7 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
globalIndex++; globalIndex++;
} }
if(!skillExists) if (!skillExists)
{ {
fprintf(stderr, "Skill doesn't exist in skill list.\n"); fprintf(stderr, "Skill doesn't exist in skill list.\n");
return -1; return -1;
@ -54,16 +54,16 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
// Check if the player has the skill: // Check if the player has the skill:
size_t playerIndex = 0; size_t playerIndex = 0;
bool playerHasSkill = false; bool playerHasSkill = false;
while(playerIndex < targetPlayer->skills->itemCount) while (playerIndex < targetPlayer->skills->itemCount)
{ {
if(strncmp(skillName, getFromList(targetPlayer->skills, playerIndex)->skill->skillName, skillNameLength) == 0) if (strncmp(skillName, getFromList(targetPlayer->skills, playerIndex)->skill->skillName, skillNameLength) == 0)
{ {
playerHasSkill = true; playerHasSkill = true;
break; break;
} }
playerIndex++; playerIndex++;
} }
if(playerHasSkill) if (playerHasSkill)
{ {
getFromList(targetPlayer->skills, playerIndex)->skill->skillPoints++; getFromList(targetPlayer->skills, playerIndex)->skill->skillPoints++;
} }
@ -85,7 +85,7 @@ int takeSkill(list * globalSkillList, char * skillName, int skillNameLength, pla
coreStat getCoreStatFromString(char * inputString, int stringLength) coreStat getCoreStatFromString(char * inputString, int stringLength)
{ {
// Check we've got a long enough string to fit a stat: // Check we've got a long enough string to fit a stat:
if(stringLength < 4) if (stringLength < 4)
{ {
return INVALID; return INVALID;
} }
@ -99,11 +99,11 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// If we have a string that's at most just the stat name plus a null character, or // If we have a string that's at most just the stat name plus a null character, or
// a dirtier string, we can check in a better order and ignore impossibilites: // a dirtier string, we can check in a better order and ignore impossibilites:
if(stringLength < 9) if (stringLength < 9)
{ {
if(stringLength <= 4) if (stringLength <= 4)
{ {
if(strncmp(string, "wits", 4) == 0) if (strncmp(string, "wits", 4) == 0)
{ {
free(string); free(string);
return WITS; return WITS;
@ -115,19 +115,19 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
} }
} }
// Hopefully one of the seven letter long ones: // Hopefully one of the seven letter long ones:
else if(stringLength <= 7) else if (stringLength <= 7)
{ {
if(strncmp(string, "strength", 7) == 0) if (strncmp(string, "strength", 7) == 0)
{ {
free(string); free(string);
return STRENGTH; return STRENGTH;
} }
else if(strncmp(string, "dexerity", 7) == 0) else if (strncmp(string, "dexerity", 7) == 0)
{ {
free(string); free(string);
return DEXERITY; return DEXERITY;
} }
if(strncmp(string, "wits", 4) == 0) if (strncmp(string, "wits", 4) == 0)
{ {
free(string); free(string);
return WITS; return WITS;
@ -141,27 +141,27 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// Hopefully one of the 8 letter long stats: // Hopefully one of the 8 letter long stats:
else else
{ {
if(strncmp(string, "intellect", 8) == 0) if (strncmp(string, "intellect", 8) == 0)
{ {
free(string); free(string);
return INTELLECT; return INTELLECT;
} }
else if(strncmp(string, "endurance", 8) == 0) else if (strncmp(string, "endurance", 8) == 0)
{ {
free(string); free(string);
return ENDURANCE; return ENDURANCE;
} }
else if(strncmp(string, "strength", 7) == 0) else if (strncmp(string, "strength", 7) == 0)
{ {
free(string); free(string);
return STRENGTH; return STRENGTH;
} }
else if(strncmp(string, "dexerity", 7) == 0) else if (strncmp(string, "dexerity", 7) == 0)
{ {
free(string); free(string);
return DEXERITY; return DEXERITY;
} }
if(strncmp(string, "wits", 4) == 0) if (strncmp(string, "wits", 4) == 0)
{ {
free(string); free(string);
return WITS; return WITS;
@ -176,27 +176,27 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
// Worst case, it's definitely a dirty string, compare them all: // Worst case, it's definitely a dirty string, compare them all:
else else
{ {
if(strncmp(string, "wits", 4) == 0) if (strncmp(string, "wits", 4) == 0)
{ {
free(string); free(string);
return WITS; return WITS;
} }
else if(strncmp(string, "intellect", 8) == 0) else if (strncmp(string, "intellect", 8) == 0)
{ {
free(string); free(string);
return INTELLECT; return INTELLECT;
} }
else if(strncmp(string, "strength", 7) == 0) else if (strncmp(string, "strength", 7) == 0)
{ {
free(string); free(string);
return STRENGTH; return STRENGTH;
} }
else if(strncmp(string, "endurance", 8) == 0) else if (strncmp(string, "endurance", 8) == 0)
{ {
free(string); free(string);
return ENDURANCE; return ENDURANCE;
} }
else if(strncmp(string, "dexerity", 7) == 0) else if (strncmp(string, "dexerity", 7) == 0)
{ {
free(string); free(string);
return DEXERITY; return DEXERITY;
@ -209,6 +209,7 @@ coreStat getCoreStatFromString(char * inputString, int stringLength)
} }
} }
// Deallocate a player's information including the skill lists and stats:
int deallocatePlayer(playerInfo * playerToDeallocate) int deallocatePlayer(playerInfo * playerToDeallocate)
{ {
// Deallocate the skill list: // Deallocate the skill list:

View File

@ -14,6 +14,7 @@ typedef struct playerPath playerPath;
typedef struct listNode listNode; typedef struct listNode listNode;
typedef struct list list; typedef struct list list;
// The basic information that needs to be stored for a player or creature's stats:
typedef struct statBlock typedef struct statBlock
{ {
// Levelling: // Levelling:
@ -36,6 +37,7 @@ typedef struct statBlock
int skillPoints; int skillPoints;
} statBlock; } statBlock;
// Information about a skill, including skill levels and modifiers for the player:
typedef struct playerSkill typedef struct playerSkill
{ {
char skillName[32]; char skillName[32];
@ -44,6 +46,7 @@ typedef struct playerSkill
bool trainedSkill; bool trainedSkill;
} playerSkill; } playerSkill;
// Information about a single player's character:
typedef struct playerInfo typedef struct playerInfo
{ {
char playerName[32]; char playerName[32];
@ -52,6 +55,7 @@ typedef struct playerInfo
list * skills; list * skills;
} playerInfo; } playerInfo;
// An enum of the main stats of the game:
typedef enum coreStat typedef enum coreStat
{ {
WITS, WITS,
@ -72,7 +76,7 @@ int takeSkillbyID(list * globalSkillList, int skillID, playerInfo * targetPlayer
// Take a string containing a core stat name and return the core stat: // Take a string containing a core stat name and return the core stat:
coreStat getCoreStatFromString(char * string, int stringLength); coreStat getCoreStatFromString(char * string, int stringLength);
// Deallocate a player: // Deallocate a player's information including the skill lists and stats:
int deallocatePlayer(playerInfo * playerToDeallocate); int deallocatePlayer(playerInfo * playerToDeallocate);
#endif #endif

View File

@ -5,10 +5,11 @@
#include <unistd.h> #include <unistd.h>
#include <ncurses.h> #include <ncurses.h>
void slowPrint(char * stringToPrint, int delay) // A character by character print, similar to a serial terminal with lower baud rate:
void slowPrint(const char * stringToPrint, int delay)
{ {
int characterIndex = 0; int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0') while (stringToPrint[characterIndex] != '\0')
{ {
putchar(stringToPrint[characterIndex]); putchar(stringToPrint[characterIndex]);
// Flush the buffer so there's no line buffering. // Flush the buffer so there's no line buffering.
@ -18,14 +19,15 @@ void slowPrint(char * stringToPrint, int delay)
} }
} }
void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded) // The same, altered to work with ncurses:
void slowPrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded)
{ {
int characterIndex = 0; int characterIndex = 0;
if(bolded) if (bolded)
{ {
wattron(window, A_BOLD); wattron(window, A_BOLD);
} }
while(stringToPrint[characterIndex] != '\0') while (stringToPrint[characterIndex] != '\0')
{ {
waddch(window, stringToPrint[characterIndex]); waddch(window, stringToPrint[characterIndex]);
// Refresh the ncurses screen. // Refresh the ncurses screen.
@ -33,17 +35,18 @@ void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bol
usleep(delay); usleep(delay);
characterIndex++; characterIndex++;
} }
if(bolded) if (bolded)
{ {
wattroff(window, A_BOLD); wattroff(window, A_BOLD);
} }
wrefresh(window); wrefresh(window);
} }
void bruteforcePrint(char * stringToPrint, int delay) // A character by character "brute-force" print, similar to Hollywood hacking scenes:
void bruteforcePrint(const char * stringToPrint, int delay)
{ {
unsigned int characterIndex = 0; unsigned int characterIndex = 0;
while(stringToPrint[characterIndex] != '\0') while (stringToPrint[characterIndex] != '\0')
{ {
for(unsigned char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++) for(unsigned char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++)
{ {
@ -58,14 +61,15 @@ void bruteforcePrint(char * stringToPrint, int delay)
} }
} }
void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded) // The same, altered to work with ncurses:
void bruteforcePrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded)
{ {
int characterIndex = 0; int characterIndex = 0;
if(bolded) if (bolded)
{ {
wattron(window, A_BOLD); wattron(window, A_BOLD);
} }
while(stringToPrint[characterIndex] != '\0') while (stringToPrint[characterIndex] != '\0')
{ {
for(char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++) for(char currentCharacter = 32; currentCharacter <= stringToPrint[characterIndex]; currentCharacter++)
{ {
@ -78,19 +82,20 @@ void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bo
waddch(window, stringToPrint[characterIndex]); waddch(window, stringToPrint[characterIndex]);
characterIndex++; characterIndex++;
} }
if(bolded) if (bolded)
{ {
wattroff(window, A_BOLD); wattroff(window, A_BOLD);
} }
wrefresh(window); wrefresh(window);
} }
// Word-wrap a string to a given width:
void wrapString(char * stringToWrap, int stringLength, int screenWidth) void wrapString(char * stringToWrap, int stringLength, int screenWidth)
{ {
int characterCount = 0; int characterCount = 0;
for(int index = 0; index < stringLength; index++) for(int index = 0; index < stringLength; index++)
{ {
if(stringToWrap[index] == '\n') if (stringToWrap[index] == '\n')
{ {
characterCount = 0; characterCount = 0;
} }
@ -98,13 +103,13 @@ void wrapString(char * stringToWrap, int stringLength, int screenWidth)
{ {
characterCount++; characterCount++;
} }
if(characterCount == screenWidth) if (characterCount == screenWidth)
{ {
while(!isspace(stringToWrap[index]) && index > 0) while (!isspace(stringToWrap[index]) && index > 0)
{ {
index--; index--;
} }
if(index == 0) if (index == 0)
{ {
return; return;
} }

View File

@ -5,20 +5,23 @@
#include <stdio.h> #include <stdio.h>
#include <ncurses.h> #include <ncurses.h>
// A character by character print, similar to a serial terminal with lower baud rate. // A character by character print, similar to a serial terminal with lower baud rate:
void slowPrint(char * stringToPrint, int delay); void slowPrint(const char * stringToPrint, int delay);
// The same, altered to work with ncurses. // The same, altered to work with ncurses:
void slowPrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded); void slowPrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded);
// A character by character "brute-force" print, similar to Hollywood hacking scenes. // A character by character "brute-force" print, similar to Hollywood hacking scenes:
void bruteforcePrint(char * stringToPrint, int delay); void bruteforcePrint(const char * stringToPrint, int delay);
// The same, altered to work with ncurses. // The same, altered to work with ncurses:
void bruteforcePrintNcurses(char * stringToPrint, int delay, WINDOW * window, bool bolded); void bruteforcePrintNcurses(const char * stringToPrint, int delay, WINDOW * window, bool bolded);
// Word-wrap a string to a given width:
void wrapString(char * stringToWrap, int stringLength, int screenWidth);
// A string containing an ASCII art version of the Silverkin Industries logo. // A string containing an ASCII art version of the Silverkin Industries logo.
char * logostring = const char * logostring =
" ///////\n" " ///////\n"
" //////////////////////////////////////////\n" " //////////////////////////////////////////\n"
" ///////////////////////////////////////////////////////////\n" " ///////////////////////////////////////////////////////////\n"
@ -32,6 +35,4 @@ char * logostring =
" # # # # # # # # ## # ### # # ## //\n" " # # # # # # # # ## # ### # # ## //\n"
" # # ### ##### ##### ### # # # # #### ### /\n"; " # # ### ##### ##### ### # # # # #### ### /\n";
void wrapString(char * stringToWrap, int stringLength, int screenWidth);
#endif #endif