Skip to content

Commit

Permalink
Merge branch 'parser_with_sprites' into doors
Browse files Browse the repository at this point in the history
  • Loading branch information
ys-zm committed Feb 9, 2024
2 parents 2a948cf + f671f3a commit 22ae619
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 39 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"files.associations": {
"cstdlib": "c",
"stdio.h": "c",
"meta.h": "c"
"meta.h": "c",
"array": "c",
"string_view": "c",
"initializer_list": "c",
"ranges": "c",
"span": "c",
"utility": "c"
}
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub
# CFLAGS += -Wall -Wextra
# CFLAGS += -g -fsanitize=address
# # CFLAGS += -g
# CFLAGS += -Ofast -flto -march=native
CFLAGS += -Ofast -flto -march=native

LIBFT := libft/build/libft.a
LIBMLX := MLX42/build/libmlx42.a
Expand Down
2 changes: 1 addition & 1 deletion include/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void sprite_calculate(t_player *p);

// test_utils.c REMOVE LATER

void print_double_array(char *msg, double *arr, uint32_t size);
void print_double_array(char *msg, double *arr, uint32_t size, t_sprite *sp, int32_t *order);
void print_ints_array(char *msg, int32_t *arr, uint32_t size);
void print_sprites_array(t_sprite *arr, uint32_t size);
void print_attributes(t_attr *attributes);
Expand Down
12 changes: 6 additions & 6 deletions src/game/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <stdlib.h>
#include "parser.h"


// Stub
int init_sprites(uint32_t sprite_count, int32_t **sprite_order, double **sprite_dist)
{
Expand Down Expand Up @@ -53,7 +52,8 @@ void sprite_calculate(t_player *p)
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);
t_vec2d sp_pos = p->meta->attributes.sprites[i].pos;
p->sprite_dist[i] = ((p->position.x - sp_pos.x) * (p->position.x - sp_pos.x)) + ((p->position.y - sp_pos.y) * (p->position.y - sp_pos.y));
i++;
}

Expand All @@ -65,8 +65,8 @@ void sprite_calculate(t_player *p)
const t_sprite *sp = p->meta->attributes.sprites;
const t_vec2d s_pos = (t_vec2d){sp[p->sprite_order[i]].pos.x - p->position.x, sp[p->sprite_order[i]].pos.y - p->position.y};

const double inv_det = 1.0 / (p->cam_plane.x * p->direction.y - p->direction.x * p->cam_plane.y);

double inv_det = 1.0 / (p->cam_plane.x * p->direction.y) - (p->cam_plane.y * p->direction.x);
const t_vec2d transform = {inv_det * (p->direction.y * s_pos.x - p->direction.x * s_pos.y),
inv_det * (-(p->cam_plane.y) * s_pos.x + p->cam_plane.x * s_pos.y)};

Expand Down Expand Up @@ -98,16 +98,16 @@ void sprite_calculate(t_player *p)
while (stripe < draw_end.x)
{
int tex_x;
tex_x = (int)(256 * (stripe - (-sprite_width / 2 + sprite_screen_x)) * p->meta->attributes.sprites[i].tex.tex->width / sprite_width) / 256;
tex_x = (int)(256 * (stripe - (-sprite_width / 2 + sprite_screen_x)) * p->meta->attributes.sprites[p->sprite_order[i]].tex.tex->width / sprite_width) / 256;
if (transform.y > 0 && stripe > 0 && stripe < p->meta->image->width && transform.y < p->z_buffer[stripe])
{
int y;
y = draw_start.y;
while (y < draw_end.y)
{
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);
int tex_y = ((d * p->meta->attributes.sprites[p->sprite_order[i]].tex.tex->height) / sprite_height) / 256;
uint32_t color = pixel_picker(p->meta->attributes.sprites[p->sprite_order[i]].tex.tex, tex_x, tex_y);
if (!is_black(color))
{
mlx_put_pixel(p->meta->image, stripe, y, color);
Expand Down
40 changes: 30 additions & 10 deletions src/game/sprite_utils.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* sprite_utils.c :+: :+: */
/* +:+ */
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* :::::::: */
/* sprite_utils.c :+: :+: */
/* +:+ */
/* By: jboeve <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/25 16:01:20 by jboeve #+# #+# */
/* Updated: 2024/01/29 12:50:12 by yesimzaim ######## odam.nl */
/* Updated: 2024/02/08 17:31:59 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,21 +52,41 @@ uint32_t partition(double *sprite_dist, int32_t *sprite_order, int32_t low, int3
}
}
swap_doubles(&sprite_dist[low], &sprite_dist[j]);
swap_ints(&sprite_order[low], &sprite_order[i]);
swap_ints(&sprite_order[low], &sprite_order[j]);
return (j);
}

void quick_sort(double *sprite_dist, int32_t *sprite_order, int32_t low, int32_t high)
{
if (low < high)
{
int32_t pivot = partition(sprite_dist, sprite_order, low, high);
quick_sort(sprite_dist, sprite_order, low, pivot - 1);
quick_sort(sprite_dist, sprite_order, pivot + 1, high);
int32_t partition_index = partition(sprite_dist, sprite_order, low, high);
quick_sort(sprite_dist, sprite_order, low, partition_index - 1);
quick_sort(sprite_dist, sprite_order, partition_index + 1, high);
}
}

void reverse(double *arr1, int32_t *arr2, uint32_t size)
{
uint32_t i;
double tmp1;
int32_t tmp2;

i = 0;
while (i < size / 2)
{
tmp1 = arr1[i];
arr1[i] = arr1[size - i - 1];
arr1[size - i - 1] = tmp1;
tmp2 = arr2[i];
arr2[i] = arr2[size - i - 1];
arr2[size - i - 1] = tmp2;
i++;
}
}

void sprite_sort(double *sprite_dist, int32_t *sprite_order, uint32_t sprite_count)
{
quick_sort(sprite_dist, sprite_order, 0, sprite_count - 1);
reverse(sprite_dist, sprite_order, sprite_count);
}
26 changes: 17 additions & 9 deletions src/test_utils.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_utils.c :+: :+: */
/* +:+ +:+ +:+ */
/* By: yzaim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 15:25:38 by yzaim #+# #+# */
/* Updated: 2024/01/26 16:10:28 by yzaim ######## odam.nl */
/* :::::::: */
/* test_utils.c :+: :+: */
/* +:+ */
/* By: yzaim <[email protected]> +#+ */
/* +#+ */
/* Created: 2024/01/08 15:25:38 by yzaim #+# #+# */
/* Updated: 2024/02/08 15:08:32 by yzaim ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,16 +77,24 @@ void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h)
}
}

void print_double_array(char *msg, double *arr, uint32_t size)
void print_double_array(char *msg, double *arr, uint32_t size, t_sprite *sp, int32_t *order)
{
printf("%s :", msg);
printf("%s: ", msg);
for (int i = 0; i < size; i++)
{
printf("%lf", arr[i]);
if (i < size - 1)
printf(" | ");
}
printf("\n");
printf("TEXTURES: ");
for (int i = 0; i < size; i++)
{
printf("%s", sp[order[i]].tex.tex_path + 17);
if (i < size - 1)
printf(" | ");
}
printf("\n");
}

void print_ints_array(char *msg, int32_t *arr, uint32_t size)
Expand Down
21 changes: 10 additions & 11 deletions test_maps/valid_tex.cub
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ SO texture_examples/purplestone2.png
F texture_examples/floor.png
C texture_examples/ceiling_square.png

SP texture_examples/barrel.png 3.5 2.5
SP texture_examples/barrel.png 4.5 2.5
SP texture_examples/barrel.png 5.5 2.5
SP texture_examples/hoofd.png 6.5 2.5
SP texture_examples/sexy.png 7.5 2.5
SP texture_examples/pillar.png 4.5 2.5
SP texture_examples/barrel.png 5.5 3.5
SP texture_examples/hoofd.png 2.5 3.5
SP texture_examples/sexy.png 2.5 2.5

11111111111111
1000000001
1000000001
1000000001
1000000001
1111101111
11111111111111
1000000001
1000000001
1000000001
1000000001
100000000111
111000N00111
100000000001
10000000011
Expand Down

0 comments on commit 22ae619

Please sign in to comment.