first ouput

This commit is contained in:
victor 2025-05-31 19:08:53 +02:00
commit ec4544060d
153 changed files with 11738609 additions and 0 deletions

52
.gitignore vendored Normal file
View File

@ -0,0 +1,52 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

184
Dockerfile Normal file
View File

@ -0,0 +1,184 @@
FROM ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
# Update with future time fix and install build dependencies
RUN apt-get -o Acquire::Max-FutureTime=86400 update -y
RUN apt-get install -y build-essential xorg libx11-dev libxext-dev libbsd-dev libpthread-stubs0-dev libasound2-dev libxrandr-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxcursor-dev libxinerama-dev libwayland-dev libxkbcommon-dev
# Build the miniRT project
WORKDIR /best_rt
COPY . .
RUN make
# Install VNC and graphics dependencies
RUN apt-get update && apt-get install -y \
tigervnc-standalone-server \
python3 \
python3-pip \
wget \
git \
openbox \
x11-utils \
libgl1-mesa-dev \
libglu1-mesa-dev \
net-tools \
procps \
lsof \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install websockify
RUN pip3 install websockify --break-system-packages
# Install noVNC
RUN git clone https://github.com/novnc/noVNC.git /opt/noVNC
# Create VNC directory and set password
RUN mkdir -p /root/.vnc && \
echo "password" | vncpasswd -f > /root/.vnc/passwd && \
chmod 600 /root/.vnc/passwd
RUN rm /opt/noVNC/*.html
RUN mv /best_rt/vnc/minimal.html /opt/noVNC/vnc.html
RUN echo '<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<title>miniRT</title>\n\
<style>\n\
body { margin: 0; padding: 0; background: #000; overflow: hidden; }\n\
iframe { width: 100vw; height: 100vh; border: none; }\n\
</style>\n\
</head>\n\
<body>\n\
<iframe src="/clean.html"></iframe>\n\
</body>\n\
</html>' > /opt/noVNC/minimal.html
# Create VNC startup script that keeps session alive
RUN echo '#!/bin/bash\n\
export USER=root\n\
export HOME=/root\n\
xsetroot -solid grey\n\
# Start openbox to keep session alive\n\
openbox-session &\n\
# Keep the session running\n\
while true; do sleep 3600; done\n\
' > /root/.vnc/xstartup && chmod +x /root/.vnc/xstartup
# Create connection monitor script
RUN echo '#!/bin/bash\n\
\n\
APP_PID=""\n\
CLIENT_TIMEOUT=30 # Stop app after 30 seconds of no clients\n\
CHECK_INTERVAL=5 # Check every 5 seconds\n\
\n\
log() {\n\
echo "[$(date "+%H:%M:%S")] $1"\n\
}\n\
\n\
has_vnc_clients() {\n\
# Check for active VNC connections\n\
netstat -tn 2>/dev/null | grep -q ":5901.*ESTABLISHED"\n\
}\n\
\n\
has_websocket_clients() {\n\
# Check for active WebSocket connections (noVNC)\n\
netstat -tn 2>/dev/null | grep -q ":6080.*ESTABLISHED"\n\
}\n\
\n\
start_app() {\n\
if [ -z "$APP_PID" ] || ! kill -0 "$APP_PID" 2>/dev/null; then\n\
log "Starting miniRT application..."\n\
cd /best_rt\n\
/best_rt/miniRT /best_rt/scenes/multilight.rt &\n\
APP_PID=$!\n\
log "miniRT started with PID: $APP_PID"\n\
fi\n\
}\n\
\n\
stop_app() {\n\
if [ -n "$APP_PID" ] && kill -0 "$APP_PID" 2>/dev/null; then\n\
log "Stopping miniRT application (PID: $APP_PID)..."\n\
kill "$APP_PID" 2>/dev/null\n\
wait "$APP_PID" 2>/dev/null\n\
APP_PID=""\n\
log "miniRT stopped"\n\
fi\n\
}\n\
\n\
# Main monitoring loop\n\
last_client_time=0\n\
\n\
while true; do\n\
current_time=$(date +%s)\n\
\n\
if has_vnc_clients || has_websocket_clients; then\n\
# Client connected\n\
last_client_time=$current_time\n\
start_app\n\
else\n\
# No clients\n\
if [ "$last_client_time" -gt 0 ]; then\n\
time_since_last_client=$((current_time - last_client_time))\n\
if [ "$time_since_last_client" -gt "$CLIENT_TIMEOUT" ]; then\n\
stop_app\n\
last_client_time=0\n\
fi\n\
fi\n\
fi\n\
\n\
sleep "$CHECK_INTERVAL"\n\
done\n\
' > /monitor-clients.sh && chmod +x /monitor-clients.sh
# Create smart startup script
RUN echo '#!/bin/bash\n\
export USER=root\n\
export HOME=/root\n\
export DISPLAY=:1\n\
\n\
log() {\n\
echo "[$(date "+%H:%M:%S")] $1"\n\
}\n\
\n\
log "Starting VNC server..."\n\
vncserver :1 -geometry 1600x900 -depth 24 -localhost no\n\
sleep 5\n\
\n\
log "Starting noVNC web interface..."\n\
cd /opt/noVNC\n\
python3 -m websockify --web . 6080 localhost:5901 &\n\
NOVNC_PID=$!\n\
\n\
sleep 3\n\
xhost + 2>/dev/null || true\n\
\n\
log "VNC ready at http://localhost:6080/vnc.html"\n\
log "Starting client monitor..."\n\
log "miniRT will start automatically when a client connects"\n\
log "miniRT will stop automatically when no clients for 30 seconds"\n\
\n\
# Start the client monitor\n\
/monitor-clients.sh &\n\
MONITOR_PID=$!\n\
\n\
# Handle cleanup on exit\n\
cleanup() {\n\
log "Shutting down..."\n\
kill $NOVNC_PID 2>/dev/null\n\
kill $MONITOR_PID 2>/dev/null\n\
vncserver -kill :1 2>/dev/null\n\
exit 0\n\
}\n\
\n\
trap cleanup SIGTERM SIGINT\n\
\n\
# Wait for processes\n\
wait\n\
' > /start.sh && chmod +x /start.sh
EXPOSE 6080 5901
CMD ["/start.sh"]

117
Makefile Normal file
View File

@ -0,0 +1,117 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/08 16:30:33 by victor #+# #+# #
# Updated: 2025/05/31 15:42:54 by victor ### ########.fr #
# #
# **************************************************************************** #
CC := cc
CFLAGS := -Wall -Werror -Wextra -g3
LDFLAGS := -lm
NAME := miniRT
SRCDIR := src
SRC := minirt.c
SCENEDIR := scene
SRCSCENE := $(addprefix $(SCENEDIR)/, $(addsuffix .c,\
camera camera_info camera_ray_define \
light scene scene_create sphere sphere_calc \
cylinder cylinder_info cylinder_calc cylinder_uv \
disk disk_calc plane plane_calc add_body \
cone_tracing cone_utils cone_info))
RENDERDIR := rendering
SRCRENDER := $(addprefix $(RENDERDIR)/, $(addsuffix .c,\
rendering image pixel ray_utils color drawing threads \
color2 light_shadow phong))
IODIR := io
SRCIO := $(addprefix $(IODIR)/, $(addsuffix .c,\
ppm id bump mouse_press mouse key_press keys parsing2 parsing glyph \
item_value_manip items_create menu slider container \
key_change_scene buffer menu_body_create menu_body_map \
menu_value_add menu_value_map explorer explorer_helper \
explorer_load_file menu_file_map))
UTILDIR := utils
SRCUTIL := $(addprefix $(UTILDIR)/, $(addsuffix .c,\
minirt_utils vector_math2 vector_math vector \
detect_overflow_utils detect_overflow))
LIBS := libft/libft.a memory/memory.a raylib/src/libraylib.a
MAP := mapgen
OBJDIR := obj
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
OBJRENDER := $(SRCRENDER:%.c=$(OBJDIR)/%.o)
OBJSCENE := $(SRCSCENE:%.c=$(OBJDIR)/%.o)
OBJIO := $(SRCIO:%.c=$(OBJDIR)/%.o)
OBJUTIL := $(SRCUTIL:%.c=$(OBJDIR)/%.o)
ifdef DEBUG
CFLAGS += -D VALGRIND
CFLAGS += -g3
else
CFLAGS += -O3
endif
all: $(OBJDIR) $(NAME)
$(NAME): $(OBJ) $(LIBS) minirt.h $(OBJIO) $(OBJSCENE) $(OBJUTIL) $(OBJRENDER)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(OBJIO) $(OBJSCENE) $(OBJUTIL) $(OBJRENDER) $(LIBS) $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/$(SCENEDIR)/%.c minirt.h
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/$(RENDERDIR)/%.c minirt.h
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/$(UTILDIR)/%.c minirt.h
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/$(IODIR)/%.c minirt.h
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.c minirt.h
$(CC) $(CFLAGS) -c $< -o $@
$(MAP): src/mapgeb/map_gen_buffer.c src/mapgeb/map.c
make all
$(CC) $(CFLAGS) $^ src/io/buffer.c $(LIBS) -o $@ -lm
$(LIBS):
make -C libft
make -C memory
make -C libft/printf
make -C raylib/src PLATFORM=PLATFORM_DESKTOP
$(OBJDIR):
mkdir -p $(OBJDIR) $(OBJDIR)/$(SRCDIR) $(OBJDIR)/$(RENDERDIR) $(OBJDIR)/$(IODIR) $(OBJDIR)/$(UTILDIR) $(OBJDIR)/$(SCENEDIR)
clean:
rm -rf $(OBJDIR)
make clean -C libft
make clean -C memory
make clean -C libft/printf
make clean -C raylib/src
fclean: clean
rm -rf $(NAME) $(MAP)
make fclean -C libft
make fclean -C memory
make fclean -C libft/printf
re: fclean all
.PHONY: all clean fclean re debug

121
README.md Normal file
View File

@ -0,0 +1,121 @@
This minimalistic raytracing engine is a school project which is still ongoing.
It is using the [Minilibx library](https://github.com/42Paris/minilibx-linux).
All code besides the directory minilibx-linux is our work.
Big thanks to my teammate [vman101](https://github.com/vman101) for implementing many fantastic creative ideas that go way beyond the scope of what is required by the school.
Credits go to him for his libft and his garbage collector in the memory directory.
We are still working on this fun raytracing engine so there is more to come!
# Run in a Docker container:
```bash
git clone git@github.com:Fapad/miniRT.git
cd miniRT
docker build -t minirt .
docker run --rm -e DISPLAY="$DISPLAY" -v /tmp/.X11-unix:/tmp/.X11-unix minirt
```
If you get the error "Authorization required, but no authorization protocol specified", you might need to enable X11 forwarding locally
```bash
xhost + local:
docker run --rm -e DISPLAY="$DISPLAY" -v /tmp/.X11-unix:/tmp/.X11-unix minirt
```
# Or compile with make
## 1. Required Dependencies
```bash
sudo apt-get update
sudo apt-get install build-essential libpthread-stubs0-dev xorg libx11-dev libxext-dev libbsd-dev
```
## 2. clone the repo and compile
```bash
git clone git@github.com:Fapad/miniRT.git
cd miniRT
make
```
## 3. launch the executable
either without arguments,
```bash
./miniRT
```
or with the path to a specific scene file as argument
```bash
./miniRT scenes/eclipse.rt
```
# Navigation:
change resolution/anti-aliasing: numpad + or -
move camera or object: WASDQE
rotate camera or object: arrow keys
change object color, reflection, texture: use object menu
object menu: right click
undo object selection: left click
move object on xy axis: lmb + drag
add object: click menu item
change field of view: mouse wheel
# the .rt file describes a 3d scene
◦ Each type of information from an element can be separated by one or more
space(s).
◦ Each type of element can be set in any order in the file.
◦ Elements which are defined by a capital letter can only be declared once in
the scene.
Each element firsts information is the type identifier (composed by one or two character(s)), followed by all specific information for each object in a strict order such as:
◦ Ambient lightning:
identifier: A
ambient lighting ratio in range [0.0,1.0]: 0.2 R,G,B colors in range [0-255]: 255, 255, 255
◦ Camera:
identifier: C
x,y,z coordinates of the view point: -50.0,0,20
orientation vector for each x,y,z axis:
0.0,0.0,1.0
FOV : Horizontal field of view in degrees in range [0,180]: 70
◦ Light:
identifier: L
x,y,z coordinates of the light point: -40.0,50.0,0.0
the light brightness ratio in range [0.0,1.0]: 0.6
(unused in mandatory part)R,G,B colors in range [0-255]: 10, 0, 255
◦ Sphere:
identifier: sp
x,y,z coordinates of the sphere center: 0.0,0.0,20.6 the sphere diameter: 12.6
R,G,B colors in range [0-255]: 10, 0, 255
◦ Plane:
identifier: pl
x,y,z coordinates of a point in the plane: 0.0,0.0,-10.0
3d vector perpendicular to the plane. for each x,y,z axis: 0.0,1.0,0.0 R,G,B colors in range [0-255]: 0,0,225
◦ Cylinder:
identifier: cy
x,y,z coordinates of the center of the cylinder: 50.0,0.0,20.6
3D vector x,y,z of axis of cylinder
the cylinder diameter: 14.2
the cylinder height: 21.42
R,G,B colors in range [0,255]: 10, 0, 255
Cone:
identifier: cn
x,y,z coordinates of the vertex of the cone: 50.0,0.0,20.6
3D vector x,y,z of axis of cone
the diameter of the bottom disk of the cone: 14.2
the cone height: 21.42
R,G,B colors in range [0,255]: 10, 0, 255

647
assets/alpha_bit_bonus Normal file
View File

@ -0,0 +1,647 @@
000000
000000
000000
000000
000000
000000
000000
000000
001000
001000
001000
001000
000000
001000
000000
010100
010100
000000
000000
000000
000000
000000
010100
111110
010100
111110
010100
010100
001000
011110
101000
011100
001010
111100
001000
000000
110010
110100
001000
010110
100110
000000
000000
011000
100100
101000
010100
100010
011100
000000
001000
001000
000000
000000
000000
000000
000000
000100
001000
001000
001000
001000
000100
000000
001000
000100
000100
000100
000100
001000
000000
000000
010100
001000
111110
001000
010100
000000
000000
001000
001000
111110
001000
001000
000000
000000
000000
000000
001000
001000
010000
000000
000000
000000
111110
000000
000000
000000
000000
000000
000000
000000
000000
001100
001100
000000
000010
000100
001000
010000
100000
000000
000000
011100
100010
100110
101010
110010
011100
000000
001000
011000
001000
001000
001000
011100
000000
011100
100010
000010
001100
010000
111110
000000
011100
100010
000100
000010
100010
011100
000000
000100
001100
010100
100100
111110
000100
000000
111110
100000
111100
000010
000010
111100
000000
011110
100000
111100
100010
100010
011100
000000
111110
000010
000100
001000
001000
001000
000000
011100
100010
011100
100010
100010
011100
000000
011100
100010
100010
011110
000010
111100
000000
011100
100010
100010
111110
100010
100010
000000
111100
100010
111100
100010
100010
111100
000000
011110
100000
100000
100000
100000
011110
000000
111100
100010
100010
100010
100010
111100
000000
111110
100000
111100
100000
100000
111110
000000
111110
100000
111100
100000
100000
100000
000000
011110
100000
100000
100110
100010
011110
000000
100010
100010
111110
100010
100010
100010
000000
011100
001000
001000
001000
001000
011100
000000
000110
000010
000010
000010
100010
011100
000000
100010
100100
111000
100100
100010
100010
000000
100000
100000
100000
100000
100000
111110
000000
100010
110110
101010
100010
100010
100010
000000
100010
110010
101010
100110
100010
100010
000000
011100
100010
100010
100010
100010
011100
000000
111100
100010
111100
100000
100000
100000
000000
011100
100010
100010
100010
101010
011100
000000
111100
100010
111100
100100
100010
100010
000000
011110
100000
011100
000010
000010
111100
000000
111110
001000
001000
001000
001000
001000
000000
100010
100010
100010
100010
100010
011100
000000
100010
100010
100010
100010
010100
001000
000000
100010
100010
100010
101010
110110
100010
000000
100010
010100
001000
010100
100010
100010
000000
100010
010100
001000
001000
001000
001000
000000
111110
000010
000100
001000
010000
111110
000000
000000
011110
000010
011110
100010
011110
000000
100000
100000
111100
100010
100010
111100
000000
000000
011100
100010
100000
100010
011100
000000
000010
000010
011110
100010
100010
011110
000000
000000
011100
100010
111110
100000
011110
000000
011100
100010
100000
111100
100000
100000
000000
000000
011110
100010
011110
000010
011100
000000
100000
100000
101100
110010
100010
100010
000000
110000
000000
111000
100000
100000
100000
000000
001100
000000
001100
000100
100100
011000
000000
100100
101000
110000
111000
100100
100010
000000
100000
100000
100000
100000
100010
011100
000000
000000
110100
101010
101010
101010
101010
000000
000000
101100
110010
100010
100010
100010
000000
000000
011100
100010
100010
100010
011100
000000
000000
111100
100010
111100
100000
100000
000000
000000
011110
100010
011110
000010
000010
000000
000000
101100
110010
100000
100000
100000
000000
000000
011110
100000
011100
000010
111100
000000
000000
010000
111100
010000
010010
001100
000000
000000
100010
100010
100010
100010
011100
000000
000000
100010
100010
100010
010100
001000
000000
000000
100010
100010
101010
101010
010100
000000
000000
100010
010100
001000
010100
100010
000000
000000
100010
011110
000010
100010
011100
000000
000000
111110
000100
001000
010000
111110
000000
000000
000000
000000
000000
000000
111110
000000
000000
000000
010000
000000
010000
000000
000000
000000
111110
000000
111110
000000
000000

500002
assets/bump maps/earth.bump Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

1433602
assets/bump maps/marble.bump Normal file

File diff suppressed because it is too large Load Diff

109272
assets/bump maps/moon.bump Normal file

File diff suppressed because it is too large Load Diff

1500005
assets/textures/earth.ppm Normal file

File diff suppressed because it is too large Load Diff

4300804
assets/textures/marble.ppm Normal file

File diff suppressed because it is too large Load Diff

327813
assets/textures/moon.ppm Normal file

File diff suppressed because it is too large Load Diff

3056980
assets/textures/sky.ppm Normal file

File diff suppressed because it is too large Load Diff

11
include/types.h Normal file
View File

@ -0,0 +1,11 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 16:38:11 by victor #+# #+# */
/* Updated: 2024/10/06 18:25:41 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */

72
libft/Makefile Normal file
View File

@ -0,0 +1,72 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/17 14:23:19 by vvobis #+# #+# #
# Updated: 2025/05/31 12:28:02 by victor ### ########.fr #
# #
# **************************************************************************** #
NAME := libft.a
CC := cc
CFLAGS:= -Wall -Wextra -Werror
SRC := ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isdigit.c \
ft_isprint.c ft_memcpy.c ft_memmove.c ft_memset.c \
ft_strlcat.c ft_strlcpy.c ft_strlen.c \
ft_isascii.c ft_strchr.c ft_strrchr.c ft_strncmp.c \
ft_toupper.c ft_tolower.c ft_memchr.c ft_strnstr.c \
ft_atoi.c ft_atod.c ft_memcmp.c ft_calloc.c ft_strdup.c \
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c \
ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \
ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \
ft_isspace.c ft_free.c ft_read.c
SRCGNL := gnl/get_next_line.c gnl/get_next_line_utils.c
SRCPRINT := printf/ft_fprintf.c printf/ft_printf.c printf/ft_putascii.c printf/ft_putptr.c printf/ft_puthex.c printf/ft_putfloat.c
SRCBON := ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c
OBJBON := $(SRCBON:%.c=%.o)
OBJ := $(SRC:%.c=%.o)
OBJGNL := $(SRCGNL:%.c=%.o)
OBJPRINT := $(SRCPRINT:%.c=%.o)
LIBS := printf
all: $(NAME)
bonus: $(OBJBON) $(OBJ)
ar rcs libft.a $(OBJ) $(OBJBON)
$(NAME): $(OBJ) $(OBJGNL) $(OBJPRINT) $(LIBS)
ar rcs libft.a $(OBJ) $(OBJGNL) $(OBJPRINT)
$(LIBS):
make -C $(LIBS)
%.o: %.c
$(CC) -c $(CFLAGS) $^ -o $@
re: fclean all
so:
$(CC) -fPIC $(CFLAGS) $(SRC) test.c
gcc -shared -o libft.so $(OBJ) $(OBJBON)
fclean: clean
rm -f $(NAME)
make fclean -C $(LIBS)
clean:
rm -f $(OBJ) $(OBJBON) $(OBJGNL)
make clean -C $(LIBS)

57
libft/ft_atod.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 22:25:35 by victor #+# #+# */
/* Updated: 2024/12/05 20:08:42 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
double atod_collect_fraction(char *n, double d)
{
int period;
int mult;
mult = 1;
period = 0;
while (*n && !ft_isspace(*n))
{
if (*n == '.' && period == 0)
{
period = 1;
n++;
continue ;
}
else if (((*n == '.' && period == 1) || (*n != '.' && !ft_isdigit(*n))))
return (ft_putendl_fd("Invalid double format: Too many periods", \
2), exit(1), 0);
if (period)
mult *= 10;
d *= 10;
d += (*n - '0');
n++;
}
return (d / mult);
}
double ft_atod(char *n)
{
double d;
int sign;
if (!n)
exit(1);
sign = 1;
if (*n == '-')
{
sign = -1;
n++;
}
d = 0;
return (atod_collect_fraction(n, d) * sign);
}

60
libft/ft_atoi.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 10:49:04 by vvobis #+# #+# */
/* Updated: 2024/09/13 22:19:12 by bszilas ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_space(char const c)
{
if ((c >= 9 && c <= 13) || c == ' ')
return (1);
return (0);
}
int ft_atoi(char const *s)
{
int nb;
char const *tmp;
nb = 0;
while (is_space(*s))
s++;
tmp = s;
if (*tmp == '+' || *tmp == '-')
tmp++;
while (*tmp >= '0' && *tmp <= '9')
{
nb *= 10;
nb += (*tmp - '0');
tmp++;
}
if (*s == '-')
nb = -nb;
return (nb);
}
long ft_atol(const char *nptr)
{
int64_t n;
int sign;
n = 0;
sign = 1;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '-')
sign = -1;
if (sign == -1 || *nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
n = n * 10 + (*nptr++ - '0');
return (n * sign);
}

19
libft/ft_bzero.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:04:55 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:53 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
while (n--)
*(char *)s++ = 0;
}

30
libft/ft_calloc.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 13:44:00 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:39:56 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

22
libft/ft_free.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
/* Updated: 2024/09/10 12:17:34 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_free(void *ptr_ptr)
{
void **ptr;
ptr = ptr_ptr;
free(*ptr);
*ptr = NULL;
}

20
libft/ft_isalnum.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:18 by vvobis #+# #+# */
/* Updated: 2024/04/02 15:59:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isdigit(c) || ft_isalpha(c))
return (1);
return (0);
}

20
libft/ft_isalpha.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

20
libft/ft_isascii.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:48:35 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:49:36 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

18
libft/ft_isdigit.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:14:56 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:38:45 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

18
libft/ft_isprint.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:00:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:52 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

18
libft/ft_isspace.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 18:27:49 by victor #+# #+# */
/* Updated: 2024/12/16 16:34:17 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(int c)
{
return (c == ' ' || (c >= 9 && c < 14));
}

75
libft/ft_itoa.c Normal file
View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:16:48 by vvobis #+# #+# */
/* Updated: 2024/04/11 14:48:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_len(int n)
{
int i;
if (n < 0)
n = -n;
i = 0;
while (n)
{
i++;
n /= 10;
}
return (i);
}
static char *is_negative(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 2);
if (!num)
return (NULL);
num[i + 1] = 0;
while (n)
{
num[i--] = -(n % 10) + 48;
n /= 10;
}
num[i] = 0x2d;
return (num);
}
static char *is_positive(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 1);
if (!num)
return (NULL);
num[i--] = 0;
while (n)
{
num[i--] = (n % 10) + 48;
n /= 10;
}
return (num);
}
char *ft_itoa(int n)
{
if (n == 0)
return (ft_strdup("0"));
else if (n < 0)
return (is_negative(n));
else
return (is_positive(n));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:51:32 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:12 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
if (!new)
return ;
if (*lst)
{
tmp = *lst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
else
*lst = new;
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 12:53:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:50 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!lst || !new)
return ;
if (!*lst)
*lst = new;
else
{
new->next = *lst;
*lst = new;
}
}

28
libft/ft_lstclear_bonus.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:44:23 by vvobis #+# #+# */
/* Updated: 2024/04/09 22:35:41 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (!*lst || !del || !lst)
return ;
while (*lst)
{
tmp = (*lst)->next;
del((*lst)->content);
free(*lst);
*lst = tmp;
}
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 14:24:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:42:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

24
libft/ft_lstiter_bonus.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:57:51 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:20:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

22
libft/ft_lstlast_bonus.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:47:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:11:51 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

44
libft/ft_lstmap_bonus.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 16:18:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:40:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *free_list(t_list **head, void (*del)(void *), void *fcontent)
{
if (fcontent)
del(fcontent);
ft_lstclear(head, del);
return (NULL);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tmp;
t_list *head;
void *fcontent;
if (!lst || !f || !del)
return (NULL);
head = NULL;
while (lst)
{
fcontent = f(lst->content);
if (!fcontent)
return (free_list(&head, del, fcontent));
tmp = ft_lstnew(fcontent);
if (!tmp)
return (free_list(&head, del, fcontent));
ft_lstadd_back(&head, tmp);
lst = lst->next;
}
return (head);
}

25
libft/ft_lstnew_bonus.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 11:10:05 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:59:48 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *tmp;
tmp = malloc(sizeof(*tmp));
if (!tmp)
return (NULL);
tmp->next = NULL;
tmp->content = content;
return (tmp);
}

28
libft/ft_lstsize_bonus.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:43:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:21:11 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
if (!lst)
return (0);
i = 1;
while (lst->next)
{
lst = lst->next;
i++;
}
return (i);
}

31
libft/ft_memchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:13:59 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:55:24 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(void *s, int c, size_t n)
{
unsigned char *p;
size_t i;
i = 0;
p = (unsigned char *)s;
while (i < n)
{
if (p[i] == (unsigned char)c)
return ((void *)&p[i]);
i++;
}
if (p[i - 1] == (unsigned char)c)
return ((void *)&p[i]);
return (NULL);
}

31
libft/ft_memcmp.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:27:15 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(void *s1, void const *s2, size_t n)
{
unsigned char *p1;
unsigned char *p2;
unsigned int i;
p1 = (unsigned char *)s1;
p2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (p1[i] != p2[i])
return (p1[i] - p2[i]);
i++;
}
return (0);
}

32
libft/ft_memcpy.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:13:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, void const *src, size_t n)
{
char *d;
char *s;
if (n == 0 || (dest == NULL && src == NULL))
return (dest);
if (dest == NULL || src == NULL)
{
*(char *)dest = 1;
*(char *)src = 1;
}
d = (char *) dest;
s = (char *) src;
while (n--)
*d++ = *s++;
return (dest);
}

30
libft/ft_memmove.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:42:28 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:54 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, void const *src, size_t n)
{
unsigned char *d;
unsigned char *s;
if (!dest && !src)
return (dest);
d = (unsigned char *)dest;
s = (unsigned char *)src;
if (s < d)
while (n--)
d[n] = s[n];
else
ft_memcpy(dest, (void *)src, n);
return (dest);
}

23
libft/ft_memset.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:57:27 by vvobis #+# #+# */
/* Updated: 2024/04/03 11:18:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
char *str;
str = (char *)s;
while (n--)
str[n] = c;
return (s);
}

18
libft/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:34:30 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:53:33 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

21
libft/ft_putendl_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:57:33 by vvobis #+# #+# */
/* Updated: 2024/04/07 18:00:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *str, int fd)
{
if (!str)
return ;
ft_putstr_fd(str, fd);
ft_putchar_fd(0x0a, fd);
}

27
libft/ft_putnbr_fd.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 18:02:14 by vvobis #+# #+# */
/* Updated: 2024/04/11 11:23:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n < 0)
ft_putchar_fd(0x2d, fd);
if (n <= -10)
ft_putnbr_fd(n / -10, fd);
if (n >= 10)
ft_putnbr_fd(n / 10, fd);
if (n >= 0)
ft_putchar_fd(n % 10 + 0x30, fd);
if (n < 0)
ft_putchar_fd(-(n % -10) + 0x30, fd);
}

21
libft/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:48:41 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:52:16 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *str, int fd)
{
if (!str)
return ;
while (*str)
ft_putchar_fd(*str++, fd);
}

26
libft/ft_read.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 12:49:30 by vvobis #+# #+# */
/* Updated: 2024/11/18 12:50:46 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_read(int fd, char *character, unsigned int size_to_read)
{
int bytes_read;
bytes_read = read(fd, character, size_to_read);
if (bytes_read == -1)
{
ft_fprintf(STDERR_FILENO, "Read failed\n");
exit (1);
}
return (bytes_read);
}

84
libft/ft_split.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:14:20 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:55:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(char const *s, char const c)
{
int n;
n = 0;
if (*s != c && *s)
n = 1;
while (*s)
{
if (*s == c && *(s + 1) != c && *(s + 1))
n++;
s++;
}
return (n);
}
static int count_sub(char const *s, char const c)
{
int i;
i = 0;
while (*s != c && *s)
{
i++;
s++;
}
return (i);
}
static char **free_all(char **back)
{
char **tmp;
tmp = back;
while (*back)
{
free(*back);
back++;
}
free(tmp);
return (NULL);
}
char **ft_split(char const *s, char const c)
{
char **tmp;
char **back;
if (!s)
return (NULL);
tmp = (char **)ft_calloc(sizeof(*tmp), count(s, c) + 1);
if (!tmp)
return (NULL);
back = tmp;
while (*s && tmp)
{
while (*s == c && *s)
s++;
if (*s)
{
*tmp = ft_substr(s, 0, count_sub(s, c));
if (!*tmp)
return (free_all(back));
}
tmp++;
while (*s != c && *s)
s++;
}
return (back);
}

29
libft/ft_strchr.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(char const *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
return (NULL);
}

31
libft/ft_strdup.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:44:05 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:44:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(char const *s)
{
char *tmp;
int i;
i = 0;
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (s[i])
{
tmp[i] = s[i];
i++;
}
tmp[i] = 0;
return (tmp);
}

27
libft/ft_striteri.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:24:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:27:07 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char const *s, void (*f)(unsigned int, char *))
{
unsigned int i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, (char *)&s[i]);
i++;
}
}

41
libft/ft_strjoin.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:08:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:07:26 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s1 || !s2)
return (NULL);
tmp = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
tmp[i] = s2[j];
i++;
j++;
}
tmp[i] = 0;
return (tmp);
}

37
libft/ft_strlcat.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 16:10:59 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:41:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, char const *src, size_t size)
{
size_t dlen;
size_t slen;
if (size == 0)
return (ft_strlen(src));
dlen = ft_strlen(dest);
slen = ft_strlen(src);
if (size <= dlen)
return (slen + size);
if (dlen + slen < size)
{
ft_memcpy(&dest[dlen], src, slen);
dest[dlen + slen] = 0;
}
else
{
ft_memcpy(&dest[dlen], src, size - dlen);
dest[size - 1] = 0;
}
return (dlen + slen);
}

32
libft/ft_strlcpy.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:03:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:28:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, char const *src, size_t size)
{
size_t i;
i = 0;
if (size != 0)
{
while (src[i] != '\0' && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
while (src[i] != '\0')
i++;
return (i);
}

27
libft/ft_strlen.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:40:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == SIZE_MAX)
break ;
}
return (i);
}

33
libft/ft_strmapi.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:01:35 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:13:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *tmp;
unsigned int i;
if (!s || !f)
return (NULL);
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s[i])
{
tmp[i] = f(i, s[i]);
i++;
}
tmp[i] = 0;
return (tmp);
}

31
libft/ft_strncmp.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 13:33:22 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:29:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(char const *s1, char const *s2, size_t n)
{
size_t i;
if (n == 0)
return (0);
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if (s1[i] < s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
else if (s1[i] > s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
i++;
}
return (0);
}

36
libft/ft_strnstr.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:36:41 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:42:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(char const *s1, char const *s2, size_t n)
{
size_t i;
size_t j;
if (!*s2)
return ((char *)s1);
i = 0;
while (i < n && s1[i])
{
j = 0;
if (s1[i + j] == s2[j])
{
while (s1[i + j] == s2[j] && s1[i + j] && i + j < n)
j++;
if (!s2[j])
return ((char *)&s1[i]);
}
i++;
}
return (NULL);
}

33
libft/ft_strrchr.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:22 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(char const *s, int c)
{
char *n;
int i;
i = 0;
n = NULL;
while (s[i] != 0)
{
if (s[i] == (char)c)
n = (char *)&s[i];
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
if (n)
return (n);
return (NULL);
}

47
libft/ft_strtrim.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:30:35 by vvobis #+# #+# */
/* Updated: 2024/04/15 13:07:55 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char c, char const *s)
{
while (*s)
if (c == *s++)
return (1);
return (0);
}
static int find_end(char const *s1, char const *set)
{
int i;
i = 0;
while (*s1)
s1++;
while (is_set(*--s1, set))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
if (!s1)
return (NULL);
if (!set)
return ((char *)s1);
while (is_set(*s1, set) && *s1)
s1++;
if ((int)ft_strlen(s1) > find_end(s1, set))
return (ft_substr(s1, 0, ft_strlen(s1) - find_end(s1, set)));
else
return (ft_calloc(1, 1));
}

41
libft/ft_substr.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */
/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= ft_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > ft_strlen(s))
len = ft_strlen(s) - start;
tmp = malloc(sizeof(*tmp) * len + 1);
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
tmp[i] = 0;
return (tmp);
}

20
libft/ft_tolower.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:01:54 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:15:18 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 65 && c <= 90)
c += 32;
return (c);
}

20
libft/ft_toupper.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:18:44 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:19:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
c -= 32;
return (c);
}

109
libft/gnl/get_next_line.c Normal file
View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 20:20:17 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:44:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *buffer_extend(void *ptr_old, size_t size_new, size_t size_old)
{
void *ptr_new;
void *ptr_back;
void *ptr_old_free;
ptr_new = g_calloc(size_new + 1, 1);
if (!ptr_new)
return (free(ptr_old), ptr_old = NULL, NULL);
ptr_back = ptr_new;
ptr_old_free = ptr_old;
while (size_old--)
*(char *)ptr_new++ = *(char *)ptr_old++;
return (free(ptr_old_free), ptr_old_free = NULL, ptr_back);
}
char *handle_no_nl(char **buf, char **left)
{
if (left)
{
free(*left);
*left = NULL;
}
if (!buf)
return (NULL);
if (g_strlen(*buf))
return (*buf);
free(*buf);
*buf = NULL;
return (NULL);
}
char *line_extract(char **buf_joined, char **left, size_t line_len)
{
char *line;
line = g_substr(*buf_joined, 0, line_len);
if (!line)
return (free(*buf_joined), *buf_joined = NULL, NULL);
*left = g_substr(*buf_joined, line_len, g_strlen(*buf_joined) - line_len);
free(*buf_joined);
*buf_joined = NULL;
if (!*left)
return (free(line), line = NULL, NULL);
return (line);
}
char *line_handle(char **buf_fetch)
{
char *buf_joined;
size_t line_len;
static char *left;
if (!buf_fetch)
return (free(left), NULL);
buf_joined = g_strjoin(left, *buf_fetch);
free(*buf_fetch);
*buf_fetch = NULL;
free(left);
left = NULL;
if (!buf_joined)
return (NULL);
line_len = find_newline(buf_joined);
if (line_len)
return (line_extract(&buf_joined, &left, line_len));
return (handle_no_nl(&buf_joined, &left));
}
char *get_next_line(int fd)
{
char *buf;
ssize_t bytes_read;
size_t buf_size_cur;
size_t buf_size_prev;
if (fd < 0)
return (line_handle(NULL), NULL);
buf = g_calloc(sizeof(*buf), BUFFER_SIZE + 1);
buf_size_prev = 0;
buf_size_cur = BUFFER_SIZE;
while (1)
{
if (!buf)
return (line_handle(NULL));
bytes_read = read(fd, buf + buf_size_prev, BUFFER_SIZE);
if (bytes_read < 0)
return (free(buf), buf = NULL, line_handle(NULL));
if (find_newline(buf) || bytes_read == 0)
break ;
buf_size_prev = buf_size_cur;
buf_size_cur += BUFFER_SIZE;
buf = buffer_extend(buf, buf_size_cur, buf_size_prev);
}
return (line_handle(&buf));
}

38
libft/gnl/get_next_line.h Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:07:36 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:43:21 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 50
# endif
# define MAX_FD 1024
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include <stdint.h>
char *get_next_line(int fd);
char *g_strjoin(char const *s1, char const *s2);
size_t g_strlen(char const *str);
void *g_calloc(size_t n, size_t s);
char *g_substr(char const *s, unsigned int start, size_t len);
char *line_handle(char **buf_fetch);
size_t find_newline(char *buf);
#endif

View File

@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:14:13 by vvobis #+# #+# */
/* Updated: 2024/05/20 11:38:03 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t g_strlen(char const *str)
{
size_t i;
if (!str)
return (0);
i = 0;
while (*str)
{
i++;
str++;
}
return (i);
}
size_t find_newline(char *buf)
{
size_t i;
if (!buf)
return (0);
i = 0;
while (*buf)
{
if (*buf == '\n')
return (i + 1);
i++;
buf++;
}
return (0);
}
char *g_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s2)
return (NULL);
if (!s1)
return (g_substr(s2, 0, g_strlen(s2)));
tmp = g_calloc(g_strlen(s1) + g_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
if (s1)
{
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
}
j = 0;
if (s2)
while (s2[j])
tmp[i++] = s2[j++];
return (tmp);
}
char *g_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= g_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > g_strlen(s))
len = g_strlen(s) - start;
tmp = g_calloc(len + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
return (tmp);
}
void *g_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

92
libft/libft.h Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 23:43:36 by vvobis #+# #+# */
/* Updated: 2025/05/31 12:27:14 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include "gnl/get_next_line.h"
# include "./printf/ft_printf.h"
# include <stdlib.h>
# include <unistd.h>
# include <stddef.h>
# include <stdint.h>
# include <limits.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isprint(int c);
int ft_isascii(int c);
int ft_isspace(int c);
/*Memory Management*/
int ft_memcmp(void *s1, void const *s2, size_t n);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, void const *src, size_t n);
void *ft_memmove(void *dest, void const *src, size_t n);
void *ft_memchr(void *s, int c, size_t n);
void *ft_calloc(size_t n, size_t s);
/*Info Conversion*/
int ft_atoi(char const *s);
double ft_atod(char *s);
char *ft_itoa(int n);
char **ft_split(char const *s, char c);
void ft_free(void *ptr_ptr);
long ft_atol(const char *nptr);
/*String Manip*/
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(char const *s1, char const *s2, size_t n);
char *ft_strnstr(char const *s1, char const *s2, size_t n);
char *ft_strchr(char const *s, int c);
char *ft_strrchr(char const *s, int c);
char *ft_strdup(char const *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char const *s, void (*f)(unsigned int, char *));
size_t ft_strlcpy(char *dest, char const *src, size_t size);
size_t ft_strlcat(char *dest, char const *src, size_t size);
size_t ft_strlen(char const *str);
/*List manip*/
int ft_lstsize(t_list *lst);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstiter(t_list *lst, void (*f)(void *));
void ft_lstadd_front(t_list **lst, t_list *node_new);
void ft_lstadd_back(t_list **lst, t_list *node_new);
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
t_list *ft_lstnew(void *content);
t_list *ft_lstlast(t_list *lst);
/*Output*/
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *str, int fd);
void ft_putendl_fd(char *str, int fd);
void ft_putnbr_fd(int n, int fd);
/* Get Next Line */
char *get_next_line(int fd);
#endif

33
libft/printf/Makefile Normal file
View File

@ -0,0 +1,33 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
# Updated: 2025/05/31 12:28:57 by victor ### ########.fr #
# #
# **************************************************************************** #
NAME := libftprintf.a
CC := cc
CFLAGS := -Wall -Werror -Wextra
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
OBJ := $(SRC:.c=.o)
all: $(NAME)
$(NAME): $(OBJ)
ar rsc $@ $(OBJ)
$(OBJ): $(SRC)
$(CC) $(CFLAGS) -c $(SRC)
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all

38
libft/printf/ft_fprintf.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "./ft_printf.h"
int ft_fprintf(int fd, const char *format, ...)
{
va_list args;
int count;
if (!format || fd < 0)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, fd);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, fd))
return (-1);
format++;
}
va_end(args);
return (count);
}

63
libft/printf/ft_printf.c Normal file
View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int handle_arg(va_list args, char format, int *count, int fd)
{
if (format == 'd' || format == 'i')
ft_putnbr(va_arg(args, int), count, fd);
else if (format == 'u')
ft_putnbr(va_arg(args, unsigned int), count, fd);
else if (format == 's')
ft_putstr(va_arg(args, char *), count, fd);
else if (format == 'X')
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
else if (format == 'x')
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
else if (format == 'p')
ft_putptr(va_arg(args, void *), count, fd);
else if (format == 'c')
ft_putchar(va_arg(args, int), count, fd);
else if (format == '%')
ft_putchar('%', count, fd);
else if (format == 'f')
ft_putnbrf(va_arg(args, double), count, 5, fd);
else
return (0);
return (1);
}
int ft_printf(const char *format, ...)
{
va_list args;
int count;
if (!format)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, STDOUT_FILENO);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
return (-1);
format++;
}
va_end(args);
return (count);
}

35
libft/printf/ft_printf.h Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:13:18 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <unistd.h>
int ft_fprintf(int fd, const char *format, ...);
int ft_printf(const char *format, ...);
int handle_arg(va_list args, char format, int *count, int fd);
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
void ft_putchar(int c, int *count, int fd);
void ft_putnbr(long n, int *count, int fd);
void ft_putnbrf(double f, int *count, int precision, int fd);
void ft_putstr(const char *str, int *count, int fd);
void ft_putptr(void *ptr, int *count, int fd);
size_t ft_strlen(const char *s);
#endif

View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(int c, int *count, int fd)
{
write(fd, &c, 1);
*count += 1;
}
void ft_putnbr(long n, int *count, int fd)
{
if (n < 0)
ft_putchar(0x2d, count, fd);
if (n <= -10)
ft_putnbr(n / -10, count, fd);
if (n >= 10)
ft_putnbr(n / 10, count, fd);
if (n >= 0)
ft_putchar(n % 10 + 0x30, count, fd);
if (n < 0)
ft_putchar(-(n % -10) + 0x30, count, fd);
}
void ft_putstr(const char *str, int *count, int fd)
{
if (!str)
{
*count += ft_fprintf(fd, "(null)");
return ;
}
*count += ft_fprintf(fd, str);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
/* Updated: 2024/12/05 14:15:28 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbrf(double f, int *count, int precision, int fd)
{
int number;
double fraction;
unsigned int mult;
char sign;
mult = 1;
sign = 0;
if (f < 0.0 && f > -1.0)
sign = 1;
while (precision-- > 0)
{
mult *= 10;
}
number = (int)f;
fraction = f - (double)number;
if (sign)
ft_putchar('-', count, fd);
ft_putnbr(number, count, fd);
ft_putchar('.', count, fd);
number = fraction * mult;
if (number < 0)
number = -number;
ft_putnbr(number, count, fd);
}

33
libft/printf/ft_puthex.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789ABCDEF";
if (nbr >= 16)
ft_puthex_upper(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789abcdef";
if (nbr >= 16)
ft_puthex_lower(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}

27
libft/printf/ft_putptr.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putptr(void *ptr, int *count, int fd)
{
void **to_print;
if (!ptr)
{
*count += ft_fprintf(fd, "(nil)");
return ;
}
to_print = &ptr;
*count += ft_printf("0x");
ft_puthex_lower((long)*to_print, count, fd);
}

28
libft/printf/ft_strlen.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "limits.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == ULONG_MAX)
break ;
}
return (i);
}

37
memory/Makefile Normal file
View File

@ -0,0 +1,37 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/08 17:46:41 by victor #+# #+# #
# Updated: 2025/05/31 12:29:32 by victor ### ########.fr #
# #
# **************************************************************************** #
CC := cc
CFLAGS := -Wall -Wextra -Werror -g3
NAME := memory.a
SRC := memory.c list.c
OBJ := $(SRC:.c=.o)
all: $(NAME)
$(NAME): $(OBJ) $(LIBS)
ar rcs $(NAME) $(OBJ)
$(OBJ): $(SRC)
$(CC) $(CFLAGS) -c $(SRC)
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all

94
memory/list.c Normal file
View File

@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 17:47:55 by victor #+# #+# */
/* Updated: 2024/09/13 10:16:10 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "memory.h"
INTERN t_clean *lst_node_new(void *content, void (*del)(void *))
{
t_clean *new;
if (!content || !del)
return (NULL);
new = malloc(sizeof(*new));
if (!new)
return (NULL);
new->content = content;
new->clean = del;
new->next = NULL;
return (new);
}
INTERN void lst_node_del(t_clean **lst)
{
(*lst)->clean((*lst)->content);
free(*lst);
*lst = 0;
}
INTERN void lst_node_del_clean(t_clean **lst, void *mem)
{
t_clean *tmp;
t_clean *head;
if (!lst || !*lst)
return ;
head = *lst;
tmp = *lst;
while ((*lst) && (*lst)->next->content != mem)
(*lst) = (*lst)->next;
if (!*lst)
return ;
tmp = *lst;
if ((*lst)->next && (*lst)->next->next)
{
tmp = (*lst)->next->next;
lst_node_del(&(*lst)->next);
(*lst)->next = tmp;
}
else if ((*lst)->next)
lst_node_del(&(*lst)->next);
else
lst_node_del(lst);
*lst = head;
}
INTERN void lst_list_clean(t_clean **head)
{
t_clean *tmp;
while (*head)
{
tmp = (*head)->next;
lst_node_del(head);
*head = tmp;
}
free(*head);
}
INTERN void lst_add_back(t_clean **node, t_clean *new)
{
t_clean *tmp;
if (!new)
{
perror("malloc");
}
if (*node)
{
tmp = *node;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
else
*node = new;
}

35
memory/memory.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 12:21:13 by victor #+# #+# */
/* Updated: 2024/09/12 16:16:33 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "memory.h"
void lst_memory(void *mem, void (*del)(void *c), int mode)
{
static t_clean *list;
t_clean *new;
if (!del)
del = free;
if (mode == FAIL)
return (lst_list_clean(&list), exit(EXIT_FAILURE));
if (mode == END)
return (lst_list_clean(&list));
if (mode == FREE)
return (lst_node_del_clean(&list, mem));
if (!mem)
return (lst_list_clean(&list), perror("malloc"), exit(EXIT_FAILURE));
new = lst_node_new(mem, del);
if (!new)
return (del(mem), lst_list_clean(&list), \
perror("malloc"), exit(EXIT_FAILURE));
lst_add_back(&list, new);
}

56
memory/memory.h Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 17:18:13 by victor #+# #+# */
/* Updated: 2024/09/08 18:03:07 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MEMORY_H
# define MEMORY_H
# include <stdlib.h>
# include <stdio.h>
# define INTERN
enum e_alloc
{
ADD,
FAIL,
END,
FREE,
};
typedef struct s_clean
{
void *content;
void (*clean)(void *del);
struct s_clean *next;
} t_clean;
/* Main memory handling function:
*
* Pointers may be passed to this function and will be stored on a linked list.
* DEL_FUNC is a function pointer that will be used to destroy the
* created node with the specified funtion.
* If DEL_FUNC is NULL, the pointer will be attempted to be freed using free()
*
* MODEs are:
* ADD: adds a node to the end of the list
* FREE: remove the specified node using the address pointed to by MEM
* END: cleans the list
* FAIL: cleans the list and exits with exit code (1)*/
void lst_memory(void *mem, void (*del_func)(void *c), int mode);
INTERN t_clean *lst_node_new(void *content, void (*del)(void *));
INTERN void lst_node_del(t_clean **lst);
INTERN void lst_node_del_clean(t_clean **lst, void *mem);
INTERN void lst_list_clean(t_clean **head);
INTERN void lst_add_back(t_clean **node, t_clean *new_node);
#endif

835
minirt.h Normal file
View File

@ -0,0 +1,835 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minirt.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 16:34:08 by victor #+# #+# */
/* Updated: 2025/05/31 16:39:41 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINIRT_H
# define MINIRT_H
#include <stdint.h>
#include <sys/types.h>
# include <unistd.h>
# include <math.h>
# include <stdlib.h>
# include <errno.h>
# include <fcntl.h>
# include "libft/libft.h"
# include "memory/memory.h"
# include <stdbool.h>
# include <pthread.h>
# include <dirent.h>
# include "./raylib/src/raylib.h"
# include <string.h>
# ifndef WI
# define WI 1920
# endif
# ifndef HI
# define HI 1080
# endif
# define MAX_BODY_INIT 16
# define READ_BUFFER_SIZE 1000
# define ASPECT_RATIO 1.77777778
# ifdef VALGRIND
# define SCENE_START_RESOLUTION_X 16
# define SCENE_START_RESOLUTION_Y 9
# else
# define SCENE_START_RESOLUTION_X 1
# define SCENE_START_RESOLUTION_Y 1
# endif
# define RESOLUTION_SCALE_X 16
# define RESOLUTION_SCALE_Y 9
# define SCENE_START_RESOLUTION_CAP 4
# define CHECKER_GRID_SIZE 16
# define THREAD_COUNT 30
# define THREAD_HEIGHT HI / THREAD_COUNT
# define MAX_DEPTH 6
# define DROPOFF_DISTANCE 6
# define MAX_LIGHTS 4
# define ANTI_ALIASING_FACTOR 9
# define SQRT_AA_FACTOR 3
# define REC_SQRT_AA_FACTOR 0.33333333333
# define SHADOW_BIAS 1e-5
# define GLOSSINESS 10
# define COS_10 0.98480775301
# define SIN_10 0.17364817766
# define RAD_TO_DEG 57.2957795131
# define INDEX_OF_SKYSPHERE_IMAGE 1
# define SKYSPHERE INDEX_OF_SKYSPHERE_IMAGE
# define SKY_COLOR 0x83E8FC
# define MAIN_MENU 0x00beef00
# define ITEM_HEIGHT 30
# define ITEM_MARGIN 5
# define CONTAINER_ITEM_COUNT 32
# define CONTAINER_TITLE_LEN 1024
# define CONTAINER_PADDING 6
# define ITEM_PADDING 8
enum e_id_groups
{
ID_GROUP_MENU_MAIN = 1,
ID_GROUP_SPHERE,
ID_GROUP_PLANE,
ID_GROUP_CYLINDER,
ID_GROUP_DISC,
ID_GROUP_CONE,
ID_GROUP_ITEM,
};
enum e_mouse
{
LEFT_CLICK = 1,
RIGHT_CLICK = 3,
SCROLL_UP = 4,
SCROLL_DOWN = 5,
};
# define SCROLL_UP_DISTANCE -ITEM_HEIGHT
# define SCROLL_DOWN_DISTANCE ITEM_HEIGHT
typedef Image t_img;
enum e_menu_bg
{
MAIN_MENU_BG = 0x202020,
MENU_ITEM_BG = 0x404040,
HELP_MENU_BG = 0x0a0a0a,
};
enum e_menu_type
{
MENU_MAIN,
MENU_SPHERE,
MENU_PLANE,
MENU_CYLINDER,
};
typedef enum e_type
{
BODY_END = 0,
BODY_DELETED,
BODY_SPHERE,
BODY_PLANE,
BODY_CYLINDER,
BODY_DISK,
BODY_CONE,
} t_type;
typedef enum e_format
{
CONTAINER_GRID,
CONTAINER_LIST
} t_format;
# define GLYPH_COUNT 82
# define GLYPH_ROW 7
# define GLYPH_COL 6
# define PATH_LENGTH 128
typedef struct s_rect
{
uint x;
uint y;
uint width;
uint height;
} t_rect;
typedef enum e_i_type
{
ITEM_BUTTON,
ITEM_SLIDER,
ITEM_DIRENT,
} t_item_type;
typedef struct s_slider
{
t_rect bar;
t_rect cursor;
uint8_t max;
uint8_t min;
uint8_t *value;
} t_slider;
typedef struct s_button
{
uint color;
void *param;
void (*func_ptr)(void *, void *);
} t_button;
typedef struct s_dirent
{
int type;
char full_path[512];
void (*func)(void *, void *);
} t_dirent;
typedef enum e_param_type
{
PARAM_TEXTURE,
PARAM_BUMP,
PARAM_SCENE,
PARAM_BOOL,
PARAM_RADIUS,
PARAM_CONE,
PARAM_CYL,
PARAM_U8,
PARAM_UINT,
PARAM_DOUBLE,
} t_param_type;
typedef struct s_param
{
void *param;
void (*func)(void *);
t_param_type type;
} t_param;
# define ITEM_TITLE_LEN 128
typedef struct s_item
{
uint id;
t_item_type type;
t_rect rect;
char description[ITEM_TITLE_LEN];
t_param param;
union
{
t_button button;
t_slider slider;
t_dirent dirent;
};
} t_item;
typedef struct s_container
{
uint id;
t_rect attr;
t_format format;
t_param param;
char title[CONTAINER_TITLE_LEN];
char buffer[1024];
uint item_count;
uint item_width;
uint item_height;
struct s_data *data;
t_item item[CONTAINER_ITEM_COUNT];
} t_container;
typedef struct s_mouse
{
bool left_is_pressed;
bool right_is_pressed;
bool is_moving;
struct s_body *grabbed;
struct s_slider *slider;
uint color_store;
int prev_x;
int prev_y;
struct s_data *data;
} t_mouse;
typedef struct s_vector
{
double x;
double y;
double z;
} t_vector;
/* p = the visible point on the object
n = the object surface normal vector from that point
v = the normal vector towards the viewer
r = normalized perfect reflection of incoming light */
typedef struct s_hit_point
{
t_vector p;
t_vector n;
t_vector v;
t_vector r;
} t_hit_point;
typedef struct s_pixel
{
uint id;
double dist;
Color *color;
uint8_t surface_smoothness;
} t_pixel;
typedef struct s_line
{
char buffer[READ_BUFFER_SIZE + 1];
uint length;
uint count;
} t_line;
typedef struct s_buffer
{
char *bytes;
uint32_t start;
uint32_t end;
uint32_t size;
uint32_t capacity;
bool empty;
} t_buffer;
typedef struct s_sphere
{
double radius;
t_vector center;
} t_sphere;
typedef struct s_plane
{
t_vector point;
t_vector normal;
t_vector inverse_normal;
} t_plane;
typedef struct s_disk
{
t_vector point;
t_vector normal;
t_vector inverse_normal;
double radius;
} t_disk;
typedef struct s_cylinder
{
t_vector bottom;
t_vector normal;
t_vector center;
t_vector top;
t_vector coeffs;
double radius;
double height;
double hit_h;
} t_cylinder;
typedef struct s_cone
{
t_vector vertex;
t_vector normal;
t_vector coeffs;
double radius;
double height;
double hit_h;
double tan_a2p1;
double t[2];
t_disk bottom_cap;
} t_cone;
# ifndef TEXTURE_PATH
# define TEXTURE_PATH "./assets/textures/earth.ppm"
# endif
# ifndef SKYBOX_PATH
# define SKYBOX_PATH "./assets/textures/sky.ppm"
# endif
typedef struct s_texture
{
Color *pixel;
char path[255];
int width;
int height;
} t_texture;
# ifndef BUMP_MAP_PATH
# define BUMP_MAP_PATH "./assets/bump maps/earth.bump"
# endif
typedef struct s_bump_map
{
char path[255];
int *map;
uint width;
uint height;
} t_bump_map;
typedef struct s_body
{
uint id;
t_type type;
uint color;
bool reflect;
bool checker_board;
bool textured;
bool bump;
uint8_t surface_smoothness;
t_texture *texture;
t_bump_map *map;
union
{
t_sphere sphere;
t_plane plane;
t_cylinder cylinder;
t_disk disk;
t_cone cone;
};
} t_body;
typedef struct s_light
{
t_vector position;
t_vector ray;
float phong;
float obj_distance;
float intensity;
uint color;
} t_light;
typedef double t_matrix4[4][4];
typedef struct s_camera
{
t_vector *ray;
t_vector position;
t_vector normal;
t_vector right;
t_vector up;
t_matrix4 to_world;
float tilt;
double fov;
double fov_f;
} t_camera;
# define MAPS_MAX 16
typedef struct s_scene
{
uint current_body_max;
uint body_cursor;
uint resolution_x;
uint resolution_y;
uint anti_aliasing;
uint light_count;
t_light light[MAX_LIGHTS];
t_light ambient;
bool ambient_was_parsed;
t_camera camera;
bool camera_was_parsed;
uint light_focus;
uint depth;
bool sky_sphere;
uint sky_color;
t_body *body_focus;
t_pixel *pixel;
t_body *body;
pthread_mutex_t mutex;
bool reload;
t_texture *texture;
t_texture sky;
uint texture_count;
t_bump_map *bump_map;
uint bump_map_count;
} t_scene;
typedef struct s_data
{
Texture2D texture;
Color *pixel_colors;
t_pixel *pixel;
t_scene scene;
t_mouse mouse;
t_container *menu;
t_container explorer;
void *param;
void (*func_ptr)(void *, void *);
struct s_thread *threads;
int thread_count;
pthread_barrier_t barrier;
pthread_rwlock_t rwlock;
bool go;
} t_data;
typedef struct s_thread
{
int id;
pthread_t thread;
t_data *data;
t_scene *scene;
t_pixel *pixel;
pthread_rwlock_t *rwlock;
uint width;
uint height;
uint startx;
uint starty;
} t_thread;
/* ft_atod.c */
void item_double_inc(void *value, void *null);
void item_double_dec(void *value, void *null);
/* Utils */
void ft_open(int *fd, const char *path, int flag, int permissons);
int ft_close(int fd);
int ft_read(int fd, char *character, unsigned int size_to_read);
void glyph_print(uint begin_x, uint begin_y, \
char const *text, t_pixel *pixel);
void data_destroy_func(void *data_ptr);
long get_current_us(struct timeval start);
/* Drawing */
void rt_draw_rect(t_rect rect, t_pixel *pixel, uint id, uint color);
void rt_draw_rect_blend(t_rect rect, t_pixel *pixel, \
uint id, uint color);
# define COLLECT_MODE_NORMAL 0
# define COLLECT_MODE_END 1
/* Generics */
bool collect_param_comma(char **entry_position, \
char *params[], uint count, int mode);
bool collect_param_single(char **entry_position, \
char *params[], int mode);
uint id_set(uint group, int id);
uint id_group_get(int id);
void err(char *body_type, uint line);
bool check_line(char *tmp);
void swap_2_ints(uint *a, uint *b);
/* Colors */
uint color_to_uint(Color c);
Color uint_to_color(uint c);
Color set_color(uint r, uint g, uint b);
void get_color_reflect(t_vector new_center, t_vector normal, \
t_scene *scene, t_pixel *pixel);
/* Returns diffuse color when called with parameters
attn = dot product of surface normal and light ray,
uint obj = object color and float gloss = 1.
Returns specular reflection color when called with parameters
attn = dot product of light ray reflection and view ray
obj = 0xFFFFFF and float gloss greater than 1 */
uint phong_reflection(uint obj, float attn, t_light l, float gloss);
void apply_shadow_bias(t_vector *p, t_vector normal, double scale);
uint get_color(uint obj, uint light, double attn);
uint color_blend(uint color1, uint color2);
/* !!! This function PRINTS a SPACE at the BEGINNING and a NEWLINE
character at the END !!! */
void color_print(uint color, int fd);
uint add_color(uint color1, uint color2);
uint parse_body_color(char *params[], int *error);
void get_background_color(t_scene *sc, t_pixel *px, t_vector v);
uint color_brightness(uint original, float brightness);
/* Function to calculate the Euclidean distance between two colors */
float color_distance(uint r1, uint g1, uint b1, uint r2, uint g2, uint b2);
/* Sphere */
bool parse_sphere(char *entry, uint line_count, \
t_body *body, uint body_count);
void pixel_sphere_set(t_pixel *pixel, t_vector camera_ray, \
t_body *body, t_scene *scene);
void body_sphere_print(t_body *body);
void sphere_save(t_sphere sphere, uint color, int fd);
double sphere_hit_distance(t_vector ray, t_vector dlt_centr, \
t_sphere sphere, int *invert);
void get_color_sphere( t_body *body, \
t_hit_point *hit, \
t_pixel *pixel);
/* Plane */
bool parse_plane(char *entry, uint line_count, \
t_body *body, uint body_count);
void body_plane_print(t_body *body);
void trace_plane(t_pixel *pixel, t_vector camera_ray, \
t_body *body, t_scene *scene);
void plane_save(t_plane plane, uint color, int fd);
bool move_plane(int keycode, t_plane *plane);
double plane_hit_distance(t_plane pl, t_vector cam, \
t_vector camera_ray, int *invert);
void get_color_plane(t_body *body, t_hit_point *hit, t_pixel *pixel);
/* Cylinder */
void body_cylinder_print(t_body *body);
void cylinder_save(t_cylinder cylinder, uint color, int fd);
bool parse_cylinder(char *entry, uint line_count, \
t_body *body, uint body_count);
void calc_cyl_data(t_cylinder *cy);
/* The coefficients a, b, c in a quadratic equation f(x) = ax² + bx + c are
saved in a t_vector coeffs, so that coeffs.x = a, .y = b, .z = c */
void cyl_equation_coefficients(t_cylinder *cy, t_vector ray, \
t_vector cam_delta);
double cyl_hit_distance(t_cylinder *cy, t_vector ray, \
t_vector cam, int *invert);
double cyl_components_shadow(t_cylinder cy, t_vector ray, t_vector p);
bool move_cylinder(int keycode, t_cylinder *cyl);
t_vector cyl_normal(t_cylinder cy, t_vector p, int invert);
void get_color_cylinder(t_body *body, \
t_vector intersect, t_pixel *pixel);
void trace_cyl_caps(t_pixel *px, t_vector ray, \
t_body *cyl, t_scene *sc);
void trace_cyl(t_pixel *pixel, t_vector ray, t_body *body, t_scene *sc);
bool finite_cylinder_hit(t_cylinder *cy, t_vector hit);
/* Disk */
void trace_disk(t_pixel *pixel, t_vector ray, \
t_body *body, t_scene *scene);
bool parse_disk(char *entry, uint line_count, \
t_body *body, uint body_count);
double disk_hit_distance(t_disk disk, t_vector ray, \
t_vector cam, int *invert);
void print_disk(t_body *body);
bool move_disk(int keycode, t_disk *disk);
void disk_save(t_disk disk, uint color, int fd);
void get_color_disk(t_body *body, t_vector intersect, t_pixel *pixel);
/* Cone */
bool move_cone(int keycode, t_cone *cone);
bool parse_cone(char *entry, uint line_count, t_body *body, \
uint body_count);
void calc_cone_data(t_cone *cn);
bool cam_inside_cone(t_cone *cn, t_vector cam, double cam_h);
/* The coefficients a, b, c in a quadratic equation f(x) = ax² + bx + c are
saved in a t_vector coeffs, so that coeffs.x = a, .y = b, .z = c */
void cone_equation_coefficients(t_cone *cn, t_vector ray, \
t_vector cam_delta);
void solve_cone_equation(t_cone *cn, t_vector ray, t_vector cam_delta);
double cone_hit_distance(t_cone *cn, t_vector ray, t_vector cam, \
int *invert);
void trace_cone(t_pixel *pixel, t_vector ray, t_body *body, \
t_scene *sc);
void trace_cone_bottom(t_pixel *px, t_vector ray, t_body *cone, \
t_scene *sc);
double cone_components_shadow(t_cone cn, t_vector ray, t_vector p);
void cone_save(t_cone cone, uint color, int fd);
t_vector cone_surface_normal(t_cone cn, t_vector p, int invert);
bool finite_cone_hit(double cone_height, double h);
/* Vector */
void normalize_vector(t_vector *vec);
double dot_product(t_vector a, t_vector b);
double vector_length(t_vector vec);
t_vector vector_subtract(t_vector vec1, t_vector vec2);
t_vector set_vector(double x, double y, double z);
t_vector scale_vector(t_vector vec, double scale);
t_vector add_vector(t_vector vec, t_vector add);
void vector_print(t_vector vector, int fd);
t_vector get_normal(t_vector from, t_vector to);
t_vector cross_product(t_vector a, t_vector b);
t_vector rot_x(t_vector vec, int dir);
t_vector rot_y(t_vector vec, int dir);
t_vector rot_z(t_vector vec, int dir);
t_vector reflect_vector(t_vector incoming, t_vector axis);
void calc_hit_point_vectors(t_hit_point *hit, t_vector ray, t_vector n);
/* Camera */
void define_camera_rays(t_pixel *pixel, t_camera *camera, \
t_scene *scene);
void set_world_matrix(t_camera *camera);
/*camera->right in this function is used as cam normal projection onto the
xz plane to save one less t_vector type */
void calc_camera_tilt(t_camera *camera);
bool parse_camera(char *entry, uint line_count, t_camera *camera, \
bool *reload);
void body_camera_print(t_camera camera);
void camera_save(t_camera *camera, int fd);
t_vector set_ray(float x, float y, t_scene *scene, t_camera *camera);
void calc_camera_space(t_camera *camera);
/* Pixel */
t_pixel *pixel_plane_create(void);
void set_pixel_distances(t_pixel *array, uint size, double dist);
void pixels_clear(t_pixel *pixel, uint wi, uint hi);
void trace_lights(t_scene *sc, t_pixel *px, t_hit_point hit);
void pixel_clear_id(t_pixel *pixel);
void set_info_to_pixel(t_pixel *pixel, t_body *body, double dist);
/* Ray Utils */
double ray_distance_from_point_squared(t_vector ray, t_vector point);
double smaller_non_negative(double a, double b);
/* Image */
t_img image_create(void *mlx_ptr, uint width, uint height);
void pixels_image_syncronize(Color *image, t_pixel *pixel);
void save_all_lights(t_scene *scene, int fd);
/* Scene */
bool body_distribute(char *entry, char *tmp, \
uint line_cursor, t_scene *scene);
void scene_print(t_scene *scene);
void scene_create(const char *filepath, t_scene *scene);
void scene_create_loop(t_scene *scene, t_line line, int fd);
void scene_replace(t_scene *scene, char *new_path);
char *buffer_read_chunk(t_line *line, uint fd, bool *file_end_reached);
bool body_determine(char *entry, uint line_count, t_scene *scene);
void scene_body_add(t_body *body, t_scene *scene);
void scene_add_cylinder(void *data_ptr, void *null);
void scene_add_disk(void *data_ptr, void *null);
void scene_add_sphere_func(void *scene_ptr, void *null);
void scene_add_plane_func(void *data_ptr, void *null);
t_body *body_get_by_id(uint id, t_scene *scene);
void scene_save(t_scene *scene);
void trace_reflection(t_pixel *px, t_hit_point hit, t_scene new_scene);
void toggle_sky_sphere(t_scene *scene);
/* Rendering */
uint rendering_loop(t_data *data);
void pixel_fill(t_pixel *pixel, t_scene *scene);
void ray_check_bodys(t_pixel *pixel, t_vector ray, t_scene *scene);
/* Light */
bool parse_light(char *entry_light, uint line_count, t_light *light, \
int count);
void body_light_print(t_light light);
void light_save(t_light light, int fd);
bool parse_ambient(char *entry_light, uint line_count, t_light *light, \
bool *relaod);
bool shadow(t_vector p, t_light l, t_body *body, \
t_scene *scene);
void ambient_save(t_light light, int fd);
/* Keys */
int key_press(int keycode, void *data);
uint key_change_res(int keycode, t_scene *scene);
bool key_move_focused(int keycode, t_vector *focus);
bool key_change_fov(int keycode, t_camera *camera);
bool calc_camera_rotation(int key, t_camera *camera);
int move_body(int keycode, t_body *body);
bool key_move_light(int keycode, t_scene *scene);
uint key_misc_function(int keycode, t_scene *scene, t_data *data);
/* Menu */
void container_draw(void *menu, void *pixel);
void container_item_get_by_id(t_container *container, \
uint id, uint x, uint y);
void help_menu_draw(void *data_ptr, void *null);
t_container container_create(const char *title, t_rect *attr, uint format);
void container_item_add(t_container *container, t_item *item);
t_item container_item_button_create(const char *title, void *param, \
void (*func_ptr)(void *, void *));
t_item container_item_slider_create(const char *title, uint8_t min, \
uint8_t max, uint8_t *value);
void container_internal_slider_config(t_item *item);
/* Slider */
bool menu_slider_is_cursor(uint x, uint y, t_rect *slider);
void menu_slider_value_calculate(t_slider *slider);
/* Menu Mapping And Creating */
void disk_menu_map(t_container *menu, t_body *body, uint *color);
void sphere_menu_map(t_container *menu, t_body *body, uint *color);
void cylinder_menu_map(t_container *menu, t_body *body, uint *color);
void plane_menu_map(t_container *menu, t_body *body, uint *color);
void cone_menu_map(t_container *menu, t_body *body, uint *color);
void menu_body_map_bool_toggle(t_item *item, bool *value);
void menu_body_map_float(t_item *item, double *value);
void menu_body_map_vector(t_item *item, t_vector *vector);
void menu_body_map_color(t_item *item, uint *color);
void menu_body_map_texture(t_item *item, t_texture **texture);
void menu_body_map_bump(t_item *item, t_bump_map **bump);
void menu_body_bool_add(t_container *menu, char *name);
void menu_body_colors_add(t_container *menu);
void menu_body_vector_position_add(t_container *menu);
void menu_body_vector_normal_add(t_container *menu);
void menu_body_slider_add(t_container *menu, char *name);
void menu_body_float_add(t_container *menu, char *name_prefix);
void menu_body_texture_add(t_container *container);
void menu_body_bump_map_add(t_container *container);
void menu_body_slider_add(t_container *menu, char *name);
void menu_body_slider_map_value(t_item *item, uint8_t *value);
void cylinder_menu_create(t_container *menu);
void cone_menu_create(t_container *menu);
void disc_menu_create(t_container *menu);
void sphere_menu_create(t_container *menu);
void plane_menu_create(t_container *menu);
void item_normal_inc(void *null, void *value);
void item_normal_dec(void *null, void *value);
void item_double_inc(void *null, void *value);
void item_double_dec(void *null, void *value);
void item_bool_toggle(void *null, void *value);
/* Mouse */
int mouse_press(int x, int y, t_data *data);
int mouse_release(int x, int y, t_data *data);
int mouse_move(int x, int y, t_data *data);
void mouse_grab(t_mouse *mouse, t_body *body);
/* Parsing */
bool check_position(char **tmp);
bool check_end(char *entry);
char temporary_terminate_string(char *end);
void restore_string(char *end, char tmp);
int64_t get_one_tenth(char *nbr);
char get_last_char(char *str);
size_t count_digits(int64_t n, int base);
bool positive_overflow(int64_t max, char *str);
bool negative_overflow(int64_t min, char *str);
bool is_a_number(char *str);
char *skip_whitespace(char *str);
int64_t set_signed_int(const char *str, int64_t min, \
int64_t max, int *error);
void minirt_fd_close(void *ptr);
bool check_normal_vector(t_vector *vec);
/* PPM READER */
bool ppm_check(int fd, int *width, int *height);
void ppm_pixels_read(t_buffer *buffer, int fd, t_texture *texture);
int ppm_read_number(int fd, bool *eof);
void ppm_image_read(const char *path, t_texture *texture);
/* Threads */
void data_init_threads(t_data *data);
void threads_init(t_thread thread[], t_data *data);
void *thread_rendering_loop(void *thread_ptr);
void thread_define_camera_rays( t_thread *thread, \
t_pixel *pixel, \
t_scene *scene, \
t_camera *camera);
/* Read buffer */
void buffer_move_next_whitespace(t_buffer *buffer);
void buffer_allignment_set(t_buffer *buffer);
void buffer_read(t_buffer *buffer, int fd);
t_buffer buffer_init(uint capacity, void *bytes);
/* Bump Map */
void bump_map_read(char *map_path, t_bump_map *map);
bool contains_nan(char buffer[], uint size);
void perturb_normal(t_vector *normal, t_bump_map *map, \
double u, double v);
int close_window(void *data_ptr);
/* Mapgen */
void map_create_from_ppm(int infile, int outfile);
/* explorer */
void explorer_read_dir(void *data, void *path);
void file_load(t_item *item, t_data *data);
void explorer_entry_func(void *item_ptr, void *data);
void container_item_swap(t_item *itema, t_item *itemb);
void container_item_desc_sort(t_container *cont);
bool ft_opendir(DIR **dir, char *path);
/* Phong */
uint phong_reflection(uint obj, float attenuation, \
t_light l, float gloss);
void calc_phong_vectors(t_hit_point *hit, t_light *l);
uint diffuse_reflection(t_pixel *px, t_hit_point hit, t_light l);
uint specular_reflection(t_pixel *px, t_hit_point hit, t_light l);
float dropoff_factor(float distance);
#endif

1
raylib Submodule

@ -0,0 +1 @@
Subproject commit b9c2ecc447a1aaed562f23932125c0c41e59e7f1

7
scenes/basic_1.rt Normal file
View File

@ -0,0 +1,7 @@
C 0,0,-5 0,0,1 90
A .30 123,234,234
L 0,2,-3 .8 234,234,123
sp 0.0,0.0,0.0 2.0 234,123,123
pl 2.0,-2.0,0.0 -0.70710,0.70710,0.0 123,234,123
cy -2.0,0.0,1.0 0.0,1.0,0.0 2.0 2.0 123,123,234
di 2.0,0.0,0.0 0.0,0.0,-1.0 4.0 255,255,255

6
scenes/basic_2.rt Normal file
View File

@ -0,0 +1,6 @@
C 0,0,-5 0,0,5 90
L 0,2,-2 1 255,255,200
A 0.5 0,0,100
sp 0,0,0 2 234,123,123
sp 0,0,0 2 123,234,123
cy 0,0,0 0,1,0 1 5 123,123,234

30
scenes/covfefe.rt Normal file
View File

@ -0,0 +1,30 @@
C -0.80287,-2.14970,-500 1.80997,-0.62646,9.81486 40
L 5.0,0.0,3.0 1.0 255,255,255
L 0.0,5.0,3.0 1.0 255,255,255
L -5.0,0.0,3.0 1.0 255,255,255
L 0.0,-5.0,3.0 1.0 255,255,255
A 0.50000 254,254,254
sp -1.0,-0.10000,5.0 2.0 28,121,241
sp 2.0,-0.10000,5.0 2.0 32,30,32
sp 0.50000,0.70000,5.0 2.0 32,30,32
sp -1.0,-1.70000,5.0 2.0 32,30,32
sp 1.99999,-1.69998,5.0 2.0 32,30,32
sp 0.50000,-2.60000,5.0 2.0 21,121,241
sp 0.50000,2.29999,5.0 2.0 241,30,32
sp -2.39997,0.60000,5.0 2.0 32,31,32
sp -2.39997,-2.60000,5.0 2.0 241,32,32
sp 3.60000,0.40000,5.0 2.0 24,121,241
sp 3.59999,-2.20000,5.0 2.0 32,122,241
sp 4.50000,-0.89999,5.0 2.0 32,31,32
sp 5.59999,-0.90000,5.0 1.0 241,255,241
sp 4.29999,1.89999,5.0 2.0 32,30,32
sp 0.50000,-4.29990,5.0 2.0 32,30,32
sp 3.70000,2.79998,4.79999 1.0 255,255,255
sp 5.19990,2.59988,4.39998 1.0 255,255,255
sp 5.19998,2.39997,5.59999 1.0 255,255,255
sp -3.19998,0.59997,4.19998 1.0 255,255,255
sp -3.39997,0.49998,5.79999 1.0 255,255,255
sp -2.69999,1.79989,5.0 1.0 255,255,255
sp -0.39999,-4.79999,4.59999 1.0 255,255,255
sp 0.50000,-5.0,5.79999 1.0 255,255,255
sp 1.39999,-4.79999,4.59999 1.0 255,255,255

9
scenes/eclipse.rt Normal file
View File

@ -0,0 +1,9 @@
C 0,0,-1 0,0,1 90
L 0,-0.13,-1 1 255,255,200
A 0.5 0,0,100
sp 0,-0.1,-0.5 0.02 200,200,200
sp -0.15,0,0 0.01 200,200,200
sp 0,-0.1,1 1 100,255,255
di 0,-0.1,1 0,1,-0.5 1.1 255,255,50
cy 0,-0.1,1 0.1,.1,-.5 0.01 1.1 255,255,50
cy 0,-0.1,1 0.3,.2,-.5 0.01 1.1 255,255,50

9
scenes/multilight.rt Normal file
View File

@ -0,0 +1,9 @@
C 0,0,-5 0,0,1 90
L 0,2,-2 1 255,255,255
L 2,2,0 1 50,255,50
L -2,2,0 1 50,255,50
L 0,2,2 1 255,50,50
A 1 50,50,50
sp 0,0,0 3 255,255,255
pl 0,-2,0 0,1,0 123,123,123
di 0,3,2 0,-1,-1 6 234,234,123

3
scenes/nothing.rt Normal file
View File

@ -0,0 +1,3 @@
C 0,0,-5 0,0,5 90
L 0,2,-2 1 255,255,200
A 0.5 0,0,0

10
scenes/snowman.rt Normal file
View File

@ -0,0 +1,10 @@
C 20,20,-20 -20,-20,20 60
L 0,0.5,-1 1 255,255,200
A 0.5 100,100,100
pl 0,-1.5,2 0,-1,0 234,234,123
sp 0,-1.25,2 1 234,234,234
sp 0,-0.5,2 0.75 234,234,234
sp 0,0,2 0.5 234,234,234
cy 0,0.25,2 0,1,0 0.3 0.3 50,50,50
di 0,0.2,2 0,1,0 0.5 150,150,150
cn 0,0,1.3 0,1,100 0.1 0.5 255,200,100

109
src/io/buffer.c Normal file
View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* buffer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 12:47:39 by vvobis #+# #+# */
/* Updated: 2024/11/26 18:06:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
bool contains_nan(char buffer[], uint size)
{
uint i;
i = 0;
while (i < size && buffer[i])
{
if (!ft_isdigit(buffer[i]) && !ft_isspace(buffer[i]) \
&& buffer[i] != '.')
{
return (true);
}
i++;
}
return (false);
}
void buffer_move_next_whitespace(t_buffer *buffer)
{
if (buffer->start == buffer->end)
{
buffer->empty = true;
return ;
}
while (buffer->start != buffer->end \
&& ft_isspace(buffer->bytes[buffer->start]))
buffer->start++;
while (buffer->start != buffer->end \
&& !ft_isspace(buffer->bytes[buffer->start]))
buffer->start++;
while (buffer->start != buffer->end \
&& ft_isspace(buffer->bytes[buffer->start]))
buffer->start++;
if (buffer->start == buffer->end)
buffer->empty = true;
}
void buffer_allignment_set(t_buffer *buffer)
{
if (buffer->start == buffer->end)
{
buffer->empty = true;
return ;
}
while (!ft_isspace(buffer->bytes[buffer->end]) \
&& buffer->end != buffer->start)
buffer->end--;
while (ft_isspace(buffer->bytes[buffer->start]) \
&& buffer->end != buffer->start)
buffer->start++;
if (buffer->start == buffer->end)
{
buffer->end = buffer->size;
buffer->start = 0;
buffer->empty = true;
}
}
void buffer_read(t_buffer *buffer, int fd)
{
uint leftover_bytes;
uint bytes_read;
leftover_bytes = 0;
if (buffer->end < buffer->size)
{
leftover_bytes = buffer->size - buffer->end;
ft_memmove(buffer->bytes, &buffer->bytes[buffer->end + 1], \
leftover_bytes);
}
bytes_read = ft_read(fd, &buffer->bytes[leftover_bytes], \
buffer->capacity - leftover_bytes);
buffer->size = bytes_read + leftover_bytes - (bytes_read > 0);
buffer->end = buffer->size;
buffer->start = 0;
buffer_allignment_set(buffer);
buffer->empty = (bytes_read == 0 && leftover_bytes == 0);
}
t_buffer buffer_init(uint capacity, void *bytes)
{
t_buffer buffer;
if (capacity == 0 || bytes == NULL)
return ((t_buffer){0});
ft_bzero(bytes, capacity);
ft_bzero(&buffer, sizeof(buffer));
buffer.bytes = bytes;
buffer.capacity = capacity;
buffer.size = 0;
buffer.start = 0;
buffer.end = 0;
buffer.empty = true;
return (buffer);
}

121
src/io/bump.c Normal file
View File

@ -0,0 +1,121 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bump.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/05 14:45:43 by vvobis #+# #+# */
/* Updated: 2024/12/17 13:36:39 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
#define BUMP_BUFFER_SIZE 10000
static void bump_map_create(t_bump_map *map, t_buffer *buffer, int fd)
{
uint buffer_ptr;
buffer_ptr = 0;
while (buffer->empty == false)
{
while (buffer->empty == false)
{
map->map[buffer_ptr++] = ft_atoi(&buffer->bytes[buffer->start]);
buffer_move_next_whitespace(buffer);
}
buffer_read(buffer, fd);
if (contains_nan(buffer->bytes, buffer->end) \
|| buffer_ptr - 1 > map->width * map->height)
return (ft_fprintf(2, "Invalid Bump Map Format\n"), \
close(fd), lst_memory(NULL, NULL, FAIL));
buffer_allignment_set(buffer);
}
if (map->width * map->height != buffer_ptr)
{
close(fd);
ft_fprintf(STDERR_FILENO, \
"Invalid Bump Map format for %s\n", map->path);
lst_memory(NULL, NULL, FAIL);
}
}
void bump_map_read(char *map_path, t_bump_map *map)
{
int fd;
t_buffer buffer;
char stack_buffer[BUMP_BUFFER_SIZE];
if (map->map)
lst_memory(map->map, NULL, FREE);
ft_bzero(map, sizeof(*map));
ft_open(&fd, map_path, O_RDONLY, 0644);
ft_memcpy(map->path, map_path, ft_strlen(map_path));
ft_bzero(stack_buffer, sizeof(stack_buffer));
buffer = buffer_init(BUMP_BUFFER_SIZE, stack_buffer);
buffer_read(&buffer, fd);
if (contains_nan(buffer.bytes, buffer.end))
{
ft_fprintf(2, "Invalid Bump Map Format\n");
return (close(fd), lst_memory(NULL, NULL, FAIL));
}
map->width = ft_atoi(&buffer.bytes[buffer.start]);
buffer_move_next_whitespace(&buffer);
map->height = ft_atoi(&buffer.bytes[buffer.start]);
buffer_move_next_whitespace(&buffer);
map->map = ft_calloc(map->width * map->height, sizeof(double));
lst_memory(map->map, free, ADD);
bump_map_create(map, &buffer, fd);
ft_close(fd);
}
double bilinear_sample(double u, double v, t_bump_map *map)
{
int x2;
int y2;
double f0;
double f1;
u = u * (map->width - 1);
v = v * (map->height - 1);
x2 = (int)u;
y2 = (int)v;
if ((int)u + 1 < (int)map->width)
x2++;
if ((int)v + 1 < (int)map->height)
y2++;
f0 = (double)(map->map[(int)v * map->width + (int)u] * 0.5) \
* (1 - (u - (int)u)) + map->map[(int)v * map->width + x2] \
* (u - (int)u);
f1 = map->map[y2 * map->width + (int)u] \
* (1 - (u - (int)u)) + (double)(map->map[y2 * map->width + x2] * 0.5) \
* (u - (int)u);
return (f0 * (1 - (v - (int)v)) + f1 * (v - (int)v));
}
void perturb_normal(t_vector *normal, t_bump_map *map, double u, double v)
{
double h00;
double h01;
double h10;
double dhdu;
double dhdv;
if (u < 0)
u += 1.0;
if (v < 0)
v += 1.0;
h00 = bilinear_sample(u, v, map);
h01 = bilinear_sample(u + 1.0 / map->width, v, map);
h10 = bilinear_sample(u, v + 1.0 / map->height, map);
dhdu = -(h01 - h00);
dhdv = -(h10 - h00);
*normal = (t_vector){\
normal->x + 1 * dhdu + 0 * dhdv, \
normal->y + 0 * dhdu + 1 * dhdv, \
normal->z + 0 * dhdu + 0 * dhdv \
};
normalize_vector(normal);
}

106
src/io/container.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* container.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/16 12:47:20 by vvobis #+# #+# */
/* Updated: 2024/12/11 12:06:40 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
void container_item_draw(t_item *item, t_pixel *pixel)
{
if (item->type == ITEM_BUTTON || item->type == ITEM_DIRENT)
{
rt_draw_rect(item->rect, pixel, item->id, MENU_ITEM_BG);
glyph_print(item->rect.x, item->rect.y + ITEM_MARGIN, \
item->description, pixel);
}
else if (item->type == ITEM_SLIDER)
{
rt_draw_rect(item->rect, pixel, item->id, MENU_ITEM_BG);
rt_draw_rect(item->slider.bar, pixel, item->id, 0x0f0f0f);
rt_draw_rect(item->slider.cursor, pixel, item->id, 0x202020);
glyph_print(item->slider.cursor.x - 6, item->slider.cursor.y, \
item->description, pixel);
}
}
void container_item_get_by_id(t_container *cont, uint id, uint x, uint y)
{
uint i;
i = 0;
while (i < cont->item_count)
{
if (cont->item[i].id == id)
{
if (cont->item[i].type == ITEM_BUTTON \
&& cont->item[i].button.func_ptr)
{
cont->item[i].button.func_ptr(cont->data, &cont->item[i].param);
if (cont->param.type == PARAM_CONE \
|| cont->param.type == PARAM_CYL)
cont->param.func(cont->param.param);
}
else if (cont->item[i].type == ITEM_SLIDER && \
menu_slider_is_cursor(x, y, &cont->item[i].slider.cursor))
cont->data->mouse.slider = &cont->item[i].slider;
else if (cont->item[i].type == ITEM_DIRENT \
&& cont->item[i].dirent.func)
cont->item[i].dirent.func(&cont->item[i], cont->data);
return ;
}
i++;
}
}
void container_draw(void *data_ptr, void *menu_ptr)
{
uint i;
t_data *data;
t_container *menu;
i = 0;
data = data_ptr;
menu = menu_ptr;
rt_draw_rect_blend(menu->attr, data->pixel, menu->id, MENU_ITEM_BG);
glyph_print(menu->attr.x + ITEM_PADDING, \
menu->attr.y + ITEM_PADDING, menu->title, data->pixel);
while (i < menu->item_count)
{
container_item_draw(&menu->item[i], data->pixel);
i++;
}
}
/* Title limit = 1024; format see t_format enum */
t_container container_create(const char *title, t_rect *attr, uint format)
{
t_container container;
static uint container_id_count = 0;
ft_bzero(&container, sizeof(container));
if (ft_strlen(title) < sizeof(container.title))
ft_strlcpy(container.title, title, ft_strlen(title) + 1);
else
ft_fprintf(STDERR_FILENO, "[CONTAINER] Container Title Too Long! "\
"TITLE_LIMIT is %d", CONTAINER_TITLE_LEN);
if (attr)
container.attr = *attr;
else
{
container.attr.width = WI / 3;
container.attr.x = 0;
container.attr.height = HI - CONTAINER_PADDING * 2;
container.attr.y = HI / 2 - container.attr.height / 2;
}
container.format = format;
container.id = id_set(ID_GROUP_MENU_MAIN, container_id_count);
container.item_count = 0;
return (container);
}

93
src/io/explorer.c Normal file
View File

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* explorer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 11:27:50 by vvobis #+# #+# */
/* Updated: 2025/05/31 15:09:14 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
void container_item_dirent_create(t_item *item, char *title, \
int type, void (*func)(void *, void *))
{
item->type = ITEM_DIRENT;
if (title && ft_strlen(title) < CONTAINER_TITLE_LEN)
ft_strlcpy(item->description, title, ft_strlen(title) + 1);
item->dirent.type = type;
item->dirent.func = func;
}
void explorer_create(t_container *explorer, t_data *data)
{
ft_bzero(explorer, sizeof(*explorer));
*explorer = container_create("Explorer", NULL, CONTAINER_LIST);
explorer->data = data;
}
void explorer_read_dir_loop(DIR *dir, void *param, \
char *cwd, t_container *explorer)
{
struct dirent *entry;
t_item item;
ft_strlcpy(explorer->title, cwd, CONTAINER_TITLE_LEN);
while (1)
{
entry = readdir(dir);
if (!entry)
break ;
container_item_dirent_create(&item, entry->d_name, \
entry->d_type, explorer_entry_func);
ft_strlcpy(item.dirent.full_path, cwd, 1024);
ft_strlcat(item.dirent.full_path, "/", 1024);
ft_strlcat(item.dirent.full_path, entry->d_name, 1024);
item.param = *(t_param *)param;
container_item_add(explorer, &item);
}
}
void explorer_read_dir(void *data, void *param)
{
DIR *dir;
static t_container explorer = {0};
char cwd[1024];
if (explorer.title[0] == 0)
explorer_create(&explorer, data);
if (!getcwd(cwd, 1024))
return ;
if (!ft_opendir(&dir, cwd))
return ;
ft_bzero(&explorer.item, sizeof(explorer.item));
explorer.item_count = 0;
explorer_read_dir_loop(dir, param, cwd, &explorer);
closedir(dir);
container_item_desc_sort(&explorer);
((t_data *)data)->func_ptr = container_draw;
((t_data *)data)->param = &explorer;
return ;
}
void explorer_entry_func(void *item_ptr, void *data)
{
t_item *item;
t_param param;
item = item_ptr;
if (item->dirent.type == DT_DIR)
{
param = item->param;
if (chdir(item->description) == 0)
explorer_read_dir(data, &param);
}
else if (item->dirent.type == DT_REG)
file_load(item, data);
else
ft_fprintf(STDERR_FILENO, "[Explorer] Invalid File Type: %s\n", \
item->description);
}

84
src/io/explorer_helper.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* explorer_helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/05 14:51:57 by vvobis #+# #+# */
/* Updated: 2024/12/11 12:47:28 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
bool ft_opendir(DIR **dir, char *path)
{
*dir = opendir(path);
if (!*dir)
{
ft_fprintf(STDERR_FILENO, "Failed to open directory: %s\n", path);
return (false);
}
return (true);
}
void scene_replace(t_scene *scene, char *new_path)
{
int fd;
t_line line;
ft_bzero(&line, sizeof(line));
ft_open(&fd, new_path, O_CREAT | O_RDWR, 0644);
scene->body_cursor = 0;
scene->light_count = 0;
scene->reload = true;
scene->camera_was_parsed = false;
scene->ambient_was_parsed = false;
ft_bzero(scene->light, sizeof(*scene->light) * scene->light_count);
ft_bzero(&scene->ambient, sizeof(*scene->light));
ft_bzero(scene->body, sizeof(t_body) * scene->current_body_max);
scene->body_focus = NULL;
scene_create_loop(scene, line, fd);
ft_close(fd);
}
void container_item_swap(t_item *itema, t_item *itemb)
{
t_item tmp;
ft_strlcpy(tmp.description, itema->description, 1024);
tmp.dirent = itema->dirent;
tmp.param = itema->param;
ft_strlcpy(itema->description, itemb->description, 1024);
itema->dirent = itemb->dirent;
itema->param = itemb->param;
ft_strlcpy(itemb->description, tmp.description, 1024);
itemb->dirent = tmp.dirent;
itemb->param = tmp.param;
}
void container_item_desc_sort(t_container *cont)
{
uint j;
bool swapped;
while (1)
{
swapped = false;
j = 0;
while (j + 1 < cont->item_count)
{
if (ft_strncmp(cont->item[j].description, \
cont->item[j + 1].description, \
ft_strlen(cont->item[j].description)) > 0)
{
container_item_swap(&cont->item[j], &cont->item[j + 1]);
swapped = true;
}
j++;
}
if (!swapped)
break ;
}
}

View File

@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* explorer_load_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/05 13:47:52 by vvobis #+# #+# */
/* Updated: 2024/12/05 14:39:45 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
static bool is_format(char *file, char *format)
{
char *tmp;
tmp = ft_strrchr(file, '.');
if (!tmp)
return (false);
if (ft_strncmp(tmp, format, ft_strlen(format)) == 0)
return (true);
else
{
ft_fprintf(STDERR_FILENO, \
"[Explorer] Invalid file format, expected: %s\n", format);
return (false);
}
}
static void bump_map_load(t_item *item, t_data *data)
{
uint i;
i = 0;
while (i < data->scene.bump_map_count)
{
if (ft_strncmp(data->scene.bump_map[i].path, \
item->dirent.full_path, 255) == 0)
return (*(t_bump_map **)item->param.param \
= &data->scene.bump_map[i], (void)0);
i++;
}
ft_printf("[Explorer] Loading file: %s\n", item->dirent.full_path);
bump_map_read(item->dirent.full_path, \
&data->scene.bump_map[data->scene.bump_map_count]);
*(t_bump_map **)item->param.param = \
&data->scene.bump_map[data->scene.bump_map_count];
data->scene.bump_map_count = (data->scene.bump_map_count + 1);
if (data->scene.bump_map_count == MAPS_MAX)
data->scene.bump_map_count = 0;
ft_printf("[Explorer] File Loaded!\n");
}
static void texture_load(t_item *item, t_data *data)
{
uint i;
i = 0;
while (i < data->scene.texture_count)
{
if (ft_strncmp(data->scene.texture[i].path, \
item->dirent.full_path, 255) == 0)
return (*(t_texture **)item->param.param \
= &data->scene.texture[i], (void)0);
i++;
}
ft_printf("[Explorer] Loading file: %s\n", item->dirent.full_path);
ppm_image_read(item->dirent.full_path, \
&data->scene.texture[data->scene.texture_count]);
*(t_texture **)item->param.param = \
&data->scene.texture[data->scene.texture_count];
if (data->scene.texture_count == MAPS_MAX)
data->scene.texture_count = 0;
ft_printf("[Explorer] File Loaded!\n");
}
void file_load(t_item *item, t_data *data)
{
if (item->param.type == PARAM_TEXTURE \
&& is_format(item->dirent.full_path, ".ppm"))
texture_load(item, data);
else if (item->param.type == PARAM_BUMP \
&& is_format(item->dirent.full_path, ".bump"))
bump_map_load(item, data);
else if (item->param.type == PARAM_SCENE \
&& is_format(item->dirent.full_path, ".rt"))
{
ft_printf("[Explorer] Loading file: %s\n", item->dirent.full_path);
scene_replace(item->param.param, item->dirent.full_path);
ft_printf("[Explorer] File Loaded!\n");
}
}

126
src/io/glyph.c Normal file
View File

@ -0,0 +1,126 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* glyph.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 17:29:23 by vvobis #+# #+# */
/* Updated: 2025/05/31 12:46:37 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
static void glyphs_create(char ****glyph, char const *path)
{
size_t i;
int fd;
size_t j;
char c;
i = 0;
*glyph = ft_calloc(GLYPH_COUNT, sizeof(*glyph));
lst_memory(*glyph, free, ADD);
ft_open(&fd, path, O_RDONLY, 0644);
while (i < GLYPH_COUNT)
{
(*glyph)[i] = ft_calloc(GLYPH_ROW, sizeof(**glyph));
lst_memory((*glyph)[i], free, ADD);
j = -1;
while (++j < GLYPH_ROW)
{
(*glyph)[i][j] = ft_calloc(GLYPH_COL, sizeof(***glyph));
lst_memory((*glyph)[i][j], free, ADD);
ft_read(fd, (*glyph)[i][j], GLYPH_COL + 1);
(*glyph)[i][j][GLYPH_COL] = 0;
}
ft_read(fd, &c, 1);
i++;
}
ft_close(fd);
}
static void glyph_draw( t_pixel *pixel, \
uint x, \
uint y, \
char **glyph)
{
size_t i;
size_t j;
i = 0;
if (x + 1 < WI)
{
while (i / 2 < GLYPH_ROW)
{
j = 0;
while (j / 2 < GLYPH_COL)
{
if (x + j + 1 < WI && y + i + 1 < HI \
&& glyph[i / 2][j / 2] == '1')
{
*pixel[(y + i) * WI + (x + j)].color = RAYWHITE;
*pixel[(y + i + 1) * WI + (x + j)].color = RAYWHITE;
*pixel[(y + i) * WI + (x + j + 1)].color = RAYWHITE;
*pixel[(y + i + 1) * WI + (x + j + 1)].color = RAYWHITE;
}
j += 2;
}
i += 2;
}
}
}
static int glyph_which(uint *begin_x, uint *begin_y, char const c)
{
if (ft_isalpha(c))
{
*begin_x += 12;
if (c >= 'A' && c <= 'Z')
return (c - 65 + 26);
return (c - 97 + 52);
}
else if (ft_isdigit(c))
{
return (*begin_x += 12, c - 48 + 16);
}
else if (c >= ' ' && c <= '/')
return (*begin_x += 12, c - 32);
else if (c == '_')
return (*begin_x += 12, 78);
else if (c == ':')
return (*begin_x += 12, 79);
else if (c == '=')
return (*begin_x += 12, 80);
else if (c == '\n')
return (*begin_y += 16, -1);
return (*begin_x += 12, -2);
}
void glyph_print( uint begin_x, \
uint begin_y, \
char const *text, \
t_pixel *pixel)
{
size_t i;
int x;
int index;
static char ***glyph = {0};
if (!glyph)
glyphs_create(&glyph, "./assets/alpha_bit_bonus");
x = begin_x;
i = 0;
while (text[i])
{
index = glyph_which(&begin_x, &begin_y, text[i]);
if (index >= 0)
{
glyph_draw(pixel, begin_x, begin_y, glyph[index]);
}
else if (index == -1)
begin_x = x;
i++;
}
}

25
src/io/id.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* id.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 18:26:35 by vvobis #+# #+# */
/* Updated: 2024/11/26 18:26:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
uint id_set(uint group, int id)
{
return ((group << 24) | (id & 0x00ffffff));
}
uint id_group_get(int id)
{
if (id < 0)
return (-1);
return ((id >> 24) & 0xff);
}

52
src/io/item_value_manip.c Normal file
View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* item_value_manip.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 18:29:13 by vvobis #+# #+# */
/* Updated: 2024/12/11 12:29:42 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
void item_bool_toggle(void *null, void *param)
{
(void)null;
*((bool *)((t_param *)param)->param) ^= 1;
}
void item_normal_inc(void *null, void *param)
{
(void)null;
*(double *)((t_param *)param)->param += .1;
if (*(double *)((t_param *)param)->param > 1)
*(double *)((t_param *)param)->param = 1;
}
void item_normal_dec(void *null, void *param)
{
(void)null;
*(double *)((t_param *)param)->param -= .1;
if (*(double *)((t_param *)param)->param < -1)
*(double *)((t_param *)param)->param = -1;
}
void item_double_inc(void *null, void *param)
{
(void)null;
*(double *)((t_param *)param)->param = \
*(double *)((t_param *)param)->param + 1;
}
void item_double_dec(void *null, void *param)
{
(void)null;
if (*(double *)((t_param *)param)->param - 1.0 <= 0 \
&& ((t_param *)param)->type == PARAM_RADIUS)
return ;
*(double *)((t_param *)param)->param = \
*(double *)((t_param *)param)->param - 1;
}

106
src/io/items_create.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* items_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 17:45:56 by vvobis #+# #+# */
/* Updated: 2024/12/05 17:40:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
t_item container_item_button_create(const char *title, void *param, \
void (*func_ptr)(void *, void *))
{
t_item item;
item.type = ITEM_BUTTON;
if (title && ft_strlen(title) < ITEM_TITLE_LEN)
ft_strlcpy(item.description, title, ft_strlen(title) + 1);
if (param)
item.button.param = param;
else
item.button.param = NULL;
if (func_ptr)
item.button.func_ptr = func_ptr;
return (item);
}
t_item container_item_slider_create(const char *title, uint8_t min, \
uint8_t max, uint8_t *value)
{
t_item item;
item.type = ITEM_SLIDER;
if (title && ft_strlen(title) < ITEM_TITLE_LEN)
ft_strlcpy(item.description, title, ft_strlen(title) + 1);
item.slider.min = min;
item.slider.max = max;
item.slider.value = value;
return (item);
}
static void container_internal_button_add_grid(t_container *container, \
t_item *item)
{
uint item_cursor;
t_rect item_attr;
item_cursor = container->item_count;
item_attr = (t_rect) \
{
.x = container->attr.x + CONTAINER_PADDING \
+ ((container->attr.width / 2) * (item_cursor % 2)),
.y = (container->attr.y + ITEM_HEIGHT) + ITEM_PADDING + (ITEM_HEIGHT \
* (item_cursor / 2)),
.width = container->attr.width / 2 - (ITEM_PADDING * 2),
.height = ITEM_HEIGHT,
};
item->rect = item_attr;
container->item[item_cursor] = *item;
}
static void container_internal_add_list(t_container *container, t_item *item)
{
uint item_cursor;
t_rect item_attr;
item_cursor = container->item_count;
item_attr = (t_rect) \
{
.x = container->attr.x + CONTAINER_PADDING,
.y = ITEM_PADDING + ITEM_HEIGHT \
+ container->item[item_cursor - (item_cursor > 0)].rect.y,
.width = container->attr.width - ITEM_PADDING,
.height = ITEM_HEIGHT,
};
item->rect = item_attr;
if (item->type == ITEM_SLIDER)
container_internal_slider_config(item);
container->item[item_cursor] = *item;
}
void container_item_add(t_container *container, t_item *item)
{
if (!container)
return (ft_fprintf(STDERR_FILENO, \
"[CONTAINER] Container Invalid!\n"), (void)0);
if (container->item_count > CONTAINER_ITEM_COUNT - 1)
return (ft_fprintf(STDERR_FILENO, \
"[CONTAINER] Container '%s' creation Failed!: " \
"Too many items (%u)! MAX_ITEM is %d\n", \
container->title, container->item_count, \
CONTAINER_ITEM_COUNT), (void)0);
if (item)
{
item->id = id_set(ID_GROUP_ITEM, container->item_count);
if (container->format == CONTAINER_GRID && item->type == ITEM_BUTTON)
container_internal_button_add_grid(container, item);
else
container_internal_add_list(container, item);
container->item_count++;
}
}

89
src/io/key_change_scene.c Normal file
View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key_change_scene.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/26 18:12:48 by vvobis #+# #+# */
/* Updated: 2025/05/30 12:44:03 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
uint key_change_res_minus(t_scene *scene)
{
if (scene->resolution_x == 1 && scene->resolution_y == 1)
return (scene->anti_aliasing = ANTI_ALIASING_FACTOR, 1);
if (scene->resolution_x == RESOLUTION_SCALE_X \
&& scene->resolution_y == RESOLUTION_SCALE_Y)
return (scene->resolution_x = 1, scene->resolution_y = 1, 1);
if (scene->resolution_x > 1 && scene->resolution_y > 1)
return (scene->resolution_x /= 2, scene->resolution_y /= 2, 1);
return (1);
}
uint key_change_res(int keycode, t_scene *scene)
{
if (keycode == KEY_MINUS)
{
if (scene->anti_aliasing)
return (scene->anti_aliasing = false, 1);
if (scene->resolution_x == 1 && scene->resolution_y == 1)
return (scene->resolution_x = RESOLUTION_SCALE_X, \
scene->resolution_y = RESOLUTION_SCALE_Y, 1);
if (scene->resolution_x >= RESOLUTION_SCALE_X * \
SCENE_START_RESOLUTION_CAP || scene->resolution_y >= \
RESOLUTION_SCALE_Y * SCENE_START_RESOLUTION_CAP)
return (false);
scene->resolution_x *= 2;
scene->resolution_y *= 2;
}
else if (keycode == KEY_EQUAL)
key_change_res_minus(scene);
else if (keycode == KEY_ENTER)
return (scene->resolution_x = scene->resolution_y = 1, 1);
else if (keycode == KEY_BACKSPACE)
return (scene->camera.position = (t_vector){0, 0, 0}, \
scene->camera.fov = 1, 1);
return (false);
}
bool key_move_focused(int keycode, t_vector *focus)
{
if (keycode == KEY_D)
return (focus->x += 0.1, true);
else if (keycode == KEY_A)
return (focus->x -= 0.1, true);
else if (keycode == KEY_E)
return (focus->y += 0.1, true);
else if (keycode == KEY_Q)
return (focus->y -= 0.1, true);
else if (keycode == KEY_W)
return (focus->z += .2, true);
else if (keycode == KEY_S)
return (focus->z -= .2, true);
return (false);
}
bool key_change_fov(int keycode, t_camera *camera)
{
if (keycode == KEY_SLASH)
{
if (camera->fov < 175)
{
camera->fov += 5;
}
return (camera->fov_f = tan(camera->fov / 2 * M_PI / 180));
}
else if (keycode == KEY_PERIOD)
{
if (camera->fov > 5)
{
camera->fov -= 5;
}
return (camera->fov_f = tan(camera->fov / 2 * M_PI / 180));
}
return (false);
}

84
src/io/key_press.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key_press.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/05 16:30:08 by vvobis #+# #+# */
/* Updated: 2025/05/31 15:08:27 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../minirt.h"
uint key_misc_function(int keycode, t_scene *scene, t_data *data)
{
if (keycode == KEY_P)
scene_save(scene);
else if (keycode == KEY_M)
return (data->func_ptr = container_draw, \
data->param = &data->menu[MENU_MAIN], true);
else if (keycode == KEY_H)
return (data->func_ptr = help_menu_draw, true);
else if (keycode == KEY_DELETE)
{
if (data->mouse.grabbed)
((t_body *)data->mouse.grabbed)->type = BODY_DELETED;
data->mouse.grabbed = NULL;
return (true);
}
else if (keycode == KEY_B)
{
scene->sky_sphere = !scene->sky_sphere;
swap_2_ints(&scene->sky_color, &scene->ambient.color);
}
return (false);
}
int key_move_camera(int keycode, t_camera *camera)
{
t_vector translation;
calc_camera_space(camera);
if (keycode == KEY_A || keycode == KEY_D)
translation = scale_vector(camera->right, \
(keycode - KEY_A - 1.5) * 0.66);
else if (keycode == KEY_W || keycode == KEY_S)
translation = scale_vector(camera->normal, (keycode - KEY_S - 2) * 0.5);
else if (keycode == KEY_Q || keycode == KEY_E)
translation = scale_vector(camera->up, (keycode - KEY_E - 6) * 0.33);
else
return (false);
camera->position = add_vector(camera->position, translation);
calc_camera_space(camera);
return (true);
}
void key_press_distribute(int keycode, t_data *data, t_scene *scene)
{
if (move_body(keycode, scene->body_focus) || \
key_move_camera(keycode, &scene->camera) || \
calc_camera_rotation(keycode, &scene->camera) || \
key_misc_function(keycode, scene, data) || \
key_change_res(keycode, scene) || \
key_move_light(keycode, scene) || \
key_change_fov(keycode, &scene->camera))
return ;
}
int key_press(int keycode, void *data_ptr)
{
t_scene *scene;
t_data *data;
data = data_ptr;
scene = &data->scene;
if (keycode == KEY_ESCAPE)
{
lst_memory(NULL, NULL, END);
exit(0);
}
key_press_distribute(keycode, data, scene);
return (1);
}

Some files were not shown because too many files have changed in this diff Show More