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.
- 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
- format:
- Ceilling and floor color
- format:
TYPE COLOR_VALUE
TYPE
:C
,F
(ceiling, floor)
- format:
- Map
- Mandatory
0
: Space for players to move around1
: Wall. The map must be surrounded by wallsN
,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 walls2
: Animated sprite
- Check if the map is surrounded by walls with a Flood fill algorithm
- Mandatory
- Texture
- The extension of the map file is
- Draw walls with a Ray-casting
- Draw a minimap with a Bresenhamβs line algorithm
- Input keys to move player(MiniLibX and Window System)
.
βββ includes # header files
βββ libs # librares
βββ src # source code
βββ src_bonus # source code for bonus
βββ maps # example map files
βββ textures # texture image files
- MacOS 12.3.1(Monterey, Intel)
Developed and tested in this environment.
To compile the mandatory version, run the lines below.
$ git clone https://github.com/42pakchoi/cub3d
$ make
To compile the bonus version, run the lines below.
$ make bonus
Run compiled executable file in the root folder.
$ ./cub3d [map_filename]
# example
$ ./cub3d maps/map.cub
- Mandatory
ESC
to exit a game. You can also use close button at a window frame.WASD
to moveLeft arrow
andRight arrow
key to rotate
- Bonus
- spacebar to open and close a door
- mouse drag in window to rotate
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.
graph LR
e1([Start])
e2[Initial]
e3[[Frame Loop]]
e4[Free]
e5([Exit])
e1 --> e2 --> e3 --> e4 --> e5
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
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