Skip to content

Commit

Permalink
sprites kinda working
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Feb 5, 2024
1 parent 325735e commit 304203d
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 61 deletions.
2 changes: 1 addition & 1 deletion include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ int lexer(t_meta *meta, char *map_file);
// lexer_utils.c

void print_lexer_map(t_map *map);
void print_lexer_elements(t_flag *elements);
void print_lexer_elements(t_flag *elements);

// map_lexer.c

Expand Down
5 changes: 4 additions & 1 deletion include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
# define INVALID_ELEMENT "There are invalid elements in the input file\n"
# define SP_CONTENT_ERR "Please format the sprite element content as follows, SP path/to/texture XX.XX XX.XX\n"
# define DOUBLE_ERR "Please check that your doubles are formatted correctly\n"
# define SP_COORD_ERR "Sprite coordinates are out of bounds\n"

# include "meta.h"

typedef enum e_err {
Expand All @@ -53,7 +55,8 @@ COLOR_CODE_WRONG,
MLX_ERROR,
INV_ELE,
SP_CONTENT,
SP_DOUBLE_ERR
SP_DOUBLE_ERR,
SP_COORD
} t_err;


Expand Down
26 changes: 14 additions & 12 deletions src/cub3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,29 @@ int init_mlx_images(t_meta *meta)
}

return (EXIT_SUCCESS);
}
}

int init_input(t_meta *meta, char *av)
{
if (lexer(meta, av))
return (EXIT_FAILURE);
if (parser(meta))
return(meta_free(meta), EXIT_FAILURE);
if (set_textures(&meta->attributes))
return (meta_free(meta), EXIT_FAILURE);
return (EXIT_SUCCESS);
}

int cub3d(int argc, char **argv)
{
t_meta meta;

// atexit(&leaks);
if (argc != 2)
return (pr_err(ARG_ERR));
ft_bzero(&meta, sizeof(t_meta));

if (lexer(&meta, argv[1]))
return (EXIT_FAILURE);
// print_lexer_map(&meta.map);
// print_lexer_elements(meta.elements);
if (parser(&meta))
return(meta_free(&meta), EXIT_FAILURE);
// print_attributes(&meta.attributes);
// print_sprites_array(meta.attributes.sprites, meta.attributes.sprite_count);
if (set_textures(&meta.attributes))
if (init_input(&meta, argv[1]))
return (EXIT_FAILURE);

init_mlx_images(&meta);
// TODO Error check.
game_init(&meta);
Expand Down
10 changes: 8 additions & 2 deletions src/game/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// Stub
int init_sprites(uint32_t sprite_count, int32_t **sprite_order, double **sprite_dist)
{
printf("sprite arrays initiated!\n");
*sprite_order = malloc(sizeof(int32_t) * sprite_count);
if (!*sprite_order)
return (pr_err(MALL_ERR), EXIT_FAILURE);
Expand Down Expand Up @@ -50,11 +51,16 @@ void sprite_calculate(t_player *p)
size_t i;

i = 0;
p->sprite_order[i] = i;
p->sprite_dist[i] = (7.2);
while (i < p->meta->attributes.sprite_count)
{
p->sprite_order[i] = i;
p->sprite_dist[i] = (p->position.x - p->meta->attributes.sprites[i].pos.x) * (p->position.x - p->meta->attributes.sprites[i].pos.x) + (p->position.y - p->meta->attributes.sprites[i].pos.y) * (p->position.y - p->meta->attributes.sprites[i].pos.y);
i++;
}

sprite_sort(p->sprite_dist, p->sprite_order, p->meta->attributes.sprite_count);

printf("sprite count: %d\n", p->meta->attributes.sprite_count);
i = 0;
while (i < p->meta->attributes.sprite_count)
{
Expand Down
2 changes: 0 additions & 2 deletions src/game/sprite_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ 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 @@ -59,7 +58,6 @@ 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 Down
4 changes: 2 additions & 2 deletions src/parser/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ 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))
{
(*mandatory)++;
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int set_up_sprites(t_meta *meta)
{
if (meta->attributes.sprite_count)
{
meta->attributes.sprites = malloc(sizeof(t_sprite) * meta->attributes.sprite_count);
meta->attributes.sprites = calloc(meta->attributes.sprite_count, sizeof(t_sprite));
if (!meta->attributes.sprites)
return (pr_err(MALL_ERR), EXIT_FAILURE);
}
Expand Down
31 changes: 29 additions & 2 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,45 @@ int create_rectangle_map_element(t_meta *meta)
return (EXIT_SUCCESS);
}

bool out_of_bounds(t_vec2d pos, uint32_t w, uint32_t h)
{
double width;
double height;

width = (double)w;
height = (double)h;
if (pos.x >= width || pos.y >= height || pos.x <= 0 || pos.y <= 0)
return (true);
return (false);

}

int sprites_coordinates(uint32_t sprite_count, t_sprite *sprites, uint32_t w, uint32_t h)
{
uint32_t i;

i = 0;
while (i < sprite_count)
{
if (out_of_bounds(sprites[i].pos, w, h))
return (EXIT_FAILURE);
i++;
}
return (EXIT_SUCCESS);
}

int parser(t_meta *meta)
{
char *file = NULL;


// things to do on the map element: SHOULD WORK
save_map_dimensions(meta->map.map_element, &meta->map.width, &meta->map.height);
create_rectangle_map_element(meta);
if (check_map(meta, meta->map.map_element))
return (EXIT_FAILURE);
if (parse_elements(meta))
return (EXIT_FAILURE);
if (sprites_coordinates(meta->attributes.sprite_count, meta->attributes.sprites, meta->map.width, meta->map.height))
return (pr_err(SP_COORD), EXIT_FAILURE);
free(meta->map.map_element);
return (EXIT_SUCCESS);
}
3 changes: 2 additions & 1 deletion src/utils/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ int pr_err(t_err type)
INVALID_ELEMENT,
SP_CONTENT_ERR,
DOUBLE_ERR,
SP_COORD_ERR
};

write(2, "Error\n", 6);
if (type >= 0 && type < 19)
if (type >= 0 && type < 20)
return (write(2, msg[type], ft_strlen(msg[type])), 1);
return (0);
}
53 changes: 17 additions & 36 deletions src/utils/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,23 @@
#include "meta.h"

// Danger with freeing everything at the end? If allocation of one fails, you should free everything in the same function, dont wait until the end

void f_tex(t_tex *attr)
{
if (attr->tex_path)
free(attr->tex_path);
if (attr->tex)
mlx_delete_texture(attr->tex);
}

static void free_t_tex(t_attr *attributes)
{
if (attributes->n.tex_path)
{
free(attributes->n.tex_path);
mlx_delete_texture(attributes->n.tex);
}
if (attributes->s.tex_path)
{
free(attributes->s.tex_path);
mlx_delete_texture(attributes->s.tex);
}
if (attributes->e.tex_path)
{
free(attributes->e.tex_path);
mlx_delete_texture(attributes->e.tex);
}
if (attributes->w.tex_path)
{
free(attributes->w.tex_path);
mlx_delete_texture(attributes->w.tex);
}
if (attributes->f.tex_path)
{
free(attributes->f.tex_path);
mlx_delete_texture(attributes->f.tex);
}
if (attributes->c.tex_path)
{
free(attributes->c.tex_path);
mlx_delete_texture(attributes->c.tex);
}
f_tex(&attributes->n);
f_tex(&attributes->s);
f_tex(&attributes->e);
f_tex(&attributes->w);
f_tex(&attributes->f);
f_tex(&attributes->c);
}

void free_t_flag_list(t_flag **list)
Expand Down Expand Up @@ -72,19 +57,15 @@ void free_t_sprites(t_sprite **sprites, uint32_t sprite_count)
arr = *sprites;
while (i < sprite_count)
{
if (arr[i].tex.tex_path)
{
free(arr[i].tex.tex_path);
mlx_delete_texture(arr[i].tex.tex);
}
f_tex(&arr[i].tex);
i++;
}
free(*sprites);
}

void meta_free(t_meta *meta)
{
free_t_tex(&(meta->attributes));
free_t_tex(&meta->attributes);
free(meta->map.level);
if (meta->attributes.sprites)
free_t_sprites(&meta->attributes.sprites, meta->attributes.sprite_count);
Expand Down
2 changes: 1 addition & 1 deletion test_maps/simple.cub
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ NO texture_examples/redbrick.png
WE texture_examples/purplestone.png
EA texture_examples/purplestone.png
SO texture_examples/redbrick.png
fweoiuf

fweoiuf
wef wefwefwefwefwef wefew

F 255,0,0
Expand Down
2 changes: 2 additions & 0 deletions test_maps/valid.cub
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ F texture_examples/redbrick.png
C texture_examples/redbrick.png

SP texture_examples/barrel.png 2.2 2.2
SP texture_examples/barrel.png 3.3 2.2
SP texture_examples/barrel.png 4.4 2.2

11111111111111
1000010001
Expand Down

0 comments on commit 304203d

Please sign in to comment.