Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static const s32 w = SCREEN_WIDTH;
- static const s32 h = SCREEN_HEIGHT;
- static f32 pos_x;
- static f32 pos_y;
- static f32 dir_x;
- static f32 dir_y;
- static f32 plane_x;
- static f32 plane_y;
- void
- raycast_draw_walls(void)
- {
- const u8 max_steps = 32;
- for (s32 x = 0; x < w; x++) {
- u8 map_x = pos_x;
- u8 map_y = pos_y;
- // x-coordinate in camera space
- f32 cam_x = (f32)x / w * 2 - 1;
- f32 ray_dir_x = dir_x + plane_x * cam_x;
- f32 ray_dir_y = dir_y + plane_y * cam_x;
- f32 delta_x = ABS(1 / ray_dir_x);
- f32 delta_y = ABS(1 / ray_dir_y);
- s8 step_x = SIGN(ray_dir_x);
- s8 step_y = SIGN(ray_dir_y);
- // distance from starting pos to first grid intersection
- f32 dist_x = step_x < 0 ?
- delta_x * (pos_x - map_x) :
- delta_x * (map_x - pos_x + 1);
- f32 dist_y = step_y < 0 ?
- delta_y * (pos_y - map_y) :
- delta_y * (map_y - pos_y + 1);
- BOOL side;
- u8 tile;
- u8 steps = 0;
- while (1) {
- if (++steps > max_steps)
- goto next_ray;
- if (dist_x < dist_y) {
- dist_x += delta_x;
- map_x += step_x;
- side = 0;
- } else {
- dist_y += delta_y;
- map_y += step_y;
- side = 1;
- }
- tile = map_at(map_x, map_y, MAP_WALL);
- if (tile)
- break;
- }
- f32 dist = side ?
- (dist_y - delta_y) :
- (dist_x - delta_x);
- s32 line_h = h / dist;
- s32 y0 = (h - line_h) / 2;
- s32 y1 = (h + line_h) / 2;
- f32 wall_x = side ?
- pos_x + dist * ray_dir_x :
- pos_y + dist * ray_dir_y;
- wall_x -= (s32)wall_x;
- s32 tex_x = wall_x * 64;
- if (side == 0 && ray_dir_x > 0) tex_x = 64 - tex_x - 1;
- if (side == 1 && ray_dir_y < 0) tex_x = 64 - tex_x - 1;
- tex_x += (tile - 1) * 64;
- f32 tex_step = 64.0 / line_h;
- f32 tex_pos = 0;
- s32 tex_y = 0;
- for (s32 y = y0; y < y1; y++) {
- tex_pos += tex_step;
- if (y < 0 || y >= h)
- continue;
- u32 col = PIXEL(g_bitmaps[BITMAP_TILES], tex_x, tex_y);
- tex_y = roundf(tex_pos);
- if (side)
- col = (col >> 1) & 0x7F7F7F;
- col = video_blend_color(col, 0, (f32)steps / max_steps);
- PIXEL(g_screen, x, y) = col;
- }
- next_ray:
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment