Skip to content

neue-dev/Minesweeper-in-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MINEZ (in C)

1 Introduction

Sample gameplay (classic, easy)

minez

Sample gameplay (classic, difficult)

minez

Sample win screen (classic, difficult)

minez

Sample lose screem (classic, difficult)

minez

The home page

minez

The login page

minez

Welcome to a rough implementation of minesweeper... in C! Brought to you by MMMM.

2 Running the Program

Windows 10

If you are using Windows 10 and have not migrated to using the modern Windows Terminal (you're still using conhost), then the following commands should suffice to run the program. Otherwise, refer to the commands for Windows 11; the Windows Terminal has a different set of features which don't allow our program to do some important stuff (like resizing the console).

gcc main.c -o main
main run

Windows 11

Fortunately, even after migrating to using the Windows Terminal, the legacy console (conhost) is still accessible when running it directly using the conhost command (or, alternatively, looking for it manually in your Windows folder). After the console appears, just follow the same commands outlined for Windows 10.

conhost
gcc main.c -o main
main run

Unix-based

Need I say more?

gcc main.c -o main
./main run

NOTE: If you do not specify the run parameter after calling the main executable, you will be greated with the following message: compilation-warning To actually run the program, you must specify the run parameter.

As an added feature, one can also specify run dev instead; this will run an alternative executable used for debugging.

3 Project Structure

📦machine-project
 ┣ 📂build
 ┃ ┣ 📂.debug
 ┃ ┃ ┣ 📜debug.txt
 ┃ ┃ ┣ 📜log.unix.txt
 ┃ ┃ ┗ 📜log.win.txt
 ┃ ┣ 📜minesweeper.unix.o
 ┃ ┗ 📜minesweeper.win.exe
 ┣ 📂src
 ┃ ┣ 📂assets
 ┃ ┃ ┣ 📜body-font.asset.txt
 ┃ ┃ ┣ 📜header-font.asset.txt
 ┃ ┃ ┣ 📜icon.asset.txt
 ┃ ┃ ┣ 📜intro.asset.txt
 ┃ ┃ ┗ 📜menu.asset.txt
 ┃ ┣ 📂data
 ┃ ┃ ┣ 📜accounts.data.txt
 ┃ ┃ ┗ 📜themes.data.txt
 ┃ ┣ 📂game
 ┃ ┃ ┣ 📜account.class.h
 ┃ ┃ ┗ 📜field.obj.h
 ┃ ┣ 📂pages
 ┃ ┃ ┣ 📜account.page.c
 ┃ ┃ ┣ 📜custom.page.c
 ┃ ┃ ┣ 📜help.page.c
 ┃ ┃ ┣ 📜intro.page.c
 ┃ ┃ ┣ 📜menu.page.c
 ┃ ┃ ┣ 📜play.page.c
 ┃ ┃ ┗ 📜settings.page.c
 ┃ ┣ 📂utils
 ┃ ┃ ┣ 📂unix
 ┃ ┃ ┃ ┣ 📜utils.io.unix.h
 ┃ ┃ ┃ ┣ 📜utils.string.unix.h
 ┃ ┃ ┃ ┗ 📜utils.thread.unix.h
 ┃ ┃ ┣ 📂win
 ┃ ┃ ┃ ┣ 📜utils.io.win.h
 ┃ ┃ ┃ ┣ 📜utils.string.win.h
 ┃ ┃ ┃ ┗ 📜utils.thread.win.h
 ┃ ┃ ┣ 📜utils.aes.h
 ┃ ┃ ┣ 📜utils.asset.h
 ┃ ┃ ┣ 📜utils.buffer.h
 ┃ ┃ ┣ 📜utils.component.h
 ┃ ┃ ┣ 📜utils.debug.h
 ┃ ┃ ┣ 📜utils.event.h
 ┃ ┃ ┣ 📜utils.file.h
 ┃ ┃ ┣ 📜utils.graphics.h
 ┃ ┃ ┣ 📜utils.grid.h
 ┃ ┃ ┣ 📜utils.hashmap.h
 ┃ ┃ ┣ 📜utils.io.h
 ┃ ┃ ┣ 📜utils.math.h
 ┃ ┃ ┣ 📜utils.page.h
 ┃ ┃ ┣ 📜utils.queue.h
 ┃ ┃ ┣ 📜utils.string.h
 ┃ ┃ ┣ 📜utils.theme.h
 ┃ ┃ ┣ 📜utils.thread.h
 ┃ ┃ ┗ 📜utils.types.h
 ┃ ┣ 📜engine.c
 ┃ ┣ 📜events.c
 ┃ ┣ 📜minesweeper.c
 ┃ ┗ 📜minesweeper.dev.c
 ┣ 📜main
 ┣ 📜main.c
 ┣ 📜main.exe

NOTE: the README and its associated files are excluded here. Additionally, the .debug folder is not populated in the github repo but may contain additional files following the build process.

3.1 build, src, and main.c

The three main components of the file tree are the build and src folders, alongside the main.c file.

The main.c file basically just acts as a "makefile" for the entire project (since we couldn't really use a makefile without enforcing people to install it for our project).

The build folder contains the resulting executables produced by main.c, which depend on the environment running the build step. Any additional logs and debug files are also created here and may be viewed in a text editor.

The src folder contains the actual source code that enables the game to operate. Any state and data files relevant to the program might also be created here (for instance, files containing information about user accounts, file containing program settings, and files containing text art assets and theme options).

3.2 src/utils

This folder basically contains a library of utilities that enable the game to function. None of the files here implement any of the game logic, and this library can be used for another program entirely. Nonetheless, it is important to note that some of the files contained in this folder depend on other utility files also included therein.

3.3 src/pages, src/events.c, src/engine.c, and src/game

3.4 src/minesweeper.c and src/minesweeper.dev.c

3.5 src/assets and src/data

4 Implementation Outline

4.1 Function Table

# Functions in engine.c Description
1
void Engine_init(Engine *this) 
Initializes the engine. This function is annotated differently because it does a lot of things.


2
void Engine_exit(Engine *this) 
Do some clean up after the entire program runs. Frees whatever was allocated.


3
void Engine_main(
p_obj pArgs_Engine,
int tArg_NULL
) 
The main thread of the engine.


4
int Engine_getState(Engine *this) 
Returns the state of the engine. Returns a 1 when the engine is currently running. Returns a 0 when all its processes have exited.


# Functions in events.c Description
5
char EventListener_keyPressed(void) 
Listens for key presses. This function can wait for a key press or just return 0 when nothing happens. In this case, it waits for a key press but the implementation can vary.


6
void EventHandler_keyPressed(
p_obj pArgs_Event,
p_obj pArgs2_EventStore
) 
Handles key presses. Basically, what happens after a key is pressed.


# Functions in minesweeper.c Description
# Functions in minesweeper.dev.c Description

# Functions in settings.c Description
7
void Settings_init(
	EventStore *pSharedEventStore,
	ThemeManager *pSharedThemeManager
) 
Sets certain keybinds in the event store. Sets the current theme in the theme manager.


8
void Settings_getKeybinds(
int *dKeybindCount,
char *sKeybindArray[]
) 
Tells us what keybinds we have for the game. This function assumes that there is enough space on the output buffer.


9
char Settings_getGameMoveUp(EventStore *pSharedEventStore) 
Returns the key bound to the move up functionality.


10
char Settings_getGameMoveDown(EventStore *pSharedEventStore) 
Returns the key bound to the move down functionality.


11
char Settings_getGameMoveLeft(EventStore *pSharedEventStore) 
Returns the key bound to the move left functionality.


12
char Settings_getGameMoveRight(EventStore *pSharedEventStore) 
Returns the key bound to the move right functionality.


13
char Settings_getGameToggleFlag(EventStore *pSharedEventStore) 
Returns the key bound to the toggle flag functionality.


# Functions in editor.game.c Description
14
void Editor_setup(Game *this) 
Initializes the game object.


15
void Editor_init(
Game *this,
int dWidth,
int dHeight
) 
Sets the size of the game field.


16
void Editor_setSaveName(
Game *this,
char *sName
) 
Sets the name of the level for saving.


17
char *Editor_getSaveName(Game *this) 
Gets the name of the game object.


18
int Editor_levelExists(
Game *this,
char *sLevelName
) 
Checks if a certain level already exists or if there are too many levels files already.


19
int Editor_levelAddable(
Game *this,
char *sLevelName
) 
Returns whether or not we can still add another level (or too much).


20
int Editor_countMines(Game *this) 
Returns the number of mines on the field. Returns 0 if no mines or if there are too many mines. Max number of mines is dWidth * dHeight - 1.


21
int Editor_loadLevel(Game *this) 
Loads a level unto the game object.


22
int Editor_saveLevel(Game *this) 
Saves a level into the levels folder. If this is called independent of Level_register, it overwrites duplicate files.


23
int Editor_register(Game* this) 
Registers a new custom level into the .levels.data.txt file and saves it's contents in the same directory. If the level exists, the function terminates.


24
void Editor_addMine(Game *this) 
Adds a mine on a tile and recomputes numbers.


25
void Editor_removeMine(Game *this) 
Removes a mine from a tile and recomputes numbers.


26
void Editor_clearMines(Game *this) 
Clears the mine grid.


27
char *Editor_getErrorMessage(Game *this) 
Returns a description of the most recent error from the editor.


# Functions in field.obj.h Description
28
void Field_init(
	Field *this,
	int dWidth,
	int dHeight
) 
Initializes the field object. We give the field object a new instance of the grid class. Note that this time the return type is void since we're not treating this construct as a class.


29
void Field_clearMines(Field *this) 
Clears the mines on the mine grid.


30
void Field_clearFlags(Field *this) 
Clears the flags on the flag grid.


31
void Field_populateRandom(
Field *this,
int dMines
) 
Populates the mine grid with random mines.


32
void Field_populateCustom(
Field *this,
char *sPath
) 
Populates the mine grid with mines from a custom level.


33
void Field_setNumbers(Field *this) 
Specifies the number of mines adjacent to each tile.


34
void Field_inspect(
Field *pField,
int x,
int y
) 
Considers a tile inspected.


# Functions in game.c Description
35
void Game_setup(
	Game *this,
	GameType eGameType,
	GameDifficulty eGameDifficulty
) 
Initializes the game object.


36
void Game_init(Game *this) 
Sets up the field of the game based on the type and the difficulty.


37
void Game_end(
Game *this,
GameOutcome eOutcome
) 
Ends the game.


38
void Game_clear(Game *this) 
Clears the mines and the flags.


39
int Game_hasWon(Game *this) 
Checks whether or not the game has been finished.


40
int Game_isWon(Game *this) 
Checks just the enum.


41
void Game_displayGrid(
Game *this,
char *sOutputBuffer
) 
Creates a display of the grid as a text asset.


42
void Game_inspect(
Game *this,
int x,
int y
) 
Inspects a tile.


43
void Game_addFlag (Game *this) 
Adds a flag on a tile only if it hasn't been inspected.


44
void Game_removeFlag(Game *this) 
Removes a flag from a tile only if it currently has a flag.


45
void Game_pause(Game *this) 
Pauses the game.


46
void Game_unpause(Game *this) 
Unpauses the game.


47
void Game_incrementX(Game *this) 
Increments the cursor along the x axis (to the right). Skips tiles that have already been inspected.


48
void Game_decrementX(Game *this) 
Decrements the cursor along the x axis (to the left). Skips tiles that have already been inspected.


49
void Game_incrementY(Game *this) 
Increments the cursor along the y axis (down). Skips tiles that have already been inspected.


50
void Game_decrementY(Game *this) 
Decrements the cursor along the y axis (up). Skips tiles that have already been inspected.


51
int Game_getCharWidth(Game *this) 
How many characters wide the grid is.


52
int Game_getCharHeight(Game *this) 
How many characters tall the grid is.


53
int Game_isDone(Game *this) 
Returns whether or not the game has ended.


54
char *Game_getTime(Game *this) 
Returns the time elapsed since the game started.


55
char *Game_getMinesLeft(Game *this) 
Returns the number of mines left.


56
char *Game_getFPS(Game *this) 
Returns how many fps the game is running at. Default is 32.


57
void Game_quit(Game *this) 
Quits the current game. This sets the outcome of the game to GAME_OUTCOME_QUIT.


58
int Game_save(Game *this) 
"Saves" the current game. Sets the save state to 1.


59
char *Game_getEndMessage(Game *this) 
Returns a description of how the game ended.


# Functions in profile.game.c Description
60
void Profile_init(Profile *this) 
Initializes the profile object.


61
int Profile_login(
Profile *this,
char *sUsername,
char *sPassword
) 
Attempts to log into a profile given the credentials. This is some weak ass password storing (plain text bruh).


62
int Profile_register(
Profile *this,
char *sUsername,
char *sPassword
) 
Sets the active profile to the one indicated.


63
int Profile_delete(
Profile *this,
char *sUsername
) 
Deletes a profile.


65
char *Profile_getErrorMessage(Profile *this) 
Returns a description of the last error encountered by the profile object.


66
ProfileError Profile_getErrorId(Profile *this) 
Returns the actual enum representing the latest error.


67
void Profile_setErrorId(
Profile *this,
ProfileError eError
) 
Sets the error stored by the profile object.


68
char *Profile_getCurrent(Profile *this) 
Returns the name of the currently active profile.


# Functions in stats.game.c Description
69
int Stats_saveGame(Game *this) 
Saves the information held by the most recent game.


# Functions in account.page.c Description
70
void PageHandler_account(p_obj pArgs_Page) 
Configures the main menu.


# Functions in editor.interactive.page.c Description
71
void PageHandler_editorI(p_obj pArgs_Page) 
Configures the main menu.


# Functions in editor.page.c Description
72
void PageHandler_editor(p_obj pArgs_Page) 
Configures the main menu.


# Functions in help.page.c Description
73
void PageHandler_help(p_obj pArgs_Page) 
Configures the main menu.


# Functions in login.page.c Description
74
void PageHandler_login(p_obj pArgs_Page) 
Configures the logjn page.


# Functions in menu.page.c Description
75
void PageHandler_menu(p_obj pArgs_Page) 
Configures the main menu.


# Functions in play.interactive.page.c Description
76
void PageHandler_playI(p_obj pArgs_Page) 
Configures the main menu.


# Functions in play.page.c Description
77
void PageHandler_play(p_obj pArgs_Page) 
Configures the main menu.


# Functions in settings.page.c Description
78
void PageHandler_settings(p_obj pArgs_Page) 
Configures the main menu.


# Functions in utils.asset.h Description
79
Asset *Asset_new() 
Allocates memory for an instance of the Asset class.


80
Asset *Asset_init(
Asset *this,
char *sName,
int dHeight,
char *sContentArray[]
) 
Initializes an instance of the Asset class.


81
Asset *Asset_create(
char *sName,
int h,
char *sContentArray[]
) 
Creates an initialized instance of the Asset class.
82
void Asset_kill(Asset *this) 
Deallocates the memory of an instance of the Asset class.


83
void AssetManager_init(AssetManager *this) 
Initializes the asset manager.


84
void AssetManager_exit(AssetManager *this) 
Cleans up after the asset manager.


85
void AssetManager_createAsset(
AssetManager *this,
char *sAssetKey,
int h,
char *sContentArray[]
) 
Creates a new asset which is then appended to its hash map.


86
char **AssetManager_getAssetText(
AssetManager *this,
char *sAssetKey
) 
Retrieves an asset from its database of assets. It returns the content of that asset as opposed to a pointer to the asset object.


87
int AssetManager_getAssetHeight(
AssetManager *this,
char *sAssetKey
) 
Retrieves an asset from its database of assets. It returns the height of the asset.


88
int AssetManager_getAssetWidth(
AssetManager *this,
char *sAssetKey
) 
Retrieves an asset from its database of assets. It returns the default width of the asset.


89
void AssetManager_createTextAsset(
AssetManager *this,
char *sText,
char *sFont
) 
Creates a new asset which is then appended to its hash map. This asset is a text asset drawn with the font provided. The provided font just refers to a set of characters drawn in Unicode. Note that the key is generated from both the text and the font. It is written as -.


90
void AssetManager_readAssetFile(
AssetManager *this,
char *sDelimeter,
char *sFilepath
) 
Reads sprites from an asset text file and converts them into instances of the asset class above. Honestly, it's okay if this file takes a while to execute because this is only run once and not during each frame anyway.


# Functions in utils.buffer.h Description
91
Buffer *Buffer_new() 
Allocates memory for a new instance of the buffer class.


92
Buffer *Buffer_init(
Buffer *this,
int dWidth,
int dHeight,
int dDefaultFG,
int dDefaultBG
) 
Initializes an instance of the buffer class. Sets its current length to 0.


93
Buffer *Buffer_create(
int dWidth,
int dHeight,
int dDefaultFG,
int dDefaultBG
) 
Creates an initialized instance of the buffer class. Sets its current length to 0.


94
void Buffer_kill(Buffer *this) 
Frees the memory associated with an instance of the buffer class.


95
void Buffer_write(
Buffer *this,
int x,
int y,
int h,
char *sBlock[]
) 
Writes onto a rectangular section of the buffer, starting on (x, y).


96
void Buffer_contextRect(
Buffer *this,
int x,
int y,
int w,
int h,
color colorFG,
color colorBG
) 
Creates a context within the buffer. In this case, a context is a basically a rectangular slice of the 2d array where a certain style (be it a color change or something else) is applied to that slice.


97
void Buffer_contextCircle(
Buffer *this,
int x,
int y,
int r,
color colorFG,
color colorBG
) 
Creates a circular context within the buffer. A context here is a basically a circular slice of the 2d array where a certain style (be it a color change or something else) is applied to that slice.


98
void Buffer_print(Buffer *this) 
Outputs the buffer to the screen as one massive blob.


# Functions in utils.component.h Description
99
Component *Component_new() 
Allocates memory for an instance of the Component class.


100
Component *Component_init(
Component *this,
char *sName,
Component *pParent,
int x,
int y,
int w,
int h,
int dAssetHeight,
char **aAsset,
color colorFG,
color colorBG
) 
Initializes an instance of the Component class.


101
Component *Component_create(
char *sName,
Component *pParent,
int x,
int y,
int w,
int h,
int dAssetHeight,
char **aAsset,
color colorFG,
color colorBG
) 
Creates an initialized instance of the Component class.


102
void Component_kill(Component *this) 
Deallocates the memory of an instance of the Component class.


103
int Component_add(
Component *this,
Component *pChild
) 
Adds a component child to the children array. If there are too many children, nothing happens. The function returns 1 on success and 0 on failure.


104
void Component_config(Component *this) 
Computes the position of the component based on parent components.

105
void ComponentManager_init(ComponentManager *this) 
Allocates memory for an instance of the ComponentManager class.


106
void ComponentManager_exit(ComponentManager *this) 
Exits the component manager.


107
int ComponentManager_add(
ComponentManager *this,
char *sKey,
char *sParentKey,
int x,
int y,
int w,
int h,
int dAssetHeight,
char **aAsset,
color colorFG,
color colorBG
) 
Adds a component as a child of the specified component with the given id.


108
void ComponentManager_setPos(
ComponentManager *this,
char *sKey,
int x,
int y
) 
Set the position of a specified component.


109
void ComponentManager_setSize(
ComponentManager *this,
char *sKey,
int w,
int h
) 
Set the size of a specified component.


110
void ComponentManager_setZIndex(
ComponentManager *this,
char *sKey,
int zIndex
) 
Set the z index of a specified component.


111
void ComponentManager_setHidden(
ComponentManager *this,
char *sKey,
int bIsHidden
) 
Sets the visibility of a component. 1 means hidden, 0 means visible.


112
void ComponentManager_setColor(
ComponentManager *this,
char *sKey,
color colorFG,
color colorBG
) 
Set the color of a specified component.


113
void ComponentManager_setAsset(
ComponentManager *this,
char *sKey,
int dAssetHeight,
char **aAsset
) 
Set the asset of the specified component.


114
int ComponentManager_exists(
ComponentManager *this,
char *sKey
) 
Checks if a component exists within the manager.


115
void ComponentManager_render(
ComponentManager *this,
Buffer *pBuffer
) 
Renders the components in the tree to the specified buffer.


116
void ComponentManager_reset(ComponentManager *this) 
Removes all the components stored by the manager. Resets the component map too.


# Functions in utils.event.h Description
117
EventListener *EventListener_new() 
Allocates memory for a new event handler instance.


118
EventListener *EventListener_init(
EventListener *this,
f_event_listener fEventListener
) 
Initializes an event listener instance.


119
EventListener *EventListener_create(f_event_listener fEventListener) 
Creates an initialized event handler instance.


120
void EventListener_kill(EventListener *this) 
Deallocates the memory associated with an event listener instance.


121
char EventListener_trigger(EventListener *this) 
Waits for the event trigger and returns the outcome of the event. This should return 0 when nothing happens.


122
EventHandler *EventHandler_new() 
Allocates memory for a new event handler instance.


123
EventHandler *EventHandler_init(
EventHandler *this,
EventHandler *pNextHandler,
f_event_handler fEventHandler
) 
Initializes an event handler instance.


124
EventHandler *EventHandler_create(
EventHandler *pNextHandler,
f_event_handler fEventHandler
) 
Creates an initialized event handler instance.


125
void EventHandler_kill(EventHandler *this) 
Deallocates the memory associated with an event handler instance.


126
void EventHandler_chain(
EventHandler *this,
EventHandler *pNextHandler
) 
Appends an event handler after another one.


127
Event *Event_new() 
Allocates memory for a new event instance.


128
Event *Event_init(
Event *this,
Event *pNextEvent,
EventType eEventType,
EventHandler *pHeadHandler,
char cState
) 
Initializes an event instance


129
Event *Event_create(
Event *pNextEvent,
EventType eEventType,
EventHandler *pHeadHandler,
char cState
) 
Creates a new initialized event.


130
void Event_kill(Event *this) 
Releases an event instance from memory.


131
void Event_chain(
Event *this,
Event *pNextEvent
) 
Chains a new event to an existing event.


132
void Event_resolve(
Event *this,
EventStore *pEventStore
) 
Resolves an event.


133
void EventStore_init(EventStore *this) 
Initializes the event store.


134
void EventStore_exit(EventStore *this) 
Cleans up after the event store.


135
void EventStore_set(
EventStore *this,
char *sKey,
char cValue
) 
This function sets the value of a certain entry to a given int. If the entry does not exist, a new entry is created. The values we store must be non-negative because the EventStore_get() function returns -1 upon encountering an error.


136
void EventStore_setString(
EventStore *this,
char *sValueKey,
char *sStringKey
) 
This function updates a string value stored by the event store object. The new value appended to the string is based on the current value stored in sValueKey.


137
void EventStore_clearString(
EventStore *this,
char *sStringKey
) 
This function clears the string specified by the key.


138
char EventStore_get(
EventStore *this,
char *sKey
) 
This function gets the value stored by the entry with a given key.


139
char *EventStore_getHistory(
EventStore *this,
char *sKey
) 
This function gets the value stored by the entry with a given key.


140
char *EventStore_getString(
EventStore *this,
char *sKey
) 
This function returns the value of the current input string indicated by the key.


141
void EventStore_clear(
EventStore *this,
char *sKey
) 
Resets a certain value on the event store.


142
void EventManager_init(
EventManager *this,
EventStore *pEventStore
) 
Initializes the event manager object.


143
void EventManager_exit(EventManager *this) 
Cleans up after the event manager object.


144
void EventManager_createEvent(
EventManager *this,
EventType eEventType,
char cState
) 
Creates an event and appends it to the event chain.


145
void EventManager_triggerEvent(
p_obj pArgs_EventManager,
int tArg_eEventType
) 
Waits for event triggers and creates events when they occur. Note that we specify event type here so that we can pass this function into different threads for each event type. That way, different event types dont block each other when triggering.


146
void EventManager_resolveEvent(
p_obj pArgs_EventManager,
int tArg_NULL
) 
Resolves the head event of the chain.


147
void EventManager_createEventHandler(
EventManager *this,
EventType eEventType,
f_event_handler fEventHandler
) 
Once an event handler has been added to the chain, it cannot be removed.


148
void EventManager_createEventListener(
EventManager *this,
EventType eEventType,
f_event_listener fEventListener
) 
Creates an event listener for the specified event type. Note that if an event listener already exists for that event type, it overwrites the original listener.


# Functions in utils.file.h Description
149
File *File_new() 
Allocates memory for an instance of the File class.


150
File *File_init(
File *this,
char *sPath
) 
Initializes an instance of the File class.


151
File *File_create(char *sPath) 
Creates an initialized instance of the File class.


152
void File_kill(File *this) 
Deallocates the memory of an instance of the File class.


153
void File_readText(
File *this,
int n,
int *h,
char **sOutputBuffer
) 
Reads the text file stored at the path.


154
void File_readBinObject(
File *this,
int dOffset,
int n,
p_obj pOutputObject
) 
Reads a single entry from the binary file stored at the path.


155
void File_writeText(
File *this,
int n,
char *sContentBuffer[]
) 
Writes to a text file. This function only appends data to the existing content of the file.


156
int File_writeBin(
File *this,
int n,
p_obj pObject
) 
Writes to a binary file. This function only appends data to the existing content of the file. If the given pointer to the data object is NULL, we just exit the function.


157
void File_clear(File *this) 
Clears the contents of a file. Literally just opens it in write mode.


158
int File_exists(char *sFilename) 
Checks if a file exists or not.


159
int File_newFile(char *sFilename) 
Creates a new file with the specified name. If the file already exists, it leaves it untouched. Returns 1 on success, 0 on failure.


160
int File_remove(char *sFilename) 
Removes a new file with the specified name. Returns 1 on success, 0 on failure.


161
void File_freeBuffer(
int n,
char *sBuffer[]
) 
Frees a buffer that a file created. Must be called after File_readText().


# Functions in utils.graphics.h Description
162
char *Graphics_getCodeFG(color color) 
Returns the sequence to modify the current color of the terminal.


163
char *Graphics_getCodeBG(color color) 
Returns the sequence to modify the current color of the background of the terminal.


164
char *Graphics_getCodeFGBG(
color colorFG,
color colorBG
) 
Returns the sequence to modify the current color of the foreground AND background of the terminal.


165
float Graphics_getColorDist(
color color1,
color color2
) 
Returns the "pythagorean distance" between two colors.


166
color Graphics_RGB(
int r,
int g,
int b
) 
Returns the color associated with a set of rgb values.


167
int Graphics_lerp(
color color1,
color color2,
float fAmount
) 
Lerps between two colors by the specified amount.


168
void Graphics_delCode(char *sCode) 
Releases allocated memory.


# Functions in utils.grid.h Description
169
Grid *Grid_new() 
Allocates memory for a new instance of the grid class.


170
Grid *Grid_init(
Grid *this,
int dWidth,
int dHeight
) 
Initializes an instance of the grid class. If the function receives a width or height that is too large, 32 becomes the default initialization of that parameter.


171
Grid *Grid_create(
int dWidth,
int dHeight
) 
Creates an initialized instance of the grid class. Sets its current length to 0.


172
void Grid_kill(Grid *this) 
Frees the memory associated with an instance of the grid class.


173
int Grid_getBit(
Grid *this,
int x,
int y
) 
Returns the value at an entry on the grid. This function assumes that x and y are within range of the dimensions of the grid.


174
void Grid_setBit(
Grid *this,
int x,
int y,
int n
) 
Sets the value of a certain bit on the grid.


175
void Grid_clear(
Grid *this,
int n
) 
Clears the values stored by a grid instance (sets them all to 0 or 1).


176
int Grid_getCount(Grid *this) 
Returns how many bits are turned on.


# Functions in utils.hashmap.h Description
177
HashMapEntry *HashMapEntry_new() 
Allocates memory for an instance of the HashMapEntry class.


178
HashMapEntry *HashMapEntry_init(
HashMapEntry *this,
char *sKey,
p_obj pObject
) 
Initializes an instance of the HashMapEntry class.


179
HashMapEntry *HashMapEntry_create(
char *sKey,
p_obj pObject
) 
Creates an initialized instance of the HashMapEntry class.


180
void HashMapEntry_kill(HashMapEntry *this) 
Deallocates the memory of an instance of the HashMapEntry class.


181
HashMap *HashMap_new() 
Allocates memory for an instance of the HashMap class.


182
HashMap *HashMap_init(HashMap *this) 
Initializes an instance of the HashMap class.


183
HashMap *HashMap_create() 
Creates an initialized instance of the HashMap class.


184
void HashMap_kill(HashMap *this) 
Deallocates the memory of an instance of the HashMap class.


185
void HashMap_resize(HashMap *this) 
Resizes the hash map if the array starts to have too many collisions. Note that our hash can only grow in size. Resize here only accomodates us when we have too much stuff, and not too little (after removing a lot of entries).


PS I encountered some ferocious bugs trying to implement this routine.


186
void HashMap_add(
HashMap *this,
char *sKey,
p_obj pObject
) 
Adds a new entry into the hash map.


187
p_obj HashMap_get(
HashMap *this,
char *sKey
) 
Since we're abstracting how this function works, we return the actual object stored by the hash map at a certain location. This function returns NULL when the key is currently not in the hashmap.


188
void HashMap_set(
HashMap *this,
char *sKey,
p_obj pObject
) 
This function changes what object the entry with a certain key points to. Note that the original object must be freed first; otherwise, a memory leak will occur.


189
void HashMap_del(
HashMap *this,
char *sKey
) 
Removes an entry from the hashmap. Note that this also frees the object associated with the entry.


190
void HashMap_getKeys(
HashMap *this,
char *sKeyArray[]
) 
Returns all the keys in the hashmap. It is not advisable to call this function a lot, unless necessary (such as deleting all elements in the hashmap during clean up); this function defeats the purpose of using a hashmap because it iterates through all elements. This function assumes the target array is big enough to hold all keys.


# Functions in utils.io.h Description

# Functions in utils.math.h Description
191
int Math_hash(
	char *sKey,
	int dMax
) 
This is a hash function that extends one of the ones given here: http://www.cse.yorku.ca/~oz/hash.html


192
float Math_lerp(
float fStart,
float fEnd,
float fAmount
) 
Linear interpolation function.


193
float Math_easeOut(
float fValue,
float fTarget,
float fSpeed
) 
An easing function used to make a value approach another value smoothly. We use a logarithmic speed system (since the rate of change here is proportional to 1/x, and the integral of that happens to be ln(x)).


194
float Math_easeIn(
float fValue,
float fTarget,
float fSpeed
) 
An easing function used to make a value approach another value smoothly. Here, we use a reciprocal slider for the speed variable.


195
float Math_dist1d(
float a,
float b
) 
Computes the 1d distance between two points. Basically just the absolute value of their difference.


# Functions in utils.page.h Description
196
Page *Page_new() 
Allocates memory for an instance of the Page class.


197
Page *Page_init(
Page *this,
AssetManager *pSharedAssetManager,
EventStore *pSharedEventStore,
ThemeManager *pSharedThemeManager,
f_page_handler fHandler
) 
Initializes an instance of the Page class.


198
Page *Page_create(
AssetManager *pSharedAssetManager,
EventStore *pSharedEventStore,
ThemeManager *pSharedThemeManager,
f_page_handler fHandler
) 
Creates an initialized instance of the Page class.


199
void Page_kill(Page *this) 
Deallocates the memory of an instance of the Page class.


200
void Page_render(Page *this) 
Actually place things into the buffer


201
void Page_setUserState(
Page *this,
char *sStateKey,
char cData
) 
Sets a specific user-defined state to the value provided. Note that user states can only store char data.


202
char Page_getUserState(
Page *this,
char *sStateKey
) 
Gets a specific user-defined state. Note that user states can only store char data.


203
int Page_update(Page *this) 
Performs a single frame update of our page instance. Returns a boolean that indicates whether or not the page was able to update.


204
void Page_addComponent(
Page *this,
char *sKey,
char *sParentKey,
int x,
int y,
int w,
int h,
int dAssetHeight,
char **aAsset,
char *sColorFGKey,
char *sColorBGKey
) 
Adds a new component to the page.


205
void Page_addComponentAsset(
Page *this,
char *sKey,
char *sParentKey,
int x,
int y,
char *sColorFGKey,
char *sColorBGKey,
char *sAssetKey
) 
Adds a component with the asset of a given key. Note that the width and height of the component are automatically determined by the asset.


206
void Page_addComponentText(
Page *this,
char *sKey,
char *sParentKey,
int x,
int y,
char *sColorFGKey,
char *sColorBGKey,
char *sText
) 
Makes it easier to insert plain text into the page without having to convert the text into an asset.


207
void Page_addComponentContainer(
Page *this,
char *sKey,
char *sParentKey,
int x,
int y
) 
Adds a component with the no asset and no colors. Note that the width and height of the component may be determined by its children, depending on its alignment type.


208
void Page_addComponentContext(
Page *this,
char *sKey,
char *sParentKey,
int x,
int y,
int w,
int h,
char *sColorFGKey,
char *sColorBGKey
) 
Adds a component with the no asset and just colors. Note that the width and height of the component may be determined by its children, depending on its alignment type.


209
void Page_addComponentPopup(
Page *this,
char *sKey,
int x,
int y,
int w,
int h,
char *sColorFGKey,
char *sColorBGKey,
char *sBodyText,
char *sOption1,
char *sOption2
) 
Creates a popup component. Appends the component to the root element by default. The component is also screen centered by default.


210
void Page_setComponentPos(
Page *this,
char *sKey,
int x,
int y
) 
Changes the position of the component.


211
void Page_setComponentSize(
Page *this,
char *sKey,
int w,
int h
) 
Changes the size of the component.


212
void Page_setComponentColor(
Page *this,
char *sKey,
char *sColorFGKey,
char *sColorBGKey
) 
Changes the color of a component.


213
void Page_setComponentText(
Page *this,
char *sKey,
char *sText
) 
Changes the text stored by a component.


214
void Page_setComponentPopupText(
Page *this,
char *sKey,
char *sText
) 
Changes the text stored by a popup.


215
void Page_setComponentPopupOptions(
Page *this,
char *sKey,
char *sOption1,
char *sOption2,
char *sColorFGKey,
char *sColorBGKey
) 
Changes the text stored by a popup.


216
void Page_resetComponents(Page *this) 
Resets the component tree of the page.


217
void Page_setNext(
Page *this,
char *sNext
) 
Sets what page will be rendered next after the current page finishes running.


218
void Page_enableComponentPopup(
Page *this,
char *sKey
) 
Enables a popup.


219
void Page_disableComponentPopup(
Page *this,
char *sKey
) 
Disables a popup. Also saves the response of the user in the page state.


220
void Page_toggleComponentPopup(
Page *this,
char *sKey,
char *sFGColorKey,
char *sBGColorKey
) 
Toggles a popup option.


221
int Page_readComponentPopup(
Page *this,
char *sKey
) 
Returns the response of the user to the popup. In other words, which option they selected.


222
void Page_idle(Page *this) 
Sets the page status to PAGE_ACTIVE_IDLE.


223
void Page_activate(Page *this) 
Sets the page status to PAGE_ACTIVE. It also resets the dT value of the page so animations can start over.


224
void Page_deactivate(Page *this) 
Sets the page status to PAGE_INACTIVE.


225
void Page_nextStage(Page *this) 
Increments the stage counter of the page.


226
void PageManager_init(
PageManager *this,
AssetManager *pSharedAssetManager,
EventStore *pSharedEventStore,
ThemeManager *pSharedThemeManager
) 
Initializes the page manager.


227
void PageManager_exit(PageManager *this) 
Exits the page manager.


228
void PageManager_createPage(
PageManager *this,
char *sPageKey,
f_page_handler fHandler
) 
Creates a new page instance and stores it in the hashmap.


229
int PageManager_getActiveState(PageManager *this) 
Returns the state of the active page.


230
void PageManager_setActive(
PageManager *this,
char *sPageKey
) 
Sets the active page.


231
void PageManager_update(PageManager *this) 
Updates the active page.


232
void PageManager_givePage(
PageManager *this,
char *sPageKey,
p_obj pSharedObject
) 
Gives a page a shared object.


# Functions in utils.queue.h Description
233
QueueEntry *QueueEntry_new() 
Allocates memory for an instance of the QueueEntry class.


234
QueueEntry *QueueEntry_init(
QueueEntry *this,
p_obj pObject
) 
Initializes an instance of the QueueEntry class.


235
QueueEntry *QueueEntry_create(p_obj pObject) 
Creates an initialized instance of the QueueEntry class.


236
void QueueEntry_kill(QueueEntry *this) 
Deallocates the memory of an instance of the QueueEntry class.


237
void QueueEntry_chain(
QueueEntry *this,
QueueEntry *pNext
) 
Chains another entry unto the specified entry.


238
Queue *Queue_new() 
Allocates memory for an instance of the Queue class.


239
Queue *Queue_init(Queue *this) 
Initializes an instance of the Queue class.


240
Queue *Queue_create() 
Creates an initialized instance of the Queue class.


241
void Queue_kill(Queue *this) 
Deallocates the memory of an instance of the Queue class.


242
p_obj Queue_getHead(Queue *this) 
Returns the object stored at the head of the queue.


243
void Queue_push(
Queue *this,
p_obj pObject
) 
Pushes an entry to the end of the queue.


244
void Queue_pop(Queue *this) 
Removes an entry from the head of the queue. Note that this also frees the instance from memory, BUT it DOESN'T free the pObject member of the entry.


# Functions in utils.string.h Description
245
char *String_create(char *sString) 
Allocates a piece of memory for a string.


246
char *String_alloc(int dLength) 
Allocates a piece of memory for a string GIVEN the size.


247
void String_clear(char *string) 
Clears a string (sets everything to 0).


248
void String_kill(char *sString) 
Deallocates a string. This is here more for code readability.


249
int String_isStartChar(char c) 
Checks whether or not the character represents the start of a "compound" character (We refer to a character as compound if it needs more than one char instances to be represented, eg UTF-8 characters).


250
int String_isChar(char c) 
Checks whether or not the character is part of a longer character encoding.


251
int String_charCount(char *string) 
Gets the number of characters in a string, including multi-byte characters.


252
char String_getLast(char *string) 
Returns the last character in a string.


253
void String_keyAndId(
char *sKey,
char *sKeyName,
int sKeyId
) 
Creates a key with the following format: <key_name>-<key_id>.


254
void String_keyAndChar(
char *sKey,
char *sKeyName,
char sKeyChar
) 
Creates a key with the following format: <key_name>-<key_char>.


255
void String_keyAndStr(
char *sKey,
char *sKeyName,
char *sKeyStr
) 
Creates a key with the following format: <key_name>-<key_str>.


256
char *String_join(
char *sDelimeter,
char *sTerminator,
int dWrapLength,
...
) 
Joins multiple strings together with the specified sequence. This function always assumes that the last argument is equal to the terminator.


257
char *String_repeat(
char *sUnit,
int dLength
) 
Repeat a certain string pattern before a certain length is reached.


258
char *String_renderEscChar(char c) 
Converts an escape character into an escaped sequence of characters. If the character is not an escape character, the function returns the original char.


259
int String_isValidFilename(char *sFilename) 
Checks if a string has only valid characters.


260
int String_isValidChar(char c) 
Returns whether or not a character is not an escape char.


261
char *String_replace(
char *string,
char cOld,
char cNew
) 
Leaves the original string untouched. Replaces every instance of a character with another character.


# Functions in utils.theme.h Description
262
Theme *Theme_new() 
Allocates memory for an instance of the Theme class.


263
Theme *Theme_init(Theme *this) 
Initializes an instance of the Theme class.


264
Theme *Theme_create() 
Creates an initialized instance of the Theme class.


265
void Theme_kill(Theme *this) 
Deallocates the memory of an instance of the Theme class.


266
void Theme_addColor(
Theme* this,
char *sKey,
color c
) 
Adds a new color to the theme given the provided key.


267
color Theme_getDarker(
Theme *this,
char *sKey,
float fAmount
) 
Gets a darker version of a specific color from the current theme. The degree of darkening is also provided, where 0.0 represents no darkening at all. 0.5 is a 50-50 combination of the selected color and black. 1.0 represnts total black.


268
color Theme_getLighter(
Theme *this,
char *sKey,
float fAmount
) 
Gets a lighter version of a specific color from the current theme. The degree of lightening is also provided, where 0.0 represents no lightening at all. 0.5 is a 50-50 combination of the selected color and white. 1.0 represnts total white.


269
color Theme_getColor(
Theme *this,
char *sKey
) 
Simply returns the color referred to by the given key.

270
void ThemeManager_init(ThemeManager *this) 
Creates a default theme which the program uses.


271
void ThemeManager_exit(ThemeManager *this) 
Exits the theme manager.


272
void ThemeManager_setActive(
ThemeManager *this,
char *sKey
) 
Changes the current active theme.


273
color ThemeManager_getActive(
ThemeManager* this,
char *sKey
) 
Gets a color from the active theme referred to by the provided key.


274
void ThemeManager_readThemeFile(
ThemeManager *this,
char *sFilepath
) 
Reads and parses the themes contained in a themes file.


# Functions in utils.thread.h Description
275
ThreadManager *ThreadManager_init(ThreadManager *this) 
Initializes the thread manager variables.


276
void ThreadManager_exit(ThreadManager *this) 
Cleans up the state of the thread manager.


277
void ThreadManager_createThread(
ThreadManager *this,
char *sThreadKey,
char *sMutexKey,
f_void_callback fCallee,
p_obj pArgs_ANY,
int tArg_ANY
) 
Creates a new thread object instance and adds it to the hashmap.


278
void ThreadManager_createMutex(
ThreadManager *this,
char *sMutexKey
) 
Creates a new mutex object instance and adds it to the hashmap. Note that once a mutex has been created, it cannot be destroyed unless we wish to destroy all the mutexes. This is a safety feature to prevent any undefined behaviour. (We don't have a function for the ThreadManager struct that deletes a single Mutex).


279
void ThreadManager_killThread(
ThreadManager *this,
char *sThreadKey
) 
Terminates the thread with the given index in the hashmap. Also deallocates the memory associated with its object.


Note that what happens here is the following: (1) The function releases the state mutex associated with the thread. (2) The pointer to the thread is then deleted within the array (although the instance is still in memory). (3) The thread, upon realizing that its state mutex is free, cleans up after itself and deletes its instance. It does this by using the Thread_kill() function, which it calls thereafter.


280
void ThreadManager_lockMutex(
ThreadManager *this,
char *sMutexKey
) 
Locks the mutex with a given key. Note that this function does not terminate until it gets a handle to the mutex.


281
void ThreadManager_unlockMutex(
ThreadManager *this,
char *sMutexKey
) 
Unlocks the data mutex of the thread with a given index. This function returns 1 on success, and 0 on failure.


# Functions in utils.types.h Description

# Functions in utils.io.unix.h Description
282
void IO_init(struct IO *this) 
Sets up some stuff for IO handling. Overrides default terminal settings so I can replicate getch behaviour on Unix-based OS's.


283
int IO_getWidth() 
Helper function that returns the width of the console. Note that this function is responsive to resizing.


284
int IO_getHeight() 
Helper function that returns the height of the console. Note that this function is responsive to resizing.


285
int IO_setSize(
int dWidth,
int dHeight
) 
A helper function that resizes the console window. Unfortunately, I could not find a POSIX-compliant implementation. This is just here as a dummy function.


286
void IO_setBuffer(int dSize) 
Set the buffer size of the output stream.


287
void IO_resetCursor() 
Resets the cursor position to the start of the terminal.

288
void IO_clear() 
Helper function that clears the console.
289
void IO_flushBuffer() 
Flush the current output buffer, if ever there's still stuff there.
290
char IO_readChar() 
Helper function that gets a single character without return key.


291
void IO_exit(struct IO *this) 
Clean up the stuff I used.


# Functions in utils.string.unix.h Description
292
void String_print(char *string) 
Prints a string.


293
char *String_toUpper(char *string) 
Converts a string to upper case. This thing leaks memory...


# Functions in utils.thread.unix.h Description
294
Mutex *Mutex_new() 
Allocates memory for a new instance of the mutex class.


295
Mutex *Mutex_init(
Mutex *this,
char *sName
) 
Initializes the instance of the mutex class we pass to the function.


296
Mutex *Mutex_create(char *sName) 
Creates an initialized mutex instance.


297
void Mutex_kill(Mutex *this) 
Frees the memory to an instance of the mutex class.


298
void Mutex_lock(Mutex *this) 
Locks a mutex. Note that this function will wait indefinitely until it is able to lock the mutex.


299
int Mutex_lockTimed(
Mutex *this,
int dMillis
) 
Locks a mutex unless a timeout is reached. Note that this function will wait only until the specified amount of time.


300
void Mutex_unlock(Mutex *this) 
Frees a mutex and makes it available for other threads. Assumes that the caller is the one who currently holds the mutex.


301
Thread *Thread_new() 
Allocates memory for a new thread object instance. Note that this does not allocate memory for a thread but rather a thread object. The thread object refers to the struct above, which stores information about the thread, not the actual memory needed by the thread. The function returns NULL when the allocation fails.


302
Thread *Thread_init(
Thread *this,
char *sName,
Mutex *pStateMutex,
Mutex *pDataMutex,
f_void_callback fCallee,
p_obj pArgs_ANY,
int tArg_ANY
) 
Initializes an instance of the thread class. Returns the initialized instance.


303
Thread *Thread_create(
char *sName,
Mutex *pStateMutex,
Mutex *pDataMutex,
f_void_callback fCallee,
p_obj pArgs_ANY,
int tArg_ANY
) 
Creates a new thread object with initialized parameters. Returns the initialized instance.


304
void Thread_kill(Thread *this) 
Terminates a thread then destroys a Thread object instance and frees the memory of the object.

305
void *ThreadHandler(void *pThread) 
Executes the callback function assigned to the thread.


# Functions in utils.io.win.h Description
306
void IO_init(struct IO *this) 
Stuff to set up for Windows. In this case, we want the Windows console to understand ANSI escape sequences.


307
int IO_getWidth() 
Helper function that returns the width of the console. Note that this function is responsive to resizing.


308
int IO_getHeight() 
Helper function that returns the height of the console. Note that this function is responsive to resizing.


309
int IO_setSize(
int dWidth,
int dHeight
) 
A helper function that resizes the console window. This function only works on Windows (I could not find an implementation for Unix users).


Note that the function is implemented this way BECAUSE: (1) the buffer size apparently cannot be smaller than the console size AND (2) the console size cannot be bigger than the screen buffer.


By shrinking the window size to the absolute minimum, we spare ourselves from "crushing" the buffer size into the window size when shrinking. It also prevents the window size from "crashing" into the buffer when making it bigger. When I tried using a rudimentary implementation that did without the minWIndowSize step, some nasty scrollbars appeared on the side.


310
void IO_setBuffer(int dSize) 
Set the buffer size of the output stream.


311
void IO_resetCursor() 
Resets the cursor position to the start of the terminal.

312
void IO_clear() 
Helper function that clears the console.
313
void IO_flushBuffer() 
Flush the current output buffer, if ever there's still stuff there.
314
char IO_readChar() 
Helper function that gets a single character without return key.


315
void IO_exit(struct IO *this) 
This only exists mainly because I need to do some housekeeping for Unix-based OS's.


# Functions in utils.string.win.h Description
316
void String_print(char *string) 
Prints a string.


317
char *String_toUpper(char *string) 
Converts a string to upper case. THIS MUTATES THE ORIGINAL STRING.


# Functions in utils.thread.win.h Description
318
Mutex *Mutex_new() 
Allocates memory for a new instance of the mutex class.


319
Mutex *Mutex_init(
Mutex *this,
char *sName
) 
Initializes the instance of the mutex class we pass to the function.


320
Mutex *Mutex_create(char *sName) 
Creates an initialized mutex instance.


321
void Mutex_kill(Mutex *this) 
Frees the memory to an instance of the mutex class.


322
void Mutex_lock(Mutex *this) 
Locks a mutex. Note that this function will wait indefinitely until it is able to lock the mutex.


323
int Mutex_lockTimed(
Mutex *this,
int dMillis
) 
Locks a mutex unless a timeout is reached. Note that this function will wait only until the specified amount of time.


324
void Mutex_unlock(Mutex *this) 
Frees a mutex and makes it available for other threads. Assumes that the caller is the one who currently holds the mutex.


325
Thread *Thread_new() 
Allocates memory for a new thread object instance. Note that this does not allocate memory for a thread but rather a thread object. The thread object refers to the struct above, which stores information about the thread, not the actual memory needed by the thread. The function returns NULL when the allocation fails.


326
Thread *Thread_init(
Thread *this,
char *sName,
Mutex *pStateMutex,
Mutex *pDataMutex,
f_void_callback fCallee,
p_obj pArgs_ANY,
int tArg_ANY
) 
Initializes an instance of the thread class. Returns the initialized instance.


327
Thread *Thread_create(
char *sName,
Mutex *pStateMutex,
Mutex *pDataMutex,
f_void_callback fCallee,
p_obj pArgs_ANY,
int tArg_ANY
) 
Creates a new thread object with initialized parameters. Returns the initialized instance.


328
void Thread_kill(Thread *this) 
Terminates a thread then destroys a Thread object instance and frees the memory of the object.

329
void ThreadHandler(void *pThread) 
Executes the callback function assigned to the thread.


5 Test Script

6 About the Authors

About

Minesweeper... in C!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages