Dungeonator  1.0.0
a dungeon generator library
Data Structures | Typedefs | Functions
grid.h File Reference

declaration file for grid functions More...

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "tile.h"

Go to the source code of this file.

Data Structures

struct  Grids
 

Typedefs

typedef Tile ** data_t
 
typedef int ** regions_t
 
typedef struct Grids Grid
 

Functions

data_t mallocGrid (int width, int height)
 
regions_t mallocRegions (int width, int height)
 
void fillGrid (Grid *grid, int x1, int y1, int x2, int y2, Tile value)
 
void fillRegion (regions_t regions, int x1, int y1, int x2, int y2, int value)
 
bool createGrid (int width, int height, Grid *grid)
 
void printGridToString (char *str, size_t bufsz, Grid *grid)
 
void printGrid (Grid *grid)
 
void debugPrintGrid (Grid *grid, regions_t regions)
 
void freeGrid (Grid *grid)
 
void freeRegions (regions_t regions, int height)
 

Detailed Description

declaration file for grid functions

a file full of grid functions to allow for operations

Typedef Documentation

◆ data_t

typedef Tile** data_t

the type for the 2d grid tile data

◆ Grid

typedef struct Grids Grid

a struct to allow access to both the data and width/height of the grid in one param

◆ regions_t

typedef int** regions_t

the type for the regions to be used by the connection algorithm in generate.h

Function Documentation

◆ createGrid()

bool createGrid ( int  width,
int  height,
Grid grid 
)

creates and returns a grid, initialized to be full of walls (true)

Parameters
[in]widththe width of the grid
[in]heightthe height of the grid
[out]gridthe created grid
Returns
a boolean value indicating the success of the creation

◆ debugPrintGrid()

void debugPrintGrid ( Grid grid,
regions_t  regions 
)

print a grid to stdout, but with the regions printed in hex for debugging

Parameters
[in]gridthe grid to print
[in]regionsthe array of regions to print

◆ fillGrid()

void fillGrid ( Grid grid,
int  x1,
int  y1,
int  x2,
int  y2,
Tile  value 
)

fill a rectangle inside a grid with a tile value

Parameters
[in,out]gridthe grid to fill
[in]x1the left edge of the rectangle (inclusive)
[in]y1the top edge of the rectangle (inclusive)
[in]x2the right edge of the rectangle (exclusive)
[in]y2the bottom edge of the rectangle (exclusive)
[in]valuethe tile value to fill the grid with

if you're confused what inclusive/exclusive means here, basically it just doesn't fill all the way to the edge since that creates unintuitive effects

◆ fillRegion()

void fillRegion ( regions_t  regions,
int  x1,
int  y1,
int  x2,
int  y2,
int  value 
)

fill a rectangle inside regions grid with a region

Parameters
[in,out]regionsthe grid to fill
[in]x1the left edge of the rectangle (inclusive)
[in]y1the top edge of the rectangle (inclusive)
[in]x2the right edge of the rectangle (exclusive)
[in]y2the bottom edge of the rectangle (exclusive)
[in]valuethe int region value to fill the grid with

if you're confused what inclusive/exclusive means here, basically it just doesn't fill all the way to the edge since that creates unintuitive effects

◆ freeGrid()

void freeGrid ( Grid grid)

free the memory to a grid

Parameters
[in]gridthe grid to free

using a grid after this point is undefined behavior

◆ freeRegions()

void freeRegions ( regions_t  regions,
int  height 
)

free the memory to a regions grid

Parameters
[in]regionsthe regions array to free
[in]heightthe height of the regions array (get this right!)

using a grid after this point is undefined behavior

◆ mallocGrid()

data_t mallocGrid ( int  width,
int  height 
)

allocate the appropriate heap memory for a maze and cast it into an array - doesn't do any initialization. reading from the array at this point is undefined behavior

Parameters
[in]widththe width of the 2d array to allocate
[in]heightthe height of the 2d array to allocate
Returns
a 2d pointer to the array

◆ mallocRegions()

regions_t mallocRegions ( int  width,
int  height 
)

allocate memory for the regions array

Parameters
[in]widththe width of the 2d array to allocate
[in]heightthe height of the 2d array to allocate
Returns
a 2d pointer to the array
Note
fun fact, this shares implementation with the other malloc, but I thought it was too unintuitive to use the underlying function

◆ printGrid()

void printGrid ( Grid grid)

print a grid to stdout

Parameters
[in]gridthe grid to print

◆ printGridToString()

void printGridToString ( char *  str,
size_t  bufsz,
Grid grid 
)

an alternative print function to allow printing a grid to a string

Parameters
[out]strthe string to write the printed grid to
[in]bufszthe size of the output buffer, the function will only print part of the grid if it's not large enough
[in]gridthe grid to print