Code Style Guidelines
Cub3D follows the 42 school norm, a strict coding standard that ensures consistency and readability.The 42 Norm
Function Length
Maximum 25 lines per function (excluding opening/closing braces)
Line Length
Maximum 80 columns per line
Functions per File
Maximum 5 functions per .c file (flexible for complex modules)
Variables
Maximum 4 variables per function, aligned declarations
Function Header Format
All functions must have the 42 header comment:Example Header
Formatting Rules
- Indentation
- Braces
- Naming
- Forbidden
Use tabs for indentation, not spaces.
- One tab per indentation level
- Align variable declarations with tabs
- Use tabs between type and variable name
Example: Norm-Compliant Function
Good Example
Adding New Features
General Workflow
Plan the feature
- Identify which module(s) need changes
- Determine if new files are needed
- Check if
t_mapstructure needs new fields - Consider performance impact
Example: Adding Minimap Feature
Here’s how to add a minimap display:Testing Changes
Build and Test Workflow
Test with valid maps
- Test movement (W, A, S, D)
- Test rotation (arrow keys)
- Test window closing (ESC or X button)
Test Map Collection
Create a comprehensive test suite:Test Script
Common Test Cases
Valid Map Tests
Valid Map Tests
- Simple rectangular room
- Complex maze
- Large open area
- Multiple rooms connected
- Player facing each direction (N, S, E, W)
- Different texture combinations
- Various RGB color values
Invalid Map Tests
Invalid Map Tests
- Map not closed by walls
- Multiple players
- No player
- Invalid characters in map
- Missing texture files
- Invalid RGB values (greater than 255 or less than 0)
- Duplicate attributes (two NO declarations)
- Missing required attributes
- Wrong file extension
Edge Cases
Edge Cases
- Player starting position at edge
- 1x1 room (minimum valid map)
- Very long corridors
- Texture file permissions issues
- Extremely large maps (performance test)
- Map with only player and walls (no spaces)
Build System
Makefile Targets
Available Commands
Makefile Structure
The Makefile automatically handles dependencies:Makefile:27-36
The Makefile rebuilds
libft and minilibx as needed. If you make changes to libft functions, run make re in the libft directory, then make in the main directory.Adding Compilation Flags
To add custom flags temporarily:Common Development Tasks
Adding a New Utility Function
Choose appropriate utils file
utils.c- Map validationutils2.c- String/map copyingutils3.c- Flood fill/map checkingutils4.c- Color validationutils5.c- Window/input handlingutils6.c- Movement/renderingutils7.c- Key events/drawing
utils8.c for new category.Modifying the t_map Structure
Adding a Field
Adding New Map Attributes
To add support for new .cub file attributes (e.g.,FLOOR_TEXTURE):
Where to Find Help
Documentation
MiniLibX
Check the MiniLibX documentation in
minilibx-linux/ directoryRaycasting Tutorial
42 Norm
Official 42 norm PDF (available in your 42 cluster)
libft Functions
Reference your own libft implementation in
libft/ directoryCode References
Usegrep to find usage examples:
Finding Examples
Key Source Files for Reference
| Task | Reference File | Key Functions |
|---|---|---|
| Initialization | main.c:15-72 | init_values(), cubed_init() |
| Raycasting | raycasting.c | raycasting(), perform_dda() |
| Rendering | drawing.c | draw_3d_dda(), draw_wall() |
| Movement | utils6.c:29-91 | move_cubed(), cubed_render() |
| Parsing | parselineutils.c | parsefloorceilingdirections() |
| Memory | freemem.c | freeall(), freeme() |
| Validation | utils3.c | flood_fill(), checkclosemap() |
Debugging Resources
See the Debugging page for:- GDB usage with raycasting
- Valgrind memory checking
- Common error solutions
- Performance profiling
Pre-submission Checklist
Code quality
- All code follows 42 norm
- No compilation warnings
- Functions are under 25 lines
- Proper function headers
- No forbidden functions used
Functionality
- Program compiles with
make - Works with valid maps
- Proper error handling for invalid maps
- Window closes cleanly with ESC or X
- Movement and rotation work smoothly
- Textures display correctly
Memory management
- No memory leaks (checked with Valgrind)
- All allocated memory is freed
- All error paths free resources
- No segmentation faults
Testing
- Tested with multiple valid maps
- Tested all invalid map cases
- Tested edge cases
- Performance is acceptable
Always run
make fclean && make before final submission to ensure a clean build from scratch.