diff --git a/src/execution/execution.c b/src/execution/execution.c index c8494a9..15d0267 100755 --- a/src/execution/execution.c +++ b/src/execution/execution.c @@ -17,11 +17,11 @@ void render_frame(void *mlx_ptr) t_mlx *mlx; mlx = mlx_ptr; - mlx_delete_image(mlx->mlx_ptr, mlx->img); // Clear previous - mlx->img = mlx_new_image(mlx->mlx_ptr, S_W, S_H); // Create new image - cub_hook(mlx, 0, 0); // Update player movement - cast_rays(mlx); // Raycast - mlx_image_to_window(mlx->mlx_ptr, mlx->img, 0, 0); // And draw it to screen! + mlx_delete_image(mlx->mlx_ptr, mlx->img); + mlx->img = mlx_new_image(mlx->mlx_ptr, S_W, S_H); + cub_hook(mlx, 0, 0); + cast_rays(mlx); + mlx_image_to_window(mlx->mlx_ptr, mlx->img, 0, 0); } int check_load_ture(t_texture_list *list) @@ -33,15 +33,15 @@ int check_load_ture(t_texture_list *list) while (tmp) { if (tmp->name && ( - !ft_strncmp(tmp->name, "NO", 2) // Check North texture - || !ft_strncmp(tmp->name, "SO", 2) // south texture - || !ft_strncmp(tmp->name, "WE", 2) // west texture - || !ft_strncmp(tmp->name, "EA", 2))) // east texture + !ft_strncmp(tmp->name, "NO", 2) + || !ft_strncmp(tmp->name, "SO", 2) + || !ft_strncmp(tmp->name, "WE", 2) + || !ft_strncmp(tmp->name, "EA", 2))) { - texture = mlx_load_png(tmp->value); // Try to load PNG + texture = mlx_load_png(tmp->value); if (!texture) return (0); - mlx_delete_texture(texture); // Clean up tmp texture + mlx_delete_texture(texture); } tmp = tmp->next; } @@ -55,13 +55,12 @@ int load_texture(t_tex *tex, t_texture_list *l_ture) if (!l_ture) return (0); tmp = l_ture; - if (!check_load_ture(l_ture)) // Validate all textures + if (!check_load_ture(l_ture)) return (0); while (tmp) { if (tmp->name && tmp->value) { - // Match texture name and load the PNG if (!ft_strncmp(tmp->name, "NO", 2)) tex->no = mlx_load_png(tmp->value); else if (!ft_strncmp(tmp->name, "SO", 2)) @@ -76,54 +75,42 @@ int load_texture(t_tex *tex, t_texture_list *l_ture) return (1); } -void get_angle(t_mlx *mlx) +void get_angle(t_mlx *mlx) { - char c; - - c = mlx->dt->sq_map[mlx->dt->p_y][mlx->dt->p_x]; - - if (c == 'N') // Norte - mira hacia ARRIBA (Y negativo) - mlx->ply->angle = 3 * M_PI / 2; // 270° (no 90°) - - if (c == 'S') // Sur - mira hacia ABAJO (Y positivo) - mlx->ply->angle = M_PI / 2; // 90° (no 270°) - - if (c == 'E') // Este - mira hacia DERECHA (X positivo) - mlx->ply->angle = 0; - - if (c == 'W') // Oeste - mira hacia IZQUIERDA (X negativo) - mlx->ply->angle = M_PI; // 180° - - mlx->ply->plyr_x = (mlx->dt->p_x * WALL_SIZE) + WALL_SIZE / 2; - mlx->ply->plyr_y = (mlx->dt->p_y * WALL_SIZE) + WALL_SIZE / 2; - mlx->ply->fov_rd = (FOV * M_PI / 180); + char c; + + c = mlx->dt->sq_map[mlx->dt->p_y][mlx->dt->p_x]; + if (c == 'N') + mlx->ply->angle = 3 * M_PI / 2; + if (c == 'S') + mlx->ply->angle = M_PI / 2; + if (c == 'E') + mlx->ply->angle = 0; + if (c == 'W') + mlx->ply->angle = M_PI; + mlx->ply->plyr_x = (mlx->dt->p_x * WALL_SIZE) + WALL_SIZE / 2; + mlx->ply->plyr_y = (mlx->dt->p_y * WALL_SIZE) + WALL_SIZE / 2; + mlx->ply->fov_rd = (FOV * M_PI / 180); } int execution(t_data *dt) { t_mlx mlx; - // Validate screen dimensions and fov if (S_H > 1440 || S_W > 2560 || FOV >= 180 || FOV <= 0) return (freelist(&dt->t_list), free_map(dt), 0); - // Allocate game objects mlx.ply = (t_player *)ft_calloc(sizeof(t_player), 1); mlx.ray = (t_ray *)ft_calloc(sizeof(t_ray), 1); mlx.tex = (t_tex *)ft_calloc(sizeof(t_tex), 1); mlx.dt = dt; - // Initialize graphics window mlx.mlx_ptr = mlx_init(S_W, S_H, "cub3D", false); if (!mlx.mlx_ptr) return (ft_exit(&mlx), 0); - // Load wall textures if (!load_texture(mlx.tex, dt->t_list)) return (ft_exit(&mlx), 0); - // Set player starting position and direction get_angle(&mlx); - // Register input handlers mlx_key_hook(mlx.mlx_ptr, &key_press, &mlx); mlx_loop_hook(mlx.mlx_ptr, &render_frame, &mlx); - // Start game loop mlx_loop(mlx.mlx_ptr); ft_exit(&mlx); return (0); diff --git a/src/execution/movement.c b/src/execution/movement.c index f551c7d..793ab58 100755 --- a/src/execution/movement.c +++ b/src/execution/movement.c @@ -14,17 +14,17 @@ void rotate_player(t_mlx *mlx, int i) { - if (i == 1) // Rotate right + if (i == 1) { mlx->ply->angle += ROTATION_SPEED; - if (mlx->ply->angle > 2 * M_PI) // If its more than 360º - mlx->ply->angle -= 2 * M_PI; // Come back to 0 + if (mlx->ply->angle > 2 * M_PI) + mlx->ply->angle -= 2 * M_PI; } - else // Rotate left + else { mlx->ply->angle -= ROTATION_SPEED; - if (mlx->ply->angle < 0) // If its less than 0º - mlx->ply->angle += 2 * M_PI; // Come back to 360º + if (mlx->ply->angle < 0) + mlx->ply->angle += 2 * M_PI; } } @@ -39,10 +39,8 @@ void move_player(t_mlx *mlx, double move_x, double move_y) new_y = mlx->ply->plyr_y + move_y; grid_x = (int)((new_x + copysign(COLLISION_MARGIN, move_x)) / WALL_SIZE); grid_y = (int)(mlx->ply->plyr_y / WALL_SIZE); - //If there's no wall, update X position if (mlx->dt->sq_map[grid_y][grid_x] != '1') mlx->ply->plyr_x = new_x; - // Then the same with check Y axe grid_x = (int)(mlx->ply->plyr_x / WALL_SIZE); grid_y = (int)((new_y + copysign(COLLISION_MARGIN, move_y)) / WALL_SIZE); if (mlx->dt->sq_map[grid_y][grid_x] != '1') @@ -52,25 +50,25 @@ void move_player(t_mlx *mlx, double move_x, double move_y) void cub_hook(t_mlx *mlx, double move_x, double move_y) { if (mlx->ply->rot == 1) - rotate_player(mlx, 1); // Right rotation + rotate_player(mlx, 1); if (mlx->ply->rot == -1) - rotate_player(mlx, 0); // Left rotation - if (mlx->ply->l_r == 1) // D Key + rotate_player(mlx, 0); + if (mlx->ply->l_r == 1) { move_x = -sin(mlx->ply->angle) * PLAYER_SPEED; move_y = cos(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->l_r == -1) // A Key + if (mlx->ply->l_r == -1) { move_x = sin(mlx->ply->angle) * PLAYER_SPEED; move_y = -cos(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->u_d == 1) // W key + if (mlx->ply->u_d == 1) { move_x = cos(mlx->ply->angle) * PLAYER_SPEED; move_y = sin(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->u_d == -1) // S key + if (mlx->ply->u_d == -1) { move_x = -cos(mlx->ply->angle) * PLAYER_SPEED; move_y = -sin(mlx->ply->angle) * PLAYER_SPEED; @@ -81,17 +79,17 @@ void cub_hook(t_mlx *mlx, double move_x, double move_y) void ft_reset_move(mlx_key_data_t keydata, t_mlx *mlx) { if (keydata.key == MLX_KEY_D && (keydata.action == MLX_RELEASE)) - mlx->ply->l_r = 0; // Stop strafe right + mlx->ply->l_r = 0; else if (keydata.key == MLX_KEY_A && (keydata.action == MLX_RELEASE)) - mlx->ply->l_r = 0; // Stop strafe left + mlx->ply->l_r = 0; else if (keydata.key == MLX_KEY_S && (keydata.action == MLX_RELEASE)) - mlx->ply->u_d = 0; // Stop backwards + mlx->ply->u_d = 0; else if (keydata.key == MLX_KEY_W && (keydata.action == MLX_RELEASE)) - mlx->ply->u_d = 0; // Stop forward + mlx->ply->u_d = 0; else if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_RELEASE) - mlx->ply->rot = 0; // Stop rotate left + mlx->ply->rot = 0; else if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_RELEASE) - mlx->ply->rot = 0; // Stop rotate right + mlx->ply->rot = 0; } void key_press(mlx_key_data_t keydata, void *ml) @@ -99,23 +97,22 @@ void key_press(mlx_key_data_t keydata, void *ml) t_mlx *mlx; mlx = ml; - //Exit game on ESC key if (keydata.key == MLX_KEY_ESCAPE && (keydata.action == MLX_PRESS || keydata.action == MLX_REPEAT)) { ft_exit(mlx); } else if (keydata.key == MLX_KEY_A && (keydata.action == MLX_PRESS)) - mlx->ply->l_r = -1; // Set strafe left + mlx->ply->l_r = -1; else if (keydata.key == MLX_KEY_D && (keydata.action == MLX_PRESS)) - mlx->ply->l_r = 1; // Set strafe right + mlx->ply->l_r = 1; else if (keydata.key == MLX_KEY_S && (keydata.action == MLX_PRESS)) - mlx->ply->u_d = -1; // Set backwards + mlx->ply->u_d = -1; else if (keydata.key == MLX_KEY_W && keydata.action == MLX_PRESS) - mlx->ply->u_d = 1; // Set forwards + mlx->ply->u_d = 1; else if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_PRESS) - mlx->ply->rot = -1; // Set rotating left + mlx->ply->rot = -1; else if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS) - mlx->ply->rot = 1; // Set rotating right + mlx->ply->rot = 1; ft_reset_move(keydata, mlx); } diff --git a/src/execution/render.c b/src/execution/render.c index cb87211..33c0f8c 100755 --- a/src/execution/render.c +++ b/src/execution/render.c @@ -17,13 +17,11 @@ void draw_floor_ceiling(t_mlx *mlx, int ray, int t_pix, int b_pix) int i; int c; - // Draw floor i = b_pix; c = get_rgba(ft_atoi(mlx->dt->ff[0]), ft_atoi(mlx->dt->ff[1]), ft_atoi(mlx->dt->ff[2]), 255); while (i < S_H) cub3d_put_pixel(mlx, ray, i++, c); - // Draw ceiling c = get_rgba(ft_atoi(mlx->dt->cc[0]), ft_atoi(mlx->dt->cc[1]), ft_atoi(mlx->dt->cc[2]), 255); i = 0; @@ -80,23 +78,18 @@ void draw_wall(t_mlx *mlx, int t_pix, int b_pix, double wall_h) uint32_t *arr; double factor; - // Get correct wall texture texture = get_texture(mlx, mlx->ray->flag); arr = (uint32_t *)texture->pixels; - // Calculate scaling factor = (double)texture->height / wall_h; x_o = get_x_o(texture, mlx); y_o = (t_pix - (S_H / 2) + (wall_h / 2)) * factor; if (y_o < 0) y_o = 0; - // Draw vertical column of wall while (t_pix < b_pix) { cub3d_put_pixel(mlx, mlx->ray->index, t_pix, reverse_bytes(arr[(int)y_o * texture->width + (int)x_o])); - // Move to next texture row y_o += factor; - // Move to next screen row t_pix++; } } diff --git a/src/map/read_map.c b/src/map/read_map.c index 89f285b..3343600 100755 --- a/src/map/read_map.c +++ b/src/map/read_map.c @@ -14,10 +14,8 @@ int is_surrounded(char *line) { - // Skip spaces while (ft_isspace(*line)) line++; - // Check first and last non-space character are '1' return (*line == '1' && line [ft_strlen(line) - 1] == '1'); } @@ -28,12 +26,11 @@ int is_validmap(char *line, int *flag) i = -1; while (line[++i]) { - // Check for invalid characters if ((line[i] != '1' && line[i] != '0' && line[i] != ' ' && line[i] != '\n' && line[i] != 'W' && line[i] != 'E' && line[i] != 'N' && line[i] != 'S')) - return (0); // Invalid character found + return (0); else if (line[i] == 'W' || line[i] == 'E' || line[i] == 'N' || line[i] == 'S') (*flag)++; @@ -46,20 +43,16 @@ char *getmap(t_data *map) char *tmp; map->map = ft_strdup(""); - // Lets read all the map until the end of file while (map->line) { - // Map can't have empty line in the middle if (map->line[0] == '\n') return (ft_putstr_fd(ERR_MAP_EMPTY, 2), freetl(map->map, map->line, -1), NULL); - // Apend current line to map->map tmp = ft_strjoin(map->map, map->line); ft_memfree(map->map); ft_memfree(map->line); map->map = ft_strdup(tmp); ft_memfree(tmp); - // Read next line map->line = get_next_line(map->fd); } return (map->map); @@ -67,16 +60,13 @@ char *getmap(t_data *map) int read_map_(t_data *map, int count) { - // Get complete map as single string map->map = getmap(map); if (!map->map) return (0); - // Split by newlines into 2D array map->map2d = ft_split(map->map, '\n'); if (!map->map2d) return (ft_memfree(map->map), 0); ft_memfree(map->map); - // Validate all map components if (!check_tures_space_tab(map->texture2d, count) || !parse_rgb(map->texture2d) || !check_dup(map) || !check_first_last_line(map->map2d) diff --git a/src/map/read_map_utils.c b/src/map/read_map_utils.c index 6aa0d51..75bafba 100755 --- a/src/map/read_map_utils.c +++ b/src/map/read_map_utils.c @@ -17,9 +17,9 @@ int check_tures_space_tab(char **texture2d, int count) int i; i = -1; - if (count != 6) // .cub must have 6 directives + if (count != 6) return (0); - while (++i < count) // Validate each directive format + while (++i < count) if (!is_valid_texture(texture2d[i])) return (ft_putstr_fd("texture error\n", 2), 0); return (1); @@ -34,12 +34,9 @@ int parse_rgb(char **texture2d) while (texture2d[i]) { ptr = texture2d[i]; - // Skip spaces while (ft_isspace(*ptr)) ptr++; - // Check only F and C lines if (ptr[0] == 'F' || ptr[0] == 'C') - // Validate that there is two , and format if (count_comma(ptr) != 2 || !check_pos_cf(ptr)) return (ft_putstr_fd("color error\n", 2), 0); i++; @@ -85,14 +82,16 @@ int surrounded_by_one(char **map) flag = 0; i = -1; - // Check each map line while (map[++i]) - if (!is_surrounded(map[i]) || !is_validmap(map[i], &flag) || flag > 1) + { + if (!is_surrounded(map[i]) + || !is_validmap(map[i], &flag) + || flag > 1) { ft_putstr_fd(ERR_MAP_ONE, 2); return (0); } - // Must have one player spawn + } if (flag == 0) { ft_putstr_fd(ERR_MAP_SPAWN, 2); diff --git a/src/parsing/lst_textures.c b/src/parsing/lst_textures.c index 4dd7503..57737a0 100755 --- a/src/parsing/lst_textures.c +++ b/src/parsing/lst_textures.c @@ -31,16 +31,12 @@ t_texture_list *new_texture(char *line) if ((!ft_strncmp(line, "NO", 2) || !ft_strncmp(line, "SO", 2)) || !ft_strncmp(line, "WE", 2) || !ft_strncmp(line, "EA", 2)) { - // Extract first 2 chars as identifier list->name = ft_substr(line, 0, 2); - // Extract everything after identifier list->value = ft_substr(line, get_index(line, 2), ft_strlen(line)); } else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1))) { - // Extract first char as identifier list->name = ft_substr(line, 0, 1); - // Extract RGB values after identifier list->value = ft_substr(line, get_index(line, 1), ft_strlen(line)); } list->next = NULL; @@ -51,7 +47,6 @@ void lst_back_ture(t_texture_list **l_ture, t_texture_list *new) { t_texture_list *tmp; - // If list is empty, new nodes becomes head if (!*l_ture) { (*l_ture) = new; @@ -69,14 +64,11 @@ int lst_ture(t_data *m, t_texture_list **l_ture) t_texture_list *tmp; i = 0; - // Process all 6 texture directives while (m->texture2d[i]) { - // Create node for each texture line tmp = new_texture(m->texture2d[i++]); if (!tmp) return (0); - // Add to end of list lst_back_ture(l_ture, tmp); } return (1); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 0ca7bdb..57b81dc 100755 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -44,11 +44,9 @@ void get_x_y_player(t_data *data) j = 0; while (data->sq_map[i][j]) { - // Check for player spawn character if (data->sq_map[i][j] == 'N' || data->sq_map[i][j] == 'S' || data->sq_map[i][j] == 'W' || data->sq_map[i][j] == 'E') { - // Store grid coordinates data->p_x = j; data->p_y = i; return ; @@ -63,9 +61,7 @@ int check_extension_map(char *file) { char *str; - // Find last '.' in file name str = ft_strrchr(file, '.'); - // Check if extension exists and equals to '.cub' return (str && !ft_strcmp(str, ".cub")); } @@ -73,25 +69,18 @@ int parsing(int ac, char **av, t_data *data) { int count; - // Validate arguments if (ac != 2 || !check_extension_map(av[1])) return (ft_putstr_fd(ERR_INV_COP, 2), 0); count = 0; - // Read and parse .cub file if (!read_map(av[1], data, &count)) return (0); - // Create uniform square map and validate walls if (!valid_map(data)) return (0); - // Initialize texture linked list data->t_list = NULL; - // Convert texture array to linked list if (!lst_ture(data, &data->t_list)) return (free_map(data), freelist(&data->t_list), 0); - // Parse and validate RGB colors if (!color_ture(data, data->t_list)) return (free_map(data), freelist(&data->t_list), 0); - // Locate player starting position get_x_y_player(data); return (1); } diff --git a/src/parsing/textures.c b/src/parsing/textures.c index afe6aa3..4397db3 100755 --- a/src/parsing/textures.c +++ b/src/parsing/textures.c @@ -17,7 +17,6 @@ int check_color_values(char **rgb) int i; i = -1; - // Check each RGB component while (rgb[++i]) if (ft_atoi(rgb[i]) > 255 || ft_atoi(rgb[i]) < 0) return (free2d(rgb), 0); @@ -26,10 +25,8 @@ int check_color_values(char **rgb) void ft_process_rgb_color(t_texture_list *tmp, t_data *m) { - // Floor color if (!ft_strncmp(tmp->name, "F", 2)) m->ff = ft_split(tmp->value, ','); - // Ceiling color else if (!ft_strncmp(tmp->name, "C", 2)) m->cc = ft_split(tmp->value, ','); return ; @@ -40,20 +37,15 @@ int color_ture(t_data *m, t_texture_list *l_ture) t_texture_list *tmp; char **colors; - // Initialize color arrays m->cc = NULL; m->ff = NULL; tmp = l_ture; - // Process each texure directive while (tmp) { - // Check for color directives (Floor and ceiling) if (tmp->name && (!ft_strncmp(tmp->name, "F", 1) || !ft_strncmp(tmp->name, "C", 1))) { - // Split RGB values by coma colors = ft_split(tmp->value, ','); - // Validate RGB range if (!check_color_values(colors)) return (ft_putstr_fd(ERR_MAP_RGB, 2), 0); ft_process_rgb_color(tmp, m); @@ -67,16 +59,13 @@ int check_color_textures(char *line) { while (ft_isspace(*line)) line++; - // Wall texture directive if ((!ft_strncmp(line, "EA", 2) || !ft_strncmp(line, "NO", 2) || !ft_strncmp(line, "SO", 2) || !ft_strncmp(line, "WE", 2)) - && ft_isspace(line[2])) // 2 chars + 1 space + && ft_isspace(line[2])) return (1); - // Color directive else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1)) - && ft_isspace(line[1])) // 1 char + 1 space + && ft_isspace(line[1])) return (1); - // Empty line (allowed between directives) else if (*line == '\n' || *line == '\0') return (0); else