Skip to content

Commit

Permalink
Simplify RGBDS-structs using RGBDS 0.5.1 features
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Rangi42 authored and ISSOtm committed May 9, 2021
1 parent 483cc56 commit d16f98b
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 287 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# RGBDS structs

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

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.

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).
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).


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

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.

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.
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.

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.

Expand Down
23 changes: 16 additions & 7 deletions examples/correct.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

INCLUDE "../structs.asm"

; Check for the expected RGBDS-structs version
rgbds_structs_version 2.0.0


; Struct declarations (ideally in a separate file, but grouped here for simplicity)
; Note that everything is happening outside of a `SECTION`

Expand Down Expand Up @@ -45,6 +48,8 @@ INCLUDE "../structs.asm"

SECTION "Code", ROM0

Routine::

; Using struct offsets
ld de, wPlayer
ld hl, NPC_InteractID
Expand Down Expand Up @@ -76,16 +81,20 @@ SECTION "Code", ROM0

ld hl, wOBJPalette0
ld de, DefaultPalette
ld c, sizeof_wOBJPalette ; Using the variable's size
ld c, sizeof_wOBJPalette0 ; Using the variable's size
call memcpy_small

; ...

DefaultPalette:
db $00, $00, $00
db $0A, $0A, $0A
db $15, $15, $15
db $1F, $1F, $1F
; Ordered instantiation of a struct passes each field in order
; Multi-byte fields repeat the byte to fill their size
dstruct RawPalette, DefaultPalette, $00, $0A, $15, $1F

; Named instantiation can be out of order
dstruct RawPalette, CustomPalette, \
.Color1=$1E\,$0A\,$06, \ ; Multi-byte fields can take a
.Color2=$1F\,$13\,$16, \ ; sequence of bytes to repeat
.Color3=$1F, .Color0=$00


memcpy_small:
Expand Down
Loading

0 comments on commit d16f98b

Please sign in to comment.