made some calc shorter

This commit is contained in:
Willem Haffmans 2025-05-08 23:19:35 +02:00
parent 03ceeb1da5
commit 93e7932084
2 changed files with 25 additions and 34 deletions

View File

@ -6,7 +6,7 @@
# By: whaffman <whaffman@student.codam.nl> +#+ # # By: whaffman <whaffman@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/05/04 16:46:48 by whaffman ######## odam.nl # # Updated: 2025/05/08 23:15:13 by whaffman ######## odam.nl #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -45,7 +45,7 @@ SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
# Build configurations # Build configurations
BUILD_CONFIGS = release debug asan tsan BUILD_CONFIGS = release debug asan tsan
release_CFLAGS = -Wall -Werror -Werror -O2 release_CFLAGS = -Wall -Werror -Werror -O3
debug_CFLAGS = -Wall -Werror -Werror -g3 -DDEBUG -DDBG='fprintf(stderr, RED "DEBUG: " RESET "%s:%d (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);' debug_CFLAGS = -Wall -Werror -Werror -g3 -DDEBUG -DDBG='fprintf(stderr, RED "DEBUG: " RESET "%s:%d (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);'
asan_CFLAGS = -Wall -Werror -Werror -fsanitize=address,leak,undefined -g3 asan_CFLAGS = -Wall -Werror -Werror -fsanitize=address,leak,undefined -g3
tsan_CFLAGS = -Wall -Werror -Werror -fsanitize=thread -g3 tsan_CFLAGS = -Wall -Werror -Werror -fsanitize=thread -g3

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* render_sprite.c :+: :+: :+: */ /* render_sprite.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */ /* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */
/* Updated: 2025/05/08 17:02:10 by qmennen ### ########.fr */ /* Updated: 2025/05/08 23:18:06 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -84,14 +84,9 @@ unsigned int sample_texture_color(mlx_texture_t *texture, int x, int y, double d
int index; int index;
int alpha; int alpha;
dist = (dist == 0) * 1e30 + (dist > 1) * dist + (dist <= 1) * 1;
alpha = 1.0 / dist * 255;
index = (x + y * texture->width) * texture->bytes_per_pixel; index = (x + y * texture->width) * texture->bytes_per_pixel;
if (texture->pixels[index + 3] == 0) dist = (dist > 1) * dist + (dist <= 1) * 1;
{ alpha = (texture->pixels[index + 3] != 0) * (255.0 / dist);
alpha = 0;
}
return texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 | alpha; return texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 | alpha;
} }
@ -110,32 +105,28 @@ void draw_sprite(t_game *game, t_sprite *sprite, t_render *render)
if (isnan(sprite->cam_frac) || sprite->dist <= 0) if (isnan(sprite->cam_frac) || sprite->dist <= 0)
return; return;
sprite_scale = (1.0 / sprite->dist) * 16.0; sprite_scale = 16.0 / sprite->dist;
x_start = (game->screen->width / 2) * (1 + sprite->cam_frac) - ((sprite->texture->width * sprite_scale) / 2); x_start = game->screen->width * 0.5 * (1.0 + sprite->cam_frac) - (sprite->texture->width * sprite_scale * 0.5);
y_start = (game->screen->height / 2) - (sprite->texture->height * sprite_scale) / 2 ; y_start = (game->screen->height - sprite->texture->height * sprite_scale )* 0.5;
x_end = (game->screen->width / 2) * (1 + sprite->cam_frac) + ((sprite->texture->width * sprite_scale) / 2); x_end = x_start + (sprite->texture->width * sprite_scale);
y_end = (game->screen->height / 2) + (sprite->texture->height * sprite_scale) / 2 ; y_end = y_start + (sprite->texture->height * sprite_scale);
x = x_start; x = x_start;
while (x < x_end) while (x < x_end)
{ {
y = y_start; if (x > 0 && x < game->screen->width && sprite->dist <= render[x].perp_dist)
if (x < 0 || x >= game->screen->width || sprite->dist > render[x].perp_dist)
{
x++;
continue;
}
while (y < y_end)
{ {
y = y_start;
tex_x = ((double)((x - x_start) / (double)(x_end - x_start))) * sprite->texture->width; tex_x = ((double)((x - x_start) / (double)(x_end - x_start))) * sprite->texture->width;
tex_y = ((double)((y- y_start) / (double)(y_end - y_start))) * sprite->texture->height; while (y < y_end)
color = sample_texture_color(sprite->texture, (int)(tex_x), (int)(tex_y), sprite->dist);
if (x < 0 || x >= game->screen->width || y < 0 || y >= game->screen->height || (color & 0xFF) == 0)
{ {
tex_y = ((double)((y- y_start) / (double)(y_end - y_start))) * sprite->texture->height;
color = sample_texture_color(sprite->texture, (int)(tex_x), (int)(tex_y), sprite->dist);
if (y > 0 && y < game->screen->height && (color & 0xFF) != 0)
{
mlx_put_pixel(game->screen->img, x, y, color);
}
y++; y++;
continue;
} }
mlx_put_pixel(game->screen->img, x, y, color);
y++;
} }
x++; x++;
} }