Browse Source

add: doors

master
narnaud 3 years ago
parent
commit
4bf2ba8e40
  1. 14
      README.md
  2. 6
      includes/cub3d.h
  3. 139
      includes/mlx.h
  4. 3
      includes/mlx_keycode.h
  5. 43
      maps/scene_big.cub
  6. 16
      sources/hooks.c
  7. 18
      sources/main.c
  8. 38
      sources/parsing.c
  9. 42
      sources/render.c

14
README.md

@ -10,11 +10,19 @@ CUBE3D
- [Tester & maps](https://github.com/MichelleJiam/cub3D_tester)
## Issues:
- report invalid mapfile:
- wrong extension,
- unacessible file (wrong permission, directory, missin name)
-
- invalid color : Ci > 255, Ci < 0, i > 4
- invalid texture (wrong permission, missing wall orientation texture, missing used block texture (warning + use default unknow texture))
- empty line in map description,
- unauthorized character in map description (!isdigt)
-
## Memo:
- ToDo: add F1 keycode on linux
- ToDo: add F1 keycode on linux,
- Add toggle for mouse control,
## Program life cycle :
### Parsing
- [x] read file,

6
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/06/02 17:24:53 by narnaud ### ########.fr */
/* Updated: 2022/06/03 13:17:47 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,7 +68,7 @@ typedef struct s_raycast
double sDist[2];
double dDist[2];
int *step;
int face;
int tex;
double dist;
double wallX;
} t_raycast;
@ -97,7 +97,7 @@ typedef struct s_env
int line_bytes;
int endian;
int *buffer;
t_img wall[4];
t_img tex[5];
int minimap;
int controls[KEYS_LIMIT];
int mouseX;

139
includes/mlx.h

@ -0,0 +1,139 @@
/*
** mlx.h for MinilibX in
**
** Made by Charlie Root
** Login <ol@epitech.net>
**
** Started on Mon Jul 31 16:37:50 2000 Charlie Root
** Last update Tue May 15 16:23:28 2007 Olivier Crouzet
*/
/*
** MinilibX - Please report bugs
*/
/*
** FR msg - FR msg - FR msg
**
** La MinilibX utilise 2 librairies supplementaires qu'il
** est necessaire de rajouter a la compilation :
** -lmlx -lXext -lX11
**
** La MinilibX permet le chargement des images de type Xpm.
** Notez que cette implementation est incomplete.
** Merci de communiquer tout probleme de chargement d'image
** de ce type.
*/
#ifndef MLX_H
#define MLX_H
void *mlx_init();
/*
** needed before everything else.
** return (void *)0 if failed
*/
/*
** Basic actions
*/
void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title);
/*
** return void *0 if failed
*/
int mlx_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
/*
** origin for x & y is top left corner of the window
** y down is positive
** color is 0x00RRGGBB
*/
/*
** Image stuff
*/
void *mlx_new_image(void *mlx_ptr,int width,int height);
/*
** return void *0 if failed
** obsolete : image2 data is stored using bit planes
** void *mlx_new_image2(void *mlx_ptr,int width,int height);
*/
char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel,
int *size_line, int *endian);
/*
** endian : 0 = sever X is little endian, 1 = big endian
** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes
*/
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y);
int mlx_get_color_value(void *mlx_ptr, int color);
/*
** dealing with Events
*/
int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param);
int mlx_loop (void *mlx_ptr);
int mlx_loop_end (void *mlx_ptr);
/*
** hook funct are called as follow :
**
** expose_hook(void *param);
** key_hook(int keycode, void *param);
** mouse_hook(int button, int x,int y, void *param);
** loop_hook(void *param);
**
*/
/*
** Usually asked...
*/
int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
char *string);
void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data,
int *width, int *height);
void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
int *width, int *height);
int mlx_destroy_window(void *mlx_ptr, void *win_ptr);
int mlx_destroy_image(void *mlx_ptr, void *img_ptr);
int mlx_destroy_display(void *mlx_ptr);
/*
** generic hook system for all events, and minilibX functions that
** can be hooked. Some macro and defines from X11/X.h are needed here.
*/
int mlx_hook(void *win_ptr, int x_event, int x_mask,
int (*funct)(), void *param);
int mlx_do_key_autorepeatoff(void *mlx_ptr);
int mlx_do_key_autorepeaton(void *mlx_ptr);
int mlx_do_sync(void *mlx_ptr);
int mlx_mouse_get_pos(void *mlx_ptr, void *win_ptr, int *x, int *y);
int mlx_mouse_move(void *mlx_ptr, void *win_ptr, int x, int y);
int mlx_mouse_hide(void *mlx_ptr, void *win_ptr);
int mlx_mouse_show(void *mlx_ptr, void *win_ptr);
int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey);
#endif /* MLX_H */

3
includes/mlx_keycode.h

@ -6,7 +6,7 @@
/* By: bledda <bledda@student.42nice.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/10 20:43:21 by bledda #+# #+# */
/* Updated: 2022/06/02 07:49:04 by narnaud ### ########.fr */
/* Updated: 2022/06/03 12:02:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,6 +63,7 @@
# define KEY_SHFT 65505
# define KEY_CTRL 65507
# define KEY_SPACE 32
# define KEY_F1 65470
# elif __APPLE__
# define KEYS_LIMIT 300
# define KEY_Q 12

43
maps/scene_big.cub

@ -1,7 +1,8 @@
NO ./textures/wood.xpm
NO ./textures/redbrick.xpm
EA ./textures/colorstone.xpm
SO ./textures/greystone.xpm
WE ./textures/redbrick.xpm
SO ./textures/redbrick.xpm
WE ./textures/greystone.xpm
DO ./textures/wood.xpm
F 0, 156, 8
C 27, 210, 227
@ -12,38 +13,38 @@ C 27, 210, 227
1001000001 1
11111 11111 1001000001 1
10001 1000111110011111111111 1
10001 1000000000000000000001 1
10001 1000000000000000000001 1
10001 10001111011111111000011111110000000000000000000001111111
10001 10001 1010000001000000000000000000000000000000000000001
10001 1000000000000000000002 1
10001 1000000000000000000002 1
10001 10001111011111111000011111111111111111111111111111111111
10001 10001 1010000001000020000000000000000000000000000000001
10001 10001 1011111111000010000010000000000000000000001000001
10001111110001 1000000000000010010010011111111111111111001001001
10000000000001 1111101111000010010010000000000000000000001001001
10000000000001 101111000010010011111111111111111111111001001
10000E00000001 100000000010010000000000000000000000000001001
10000000000001 100000000010011111111111111111111111111111001
10111111111111 111111111110000000000000000000000000000000001
100000001 100000000011111111111111111111111111111111101
1111110011111000000010000000000 101
10000000100010000000000000000111 101
10000000000000000000000000000101 101
11111111110000000001111111111101111111111111111111111111111111101111111
10000000010000000001000000000100000000000000000000000000000000000000001
10000000010000000001000000000000111111111111111100000000000000000000001
10000000010000000001000000000000000000000010000000000000000000110000001
12111111111111 111111111110000000000000000000000000000000001
100000001 100000000112211111111111111111111111111111101
111111001111100000001000000002 101
100000001000100000000000000001 101
100000000000000000000000000001 101
11111111110000000001111111111112211111111111111111111111111111101111111
10000000010000000001000000000100000000000000000200000000000000000000001
10000000010000000001000000000200111111111111111100000000000000000000001
10000000010000000001000000000200000000000010000000000000000011110000001
1000000001000000000100000000010111111111111000000000000000001 10000001
1011111111000000000111111000010100000000000001100000000000001 10000001
1211111111000000000111111000010100000000000001100000000000001 10000001
1010000000011110000000001000010100000000000001100000111111111 10000001
1010000000011110000000001000010100000000000001111111100000000 11111111
10100000000111100000000010000101001111111111111111111111111111111111111
10100000000111100000000010000101000000000000011111111 11111111
10100000000111100000000010000101001111111111111111111211111111111111111
10100000000111100000000011111101000000000000000000000000000000000000001
10101111111101111111100000000001010001000100010001000100010001000100101
10121111111101111111100000000001010001000100010001000100010001000100101
10101111111111111111111111111111000100010001000100010001000100010001001
10101000000000000100000000000010010001000100010001000100010001000100011
10101111110111110101111111101111000100010001000100010001000100010001001
10101000000101000001000000000010010001000100010001000100010001000100011
10101011111101011101011111111111000100010001000100010001000100010001001
10101000000000010101000000000000010001000100010001000100010001000100011
10101000000000010101000000000020010001000100010001000100010001000100011
10000011111111110101011111111111000100010001000100010001000100010001001
10101000001000010101000000000010010001000100010001000100010001000100011
10111111111111010101111111111111000100010001000100010001000100010001001

16
sources/hooks.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */
/* Updated: 2022/06/02 17:50:36 by narnaud ### ########.fr */
/* Updated: 2022/06/03 13:26:38 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,8 +16,10 @@ int key_press_hook(int keycode, t_env *env)
{
if (keycode == KEY_M)
env->minimap = !env->minimap;
if (keycode == KEY_F1)
else if (keycode == KEY_F1)
env->debug = !env->debug;
else if (keycode == KEY_ECHAP)
exit(EXIT_SUCCESS);
else
env->controls[keycode] = 1;
if (env->debug)
@ -70,17 +72,17 @@ int update_hook(t_env *env)
env->tick++;
dX = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x
+ (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.x )
+ (env->controls[KEY_E] - env->controls[KEY_Q]) * env->camPlan.x )
/ (20 - env->controls[KEY_SHFT] * 10);
dY = ((env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y
+ (env->controls[KEY_D] - env->controls[KEY_A]) * env->camPlan.y)
+ (env->controls[KEY_E] - env->controls[KEY_Q]) * env->camPlan.y)
/ (20 - env->controls[KEY_SHFT] * 10);
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0')
if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] != '1')
set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y);
if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0')
if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] != '1')
set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY);
env->playerDir = rot_vec(env->playerDir,
(double)(env->controls[KEY_E] - env->controls[KEY_Q]) * M_PI / 72, 1);
(double)(env->controls[KEY_D] - env->controls[KEY_A]) * M_PI / 72, 1);
env->camPlan = rot_vec(env->playerDir, M_PI / 2, 0.66);
if (env->debug && !(env->tick % 50))
{

18
sources/main.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */
/* Updated: 2022/06/02 17:35:19 by narnaud ### ########.fr */
/* Updated: 2022/06/03 13:20:27 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,8 +40,8 @@ int main(int argc, char **argv)
if (DEBUG)
{
printf("\e[1;32m========> WALLS <========\e[0m\nNorth: %s\nSouth: %s\nWest: %s\nEast: %s\n",\
env->wall[0].file, env->wall[1].file,\
env->wall[2].file, env->wall[3].file);
env->tex[0].file, env->tex[1].file,\
env->tex[2].file, env->tex[3].file);
printf("\e[1;32m========================\e[0m\n");
printf("=> Floor color: %d\n=> Ceil color: %d\n", env->floorColor, env->ceilColor);
printf("\e[1;32m========> MAP <========\e[0m\n");
@ -51,13 +51,13 @@ int main(int argc, char **argv)
}
init_window(env);
y = 0;
while (y < 4)
while (y < 5)
{
env->wall[y].img = mlx_xpm_file_to_image(env->mlx,
env->wall[y].file, &env->wall[y].width, &env->wall[y].height);
env->wall[y].buffer = (int *)mlx_get_data_addr(env->wall[y].img, \
&env->wall[y].pixel_bits, &env->wall[y].line_bytes, &env->wall[y].endian);
env->wall[y].line_bytes /= 4;
env->tex[y].img = mlx_xpm_file_to_image(env->mlx,
env->tex[y].file, &env->tex[y].width, &env->tex[y].height);
env->tex[y].buffer = (int *)mlx_get_data_addr(env->tex[y].img, \
&env->tex[y].pixel_bits, &env->tex[y].line_bytes, &env->tex[y].endian);
env->tex[y].line_bytes /= 4;
y++;
}
env->img = mlx_new_image(env->mlx, WIN_X_SZ, WIN_Y_SZ);

38
sources/parsing.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/23 17:20:29 by narnaud #+# #+# */
/* Updated: 2022/06/02 17:30:35 by narnaud ### ########.fr */
/* Updated: 2022/06/03 12:29:50 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,14 +15,16 @@
int cleanup_datas(t_env *env)
{
if (env->wall[0].file)
free(env->wall[0].file);
if (env->wall[1].file)
free(env->wall[1].file);
if (env->wall[2].file)
free(env->wall[2].file);
if (env->wall[3].file)
free(env->wall[3].file);
if (env->tex[0].file)
free(env->tex[0].file);
if (env->tex[1].file)
free(env->tex[1].file);
if (env->tex[2].file)
free(env->tex[2].file);
if (env->tex[3].file)
free(env->tex[3].file);
if (env->tex[4].file)
free(env->tex[4].file);
return (1);
}
@ -34,13 +36,15 @@ void register_settings(int *progress, t_env *env, char *line)
if (!elem[0] || !elem[1])
return ;
if (!ft_strncmp(elem[0],"NO", 3))
env->wall[0].file = ft_strtrim(elem[1], "\n");
env->tex[0].file = ft_strtrim(elem[1], "\n");
else if (!ft_strncmp(elem[0],"SO", 3))
env->wall[1].file = ft_strtrim(elem[1], "\n");
env->tex[1].file = ft_strtrim(elem[1], "\n");
else if (!ft_strncmp(elem[0],"WE", 3))
env->wall[2].file = ft_strtrim(elem[1], "\n");
env->tex[2].file = ft_strtrim(elem[1], "\n");
else if (!ft_strncmp(elem[0],"EA", 3))
env->wall[3].file = ft_strtrim(elem[1], "\n");
env->tex[3].file = ft_strtrim(elem[1], "\n");
else if (!ft_strncmp(elem[0],"DO", 3))
env->tex[4].file = ft_strtrim(elem[1], "\n");
else if (!ft_strncmp(elem[0],"F", 2))
env->floorColor = rgb_to_int(elem);
else if (!ft_strncmp(elem[0],"C", 2))
@ -175,6 +179,7 @@ int is_in_open_room(t_env *env, int x, int y)
return (0);
}
t_env *parse_envFile(char *filename)
{
int fd;
@ -188,19 +193,20 @@ t_env *parse_envFile(char *filename)
ret = ft_calloc(1, sizeof(t_env));
ret->deep = 0;
ret->wide = 0;
// ret->mouseX = 0;
ret->mouseX = 0;
ret->debug = DEBUG;
fd = open(filename, O_RDONLY);
line = get_next_line(fd);
while (line)
{
if (*line && *line != '\n' && progress > 5 && progress++)
if (*line && *line != '\n' && progress > 6 && progress++)
ft_slst_add_back(&e_map, read_map_line(ret, line));
else
register_settings(&progress, ret, line);
free(line);
line = get_next_line(fd);
}
if (progress < 6 && cleanup_datas(ret))
if (progress < 7 && cleanup_datas(ret))
return (NULL);
else
ret->map = create_map_array(e_map, ret->wide, ret->deep);

42
sources/render.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */
/* Updated: 2022/06/02 16:05:48 by narnaud ### ########.fr */
/* Updated: 2022/06/03 15:03:57 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,6 +55,8 @@ void render_minimap(t_env *env, t_vec size)
draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 39424}, 1);
else if (map[vec.y][vec.x] == '1')
draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 9539977}, 1);
else if (map[vec.y][vec.x] == '2')
draw_rectangle(env, (t_rectangle){vec.x * dX, vec.y * dY, dX, dY, 100 << 16 | 20 << 8| 100}, 1);
vec.x++;
}
vec.y++;
@ -71,6 +73,7 @@ void render_view(t_env* env)
int x;
int startEnd[2];
int lineHeight;
int mapVal;
x = 0;
while (x < WIN_X_SZ)
@ -103,18 +106,27 @@ void render_view(t_env* env)
if (rc.sDist[0] > rc.sDist[1])
side++;
rc.cell[side] += rc.step[side];
if (env->map[rc.cell[1]][rc.cell[0]] > '0')
mapVal = env->map[rc.cell[1]][rc.cell[0]];
if (mapVal > '0')
{
rc.face = 3 - side * 2 - (rc.step[side] + 1) / 2;
if (!side)
rc.wallX= env->playerPos.y + rc.sDist[side] * rc.vec.y;
if (mapVal == '2' && (rc.sDist[abs(side + 1)] > 4 && rc.sDist[side] > 3))
rc.tex = 4;
else if (mapVal == '1')
rc.tex = 3 - side * 2 - (rc.step[side] + 1) / 2;
else
rc.wallX = env->playerPos.x + rc.sDist[side] * rc.vec.x;
rc.wallX -= floor(rc.wallX);
{
rc.sDist[side] += rc.dDist[side];
continue ;
}
break ;
}
rc.sDist[side] += rc.dDist[side];
}
if (!side)
rc.wallX= env->playerPos.y + rc.sDist[side] * rc.vec.y;
else
rc.wallX = env->playerPos.x + rc.sDist[side] * rc.vec.x;
rc.wallX -= floor(rc.wallX);
lineHeight = (int)(WIN_Y_SZ / rc.sDist[side]);
if (lineHeight < 0)
lineHeight = 0;
@ -125,24 +137,22 @@ void render_view(t_env* env)
if(startEnd[1] >= WIN_Y_SZ)
startEnd[1] = WIN_Y_SZ - 1;
int texHeight = env->wall[rc.face].height;
int texHeight = env->tex[rc.tex].height;
draw_rectangle(env, (t_rectangle){x, 0, 1, startEnd[0], env->ceilColor}, 0);
//draw_rectangle(env, (t_rectangle){x, startEnd[0], 1, startEnd[1] - startEnd[0] - 1, color}, 0);
//int texY = (int)(y * env->wall[rc.face].height / (startEnd[1] - startEnd[0]));
int texX = (int)(rc.wallX * (double)env->wall[rc.face].width);
if(rc.face >= 2 && rc.vec.x > 0)
texX = env->wall[rc.face].width - texX - 1;
if(rc.face < 2 && rc.vec.y < 0)
texX = env->wall[rc.face].width - texX - 1;
int texX = (int)(rc.wallX * (double)env->tex[rc.tex].width);
if(!side && rc.vec.x > 0)
texX = env->tex[rc.tex].width - texX - 1;
if(side && rc.vec.y < 0)
texX = env->tex[rc.tex].width - texX - 1;
double step = 1.0 * texHeight / lineHeight;
// ---
double texPos = (startEnd[0] - WIN_Y_SZ / 2 + lineHeight / 2) * step;
for(int y = startEnd[0]; y<startEnd[1]; y++)
{
int texY = (int)texPos & (texHeight - 1);
texPos += step;
env->buffer[env->line_bytes * y + x] \
= env->wall[rc.face].buffer[texHeight * texY + texX];
= env->tex[rc.tex].buffer[texHeight * texY + texX];
}
draw_rectangle(env, (t_rectangle){x, startEnd[1], 1, WIN_Y_SZ - startEnd[1], env->floorColor}, 0);

Loading…
Cancel
Save