Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version 0.4, D port #3

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2235bc7
add stub d files
redthing1 Nov 16, 2021
a09c0e0
broken stubs for dusk
redthing1 Nov 16, 2021
124ca0f
bad partial dusk port
redthing1 Nov 16, 2021
9be72b5
more bad stubs
redthing1 Nov 16, 2021
3b0a70a
add gshared
redthing1 Nov 16, 2021
86ff4c8
port everything to use proper mixed D and C
redthing1 Nov 17, 2021
92f2ecb
fix irq add IrqIndex var
redthing1 Nov 18, 2021
dba9e7a
remove enum prefix from IrqIndex
redthing1 Nov 21, 2021
6729dc1
use dma for bg
Jan 1, 2022
a15e22e
optional dma
Jan 1, 2022
9e3376a
update log level enum
redthing1 Jan 7, 2022
c2edf77
add init func to Sprite and set visible flag without overwriting
redthing1 Jan 7, 2022
06f1bd1
add posprintf
redthing1 Jan 8, 2022
8d88b90
eat prints if not open
redthing1 Jan 8, 2022
15e51e8
add ds_assert
redthing1 Jan 8, 2022
2219961
badbrok
redthing1 Jan 9, 2022
c4d9136
simplify palfix
redthing1 Jan 9, 2022
92d4dda
fix pal offset bg
redthing1 Jan 9, 2022
7482bb6
fix bgupl
redthing1 Jan 9, 2022
a1706b6
assert always available
redthing1 Jan 9, 2022
942cba3
convert gbamap to single file
redthing1 Jan 13, 2022
a157e85
add rtc lib
redthing1 Jan 24, 2022
8c74fe6
rtc ex test
redthing1 Jan 24, 2022
4b0e415
fix rtc datetime read
redthing1 Jan 24, 2022
e03f458
fix rtc year
redthing1 Jan 24, 2022
9a43fbb
fix versiondata strings
redthing1 Feb 23, 2022
473b11b
fix possible name conflicts
redthing1 Feb 23, 2022
cc489cf
bump version
redthing1 Feb 23, 2022
76d433b
add info on version fork
redthing1 Feb 23, 2022
0339044
add priority defines
redthing1 Apr 20, 2022
e5390a0
Merge branch 'd-port' of https://github.com/xdrie/dusk into d-port
redthing1 Apr 20, 2022
9dbf681
stub upload section
redthing1 Apr 20, 2022
ff1db43
move upl sec out
redthing1 Apr 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

# ---> C
# Prerequisites
*.d
# *.d
build/

# Object files
*.o
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@

# dusk

DUsK, a library for gba dev
**dusk**, a library for gba dev

supported toolchains:
- DevkitARM/C/C++ (version 0.1.x), [view](https://github.com/redthing1/dusk/tree/lang_c)
- DevkitARM/D (version 0.2+) [view](https://github.com/redthing1/dusk/tree/d-port)

## example

<img src="media/duskdemo.webp" width="256">
<img src="media/fountain2.webp" width="256">

## features
+ simple, intuitive C API
+ simple, intuitive C/D API
+ library integration
+ built in support for TONC, GBFS
+ graphics
+ scene architecture
+ 8bpp texture atlas packing
+ sprite/animation helpers
+ tiled map exporter and loader (via Tiled2GBA)
+ tiled map exporter and loader
+ (WIP) saves

## hacking
Expand Down
21 changes: 17 additions & 4 deletions src/dusk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

LIBNAME := dusk
VERSION := 0.1.7
VERSION := 0.2.0

#---------------------------------------------------------------------------------
.SUFFIXES:
Expand All @@ -18,7 +18,7 @@ endif
include $(DEVKITARM)/gba_rules

BUILD := build
SRCDIRS := src src/contrib
SRCDIRS := c_src c_src/contrib
INCDIRS := include include/contrib

DEPFLAGS := -I$(DEVKITPRO)/libtonc/include -L$(DEVKITPRO)/libtonc/lib -ltonc
Expand All @@ -33,6 +33,10 @@ IARCH := -mthumb-interwork -marm
bTEMPS := 0 # Save gcc temporaries (.i and .s files)
# DEBUG := 0 # Generate debug info

# get revision id from git
GITVERSION_ID := $(shell printf "r%s" "$(shell git rev-parse --short HEAD)")
$(info build id: $(GITVERSION_ID))

#---------------------------------------------------------------------------------
# Options for code generation
#---------------------------------------------------------------------------------
Expand All @@ -46,8 +50,13 @@ ifeq ($(DEBUG),1)
CBASE += -g -DDEBUG
endif

DEFINES = -DGBA -DDUSK_VERSION=\"$(GITVERSION)\"
CBASE += $(DEFINES)
# add define for gba
EXTRA_DEFINES := -DGBA

# add EXTRA_DEFINES for version and git version
EXTRA_DEFINES += -DVERSION_ID_DEF="\"$(VERSION)\"" -DLIB_BUILD_ID_DEF="\"$(GITVERSION_ID)\""

CBASE += $(EXTRA_DEFINES)

RCFLAGS := $(CBASE) $(RARCH)
ICFLAGS := $(CBASE) $(IARCH) -mlong-calls #-fno-gcse
Expand Down Expand Up @@ -130,6 +139,10 @@ $(TARGET): $(OFILES)
@echo $(notdir $<)
$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(RCFLAGS) -c $< -o $@

%.o : %.s
@echo $(notdir $<)
$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(RCFLAGS) -c $< -o $@

-include $(DEPENDS)

endif
Expand Down
216 changes: 216 additions & 0 deletions src/dusk/c_src/contrib/gbamap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
#include <string.h>
#include "gbamap.h"

typedef unsigned char u8, byte;
typedef unsigned short u16, hword;
typedef unsigned int u32, word;
typedef unsigned long long u64;

typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long long s64;

typedef volatile u8 vu8;
typedef volatile u16 vu16;
typedef volatile u32 vu32;
typedef volatile u64 vu64;

typedef volatile s8 vs8;
typedef volatile s16 vs16;
typedef volatile s32 vs32;
typedef volatile s64 vs64;

#define INLINE static inline
#define ALIGN(n) __attribute__((aligned(n)))

#define TRUE 1
#define FALSE 0

/**
* Defines the general memory locations.
*/

#define MEMORY_IO 0x04000000
#define MEMORY_PALETTE 0x05000000
#define MEMORY_VIDEORAM 0x06000000
#define MEMORY_OBJECT_ATTRIBUTES 0x07000000

#define REGISTER_BASE MEMORY_IO

#define BIT_FIELD_PREP(x, name) (((x) << name##_SHIFT) & name##_MASK)
#define BIT_FIELD_GET(y, name) (((y)&name##_MASK) >> name##_SHIFT)
#define BIT_FIELD_SET(y, x, name) (y = ((y) & ~name##_MASK) | BIT_FIELD_PREP(x, name))

typedef u16 Color;

#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 160

#define REGISTER_DISPLAY_CONTROL *(vu32*)(REGISTER_BASE + 0x0000)
#define REGISTER_VERTICAL_COUNT *(vu16*)(REGISTER_BASE + 0x0006)

#define FLAG_MODE0 0

/**
* Wait till the background has been rendered and it is safe to modify it.
**/
INLINE void videoSync() {
while (REGISTER_VERTICAL_COUNT >= 160)
;
while (REGISTER_VERTICAL_COUNT < 160)
;
}

typedef u16 ScreenEntry;

typedef struct {
u32 data[8];
} Tile;

typedef ScreenEntry ScreenBlock[1024];
typedef Tile CharBlock[512];

#define MEMORY_BACKGROUND_PALETTE ((Color*)MEMORY_PALETTE)
#define MEMORY_BACKGROUND_PALETTE_SIZE 0x00200

#define MEMORY_SCREEN_BLOCK ((ScreenBlock*)MEMORY_VIDEORAM)
#define MEMORY_CHAR_BLOCK ((CharBlock*)MEMORY_VIDEORAM)

#define REGISTER_BACKGROUND_CONTROL ((vu16*)(REGISTER_BASE + 0x0008))
#define REGISTER_BACKGROUND_OFFSET ((BackgroundPoint*)(REGISTER_BASE + 0x0010))

#define FLAG_BACKGROUND_4BPP 0
#define FLAG_BACKGROUND_8BPP 0x0080
#define FLAG_BACKGROUND_REGULAR_32x32 0
#define FLAG_BACKGROUND_REGULAR_64x32 0x4000
#define FLAG_BACKGROUND_REGULAR_32x64 0x8000
#define FLAG_BACKGROUND_REGULAR_64x64 0xC000

#define MASK_FLAG_BACKGROUND_CHAR_BLOCK 0x000C
#define SHIFT_FLAG_BACKGROUND_CHAR_BLOCK 2
#define FLAG_BACKGROUND_CHAR_BLOCK(n) ((n) << SHIFT_FLAG_BACKGROUND_CHAR_BLOCK)

#define MASK_FLAG_BACKGROUND_SCREEN_BLOCK_MASK 0x1F00
#define SHIFT_FLAG_BACKGROUND_SCREEN_BLOCK 8
#define FLAG_BACKGROUND_SCREEN_BLOCK(n) ((n) << SHIFT_FLAG_BACKGROUND_SCREEN_BLOCK)

#define FLAG_BACKGROUND(n) ((0x0100) << n)
#define FLAG_BACKGROUND_PRIORITY(n) ((n * 1) & 0x0003)

void map_init_registers() {
// turn off all backgrounds
for (u32 layerIndex = MAX_LAYERS; layerIndex-- > 0;) {
REGISTER_BACKGROUND_CONTROL[layerIndex] = 0;
}

// reset display control
REGISTER_DISPLAY_CONTROL = 0;
}

Map map_load_from_rom(const u16* map_data) {
Map map;
u16 index = 0;

map.sizeFlag = map_data[index++];

map.paletteLength = map_data[index++];
map.palette = &map_data[index];
index += map.paletteLength + 1;

map.tileSetLength = map_data[index++];
map.tileSet = &map_data[index];
index += map.tileSetLength + 1;

map.terrainMapLength = map_data[index++];
map.terrainMap = &map_data[index];
index += map.terrainMapLength;

map.numLayers = map_data[index++];
map.numLayers = map.numLayers > MAX_LAYERS ? MAX_LAYERS : map.numLayers;
map.tileMapLength = map_data[index++];
for (u32 layerIndex = 0; layerIndex < map.numLayers; ++layerIndex) {
map.tileMapLayers[layerIndex] = &map_data[index];
index += map.tileMapLength;
}

u16 lengthObjectData = map_data[index++];
u32 endObjectData = index + lengthObjectData;
u32 objectCount = 0;
while (index != endObjectData) {
MapObject object = map_load_object(map_data, &index);
map.objects[objectCount] = object;
objectCount++;
}
map.numObjects = objectCount;

return map;
}

MapObject map_load_object(const u16* object_data, u16* index) {
MapObject object;
object.id = map_load_object_id(object_data, index);
object.position = map_load_object_position(object_data, index);
object.name = map_load_string(object_data, index);
object.type = map_load_string(object_data, index);
return object;
}

u32 map_load_object_id(const u16* idData, u16* index) {
u32 upperID = map_pop_value(idData, index);
u32 lowerID = map_pop_value(idData, index);
return (upperID << 16) | lowerID;
}

ObjectPoint map_load_object_position(const u16* positionData, u16* index) {
ObjectPoint position;
position.x = map_pop_value(positionData, index);
position.y = map_pop_value(positionData, index);
return position;
}

const char* map_load_string(const u16* stringData, u16* index) {
u16 length = map_pop_value(stringData, index);
const char* string = (const char*)&stringData[*index];
*index += length;
return string;
}

u16 map_pop_value(const u16* data, u16* index) {
u16 value = data[*index];
*index += 1;
return value;
}

void map_set_onscreen(Map map) {
memcpy(MEMORY_BACKGROUND_PALETTE, map.palette, map.paletteLength * 2);
memcpy(&MEMORY_CHAR_BLOCK[0][0], map.tileSet, map.tileSetLength * 2);

u32 screenBlockStep = map.tileMapLength / ENTRIES_IN_SCREEN_BLOCK;
u32 usedBackgrounds = 0x00;
for (u32 layerIndex = map.numLayers; layerIndex-- > 0;) {
u32 screenBlockIndex = NUM_SCREEN_BLOCKS - screenBlockStep * (layerIndex + 1);
memcpy(&MEMORY_SCREEN_BLOCK[screenBlockIndex][0], map.tileMapLayers[layerIndex], map.tileMapLength * 2);

REGISTER_BACKGROUND_CONTROL[layerIndex] |=
FLAG_BACKGROUND_CHAR_BLOCK(0) | FLAG_BACKGROUND_SCREEN_BLOCK(screenBlockIndex) | FLAG_BACKGROUND_8BPP |
map.sizeFlag | FLAG_BACKGROUND_PRIORITY(map.numLayers - layerIndex);
usedBackgrounds |= FLAG_BACKGROUND(layerIndex);
}

REGISTER_DISPLAY_CONTROL |= FLAG_MODE0 | usedBackgrounds;
}

void map_shift(Map map, BackgroundPoint offset) {
for (u16 layer = 0; layer < map.numLayers; layer++)
map_shift_layer(layer, offset);
}

void map_shift_layer(u16 layer, BackgroundPoint offset) { REGISTER_BACKGROUND_OFFSET[layer] = offset; }

void map_shift_objects(MapObject* objects, ObjectPoint shift, u32 count) {
for (u32 index = 0; index < count; index++) {
objects[index].position.x -= shift.x;
objects[index].position.y -= shift.y;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "contrib/mgba.h"

int mgba_is_open = 0;

#ifdef DEBUG
void mgba_printf(int level, const char* ptr, ...) {
if (!mgba_is_open) return;
va_list args;
level &= 0x7;
va_start(args, ptr);
Expand All @@ -12,7 +15,11 @@ void mgba_printf(int level, const char* ptr, ...) {

BOOL mgba_open(void) {
*REG_DEBUG_ENABLE = 0xC0DE;
return *REG_DEBUG_ENABLE == 0x1DEA;
if (*REG_DEBUG_ENABLE == 0x1DEA) {
mgba_is_open = 1;
return TRUE;
}
return FALSE;
}

void mgba_close(void) { *REG_DEBUG_ENABLE = 0; }
Expand Down
Loading