Giter Club home page Giter Club logo

cub3d's Introduction

42_CUB3D

📑 Introduction

CUB3D is a project that involves creating a basic raycasting game engine inspired by the classic game Wolfenstein 3D.
It's a visual exploration of raycasting techniques to render a 3D-like environment.

🎮️ Description

The CUB3D project challenges you to build a simple game engine that can render a textured maze-like environment in a 2D window.
The core of the project revolves around raycasting, a technique that simulates a 3D perspective by casting rays from the player's viewpoint
and calculating the intersections with walls and other objects. The project requirement asked us to parse a map file that defines the layout of the maze, handle player movement,
and render the scene using textures.
Other features, such as wall collision, mouse mouvement, controller handling and such were optional.

🛠️ Implementation

Creating the CUB3D game engine asked us to work on the following components:

  • Map parsing: Read and interpret the map file that defines the maze layout, textures, and other properties.
  • Raycasting: Cast rays to calculate distances to walls and objects in the player's view, creating the 3D-like effect.
  • Texturing: Apply textures to walls and other surfaces based on the raycasting results.
  • Player movement: Implement controls for player movement, rotation.
  • Graphic rendering: Set up a window and render the scene using the MinilibX graphical library
  • Game controller handling: Handle game controller inputs by reading the controller input file on a separate thread.

💽 Usage

To compile the program, run the following command:

make

It will compile the program and create an executable called "cub3D".

To run the program, simply execute the created executable:

./cub3D path_to_map.cub

This will only give you access to the mandatory part of the project.
To compile the bonus part, run the following command:

make bonus

Then, launch the bonus executable with the following command:

./cub3D_bonus path_to_map.cub

🎮 Controller handling

In the bonus part, we implemented a game controller handling feature.
Press "M" on the keyboard to swap beetwen four controller modes:

  • Keyboard only
  • Keyboard + Mouse
  • Controller only (tested with an xbox 360 controller, but should work with any controller)
  • Guitar Hero controller (tested with a Guitar Hero 3 controller, but should work with any guitar hero controller)

⚠️ Warning

This project is a part of the 42-Cursus curriculum and is intended for educational purposes.
If you are currently a student in any 42-affiliated campus, we strongly advise you not to copy this project or submit it as your own work, as it goes against the school's academic integrity policy.
We will not be responsible for any misuse of the project or academic penalties that may arise from it. However, you are welcome to use this project as a reference or for learning purposes.

🕒 Project Duration

The mandatory part was made in roughly one month.
Every other Bonus features were added in the span of another month.

🙇 Author

Guillaume d'harcourt

Benoit Roux

cub3d's People

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

gd-harco

cub3d's Issues

Data race detected

==================
WARNING: ThreadSanitizer: data race (pid=602059)
Write of size 1 at 0x7b8c00000028 by thread T1:
#0 update_gamepads bonus/srcs/gamepad/update_gamepads_bonus.c:38 (cub3D_bonus+0x14b47)
#1 input_loop bonus/srcs/gamepad/input_loop_bonus.c:24 (cub3D_bonus+0x15d01)

Previous read of size 1 at 0x7b8c00000028 by main thread:
[failed to restore the stack]

Location is heap block of size 6224 at 0x7b8c00000000 allocated by main thread:
#0 malloc ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:655 (libtsan.so.0+0x31c57)
#1 add_gamepad bonus/srcs/gamepad/init_gamepads_bonus.c:43 (cub3D_bonus+0x150f3)
#2 init_gamepads bonus/srcs/gamepad/init_gamepads_bonus.c:28 (cub3D_bonus+0x1502c)
#3 main bonus/srcs/main_bonus.c:28 (cub3D_bonus+0x8fa6)

Thread T1 (tid=602064, running) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:969 (libtsan.so.0+0x605b8)
#1 init_gamepads bonus/srcs/gamepad/init_gamepads_bonus.c:32 (cub3D_bonus+0x15089)
#2 main bonus/srcs/main_bonus.c:28 (cub3D_bonus+0x8fa6)

SUMMARY: ThreadSanitizer: data race bonus/srcs/gamepad/update_gamepads_bonus.c:38 in update_gamepads

ThreadSanitizer: reported 1 warnings

Comment on gere les espaces

Ton avis sur la gestion des espaces dans les map ? Je suis d'avis de les traiter comme un sol, t'en penses quoi ?

Parsing "éclatax"

Apparemment le parsing est "éclatax" selon les dire de son créateur.

Conditional Jump depend on unitialized value

gd-harco in ~/Documents/level_4/cub3D on fixes ● ● λ valgrind --track-origins=yes ./cub3D_bonus maps/good/matrix.cub
==103327== Memcheck, a memory error detector
==103327== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==103327== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==103327== Command: ./cub3D_bonus maps/good/matrix.cub
==103327==
==103327== Conditional jump or move depends on uninitialised value(s)
==103327== at 0x4A3BDE2: __cos_fma (s_sin.c:280)
==103327== by 0x403CD7: draw_wall_slice (render_bonus.c:54)
==103327== by 0x403B20: render (render_bonus.c:29)
==103327== by 0x403F7F: render_to_window (render_bonus.c:107)
==103327== by 0x4037C4: render_locked_fps (frame_second_bonus.c:42)
==103327== by 0x40283E: main (main_bonus.c:41)
==103327== Uninitialised value was created by a stack allocation
==103327== at 0x404710: cast_vert (casts_bonus.c:20)
==103327==
==103327== Conditional jump or move depends on uninitialised value(s)
==103327== at 0x4A3BDE2: __cos_fma (s_sin.c:280)
==103327== by 0x403CD7: draw_wall_slice (render_bonus.c:54)
==103327== by 0x403C0C: render (render_bonus.c:35)
==103327== by 0x403F7F: render_to_window (render_bonus.c:107)
==103327== by 0x4037C4: render_locked_fps (frame_second_bonus.c:42)
==103327== by 0x40283E: main (main_bonus.c:41)
==103327== Uninitialised value was created by a stack allocation
==103327== at 0x404B70: cast_horiz (casts_bonus.c:43)
==103327==
==103327== Conditional jump or move depends on uninitialised value(s)
==103327== at 0x4A3BDE2: __cos_fma (s_sin.c:280)
==103327== by 0x403CD7: draw_wall_slice (render_bonus.c:54)
==103327== by 0x403B96: render (render_bonus.c:32)
==103327== by 0x403F7F: render_to_window (render_bonus.c:107)
==103327== by 0x4037C4: render_locked_fps (frame_second_bonus.c:42)
==103327== by 0x40283E: main (main_bonus.c:41)
==103327== Uninitialised value was created by a stack allocation
==103327== at 0x404710: cast_vert (casts_bonus.c:20)
==103327==
==103327==
==103327== HEAP SUMMARY:
==103327== in use at exit: 0 bytes in 0 blocks
==103327== total heap usage: 51,678 allocs, 51,678 frees, 47,482,111 bytes allocated
==103327==
==103327== All heap blocks were freed -- no leaks are possible
==103327==
==103327== For lists of detected and suppressed errors, rerun with: -s
==103327== ERROR SUMMARY: 1800 errors from 3 contexts (suppressed: 1 from 1)

Memory leaks on Master bonus

With an invalid map file :

==21428== 88 bytes in 1 blocks are still reachable in loss record 1 of 1
==21428==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21428==    by 0x11575E: add_gamepad (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21428==    by 0x1156F6: init_gamepads (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21428==    by 0x10FBCF: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)

With a valid map and no input :

HEAP SUMMARY:
==21515==     in use at exit: 4,398,776 bytes in 935 blocks
==21515==   total heap usage: 69,478 allocs, 68,543 frees, 115,561,250 bytes allocated
==21515== 
==21515== 40 bytes in 1 blocks are still reachable in loss record 1 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x112C39: new_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x112B37: img_to_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110F14: render_to_window (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCE3: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 88 bytes in 1 blocks are still reachable in loss record 2 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111C14: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FC67: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 88 bytes in 1 blocks are still reachable in loss record 3 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111C14: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110335: create_map (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCB7: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 88 bytes in 1 blocks are still reachable in loss record 4 of 9
==21515==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1163EB: mlx_int_new_xshm_image (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x116712: mlx_new_image (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x112C62: new_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x112B37: img_to_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110F14: render_to_window (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCE3: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 136 bytes in 1 blocks are still reachable in loss record 5 of 9
==21515==    at 0x484DA83: calloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x48750C3: XShmCreateImage (in /usr/lib/x86_64-linux-gnu/libXext.so.6.4.0)
==21515==    by 0x116416: mlx_int_new_xshm_image (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x116712: mlx_new_image (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x112C62: new_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x112B37: img_to_mlx_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110F14: render_to_window (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCE3: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 1,280 bytes in 1 blocks are still reachable in loss record 6 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111C4F: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110335: create_map (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCB7: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 6,144 bytes in 1 blocks are still reachable in loss record 7 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111C4F: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FC67: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 194,560 bytes in 160 blocks are still reachable in loss record 8 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111CA5: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x110335: create_map (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FCB7: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== 4,196,352 bytes in 768 blocks are still reachable in loss record 9 of 9
==21515==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==21515==    by 0x1187B2: ft_calloc (ft_calloc.c:21)
==21515==    by 0x111CA5: init_img (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515==    by 0x10FC67: main (in /home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus)
==21515== 
==21515== LEAK SUMMARY:
==21515==    definitely lost: 0 bytes in 0 blocks
==21515==    indirectly lost: 0 bytes in 0 blocks
==21515==      possibly lost: 0 bytes in 0 blocks
==21515==    still reachable: 4,398,776 bytes in 935 blocks
==21515==         suppressed: 0 bytes in 0 blocks
==21515== 

Heap buffer overflow in create_map

step to reproduce :
./cub3D_bonus maps/map1_bonus.cub avec fsanitize.

`➜ cub3D git:(fix_parsing) ✗ ./cub3D_bonus maps/map1_bonus.cub > log

==12427==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6030000f1a3a at pc 0x558e6b88e9e3 bp 0x7ffd830b08a0 sp 0x7ffd830b0890
READ of size 1 at 0x6030000f1a3a thread T0
#0 0x558e6b88e9e2 in create_map bonus/srcs/minimap_bonus.c:33
#1 0x558e6b88d248 in main bonus/srcs/main_bonus.c:35
#2 0x7fec856abd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#3 0x7fec856abe3f in __libc_start_main_impl ../csu/libc-start.c:392
#4 0x558e6b88ce44 in _start (/home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus+0x9e44)

0x6030000f1a3a is located 0 bytes to the right of 26-byte region [0x6030000f1a20,0x6030000f1a3a)
allocated by thread T0 here:
#0 0x7fec85bb6887 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
#1 0x558e6b8a90e5 in ft_strdup (/home/gd-harco/Documents/42/level_4/cub3D/cub3D_bonus+0x260e5)
#2 0x558e6b89c530 in parsing bonus/srcs/parsing/parsing_bonus.c:34
#3 0x558e6b88d07b in main bonus/srcs/main_bonus.c:26
#4 0x7fec856abd8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: heap-buffer-overflow bonus/srcs/minimap_bonus.c:33 in create_map
Shadow bytes around the buggy address:
0x0c06800162f0: fd fd fd fa fa fa fd fd fd fa fa fa fd fd fd fa
0x0c0680016300: fa fa fd fd fd fa fa fa fd fd fd fa fa fa fd fd
0x0c0680016310: fd fa fa fa fd fd fd fa fa fa fd fd fd fa fa fa
0x0c0680016320: fd fd fd fa fa fa fd fd fd fa fa fa fd fd fd fa
0x0c0680016330: fa fa fd fd fd fa fa fa fd fd fd fd fa fa fd fd
=>0x0c0680016340: fd fd fa fa 00 00 00[02]fa fa 00 00 00 02 fa fa
0x0c0680016350: fd fd fd fa fa fa fd fd fd fd fa fa 00 00 00 02
0x0c0680016360: fa fa fd fd fd fa fa fa 00 00 00 02 fa fa 00 00
0x0c0680016370: 00 02 fa fa 00 00 00 02 fa fa 00 00 00 02 fa fa
0x0c0680016380: 00 00 00 02 fa fa fd fd fd fa fa fa 00 00 00 02
0x0c0680016390: fa fa 00 00 00 02 fa fa 00 00 00 02 fa fa 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==12427==ABORTING
`

img memory leaks

True for Mandatory and Bonus part, see attached valgrind log file.

Step to reproduce :

Make re
valgrind --leak-check=full --show-leak-kinds=all ./cub3D maps/good/dungeon.cubvalgrind --leak-check=full --show-leak-kinds=all --log-file=val.log ./cub3D maps/good/dungeon.cub

val.log

Quit immediately

incompatible type in get_textures

srcs/parsing/get_textures.c: In function ‘open_texture’:
srcs/parsing/get_textures.c:70:19: error: incompatible types when assigning to type ‘t_uint_img’ {aka ‘struct s_uint_img’} from type ‘t_uint_img *’ {aka ‘struct s_uint_img *’}
70 | = ft_xpm_to_img(data->mlx,
| ^~~~~~~~~~~~~
srcs/parsing/get_textures.c:75:19: error: incompatible types when assigning to type ‘t_uint_img’ {aka ‘struct s_uint_img’} from type ‘t_uint_img *’ {aka ‘struct s_uint_img *’}
75 | = ft_xpm_to_img(data->mlx,
| ^~~~~~~~~~~~~
srcs/parsing/get_textures.c:80:19: error: incompatible types when assigning to type ‘t_uint_img’ {aka ‘struct s_uint_img’} from type ‘t_uint_img *’ {aka ‘struct s_uint_img *’}
80 | = ft_xpm_to_img(data->mlx,
| ^~~~~~~~~~~~~
srcs/parsing/get_textures.c:85:19: error: incompatible types when assigning to type ‘t_uint_img’ {aka ‘struct s_uint_img’} from type ‘t_uint_img *’ {aka ‘struct s_uint_img *’}
85 | = ft_xpm_to_img(data->mlx,
| ^~~~~~~~~~~~~
make: *** [Makefile:34 : srcs/parsing/get_textures.o] Erreur 1

Conflicting type for "ft_xpm_to_img"

srcs/img/ft_xpm_to_img.c:27:12: error: conflicting types for 'ft_xpm_to_img'
t_uint_img ft_xpm_to_img(void *mlx_ptr, char *path)
^
incs/img.h:180:13: note: previous declaration is here
t_uint_img *ft_xpm_to_img(void *mlx_ptr, char *path);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.