diff --git a/includes/cub3d.h b/includes/cub3d.h index cbf2dfe..ea8d80e 100755 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: aortigos 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); + 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! } 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) - || !ft_strncmp(tmp->name, "SO", 2) - || !ft_strncmp(tmp->name, "WE", 2) - || !ft_strncmp(tmp->name, "EA", 2))) + !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 { - texture = mlx_load_png(tmp->value); + texture = mlx_load_png(tmp->value); // Try to load PNG if (!texture) return (0); - mlx_delete_texture(texture); + mlx_delete_texture(texture); // Clean up tmp texture } tmp = tmp->next; } @@ -55,12 +55,13 @@ 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)) + if (!check_load_ture(l_ture)) // Validate all textures 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)) @@ -79,38 +80,46 @@ 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 = M_PI / 2; // 90° - no 270° - if (c == 'S') // Sur - mira hacia abajo (Y positivo) - mlx->ply->angle = 3 * M_PI / 2; // 270° - no 90° - if (c == 'E') // Este - mira hacia derecha (X positivo) + c = mlx->dt->sq_map[mlx->dt->p_y][mlx->dt->p_x]; + if (c == 'N') // North - faceing up + mlx->ply->angle = M_PI / 2; + if (c == 'S') // South - facing down + mlx->ply->angle = 3 * M_PI / 2; + if (c == 'E') // East - facing right mlx->ply->angle = 0; - if (c == 'W') // Oeste - mira hacia izquierda (X negativo) - mlx->ply->angle = M_PI; // 180° + if (c == 'W') // West - facing left + mlx->ply->angle = M_PI; + //Center player in starting cell 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); + mlx->ply->fov_rd = (FOV * M_PI / 180); // Fov from degrees to radians } 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 3684228..e1f343d 100755 --- a/src/execution/movement.c +++ b/src/execution/movement.c @@ -6,7 +6,7 @@ /* By: aortigos ply->angle += ROTATION_SPEED; - if (mlx->ply->angle > 2 * M_PI) - mlx->ply->angle -= 2 * M_PI; + if (mlx->ply->angle > 2 * M_PI) // If its more than 360º + mlx->ply->angle -= 2 * M_PI; // Come back to 0 } - else + else // Rotate left { mlx->ply->angle -= ROTATION_SPEED; - if (mlx->ply->angle < 0) - mlx->ply->angle += 2 * M_PI; + if (mlx->ply->angle < 0) // If its less than 0º + mlx->ply->angle += 2 * M_PI; // Come back to 360º } } @@ -39,8 +39,10 @@ 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') @@ -50,25 +52,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); + rotate_player(mlx, 1); // Right rotation if (mlx->ply->rot == -1) - rotate_player(mlx, 0); - if (mlx->ply->l_r == 1) + rotate_player(mlx, 0); // Left rotation + if (mlx->ply->l_r == 1) // D Key { move_x = -sin(mlx->ply->angle) * PLAYER_SPEED; move_y = cos(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->l_r == -1) + if (mlx->ply->l_r == -1) // A Key { move_x = sin(mlx->ply->angle) * PLAYER_SPEED; move_y = -cos(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->u_d == 1) + if (mlx->ply->u_d == 1) // W key { move_x = cos(mlx->ply->angle) * PLAYER_SPEED; move_y = sin(mlx->ply->angle) * PLAYER_SPEED; } - if (mlx->ply->u_d == -1) + if (mlx->ply->u_d == -1) // S key { move_x = -cos(mlx->ply->angle) * PLAYER_SPEED; move_y = -sin(mlx->ply->angle) * PLAYER_SPEED; @@ -79,17 +81,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; + mlx->ply->l_r = 0; // Stop strafe right else if (keydata.key == MLX_KEY_A && (keydata.action == MLX_RELEASE)) - mlx->ply->l_r = 0; + mlx->ply->l_r = 0; // Stop strafe left else if (keydata.key == MLX_KEY_S && (keydata.action == MLX_RELEASE)) - mlx->ply->u_d = 0; + mlx->ply->u_d = 0; // Stop backwards else if (keydata.key == MLX_KEY_W && (keydata.action == MLX_RELEASE)) - mlx->ply->u_d = 0; + mlx->ply->u_d = 0; // Stop forward else if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_RELEASE) - mlx->ply->rot = 0; + mlx->ply->rot = 0; // Stop rotate left else if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_RELEASE) - mlx->ply->rot = 0; + mlx->ply->rot = 0; // Stop rotate right } void key_press(mlx_key_data_t keydata, void *ml) @@ -97,22 +99,23 @@ 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; + mlx->ply->l_r = -1; // Set strafe left else if (keydata.key == MLX_KEY_D && (keydata.action == MLX_PRESS)) - mlx->ply->l_r = 1; + mlx->ply->l_r = 1; // Set strafe right else if (keydata.key == MLX_KEY_S && (keydata.action == MLX_PRESS)) - mlx->ply->u_d = -1; + mlx->ply->u_d = -1; // Set backwards else if (keydata.key == MLX_KEY_W && keydata.action == MLX_PRESS) - mlx->ply->u_d = 1; + mlx->ply->u_d = 1; // Set forwards else if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_PRESS) - mlx->ply->rot = -1; + mlx->ply->rot = -1; // Set rotating left else if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS) - mlx->ply->rot = 1; + mlx->ply->rot = 1; // Set rotating right ft_reset_move(keydata, mlx); } diff --git a/src/execution/render.c b/src/execution/render.c index 39fbfeb..cb87211 100755 --- a/src/execution/render.c +++ b/src/execution/render.c @@ -6,7 +6,7 @@ /* By: aortigos 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; @@ -78,18 +80,23 @@ 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/main.c b/src/main.c index 4b29c82..878af3a 100755 --- a/src/main.c +++ b/src/main.c @@ -6,23 +6,12 @@ /* By: aortigos -// we parse the file .cub -> -// Inicializate player / map / textures -> -// Inicializate mlx -> -// Game loop -> -// -> -> move player -// -> -> shoot raycast -// -> -> draw walls -// -> -> show window -// exit and free memory - int main(int argc, char **argv) { t_data data; diff --git a/src/map/read_map.c b/src/map/read_map.c index e9445c8..89f285b 100755 --- a/src/map/read_map.c +++ b/src/map/read_map.c @@ -6,7 +6,7 @@ /* By: aortigos 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); @@ -60,13 +67,16 @@ 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 42f293a..67aed1f 100755 --- a/src/map/read_map_utils.c +++ b/src/map/read_map_utils.c @@ -6,7 +6,7 @@ /* By: aortigos 1) return (ft_putstr_fd("Invalid map\n", 2), 0); + // Must have one player spawn if (flag == 0) return (ft_putstr_fd("no position map\n", 2), 0); return (1); diff --git a/src/parsing/lst_textures.c b/src/parsing/lst_textures.c index 961241c..4dd7503 100755 --- a/src/parsing/lst_textures.c +++ b/src/parsing/lst_textures.c @@ -6,7 +6,7 @@ /* By: aortigos 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; @@ -47,6 +51,7 @@ 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; @@ -64,11 +69,14 @@ 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 6b9a249..0ca7bdb 100755 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: aortigos 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 ; @@ -61,7 +63,9 @@ 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")); } @@ -69,18 +73,25 @@ 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 cf67293..afe6aa3 100755 --- a/src/parsing/textures.c +++ b/src/parsing/textures.c @@ -6,7 +6,7 @@ /* By: aortigos 255 || ft_atoi(rgb[i]) < 0) return (free2d(rgb), 0); @@ -25,8 +26,10 @@ 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 ; @@ -37,15 +40,20 @@ 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); @@ -59,13 +67,16 @@ 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])) + && ft_isspace(line[2])) // 2 chars + 1 space return (1); + // Color directive else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1)) - && ft_isspace(line[1])) + && ft_isspace(line[1])) // 1 char + 1 space return (1); + // Empty line (allowed between directives) else if (*line == '\n' || *line == '\0') return (0); else