Consistent code style makes the codebase easier to read and maintain.
C Code
General
- Indentation: 4 spaces (no tabs)
- Line length: 80 characters soft limit, 100 hard limit
- Braces: K&R style (opening brace on same line)
if (condition) {
do_something();
} else {
do_other();
}
if (condition)
{
do_something();
}
Naming Conventions
| Element | Convention | Example |
| Functions | snake_case | sprite_init() |
| Variables | snake_case | player_x |
| Constants | UPPER_CASE | MAX_SPRITES |
| Types | PascalCase | SpriteData |
| Macros | UPPER_CASE | DMA_ENABLE |
| Enums | UPPER_CASE | SPRITE_SIZE_8X8 |
Types
Use fixed-width types from snes.h:
unsigned char byte_value;
int value;
signed int s32
32-bit signed integer (-2147483648 to 2147483647)
Definition types.h:79
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:50
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
Function Documentation
Use Doxygen-style comments:
Error Handling
}
}
if (!sprite_init(id, x, y)) {
}
#define TRUE
Definition types.h:151
#define FALSE
Definition types.h:150
#define MAX_SPRITES
Maximum number of hardware sprites.
Definition sprite.h:46
Memory
void* ptr = malloc(size);
}
SpriteData sprite;
memset(&sprite, 0, sizeof(sprite));
#define NULL
Null pointer constant.
Definition types.h:167
Assembly (65816 - WLA-DX)
General
- Indentation: Labels at column 0, instructions indented with tab
- Comments: Explain non-obvious operations
- Sections: Use .section for code organization
Format
;============================================================================
; Function: sprite_update
; Description: Update all active sprites
; Input: None
; Output: None
; Clobbers: A, X, Y
;============================================================================
.section ".text" superfree
sprite_update:
php ; Save processor status
rep #$20 ; 16-bit accumulator
ldx #0 ; Start at sprite 0
- lda sprite_active,x ; Check if active
beq + ; Skip if inactive
jsr _update_single ; Update this sprite
+ inx
inx ; Next sprite (16-bit index)
cpx #MAX_SPRITES * 2
bne -
plp ; Restore processor status
rtl
.ends
Register Documentation
; Document register state changes
rep #$20 ; A = 16-bit
sep #$10 ; X/Y = 8-bit
; Document expected register values
; Input: A = sprite ID (0-127)
; Output: X = OAM offset
Labels
; Public labels: _function_name
_sprite_init:
; Local labels: use anonymous (+ -)
bne + ; Forward reference
bra - ; Backward reference
; Named local: .local_name
.calculate_offset:
File Organization
Headers (.h)
#ifndef OPENSNES_SPRITE_H
#define OPENSNES_SPRITE_H
#define MAX_SPRITES 128
typedef struct {
} SpriteData;
void sprite_update(void);
#endif
OpenSNES common-case master header.
Source (.c)
}
SNES Sprite (Object) Management.
Formatting Tools
Consider using:
- clang-format for C code (config in .clang-format)
- Consistent editor settings (.editorconfig)