From 325735ee4954068635b21c8479ce829baa0235f9 Mon Sep 17 00:00:00 2001 From: yesimzaim Date: Mon, 5 Feb 2024 11:26:50 +0100 Subject: [PATCH] working --- include/meta.h | 3 +-- src/game/sprite.c | 17 ++++++++--------- src/game/sprite_utils.c | 6 ++++-- src/parser/check_elements.c | 2 +- src/parser/lexer.c | 10 ++++++---- test_maps/simple.cub | 6 ------ test_maps/simple1.cub | 1 + test_maps/valid.cub | 13 +++++++------ test_maps/valid_tex.cub | 1 - 9 files changed, 28 insertions(+), 31 deletions(-) diff --git a/include/meta.h b/include/meta.h index 27a24f6..d817391 100644 --- a/include/meta.h +++ b/include/meta.h @@ -314,8 +314,7 @@ void print_attributes(t_attr *attributes); // sprite_utils.c -void sprite_sort(double *sprite_dist, int32_t *sprite_order); - +void sprite_sort(double *sprite_dist, int32_t *sprite_order, uint32_t sprite_count); // lexer.c char *extract_file(char *map_file); diff --git a/src/game/sprite.c b/src/game/sprite.c index ae18277..1d1ba09 100644 --- a/src/game/sprite.c +++ b/src/game/sprite.c @@ -31,19 +31,18 @@ int init_sprites(uint32_t sprite_count, int32_t **sprite_order, double **sprite_ return (EXIT_SUCCESS); } -bool if_black(uint32_t color) +// extracts individual color channels, checks if color channels are 0 (black) +bool is_black(uint32_t color) { - // Extract individual color channels uint8_t r = (color >> 24) & 0xFF; uint8_t g = (color >> 16) & 0xFF; uint8_t b = (color >> 8) & 0xFF; - // Check if all color channels are 0 (black) - if (r == 0 && g == 0 && b == 0) { - return true; // Black - } else { - return false; // Not black + if (r == 0 && g == 0 && b == 0) + { + return true; } + return false; } void sprite_calculate(t_player *p) @@ -54,7 +53,7 @@ void sprite_calculate(t_player *p) p->sprite_order[i] = i; p->sprite_dist[i] = (7.2); - sprite_sort(p->sprite_dist, p->sprite_order); + sprite_sort(p->sprite_dist, p->sprite_order, p->meta->attributes.sprite_count); i = 0; while (i < p->meta->attributes.sprite_count) @@ -107,7 +106,7 @@ void sprite_calculate(t_player *p) int d = (y) * 256 - p->meta->image->height * 128 + sprite_height * 128; //256 and 128 factors to avoid floats int tex_y = ((d * p->meta->attributes.sprites[i].tex.tex->height) / sprite_height) / 256; uint32_t color = pixel_picker(p->meta->attributes.sprites[i].tex.tex, tex_x, tex_y); - if (!if_black(color)) + if (!is_black(color)) { mlx_put_pixel(p->meta->image, stripe, y, color); } diff --git a/src/game/sprite_utils.c b/src/game/sprite_utils.c index 1713ea0..98c06f8 100644 --- a/src/game/sprite_utils.c +++ b/src/game/sprite_utils.c @@ -41,6 +41,7 @@ uint32_t partition(double *sprite_dist, int32_t *sprite_order, int32_t low, int3 while (i < j) { // TODO: find a better way to compare doubles + printf("i: %d | j: %d\n", i, j); while (sprite_dist[i] <= pivot && i <= high - 1) i++; while (sprite_dist[j] > pivot && j >= low + 1) @@ -58,6 +59,7 @@ uint32_t partition(double *sprite_dist, int32_t *sprite_order, int32_t low, int3 void quick_sort(double *sprite_dist, int32_t *sprite_order, int32_t low, int32_t high) { + printf("low %d, high %d\n", low, high); if (low < high) { int32_t pivot = partition(sprite_dist, sprite_order, low, high); @@ -66,7 +68,7 @@ void quick_sort(double *sprite_dist, int32_t *sprite_order, int32_t low, int32_t } } -void sprite_sort(double *sprite_dist, int32_t *sprite_order) +void sprite_sort(double *sprite_dist, int32_t *sprite_order, uint32_t sprite_count) { - quick_sort(sprite_dist, sprite_order, 0, SPRITE_COUNT - 1); + quick_sort(sprite_dist, sprite_order, 0, sprite_count - 1); } diff --git a/src/parser/check_elements.c b/src/parser/check_elements.c index 9621a0a..faa9b67 100644 --- a/src/parser/check_elements.c +++ b/src/parser/check_elements.c @@ -21,7 +21,7 @@ bool is_valid_element(char *file) i = 0; while (i < 6) { - if (ft_strncmp(file, el[i], ft_strlen(el[i]) + 1) == 0) + if (!ft_strncmp(file, el[i], ft_strlen(el[i]) + 1)) { return (true); } diff --git a/src/parser/lexer.c b/src/parser/lexer.c index 998e1d3..06b186a 100644 --- a/src/parser/lexer.c +++ b/src/parser/lexer.c @@ -35,7 +35,7 @@ char *get_key(char *file) char *key; i = 0; - while (file[i] != ' ' && file[i] != '\n') + while ((file[i] != ' ' && file[i] != '\t') && file[i] != '\n') i++; if (i) { @@ -55,9 +55,9 @@ char *get_val(char *file) i = 0; j = 0; - while (file[j] != ' ' && file[j] != '\n') + while ((file[j] != ' ' && file[j] != '\t') && file[j] != '\n') j++; - while (file[j] == ' ' && file[j] != '\n') + while ((file[j] == ' ' || file[j] == '\t') && file[j] != '\n') j++; i = j; while (file[j] && file[j] != '\n') @@ -118,7 +118,8 @@ bool is_duplicate_flag(t_flag *elements, char *key) // if key is not a duplicate and is mandatory bool is_valid_key(t_flag *elements, t_flag *new_node, int *mandatory) { - + printf("flag: %s\n", new_node->flag); + printf("content: %s\n", new_node->content); if (is_duplicate_flag(elements, new_node->flag) && is_valid_element(new_node->flag)) return (false); if (!is_duplicate_flag(elements, new_node->flag) && is_valid_element(new_node->flag)) @@ -135,6 +136,7 @@ int lex(char *file, t_map *map, t_flag **elements) int skip; int mandatory; + int i = 0; mandatory = 0; while (*file) { diff --git a/test_maps/simple.cub b/test_maps/simple.cub index 6f442bf..2b26ae4 100644 --- a/test_maps/simple.cub +++ b/test_maps/simple.cub @@ -9,12 +9,6 @@ wef wefwefwefwefwef wefew F 255,0,0 C 255,0,0 - -SP texture_examples 9.0 9.0 - -SP texture_examples 9.0 9.0 -SP texture_examples 9.0 9.0 - 11111 10001 10E01 diff --git a/test_maps/simple1.cub b/test_maps/simple1.cub index 8b2a5bb..a14c255 100644 --- a/test_maps/simple1.cub +++ b/test_maps/simple1.cub @@ -4,6 +4,7 @@ EA path_to_east SO path_to_south F 123,234,213 C 12,34,3 + 111111 110001 10N001 diff --git a/test_maps/valid.cub b/test_maps/valid.cub index fbe8060..c42acb1 100644 --- a/test_maps/valid.cub +++ b/test_maps/valid.cub @@ -1,11 +1,12 @@ -NO texture_examples/redbrick.png -WE texture_examples/purplestone.png -EA texture_examples/purplestone.png -SO texture_examples/redbrick.png -F texture_examples/redbrick.png -C texture_examples/redbrick.png +NO texture_examples/redbrick.png +WE texture_examples/purplestone.png +EA texture_examples/purplestone.png +SO texture_examples/redbrick.png +F texture_examples/redbrick.png +C texture_examples/redbrick.png SP texture_examples/barrel.png 2.2 2.2 + 11111111111111 1000010001 1000010001 diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub index 5b82801..d7c6b7d 100644 --- a/test_maps/valid_tex.cub +++ b/test_maps/valid_tex.cub @@ -5,7 +5,6 @@ SO texture_examples/purplestone2.png F texture_examples/floor.png C texture_examples/ceiling_square.png - 11111111111111 1000010001 1000N10001