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)
} else {
}
{
}
static u16 bx
Definition main.c:159
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:
signed int s32
32-bit signed integer (-2147483648 to 2147483647)
Definition types.h:59
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:49
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:52
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:46
Function Documentation
Use Doxygen-style comments:
Error Handling
}
}
}
#define TRUE
Definition types.h:126
#define FALSE
Definition types.h:125
#define MAX_SPRITES
Maximum number of hardware sprites.
Definition sprite.h:46
Memory
}
#define NULL
Null pointer constant.
Definition types.h:142
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 {
#endif
Source (.c)
}
SNES Sprite (Object) Management.
Formatting Tools
Consider using:
clang-format for C code (config in .clang-format)
- Consistent editor settings (
.editorconfig)