Back to École 42 Projects

so_long

EN | FR
Your browser does not support SVG

Visual representation of the so_long project architecture and components

Project Essence

so_long is a 2D game development project that introduces you to graphics programming, event handling, and game logic. It's your first step into creating interactive visual applications at École 42, combining your C programming skills with new concepts in game development.

The Core Challenge

Create a simple 2D game where a player character navigates a map to collect items and reach an exit while avoiding obstacles. The game must use the MiniLibX graphics library, handle user input, validate maps, and track game statistics.

This project tests your ability to manage complex data structures, implement game mechanics, and create a cohesive user experience through graphics and interaction.

so_long challenges you to think about:

  • How to represent and render a 2D game world
  • How to handle user input and game events
  • How to implement game mechanics and rules
  • How to organize code for a complex interactive application
  • How to create an engaging user experience through graphics and feedback

Why This Matters in the Real World

The skills you develop in so_long have significant applications across multiple industries:

  • Game Development Industry: The global gaming market exceeded $180 billion in 2021, with 2D games remaining popular across mobile, indie, and casual gaming sectors. The fundamentals you learn here are the same ones used by professional game developers.
  • Interactive Data Visualization: Companies like Bloomberg, Tableau, and countless financial institutions use similar graphics programming techniques to create interactive dashboards that help analysts make sense of complex data.
  • Simulation Software: Industries ranging from aerospace to healthcare use graphical simulations built on the same event-loop architecture and rendering techniques you'll implement in this project.
  • User Interface Development: Modern UI frameworks for desktop, web, and mobile applications all rely on the event-driven programming model and graphics rendering concepts you'll master here.
  • Digital Twin Technology: The emerging field of digital twins, which creates virtual replicas of physical systems, uses similar techniques to visualize and interact with complex systems in real-time.

Companies like Unity Technologies, Epic Games, and even non-gaming giants like Microsoft, Apple, and Google employ thousands of developers who understand these fundamental graphics and interaction programming concepts.

100/100
Project Score
MiniLibX
Graphics Library
2D
Game Type
Medium
Complexity

Mental Models

To approach so_long effectively, consider these mental models that will help you conceptualize the game development process:

The Grid World Model

Think of your game world as a grid of cells, each containing a specific element (wall, empty space, collectible, exit, player). This model helps you understand map representation, collision detection, and movement mechanics.

By visualizing the game as a grid, you can more easily implement pathfinding, validate maps, and track game state changes.

The Event Loop Model

Visualize your game as a continuous cycle of: check for input → update game state → render to screen → repeat. This mental model helps you understand the core game loop and event-driven programming.

This perspective clarifies how user actions translate to game state changes and visual updates, forming the foundation of interactive applications.

The Layered Canvas Model

Imagine your game visuals as a stack of transparent layers: background, terrain, collectibles, characters, UI elements. This model helps you organize your rendering logic and understand the relationship between game elements.

This approach simplifies complex visual scenes by breaking them down into manageable components that can be updated independently.

These mental models will help you approach the project not just as a coding exercise, but as a design challenge that requires thinking about user experience, visual representation, and interactive systems.

Key Concepts

Before diving into implementation, make sure you understand these fundamental concepts:

Historical Context: The Evolution of 2D Game Development

The techniques you'll use in so_long have evolved through decades of game development history:

  • Early Arcade Era (1970s-1980s): Games like Pac-Man and Space Invaders pioneered tile-based 2D worlds and sprite animation. Developers worked with extreme hardware limitations, often programming directly in assembly language and managing individual pixels.
  • Home Computer Revolution (1980s-1990s): As personal computers became more accessible, libraries like SDL (Simple DirectMedia Layer) emerged to abstract hardware differences, similar to how MiniLibX provides a simplified interface to X11/Cocoa.
  • Object-Oriented Game Design (1990s-2000s): Games began to use more sophisticated architectures with clear separation between game logic, rendering, and input handling—principles you'll apply in your implementation.
  • Game Engine Era (2000s-Present): Modern engines like Unity and Unreal handle much of the low-level work for developers, but understanding the fundamentals you'll learn in so_long remains crucial for efficient game development.
  • Indie Renaissance (2010s-Present): 2D games have seen a resurgence with indie hits like Stardew Valley and Hollow Knight, proving that mastery of 2D game development fundamentals can lead to commercially successful and critically acclaimed games.

By implementing so_long, you're connecting with this rich heritage and gaining insights into the fundamental techniques that have powered interactive entertainment for decades.

1. Graphics Programming Basics

Understanding how graphics are displayed and manipulated:

  • Window Management: Creating and controlling application windows
  • Pixel Manipulation: Setting and reading pixel colors in an image
  • Sprites: Pre-drawn images used to represent game elements
  • Double Buffering: Technique to prevent flickering by drawing to an off-screen buffer

2. MiniLibX Library

The graphics library you'll use for the project:

  • Window Functions: Creating, displaying, and destroying windows
  • Image Functions: Loading, creating, and manipulating images
  • Event Handling: Capturing keyboard and mouse events
  • Hooks: Functions that are called when specific events occur

3. Game Development Concepts

Core ideas that drive game implementation:

  • Game Loop: The continuous cycle of input, update, and render
  • Collision Detection: Determining when game objects interact
  • State Management: Tracking and updating the game's current state
  • Tile-Based Design: Organizing game worlds as grids of tiles

4. Map Parsing and Validation

Reading and verifying game maps:

  • File I/O: Reading map data from .ber files
  • Map Representation: Storing map data in appropriate data structures
  • Validation Rules: Checking maps for proper format, enclosure, and required elements
  • Path Validation: Ensuring the player can reach all collectibles and the exit

5. Memory Management

Properly allocating and freeing resources:

  • Dynamic Allocation: Creating and managing memory for game data
  • Resource Cleanup: Properly freeing images, windows, and other resources
  • Memory Leaks: Preventing and detecting memory leaks in long-running applications

Progress Checkpoints: Test Your Understanding

Before proceeding with your implementation, make sure you can answer these questions:

Graphics Programming

  1. What is the purpose of double buffering in graphics programming, and how does MiniLibX implement it?
  2. How would you handle window resizing events in a MiniLibX application?
  3. What's the difference between pixel-based and sprite-based rendering, and which is more appropriate for so_long?

Game Architecture

  1. How would you structure your code to separate game logic from rendering code?
  2. What data structure would be most appropriate for storing and accessing your game map?
  3. How would you implement a system to track collectibles that have been picked up?

Event Handling

  1. How does MiniLibX's event system work, and how do you register callbacks for keyboard events?
  2. What's the difference between key press and key release events, and which would be more appropriate for player movement?
  3. How would you handle multiple keys being pressed simultaneously?

If you can confidently answer these questions, you have a solid foundation for implementing so_long. If not, revisit the relevant concepts before proceeding.

Implementation Approach

Here's a structured approach to help you implement the so_long project:

1. Project Architecture

Before writing code, plan your project structure:

  • Define the main components: map parser, game logic, rendering engine, input handler
  • Design data structures to represent the game state, map, and entities
  • Establish clear interfaces between components to maintain modularity
  • Create a logical file organization that reflects your architecture

Architecture Questions

  • How will you represent the game map in memory?
  • What data structure will you use to track collectibles?
  • How will you organize your code to separate concerns?
  • What information needs to be passed between different parts of your program?
  • How will you handle different game states (playing, win, lose)?

2. Implementation Strategy

A step-by-step approach to building your game:

Comparative Approaches: Game Architecture Patterns

There are several ways to structure your so_long implementation, each with different trade-offs:

Architecture Advantages Disadvantages Best When
Monolithic Approach
All game logic in a single file with minimal separation
  • Simple to implement initially
  • No need to pass data between modules
  • Easier to understand the whole system
  • Becomes unwieldy as project grows
  • Difficult to debug specific issues
  • Hard to reuse code in other projects
You're creating a minimal implementation and prioritize simplicity over extensibility
Component-Based
Separate modules for map, player, rendering, etc.
  • Clear separation of concerns
  • Easier to debug specific components
  • More maintainable as project grows
  • Requires careful interface design
  • More complex initial setup
  • Need to manage data sharing
You want a clean, maintainable codebase that could be extended with bonus features
State Machine
Explicit states (menu, playing, game over) with transitions
  • Clear handling of different game states
  • Easy to add new states and transitions
  • Predictable program behavior
  • More complex implementation
  • Overhead for simple games
  • Requires planning state transitions
You're implementing multiple game states or planning to add complex features like menus or animations

The approach you choose should reflect your priorities and coding style. Many successful implementations combine elements from different approaches.

Phase 1: Foundation

Build the core infrastructure:

  • Set up the MiniLibX environment
  • Create window management functions
  • Implement basic event handling
  • Establish a simple game loop

Phase 2: Map System

Create the game world:

  • Implement map file parsing
  • Validate maps according to requirements
  • Create data structures to represent the map
  • Implement map rendering functions

Phase 3: Game Mechanics

Add interactive elements:

  • Implement player movement and controls
  • Add collision detection
  • Create collectible and exit logic
  • Track and display move count

Phase 4: Graphics

Enhance visual presentation:

  • Create or source sprite assets
  • Implement sprite loading and rendering
  • Add visual feedback for game events
  • Optimize rendering performance

Phase 5: Polish

Refine the experience:

  • Add game state management (start, win, lose)
  • Implement proper error handling
  • Ensure memory management is correct
  • Add finishing touches to the UI

Phase 6: Testing

Verify functionality:

  • Test with various valid and invalid maps
  • Check for memory leaks
  • Verify all requirements are met
  • Get feedback from peers

3. Code Organization

A suggested file structure for your project:

include/ so_long.h # Main header with structures and function prototypes map.h # Map-related definitions and structures graphics.h # Graphics-related definitions src/ main.c # Entry point and game loop map_parser.c # Functions for reading and validating maps map_utils.c # Helper functions for map operations graphics.c # Window and image management render.c # Functions for drawing game elements player.c # Player movement and interaction logic game_logic.c # Game state and rules management events.c # Event handling functions utils.c # General utility functions cleanup.c # Memory management and cleanup assets/ sprites/ # Game sprites and images maps/ # Example map files

4. Testing Strategy

Approaches to verify your implementation:

  • Create a suite of test maps that cover all edge cases
  • Implement debug visualization for collision detection and pathfinding
  • Use tools like Valgrind to check for memory leaks
  • Create a checklist of all project requirements to verify completion
  • Test on different systems if possible (Linux, macOS)

Common Pitfalls

Be aware of these common challenges when working on so_long:

1. MiniLibX Challenges

  • Library Installation: Difficulties setting up MiniLibX on different systems
  • Documentation Gaps: Limited official documentation requiring experimentation
  • Event Handling: Misunderstanding how hooks and event loops work
  • Memory Management: Not properly freeing images and windows

2. Map Validation Issues

  • Incomplete Validation: Missing edge cases in map validation
  • Path Checking: Not verifying that all collectibles and the exit are reachable
  • Map Storage: Inefficient data structures for map representation
  • Error Handling: Poor error messages for invalid maps

3. Game Logic Errors

  • Collision Detection: Incorrect boundary checking leading to movement through walls
  • Move Counting: Inaccurate tracking of player movements
  • Win Condition: Not properly checking if all collectibles are gathered
  • Game State: Confusion between different game states

Debugging Tips

To overcome common challenges:

  • Create visual debugging tools that display collision boundaries and pathfinding
  • Implement detailed logging to track game state changes and events
  • Build the project incrementally, testing each component thoroughly
  • Use preprocessor macros to create debug and release modes
  • Create small test programs to experiment with MiniLibX functions
  • Maintain a clean separation between game logic and rendering code

Debugging Scenarios

Here are some common issues you might encounter and how to approach debugging them:

Scenario 1: Segmentation Fault on Image Rendering

Symptoms: Program crashes with segmentation fault when trying to render images or sprites.

Debugging Approach:

  • Verify image files exist and are in the correct format/location
  • Check if mlx_xpm_file_to_image is returning NULL (failed to load)
  • Ensure you're not trying to access NULL pointers when rendering
  • Confirm window initialization was successful before attempting to render
  • Use GDB to identify the exact line causing the segfault: gdb ./so_long

Scenario 2: Map Validation Problems

Symptoms: Program rejects valid maps or accepts invalid ones; pathfinding validation fails.

Debugging Approach:

  • Create a visual representation of the map parsing process
  • Print the map after reading to verify it matches the file
  • Add detailed error messages specifying which validation rule failed
  • For pathfinding issues, visualize the flood-fill algorithm step by step
  • Test with minimal maps that isolate specific validation rules

Scenario 3: Event Handling Issues

Symptoms: Keyboard inputs not registering, game not responding to key presses, or unexpected behavior.

Debugging Approach:

  • Print key codes to verify the correct events are being captured
  • Check if mlx_hook is properly set up with correct parameters
  • Verify the event loop is running with mlx_loop
  • Implement a simple key press counter to confirm events are registering
  • Test with a minimal program that only handles keyboard input

Learning Outcomes

Completing so_long will equip you with valuable skills that extend beyond the project itself:

Technical Skills

You'll develop expertise in:

  • Graphics programming fundamentals
  • Event-driven programming
  • Game development concepts
  • File parsing and validation
  • Resource management in graphical applications

Design Thinking

You'll cultivate abilities in:

  • System architecture design
  • User experience considerations
  • Visual feedback design
  • Game mechanics balancing
  • Interactive system design

Problem-Solving

You'll strengthen your approach to:

  • Algorithmic thinking for game logic
  • Debugging complex interactive systems
  • Performance optimization
  • Edge case identification and handling
  • Incremental development of complex systems

Beyond the Project: Career Applications

The skills you develop in so_long have direct applications in professional settings:

Game Development
The fundamentals you learn form the foundation for more advanced game programming
UI/UX Development
Graphics programming and event handling are essential for creating interactive interfaces
Simulation
The techniques used in games apply to scientific and industrial simulations
Data Visualization
Graphics programming skills translate to creating interactive data visualizations

Reflection Questions

  • How has this project changed your understanding of interactive software development?
  • What aspects of game development did you find most challenging, and how did you overcome them?
  • How would you approach this project differently if you were to start over?
  • What design principles from game development could you apply to other software projects?
  • How might you extend this project to create a more complex game?

A Gateway to Interactive Applications

so_long serves as an introduction to the world of interactive, visual applications. While it may seem like "just a game," the concepts you learn—event handling, state management, user feedback, and resource management—are fundamental to many types of modern software.

Whether you pursue game development or apply these skills to other domains like web applications, desktop software, or data visualization, the experience of creating a responsive, visual system will enhance your understanding of how users interact with software and how to create engaging digital experiences.

Going Further: Resources for Deeper Understanding

If you want to explore the concepts in so_long more deeply, here are some valuable resources:

Books and Documentation

  • "Game Programming Patterns" by Robert Nystrom - Excellent resource for understanding common design patterns in game development
  • "Fundamentals of 2D Game Programming" by Jonathan S. Harbour - Covers core concepts of 2D game development
  • "The MiniLibX Documentation" - While sparse, understanding the official documentation is essential

Online Resources

  • Lazy Foo's SDL Tutorials - While focused on SDL, the concepts translate well to MiniLibX
  • Game Dev Mathematics - Articles on the math behind collision detection, pathfinding, and other game mechanics
  • Tile-Based Game Development - Resources specifically about tile-based games like so_long

Advanced Topics to Explore

  • Animation Techniques - Learn about sprite animation, frame-based animation, and tweening
  • Pathfinding Algorithms - Explore A*, Dijkstra's algorithm, and other pathfinding techniques
  • Game Design Principles - Study what makes games engaging and how to apply these principles to your projects

These resources will help you build on the foundation you've established in so_long and develop more advanced game development and interactive application skills.