diff --git a/Makefile b/Makefile index ce0f539..9262555 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ D_SRCS = srcs \ srcs/init \ srcs/hook \ srcs/draw \ + srcs/player \ srcs/mem SRCS = cube3d.c @@ -27,11 +28,15 @@ SRCS += init_struct.c \ # hook/ SRCS += keyhook.c \ key_do_action.c \ - key_is_action_1.c + key_is_action_1.c \ + key_is_action_2.c +# player/ +SRCS += player_moves.c \ + player_rotates.c \ + player_limits.c # draw/ SRCS += draw.c \ - player_moves.c \ - player_limits.c + draw_window.c # headers D_HEADERS = headers diff --git a/headers/cube3d.h b/headers/cube3d.h index 330dd20..ea44b55 100644 --- a/headers/cube3d.h +++ b/headers/cube3d.h @@ -6,6 +6,7 @@ # include // for sleep() # include // for atoi() # include // for printf() +# include // for M_PI, cos(), sin() # include "colors.h" # include "memorybook.h" diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index d848f36..0acd529 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -2,6 +2,9 @@ # define CUBE3D_MACRO_H # define MAX_NB_KEY 3 +# define SCREEN_DIST 50 +# define SCREEN_SIZE 100 +# define SCREEN_DEF 50 # define ARROW_LEFT 65361 # define ARROW_UP 65362 diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 2816e1e..3fb72cb 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -32,24 +32,42 @@ int keyrelease(int keycode, t_game *game); // key_do_action.c void keypress_do_action(t_game *game); // key_is_action_1.c -int is_esc(int *k_hook); -int is_go_left(int *k_hook); -int is_go_right(int *k_hook); -int is_go_forward(int *k_hook); -int is_go_backward(int *k_hook); +int is_esc(int *k_hook, int *is_action); +int is_go_left(int *k_hook, int *is_action); +int is_go_right(int *k_hook, int *is_action); +int is_go_forward(int *k_hook, int *is_action); +int is_go_backward(int *k_hook, int *is_action); +// key_is_action_2.c +int is_turn_left(int *k_hook, int *is_action); +int is_turn_right(int *k_hook, int *is_action); // ------------------------------- -// SRC/DRAW +// SRC/PLAYER // ------------------------------- -// draw.c -void draw(t_game *game); // player_moves.c void plr_posx_decrement(t_game *game); void plr_posy_decrement(t_game *game); void plr_posx_increment(t_game *game); void plr_posy_increment(t_game *game); +// player_rotates.c +void plr_turn_right(t_game *game); +void plr_turn_left(t_game *game); // player_limits.c -void plr_limits(t_game *game, int x, int y); +int plr_out_limits(t_game *game, int x, int y); +int is_wall(t_game *game, int cell_x, int cell_y); + +// ------------------------------- +// SRC/DRAW +// ------------------------------- +// draw.c +void draw_pixel(t_game *game, int x, int y, int color); +void rotate(t_game *game, int *x, int *y); +void rotate_double(t_game *game, double *x, double *y); +void draw_line(t_game *game, int start_x, int start_y, int end_x, int end_y, int color); +void draw(t_game *game); +// draw_window.c +void draw_rays(t_game *game); +void draw_map(t_game *game); #endif diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 02f2486..56d3237 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -1,6 +1,14 @@ #ifndef CUBE3D_STRUCT_H # define CUBE3D_STRUCT_H +typedef struct s_vec +{ + int start_x; + int start_y; + int end_x; + int end_y; +} t_vec; + typedef struct s_game { // window @@ -8,7 +16,27 @@ typedef struct s_game void *win_ptr; int win_size_x; int win_size_y; + // rays + t_vec screen_dist; + t_vec screen_size; + t_vec ray; + // tmp + int ray_highlight; + int ray_activ; + // map + int map_size_x; + int map_size_y; + char **map; + int cell; + // rot + int rot; + double cosi; + double cosj; + double sini; + double sinj; // player + double plr_exact_x; + double plr_exact_y; int plr_x; int plr_y; // key hook diff --git a/headers/memorybook.h b/headers/memorybook.h index 19c4c73..09651c1 100644 --- a/headers/memorybook.h +++ b/headers/memorybook.h @@ -1,14 +1,6 @@ #ifndef MEMORYBOOK_H # define MEMORYBOOK_H -typedef void(*t_mb_func_exit)(void*); - -typedef struct s_exit -{ - t_mb_func_exit f; - void *param; -} t_exit; - void mb_init(void(*f)(void*), void *param); void *mb_alloc(size_t size); void mb_add(void *addr); diff --git a/srcs/cube3d.c b/srcs/cube3d.c index 8940f24..c5481e7 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -24,6 +24,8 @@ int main(int ac, char **av) init_parsing(ac, av, game); mb_init(destroy_mlx, game); + // draw game a first time before it start + draw(game); // receive a keypress event mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game); // receive a keyprelease event diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 2b73b9f..ae18167 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -2,8 +2,8 @@ static int pxl_out_limits(t_game *game, int x, int y) { - int xmax; - int ymax; + int xmax; + int ymax; xmax = game->sizel / (game->bpp / 8); ymax = game->win_size_y; @@ -12,7 +12,7 @@ static int pxl_out_limits(t_game *game, int x, int y) return (0); } -static void draw_pixel(t_game *game, int x, int y, int color) +void draw_pixel(t_game *game, int x, int y, int color) { unsigned int position; @@ -22,30 +22,73 @@ static void draw_pixel(t_game *game, int x, int y, int color) *(unsigned int*)(game->img_data + position) = color; } +void rotate(t_game *game, int *x, int *y) +{ + int old_x; + int tmp_x; + int tmp_y; + + // do nothing if not rotating + if (game->rot == 0) + return ; + // offset center + tmp_x = *x - game->plr_x; + tmp_y = *y - game->plr_y; + // calculate new coordinates + old_x = tmp_x; + tmp_x = tmp_x * game->cosi + tmp_y * game->cosj; + tmp_y = old_x * game->sini + tmp_y * game->sinj; + // de-offset center + *x = tmp_x + game->plr_x; + *y = tmp_y + game->plr_y; +} + +void rotate_double(t_game *game, double *x, double *y) +{ + double old_x; + double tmp_x; + double tmp_y; + + // do nothing if not rotating + if (game->rot == 0) + return ; + // offset center + tmp_x = *x - game->plr_exact_x; + tmp_y = *y - game->plr_exact_y; + // calculate new coordinates + old_x = tmp_x; + tmp_x = tmp_x * game->cosi + tmp_y * game->cosj; + tmp_y = old_x * game->sini + tmp_y * game->sinj; + // de-offset center + *x = tmp_x + game->plr_exact_x; + *y = tmp_y + game->plr_exact_y; +} + +void draw_line(t_game *game, int start_x, int start_y, int end_x, int end_y, int color) +{ + int dx; + int dy; + int i; + int j; + + dx = end_x - start_x; + dy = end_y - start_y; + i = 0; + j = 0; + while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy)) + { + draw_pixel(game, start_x + i, start_y + j, color); + if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx)) + j += ft_sign(dy); + else + i += ft_sign(dx); + } +} + void draw(t_game *game) { -// temp, draw a map of points - int x; - int y; - int x_size; -// unsigned int screen_size; - - x_size = game->sizel / (game->bpp / 8); -// screen_size = x_size * game->win_size_y / 5; - x = 0; - y = 0; - while (y <= game->win_size_y) - { -// y = x % x_size * 5; - draw_pixel(game, x, y, 0x00999999); - x += 5; - if (x > x_size) - { - x = 0; - y += 5; - } - } -// temp - draw_pixel(game, game->plr_x, game->plr_y, 0x0000FF00); + draw_map(game); + draw_rays(game); + //draw_view(game); mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0); } diff --git a/srcs/draw/draw_window.c b/srcs/draw/draw_window.c new file mode 100644 index 0000000..31f3f8c --- /dev/null +++ b/srcs/draw/draw_window.c @@ -0,0 +1,202 @@ +#include "cube3d.h" + +void ray_intersect(t_game *game, int start_x, int start_y, int *end_x, int *end_y) +{ + int cell_x; + int cell_y; + int slope_x; + int slope_y; + int ray_sign_x; + int ray_sign_y; + int first_next_x; + int first_next_y; + int next_x; + int next_y; + int next_cell_x; + int next_cell_y; + int ray_step_x; + int ray_step_y; + int is_x; + + is_x = 0; + // ray sign + ray_sign_x = 1; + if (start_x < *end_x) + ray_sign_x = -1; + ray_sign_y = 1; + if (start_y < *end_y) + ray_sign_y = -1; + // cell position of ray + cell_x = start_x / game->cell; + cell_y = start_y / game->cell; + // slope of ray + slope_x = *end_x - start_x; + slope_y = *end_y - start_y; + // direction of next cell + next_cell_x = -ray_sign_x; + next_cell_y = -ray_sign_y; + // ray steps in both axis + ray_step_x = ft_abs(game->cell * slope_y); + ray_step_y = ft_abs(game->cell * slope_x); + // first next time ray cross grid + first_next_x = start_x % game->cell; + if (first_next_x && ray_sign_x < 0) + first_next_x = game->cell - first_next_x; + if (!first_next_x && ray_sign_x < 0) + first_next_x = game->cell; + first_next_y = start_y % game->cell; + if (first_next_y && ray_sign_y < 0) + first_next_y = game->cell - first_next_y; + if (!first_next_y && ray_sign_y < 0) + first_next_y = game->cell; + next_x = ft_abs(first_next_x * slope_y); + next_y = ft_abs(first_next_y * slope_x); + // loop through grid +if (game->ray_activ == 1) printf("\ncell_x:%i, cell_y:%i, first_next_x:%i, first_next_y:%i\n", cell_x, cell_y, first_next_x, first_next_y); + while (!is_wall(game, cell_x, cell_y)) + { +if (game->ray_activ == 1) printf("next_x:%-4i, next_y:%-4i, ", next_x, next_y); + if (next_x > next_y) + { + cell_y += next_cell_y; + next_y += ray_step_y; + is_x = 0; + } + else + { + cell_x += next_cell_x; + next_x += ray_step_x; + is_x = 1; + } +if (game->ray_activ == 1) printf("cell_x:%-4i, cell_y:%-4i\n", cell_x, cell_y); + } + // end ray position + double ratio; + if (is_x) + { + *end_x = cell_x * game->cell; + if (ray_sign_x == 1) + *end_x += game->cell; + if (slope_x) + { + ratio = (double)(*end_x - start_x) / (double)slope_x; + *end_y = start_y + (double)slope_y * ratio; + } + } + else + { + *end_y = cell_y * game->cell; + if (ray_sign_y == 1) + *end_y += game->cell; + if (slope_y) + { + ratio = (double)(*end_y - start_y) / (double)slope_y; + *end_x = start_x + (double)slope_x * ratio; + } + } +} + +void draw_rays(t_game *game) +{ + int start_x; + int start_y; + int end_x; + int end_y; + int i; + + // draw screen size + start_x = game->screen_size.start_x + game->plr_x; + start_y = game->screen_size.start_y + game->plr_y; + rotate(game, &start_x, &start_y); + end_x = game->screen_size.end_x + game->plr_x; + end_y = game->screen_size.end_y + game->plr_y; + rotate(game, &end_x, &end_y); + draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF); + // draw rays + i = 0; + while (i <= SCREEN_DEF) + { + // tmp display one ray with infos + if (i == game->ray_highlight) + game->ray_activ = 1; + // tmp end + start_x = game->ray.start_x + game->plr_x; + start_y = game->ray.start_y + game->plr_y; + end_x = game->ray.end_x + game->plr_x + i * SCREEN_SIZE / SCREEN_DEF; + end_y = game->ray.end_y + game->plr_y; + rotate(game, &end_x, &end_y); + ray_intersect(game, start_x, start_y, &end_x, &end_y); + draw_line(game, start_x, start_y, end_x, end_y, 0x00FF00FF); + // tmp display one ray with infos + if (game->ray_activ == 1) + draw_line(game, start_x, start_y, end_x, end_y, 0x00FF0000); + // tmp end + i++; + game->ray_activ = 0; + } + // draw screen dist + start_x = game->screen_dist.start_x + game->plr_x; + start_y = game->screen_dist.start_y + game->plr_y; + end_x = game->screen_dist.end_x + game->plr_x; + end_y = game->screen_dist.end_y + game->plr_y; + rotate(game, &end_x, &end_y); + draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF); +} + +static void draw_square(t_game *game, int x, int y, int border, int fill, int size, int rotation) +{ + int i; + int j; + int new_x; + int new_y; + + i = 0; + while (i < size) + { + j = 0; + while (j < size) + { + new_x = x + j; + new_y = y + i; + if (rotation) + rotate(game, &new_x, &new_y); + if (!i || i == size - 1) + draw_pixel(game, new_x, new_y, border); + else if (!j || j == size - 1) + draw_pixel(game, new_x, new_y, border); + else + draw_pixel(game, new_x, new_y, fill); + j++; + } + i++; + } +} + +void draw_map(t_game *game) +{ + int i; + int j; + int x; + int y; + + i = 0; + x = 0; + y = 0; + while ((game->map)[i]) + { + j = 0; + while ((game->map)[i][j]) + { + if ((game->map)[i][j] == '1' ) + draw_square(game, x, y, 0x00999999, 0x00000000, game->cell, 0); + else + draw_square(game, x, y, 0x00555555, 0x00333333, game->cell, 0); + j++; + x += game->cell; + } + i++; + x = 0; + y += game->cell; + } + draw_square(game, game->plr_x - game->cell / 2, game->plr_y - game->cell / 2, 0x00999900, 0x00330033, game->cell, 1); +} diff --git a/srcs/draw/player_limits.c b/srcs/draw/player_limits.c deleted file mode 100644 index af4634c..0000000 --- a/srcs/draw/player_limits.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "cube3d.h" - -void plr_limits(t_game *game, int x, int y) -{ - int xmax; - int ymax; - - xmax = game->sizel / (game->bpp / 8); - ymax = game->win_size_y; - if (x < 0) - plr_posx_increment(game); - else if (y < 0) - plr_posy_increment(game); - else if (x > xmax) - plr_posx_decrement(game); - else if (y > ymax) - plr_posy_decrement(game); -} diff --git a/srcs/draw/player_moves.c b/srcs/draw/player_moves.c deleted file mode 100644 index 2e9e2c7..0000000 --- a/srcs/draw/player_moves.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "cube3d.h" - -void plr_posx_decrement(t_game *game) -{ - (game->plr_x) -= 5; - plr_limits(game, game->plr_x, game->plr_y); -} - -void plr_posy_decrement(t_game *game) -{ - (game->plr_y) -= 5; - plr_limits(game, game->plr_x, game->plr_y); -} - -void plr_posx_increment(t_game *game) -{ - (game->plr_x) += 5; - plr_limits(game, game->plr_x, game->plr_y); -} - -void plr_posy_increment(t_game *game) -{ - (game->plr_y) += 5; - plr_limits(game, game->plr_x, game->plr_y); -} diff --git a/srcs/hook/key_do_action.c b/srcs/hook/key_do_action.c index 5257d1a..f33aec3 100644 --- a/srcs/hook/key_do_action.c +++ b/srcs/hook/key_do_action.c @@ -3,15 +3,41 @@ // temp, to test keypress hook void keypress_do_action(t_game *game) { - if (is_esc(game->k_hook)) + int is_action; + + is_action = 0; + if (is_esc(game->k_hook, &is_action)) shut_down(game); - if (is_go_left(game->k_hook)) + if (is_go_left(game->k_hook, &is_action)) plr_posx_decrement(game); - if (is_go_right(game->k_hook)) + if (is_go_right(game->k_hook, &is_action)) plr_posx_increment(game); - if (is_go_forward(game->k_hook)) + if (is_go_forward(game->k_hook, &is_action)) plr_posy_decrement(game); - if (is_go_backward(game->k_hook)) + if (is_go_backward(game->k_hook, &is_action)) plr_posy_increment(game); - draw(game); + if (is_turn_left(game->k_hook, &is_action)) + plr_turn_left(game); + if (is_turn_right(game->k_hook, &is_action)) + plr_turn_right(game); + // temp display one ray with infos + if (!ft_arrintchr(game->k_hook, 65364, MAX_NB_KEY)) + { + if (game->ray_highlight <= SCREEN_DEF) + game->ray_highlight++; + else + game->ray_highlight = -1; + is_action = 1; + } + if (!ft_arrintchr(game->k_hook, 65362, MAX_NB_KEY)) + { + if (game->ray_highlight >= 0) + game->ray_highlight--; + else + game->ray_highlight = SCREEN_DEF; + is_action = 1; + } + // temp end + if (is_action) + draw(game); } diff --git a/srcs/hook/key_is_action_1.c b/srcs/hook/key_is_action_1.c index 860a656..6f7d40a 100644 --- a/srcs/hook/key_is_action_1.c +++ b/srcs/hook/key_is_action_1.c @@ -1,41 +1,61 @@ #include "cube3d.h" -int is_esc(int *k_hook) +int is_esc(int *k_hook, int *is_action) { - if (!ft_arrintchr(k_hook, KEY_ESC, 3)) + if (!ft_arrintchr(k_hook, KEY_ESC, MAX_NB_KEY)) + { + *is_action = 1; return 1; + } return 0; } -int is_go_left(int *k_hook) +int is_go_left(int *k_hook, int *is_action) { - if (!ft_arrintchr(k_hook, KEY_A, 3)) + if (!ft_arrintchr(k_hook, KEY_A, MAX_NB_KEY)) + { + *is_action = 1; return 1; - if (!ft_arrintchr(k_hook, KEY_Q, 3)) + } + if (!ft_arrintchr(k_hook, KEY_Q, MAX_NB_KEY)) + { + *is_action = 1; return 1; + } return 0; } -int is_go_right(int *k_hook) +int is_go_right(int *k_hook, int *is_action) { - if (!ft_arrintchr(k_hook, KEY_D, 3)) + if (!ft_arrintchr(k_hook, KEY_D, MAX_NB_KEY)) + { + *is_action = 1; return 1; + } return 0; } -int is_go_forward(int *k_hook) +int is_go_forward(int *k_hook, int *is_action) { - if (!ft_arrintchr(k_hook, KEY_W, 3)) + if (!ft_arrintchr(k_hook, KEY_W, MAX_NB_KEY)) + { + *is_action = 1; return 1; - if (!ft_arrintchr(k_hook, KEY_Z, 3)) + } + if (!ft_arrintchr(k_hook, KEY_Z, MAX_NB_KEY)) + { + *is_action = 1; return 1; + } return 0; } -int is_go_backward(int *k_hook) +int is_go_backward(int *k_hook, int *is_action) { - if (!ft_arrintchr(k_hook, KEY_S, 3)) + if (!ft_arrintchr(k_hook, KEY_S, MAX_NB_KEY)) + { + *is_action = 1; return 1; + } return 0; } - diff --git a/srcs/hook/key_is_action_2.c b/srcs/hook/key_is_action_2.c new file mode 100644 index 0000000..f90888e --- /dev/null +++ b/srcs/hook/key_is_action_2.c @@ -0,0 +1,21 @@ +#include "cube3d.h" + +int is_turn_left(int *k_hook, int *is_action) +{ + if (!ft_arrintchr(k_hook, ARROW_LEFT, MAX_NB_KEY)) + { + *is_action = 1; + return 1; + } + return 0; +} + +int is_turn_right(int *k_hook, int *is_action) +{ + if (!ft_arrintchr(k_hook, ARROW_RIGHT, MAX_NB_KEY)) + { + *is_action = 1; + return 1; + } + return 0; +} diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index a6e4e74..5bfa312 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -15,6 +15,7 @@ int keypress(int keycode, t_game *game) // temp print_keycode(keycode); + // temp end i = 0; while (i < MAX_NB_KEY && game->k_hook[i] != 0 && game->k_hook[i] != keycode) diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 29b9e5b..3348d2a 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -5,12 +5,24 @@ t_game *init_game(void) t_game *game; game = mb_alloc(sizeof(t_game)); + game->cell = 20; // player first position - game->plr_x = 0; - game->plr_y = 0; + game->plr_exact_x = 2 * game->cell; + game->plr_exact_y = 2 * game->cell; + game->plr_x = game->plr_exact_x; + game->plr_y = game->plr_exact_y; + // rotation + game->rot = 0; + game->cosi = 0; + game->cosj = 0; + game->sini = 0; + game->sinj = 0; + // map size + game->map_size_x = 24; + game->map_size_y = 24; // size window - game->win_size_x = 500; - game->win_size_y = 500; + game->win_size_x = game->map_size_x * game->cell; + game->win_size_y = game->map_size_y * game->cell; // init connexion to server game->mlx_ptr = mlx_init(); mb_add(game->mlx_ptr); @@ -19,6 +31,72 @@ t_game *init_game(void) game->win_size_y, "test"); // k(ey)_hook is the array containing the values of key press events ft_bzero(&game->k_hook, sizeof(game->k_hook)); + // map + int tmp[24][24] = { + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1}, + {1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, + }; + int i = 0; + game->map = mb_alloc(sizeof(char*) * (game->map_size_y + 1)); + (game->map)[game->map_size_y] = NULL; + while (i < 24) + { + (game->map)[i] = mb_alloc(sizeof(char) * (game->map_size_x + 1)); + (game->map)[i][game->map_size_x] = '\0'; + i++; + } + int j; + i = 0; + while (i < game->map_size_y) + { + j = 0; + while (j < game->map_size_x) + { + (game->map)[i][j] = tmp[i][j] + '0'; + j++; + } + i++; + } + // draw screen dist + game->screen_dist.start_x = 0; + game->screen_dist.start_y = 0; + game->screen_dist.end_x = 0; + game->screen_dist.end_y = -SCREEN_DIST; + // draw screen size + game->screen_size.start_x = -SCREEN_SIZE / 2; + game->screen_size.start_y = -SCREEN_DIST; + game->screen_size.end_x = SCREEN_SIZE / 2; + game->screen_size.end_y = -SCREEN_DIST; + // draw ray + game->ray.start_x = 0; + game->ray.start_y = 0; + game->ray.end_x = -SCREEN_SIZE / 2; + game->ray.end_y = -SCREEN_DIST; + // tmp + game->ray_highlight = -1; + game->ray_activ = 0; // create image and get its data address game->img_ptr = mlx_new_image(game->mlx_ptr, game->win_size_x, game->win_size_y); diff --git a/srcs/mem/memorybook.c b/srcs/mem/memorybook.c index 9c4fddb..069ec5c 100644 --- a/srcs/mem/memorybook.c +++ b/srcs/mem/memorybook.c @@ -1,16 +1,13 @@ #include "cube3d.h" -t_exit **mb_exit_func(); -t_list **mb_lst(); -int mb_comp_addr(void *to_find, void *to_compare); +void mb_set_params_exit(void (*f)(void *), void *param); +void mb_exec_exit_func(); +t_list **mb_get_lst(void); +int mb_comp_addr(void *to_find, void *to_compare); -void mb_init(void(*f)(void*), void *param) +void mb_init(void (*f)(void *), void *param) { - t_exit **texit; - - texit = mb_exit_func(); - (*texit)->param = param; - (*texit)->f = f; + mb_set_params_exit(f, param); } void *mb_alloc(size_t size) @@ -18,7 +15,7 @@ void *mb_alloc(size_t size) void *tmp; t_list **lst; - lst = mb_lst(); + lst = mb_get_lst(); tmp = ft_memalloc(size); if (!tmp) mb_exit(B_RED"failed create new allocation"RESET"\n"); @@ -31,7 +28,7 @@ void mb_add(void *addr) { t_list **lst; - lst = mb_lst(); + lst = mb_get_lst(); if (!ft_lstpush_back(lst, ft_lstcreate(addr))) mb_exit(B_RED"failed add new element to list"RESET"\n"); } @@ -41,22 +38,19 @@ void mb_free(void *addr) t_list **lst; t_list *tmp; - lst = mb_lst(); + lst = mb_get_lst(); tmp = ft_lstfind((*lst), addr, mb_comp_addr); if (!tmp) - ft_putstr_fd(B_RED"element to free doesn't exist (maybe it was already freed)"RESET"\n", 2); + ft_putstr_fd(B_RED"you try to free not allocated address"RESET"\n", 2); ft_lsterase(tmp, free); } void mb_exit(char *str) { t_list **lst; - t_exit **texit; - lst = mb_lst(); - texit = mb_exit_func(); - if ((*texit)->f) - (*texit)->f((*texit)->param); + lst = mb_get_lst(); + mb_exec_exit_func(); ft_putstr_fd(str, 2); ft_lstfree((*lst), free); exit(0); diff --git a/srcs/mem/memorybook_utils.c b/srcs/mem/memorybook_utils.c index 3ebab5d..90e4f9a 100644 --- a/srcs/mem/memorybook_utils.c +++ b/srcs/mem/memorybook_utils.c @@ -1,15 +1,46 @@ #include "cube3d.h" -t_exit **mb_exit_func() -{ - static t_exit *texit = NULL; +/* private struct definition : + * https://stackoverflow.com/questions/71724770/prototype-of-a-struct-in-c + * it wasn't necessary after all :p I just added setters and getters here + */ - if (!texit) - texit = mb_alloc(sizeof(t_exit)); - return (&texit); +typedef void(*t_mb_func_exit)(void*); + +typedef struct s_mb_exit +{ + t_mb_func_exit f; + void *param; +} t_mb_exit; + +static t_mb_exit *mb_get_exit_struct(void) +{ + static t_mb_exit *mbexit = NULL; + + if (!mbexit) + mbexit = mb_alloc(sizeof(t_mb_exit)); + return (mbexit); } -t_list **mb_lst() +void mb_set_params_exit(void (*f)(void *), void *param) +{ + t_mb_exit *mbexit; + + mbexit = mb_get_exit_struct(); + mbexit->param = param; + mbexit->f = f; +} + +void mb_exec_exit_func(void) +{ + t_mb_exit *mbexit; + + mbexit = mb_get_exit_struct(); + if (mbexit->f) + mbexit->f(mbexit->param); +} + +t_list **mb_get_lst(void) { static t_list *lst = NULL; @@ -20,4 +51,3 @@ int mb_comp_addr(void *to_find, void *to_compare) { return (to_find == to_compare); } - diff --git a/srcs/player/player_limits.c b/srcs/player/player_limits.c new file mode 100644 index 0000000..4d6961e --- /dev/null +++ b/srcs/player/player_limits.c @@ -0,0 +1,30 @@ +#include "cube3d.h" + +int is_wall(t_game *game, int cell_x, int cell_y) +{ + if (cell_x < 0 || cell_y < 0) + return (1); + if (cell_x > game->map_size_x || cell_y > game->map_size_y) + return (1); + if (game->map[cell_y][cell_x] != '0') + return (1); + return (0); +} + +int plr_out_limits(t_game *game, int x, int y) +{ + int xmax; + int ymax; + int cell_x; + int cell_y; + + xmax = game->sizel / (game->bpp / 8); + ymax = game->win_size_y; + cell_x = x / game->cell; + cell_y = y / game->cell; + if (is_wall(game, cell_x, cell_y)) + return (1); + if (x < 0 || y < 0 || x > xmax || y > ymax) + return (1); + return (0); +} diff --git a/srcs/player/player_moves.c b/srcs/player/player_moves.c new file mode 100644 index 0000000..daf299c --- /dev/null +++ b/srcs/player/player_moves.c @@ -0,0 +1,65 @@ +#include "cube3d.h" + +void plr_posx_decrement(t_game *game) +{ + double pos_x; + double pos_y; + + pos_x = game->plr_exact_x - 5; + pos_y = game->plr_exact_y; + rotate_double(game, &pos_x, &pos_y); + if (plr_out_limits(game, pos_x, pos_y)) + return ; + game->plr_exact_x = pos_x; + game->plr_exact_y = pos_y; + (game->plr_x) = (int)pos_x; + (game->plr_y) = (int)pos_y; +} + +void plr_posy_decrement(t_game *game) +{ + double pos_x; + double pos_y; + + pos_x = game->plr_exact_x; + pos_y = game->plr_exact_y - 5; + rotate_double(game, &pos_x, &pos_y); + if (plr_out_limits(game, pos_x, pos_y)) + return ; + game->plr_exact_x = pos_x; + game->plr_exact_y = pos_y; + (game->plr_x) = (int)pos_x; + (game->plr_y) = (int)pos_y; +} + +void plr_posx_increment(t_game *game) +{ + double pos_x; + double pos_y; + + pos_x = game->plr_exact_x + 5; + pos_y = game->plr_exact_y; + rotate_double(game, &pos_x, &pos_y); + if (plr_out_limits(game, pos_x, pos_y)) + return ; + game->plr_exact_x = pos_x; + game->plr_exact_y = pos_y; + (game->plr_x) = (int)pos_x; + (game->plr_y) = (int)pos_y; +} + +void plr_posy_increment(t_game *game) +{ + double pos_x; + double pos_y; + + pos_x = game->plr_exact_x; + pos_y = game->plr_exact_y + 5; + rotate_double(game, &pos_x, &pos_y); + if (plr_out_limits(game, pos_x, pos_y)) + return ; + game->plr_exact_x = pos_x; + game->plr_exact_y = pos_y; + (game->plr_x) = (int)pos_x; + (game->plr_y) = (int)pos_y; +} diff --git a/srcs/player/player_rotates.c b/srcs/player/player_rotates.c new file mode 100644 index 0000000..d4d05a7 --- /dev/null +++ b/srcs/player/player_rotates.c @@ -0,0 +1,35 @@ +#include "cube3d.h" + +void plr_turn_left(t_game *game) +{ + double radi; + double radj; + + if (game->rot == -180) + (game->rot) *= -1; + (game->rot)--; + // calculate trigo for rotations + radi = game->rot * M_PI / 180; + radj = radi + (M_PI / 2); + game->cosi = cos(radi); + game->sini = sin(radi); + game->cosj = cos(radj); + game->sinj = sin(radj); +} + +void plr_turn_right(t_game *game) +{ + double radi; + double radj; + + if (game->rot == 180) + (game->rot) *= -1; + (game->rot)++; + // calculate trigo for rotations + radi = game->rot * M_PI / 180; + radj = radi + (M_PI / 2); + game->cosi = cos(radi); + game->sini = sin(radi); + game->cosj = cos(radj); + game->sinj = sin(radj); +}