/cub3d

ray-casting with C

Primary LanguageC

Team Pakchoi πŸ₯¬

42 result
code-size last-commit

cub3d

Ray-casting with C
This project is inspired by the world-famous eponymous 90's game, which was the first FPS ever. It will enable you to explore ray-casting. Your goal will be to make a dynamic view inside a maze, in which you'll have to find your way.

Implementations

  • Parse a map file
    • The extension of the map file is .cub.
    • The map file includes the following information.
      • Texture
        • format: [DIRECTION_TYPE] [IMAGE_FILE_PATH]
        • DIRECTION_TYPE: NO, SO, WE , EA (north, south, west, east)
        • IMAGE_FILE_PATH: Absolute path of image file or relative path based on executable file
      • Ceilling and floor color
        • format: TYPE COLOR_VALUE
        • TYPE: C, F (ceiling, floor)
      • Map
        • Mandatory
          • 0 : Space for players to move around
          • 1 : Wall. The map must be surrounded by walls
          • N, S, E, W : The starting position and direction in which the player is spawned (only one player)
        • Bonus
          • D: Door. Doors should be located between the left and right or upper and lower walls
          • 2 : Animated sprite
        • Check if the map is surrounded by walls with a Flood fill algorithm
  • Draw walls with a Ray-casting
  • Draw a minimap with a Bresenham’s line algorithm
  • Input keys to move player(MiniLibX and Window System)

Project Structure

.
β”œβ”€β”€ includes    # header files
β”œβ”€β”€ libs        # librares
β”œβ”€β”€ src         # source code
β”œβ”€β”€ src_bonus   # source code for bonus
β”œβ”€β”€ maps        # example map files 
└── textures    # texture image files

Environment

  • MacOS 12.3.1(Monterey, Intel)

Developed and tested in this environment.

Compile

Mandatory

To compile the mandatory version, run the lines below.

$ git clone https://github.com/42pakchoi/cub3d
$ make

Bonus

To compile the bonus version, run the lines below.

$ make bonus

Execute

Run compiled executable file in the root folder.

$ ./cub3d [map_filename]

# example
$ ./cub3d maps/map.cub

Controls

  • Mandatory
    • ESC to exit a game. You can also use close button at a window frame.
    • WASD to move
    • Left arrow and Right arrow key to rotate
  • Bonus
    • spacebar to open and close a door
    • mouse drag in window to rotate

Test

Test using files in maps directory. Files marked with 'error_' are files that assume an exception condition.

$ ./cub3d [error_filepath]

# example
$ ./cub3d maps/error_invalid_char.cub # Failed to parse map
$ ./cub3d maps/error_invalid_extension.txt # Invalid file extension.

Logics

Flow chart

graph LR
    e1([Start])
    e2[Initial]
    e3[[Frame Loop]]
    e4[Free]
    e5([Exit])

    e1 --> e2 --> e3 --> e4 --> e5

Initial

flowchart TD
    x1(Read file)
    x2(Parse map)
    x3(Clear map raw)
    x4(Close file)

    y1(Init player struct)
    y2(Set player position)
    y3(Set player direction)

    z1(Init mlx)
    z2(Init window)
    z3(Init images)
    z4(Init hooks)

    subgraph s1[Init map]
      direction LR
      x1 --> x2 --> x3 --> x4
    end
    subgraph s2 [Init player]
      direction LR
      y1 --> y2 --> y3
    end
    subgraph s3 [Init mlx]
      direction LR
      z1 --> z2 --> z3 --> z4
    end

    s1 --> s2 --> s3

Frame Loop

flowchart TD
    x1(Get delta)
    x2(Set player position)
    x3(Set player direction)

    y1("Set screen image <br/>( ceiling & floor )")
    y2("Set screen image <br/>( wall )")
    y3(Set minimap image)

    z1(Set sprite counter)
    z2(Put screen image)
    z3(Put minimap image)

    subgraph s1 [Set player]
      direction LR
      x1 --> x2 --> x3
    end
    subgraph s2 [Update frame]
      direction LR
      y1 --> y2 --> y3
    end
    subgraph s3 [Draw images]
      direction LR
      z1 --> z2 --> z3
    end

    s1 --> s2 --> s3