From af6015d9ee5c753b1da4976a13b8a82c7b24a2f6 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 7 Jun 2022 18:04:24 +0200 Subject: [PATCH] ? --- sources/render.c | 182 +++++++++++++++++++++++++---------------------- 1 file changed, 96 insertions(+), 86 deletions(-) diff --git a/sources/render.c b/sources/render.c index 7e34a41..90fd068 100644 --- a/sources/render.c +++ b/sources/render.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ -/* Updated: 2022/06/07 09:22:47 by narnaud ### ########.fr */ +/* Updated: 2022/06/07 11:46:39 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,15 +65,101 @@ void render_minimap(t_env *env, t_vec size) env->playerPos.x * dX - dX / 4, env->playerPos.y * dY - dY / 4, dX / 2, dY / 2, 255}, 0); } -void render_view(t_env* env) + +void draw_vert(t_env *env, t_raycast *rc, int side, int x) { - double camX; - int side; - t_raycast rc; - int x; int startEnd[2]; + int texHeight; + int texX; + int texY; + double step; + double texPos; int lineHeight; - int mapVal; + + lineHeight = (int)(WIN_Y_SZ / rc->sDist[side]); + if (lineHeight < 0) + lineHeight = 0; + startEnd[0] = -lineHeight / 2 + WIN_Y_SZ / 2; + if (startEnd[0] < 0) + startEnd[0] = 0; + startEnd[1] = lineHeight / 2 + WIN_Y_SZ / 2; + if(startEnd[1] >= WIN_Y_SZ) + startEnd[1] = WIN_Y_SZ - 1; + texHeight = env->tex[rc->tex].height; + draw_rectangle(env, (t_rectangle){x, 0, 1, startEnd[0], env->ceilColor}, 0); + //draw_rectangle(env, (t_rectangle){x, startEnd[0], 1, startEnd[1] - startEnd[0] - 1, color}, 0); + texX = (int)(rc->wallX * (double)env->tex[rc->tex].width); + if(!side && rc->vec.x > 0) + texX = env->tex[rc->tex].width - texX - 1; + if(side && rc->vec.y < 0) + texX = env->tex[rc->tex].width - texX - 1; + step = 1.0 * texHeight / lineHeight; + texPos = (startEnd[0] - WIN_Y_SZ / 2 + lineHeight / 2) * step; + while (startEnd[0] < startEnd[1]) + { + texY = (int)texPos & (texHeight - 1); + texPos += step; + env->buffer[env->line_bytes * startEnd[0] + x] \ + = env->tex[rc->tex].buffer[texHeight * texY + texX]; + startEnd[0]++; + } + draw_rectangle(env, (t_rectangle){x, startEnd[1], 1, WIN_Y_SZ - startEnd[1], env->floorColor}, 0); +} + +int to_draw(t_env *env, t_raycast *rc, int *side) +{ + *side = 0; + if (rc->sDist[0] > rc->sDist[1]) + (*side)++; + rc->cell[*side] += rc->step[*side]; + if (env->map[rc->cell[1]][rc->cell[0]] > '0') + { + if (env->map[rc->cell[1]][rc->cell[0]] == '1') + rc->tex = 3 - *side * 2 - (rc->step[*side] + 1) / 2; + else if (env->map[rc->cell[1]][rc->cell[0]] == '2' && rc->tex != 4) + rc->tex = 4; + else + return (0); + if (!*side) + rc->wallX= env->playerPos.y + rc->sDist[*side] * rc->vec.y; + else + rc->wallX = env->playerPos.x + rc->sDist[*side] * rc->vec.x; + rc->wallX -= floor(rc->wallX); + if (rc->tex != 4 || 2 * rc->wallX - vec_len((t_vec_d){rc->cell[0] - env->playerPos.x, rc->cell[1] - env->playerPos.y}) < -2) + return (1); + } + else + rc->tex = 0; + return (0); +} + +void init_vert_rc(t_env *env, t_raycast *rc) +{ + if (rc->vec.x == 0) + rc->dDist[0] = 1e30; + else + rc->dDist[0] = fabs(1 / rc->vec.x); + if (rc->vec.y == 0) + rc->dDist[1] = 1e30; + else + rc->dDist[1] = fabs(1 / rc->vec.y); + rc->step = (int[2]){0, 0}; + if (rc->vec.x < 0 && --rc->step[0]) + rc->sDist[0] = (env->playerPos.x - rc->cell[0]) * rc->dDist[0]; + else if (++rc->step[0]) + rc->sDist[0] = (rc->cell[0] + 1.0 - env->playerPos.x) * rc->dDist[0]; + if (rc->vec.y < 0 && --rc->step[1]) + rc->sDist[1] = (env->playerPos.y - rc->cell[1]) * rc->dDist[1]; + else if (++rc->step[1]) + rc->sDist[1] = (rc->cell[1] + 1.0 - env->playerPos.y) * rc->dDist[1]; +} + +void render_view(t_env* env) +{ + int side; + t_raycast rc; + int x; + double camX; x = 0; while (x < WIN_X_SZ) @@ -83,86 +169,10 @@ void render_view(t_env* env) set_vec(&rc.vec, env->playerDir.x + env->camPlan.x * camX, env->playerDir.y + env->camPlan.y * camX); rc.cell = (int[2]){env->playerPos.x, env->playerPos.y}; - if (rc.vec.x == 0) - rc.dDist[0] = 1e30; - else - rc.dDist[0] = fabs(1 / rc.vec.x); - if (rc.vec.y == 0) - rc.dDist[1] = 1e30; - else - rc.dDist[1] = fabs(1 / rc.vec.y); - rc.step = (int[2]){0, 0}; - if (rc.vec.x < 0 && --rc.step[0]) - rc.sDist[0] = (env->playerPos.x - rc.cell[0]) * rc.dDist[0]; - else if (++rc.step[0]) - rc.sDist[0] = (rc.cell[0] + 1.0 - env->playerPos.x) * rc.dDist[0]; - if (rc.vec.y < 0 && --rc.step[1]) - rc.sDist[1] = (env->playerPos.y - rc.cell[1]) * rc.dDist[1]; - else if (++rc.step[1]) - rc.sDist[1] = (rc.cell[1] + 1.0 - env->playerPos.y) * rc.dDist[1]; - while (1) - { - side = 0; - if (rc.sDist[0] > rc.sDist[1]) - side++; - rc.cell[side] += rc.step[side]; - mapVal = env->map[rc.cell[1]][rc.cell[0]]; - if (mapVal > '0') - { - if (mapVal == '2' && rc.tex != 4) - rc.tex = 4; - else if (mapVal == '1') - rc.tex = 3 - side * 2 - (rc.step[side] + 1) / 2; - else - { - rc.sDist[side] += rc.dDist[side]; - continue ; - } - if (!side) - rc.wallX= env->playerPos.y + rc.sDist[side] * rc.vec.y; - else - rc.wallX = env->playerPos.x + rc.sDist[side] * rc.vec.x; - rc.wallX -= floor(rc.wallX); - if (rc.tex == 4 && 2 * rc.wallX - vec_len((t_vec_d){rc.cell[0] - env->playerPos.x, rc.cell[1] - env->playerPos.y}) > -2) - { - rc.sDist[side] += rc.dDist[side]; - continue; - } - break ; - } - else - rc.tex = 0; + init_vert_rc(env, &rc); + while (!to_draw(env, &rc, &side)) rc.sDist[side] += rc.dDist[side]; - } - lineHeight = (int)(WIN_Y_SZ / rc.sDist[side]); - if (lineHeight < 0) - lineHeight = 0; - startEnd[0] = -lineHeight / 2 + WIN_Y_SZ / 2; - if (startEnd[0] < 0) - startEnd[0] = 0; - startEnd[1] = lineHeight / 2 + WIN_Y_SZ / 2; - if(startEnd[1] >= WIN_Y_SZ) - startEnd[1] = WIN_Y_SZ - 1; - - int texHeight = env->tex[rc.tex].height; - draw_rectangle(env, (t_rectangle){x, 0, 1, startEnd[0], env->ceilColor}, 0); - //draw_rectangle(env, (t_rectangle){x, startEnd[0], 1, startEnd[1] - startEnd[0] - 1, color}, 0); - int texX = (int)(rc.wallX * (double)env->tex[rc.tex].width); - if(!side && rc.vec.x > 0) - texX = env->tex[rc.tex].width - texX - 1; - if(side && rc.vec.y < 0) - texX = env->tex[rc.tex].width - texX - 1; - double step = 1.0 * texHeight / lineHeight; - double texPos = (startEnd[0] - WIN_Y_SZ / 2 + lineHeight / 2) * step; - for(int y = startEnd[0]; ybuffer[env->line_bytes * y + x] \ - = env->tex[rc.tex].buffer[texHeight * texY + texX]; - } - draw_rectangle(env, (t_rectangle){x, startEnd[1], 1, WIN_Y_SZ - startEnd[1], env->floorColor}, 0); - + draw_vert(env, &rc, side, x); x++; } }