Loading...
Searching...
No Matches
OpenSNES Code Style Guide

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)
// Good
if (condition) {
} else {
}
// Bad
{
}
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:

// Good
// Bad
unsigned char byte_value;
int value;
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:

u8 sprite_init(u8 id, u16 x, u8 y);

Error Handling

// Return FALSE/NULL on error
u8 sprite_init(u8 id, u16 x, u8 y) {
if (id >= MAX_SPRITES) {
return FALSE;
}
// ...
return TRUE;
}
// Check return values
if (!sprite_init(id, x, y)) {
// Handle error
}
#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

// Always check allocations
void* ptr = malloc(size);
if (ptr == NULL) {
// Handle allocation failure
}
// Zero-initialize structures
memset(&sprite, 0, sizeof(sprite));
#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
#include <snes.h>
/* Constants */
#define MAX_SPRITES 128
/* Types */
typedef struct {
u16 x;
u8 y;
u16 tile;
/* Functions */
u8 sprite_init(u8 id, u16 x, u8 y);
void sprite_update(void);
#endif /* OPENSNES_SPRITE_H */
OpenSNES Master Header.

Source (.c)

#include "sprite.h"
/* Private data */
/* Public functions */
u8 sprite_init(u8 id, u16 x, u8 y) {
// Implementation
}
SNES Sprite (Object) Management.

Formatting Tools

Consider using:

  • clang-format for C code (config in .clang-format)
  • Consistent editor settings (.editorconfig)