Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Add bit array
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Dec 2, 2022
1 parent 575376a commit bc63644
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ CFLAGS := -g -Wall -Wextra -Wno-free-nonheap-object -Wno-unknown-pragmas -Wno-ps
-ffast-math

CFLAGS += $(INCLUDE) -DARM9
ifeq ($(DEBUG_BUILD),yes)
CFLAGS += -DDEBUG_BUILD
endif
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -Wno-reorder

ASFLAGS := -g $(ARCH)
Expand Down Expand Up @@ -185,15 +188,15 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) BUILDDIR=`cd $(BUILD) && pwd` --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
@$(MAKE) BUILDDIR=`cd $(BUILD) && pwd` --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile DEBUG_BUILD=$(DEBUG_BUILD)

#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(SOUNDBANK)

#---------------------------------------------------------------------------------
EMULATOR := E:/melonds/melonds
EMULATOR := ~/melonDS
run:
@echo run ...
@$(EMULATOR) $(TARGET).nds
Expand Down
20 changes: 20 additions & 0 deletions include/bitarray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

typedef struct
{
size_t bitCount;
uint8_t *array;
} BitArray;

// bit count must be divisible by 8 for this to work
BitArray bitArrayCreate(size_t bitCount);
void bitArrayDelete(BitArray *array);

void bitArraySet(BitArray *array, size_t index);
void bitArrayClear(BitArray *array, size_t index);
bool bitArrayGet(BitArray *array, size_t index);
void bitArrayWrite(BitArray array, const char *file);
54 changes: 54 additions & 0 deletions source/bitarray.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "bitarray.h"
#include <nds/system.h>
#include <stdlib.h>
#include <stdio.h>

BitArray bitArrayCreate(size_t bitCount)
{
BitArray array;
array.array = (uint8_t *)calloc(bitCount, 1);
array.bitCount = bitCount;
return array;
}

void bitArrayDelete(BitArray *array)
{
free(array->array);
}

void bitArraySet(BitArray *array, size_t index)
{
array->array[index / 8] |= (1 << (index % 8));
}

void bitArrayClear(BitArray *array, size_t index)
{
array->array[index / 8] &= ~(1 << (index % 8));
}

bool bitArrayGet(BitArray *array, size_t index)
{
if (array->array[index / 8] & (1 << index % 8))
return true;
return false;
}

void bitArrayWrite(BitArray array, const char *file)
{
FILE *fp = fopen(file, "w");
if (!fp)
{
FILE *fpError = fopen("error.log", "w");
if (fpError)
{
// write error into an error file
fprintf(fpError, "Failed to open file %s for writing bit array", file);
fclose(fpError);
}
systemShutDown();
}

for (size_t i = 0; i < array.bitCount; ++i)
fputc((bitArrayGet(&array, i)) ? '1' : '0', fp);
fclose(fp);
}
4 changes: 4 additions & 0 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "soundbank.h"
#include "colors.h"
#include "ppm.h"
#include "bitarray.h"
#include "background.h"
#define COLORS_PER_PAGE 7
#define MAX_COLOR_PAGE 1
Expand Down Expand Up @@ -167,6 +168,9 @@ int main(int argc, char **argv)
mkdir("paintds_data", 0700);
chdir("paintds_data");

BitArray test = bitArrayCreate(32);
bitArrayWrite(test, "test.bitarr");

mmInitDefault("nitro:/soundbank.bin");

mmLoadEffect(SFX_PENCIL);
Expand Down

0 comments on commit bc63644

Please sign in to comment.