Skip to content

Commit d16f98b

Browse files
Rangi42ISSOtm
authored andcommitted
Simplify RGBDS-structs using RGBDS 0.5.1 features
- DEF allows variable assignments to be indented - FOR loops are shorter than REPT with an explicit variable - BREAK allows early exit from loops, and can avoid macro recursion - {Interpolation} outside of string literals avoids temporary EQUS - REDEF simplifies updating EQUS without having to PURGE a temporary - DS with a sequence of bytes helps initialize multi-byte fields - STRRPL replaces the strreplace macro - Multi-arg STRCAT makes it easier to wrap long strings across lines - STRSUB without a length argument reads to the end of a string - \<ARG_NUM> makes SHIFTing unnecessary
1 parent 483cc56 commit d16f98b

File tree

3 files changed

+223
-287
lines changed

3 files changed

+223
-287
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# RGBDS structs
32

43
An attempt at using macros to add struct-like functionality to RGBDS.
@@ -14,7 +13,7 @@ An attempt at using macros to add struct-like functionality to RGBDS.
1413

1514
This doesn't actually require any installing, only to `INCLUDE` the file `structs.asm` in your project. Examples can be found in the `examples` folder. This project is licensed under the MIT license.
1615

17-
This is confirmed to work with RGBDS 0.3.7, but should also work with versions 0.3.3 and newer. If you find a compatibility issue, please file it [here](https://github.com/ISSOtm/rgbds-structs/issues/new).
16+
The latest RGBDS-structs version is **2.0.0**. It will only work with RGBDS 0.5.1 and newer. The previous version, 1.3.0, is confirmed to work with RGBDS 0.3.7, but should also work with versions 0.3.3 and newer. If you find a compatibility issue, please file it [here](https://github.com/ISSOtm/rgbds-structs/issues/new).
1817

1918

2019
# Usage
@@ -23,7 +22,7 @@ This is confirmed to work with RGBDS 0.3.7, but should also work with versions 0
2322

2423
Begin the declaration with `struct StructName`. This need not be in a `SECTION`, and it is rather recommended to declare structs in header files, as with C.
2524

26-
Then, declare each member; the declaration style is inspired by RGBDS' `_RS` command group: the macro name is the field type, the first argument dictates how many units the argument uses, and the second argument gives the field name.
25+
Then, declare each member. The declaration style is inspired by RGBDS' `_RS` command group: the macro name is the field type, the first argument dictates how many units the argument uses, and the second argument gives the field name.
2726

2827
Finally, you must close the declaration with `end_struct`. This is required to properly define all of the struct's variables, and to be able to declare another struct (which will otherwise fail with a descriptive error message). Please note that you can forget to add `end_struct` and not get any error messages, so please be careful.
2928

examples/correct.asm

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
21
INCLUDE "../structs.asm"
32

3+
; Check for the expected RGBDS-structs version
4+
rgbds_structs_version 2.0.0
5+
6+
47
; Struct declarations (ideally in a separate file, but grouped here for simplicity)
58
; Note that everything is happening outside of a `SECTION`
69

@@ -45,6 +48,8 @@ INCLUDE "../structs.asm"
4548

4649
SECTION "Code", ROM0
4750

51+
Routine::
52+
4853
; Using struct offsets
4954
ld de, wPlayer
5055
ld hl, NPC_InteractID
@@ -76,16 +81,20 @@ SECTION "Code", ROM0
7681

7782
ld hl, wOBJPalette0
7883
ld de, DefaultPalette
79-
ld c, sizeof_wOBJPalette ; Using the variable's size
84+
ld c, sizeof_wOBJPalette0 ; Using the variable's size
8085
call memcpy_small
8186

8287
; ...
8388

84-
DefaultPalette:
85-
db $00, $00, $00
86-
db $0A, $0A, $0A
87-
db $15, $15, $15
88-
db $1F, $1F, $1F
89+
; Ordered instantiation of a struct passes each field in order
90+
; Multi-byte fields repeat the byte to fill their size
91+
dstruct RawPalette, DefaultPalette, $00, $0A, $15, $1F
92+
93+
; Named instantiation can be out of order
94+
dstruct RawPalette, CustomPalette, \
95+
.Color1=$1E\,$0A\,$06, \ ; Multi-byte fields can take a
96+
.Color2=$1F\,$13\,$16, \ ; sequence of bytes to repeat
97+
.Color3=$1F, .Color0=$00
8998

9099

91100
memcpy_small:

0 commit comments

Comments
 (0)