Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Feb 5, 2024
1 parent c4c96d4 commit 325735e
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 31 deletions.
3 changes: 1 addition & 2 deletions include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions src/game/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions src/game/sprite_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion src/parser/check_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 6 additions & 4 deletions src/parser/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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')
Expand Down Expand Up @@ -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))
Expand All @@ -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)
{
Expand Down
6 changes: 0 additions & 6 deletions test_maps/simple.cub
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test_maps/simple1.cub
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ EA path_to_east
SO path_to_south
F 123,234,213
C 12,34,3

111111
110001
10N001
Expand Down
13 changes: 7 additions & 6 deletions test_maps/valid.cub
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion test_maps/valid_tex.cub
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ SO texture_examples/purplestone2.png
F texture_examples/floor.png
C texture_examples/ceiling_square.png


11111111111111
1000010001
1000N10001
Expand Down

0 comments on commit 325735e

Please sign in to comment.