diff --git a/doc/014.png b/doc/014.png new file mode 100644 index 0000000..007d4f7 Binary files /dev/null and b/doc/014.png differ diff --git a/tinyraycaster.cpp b/tinyraycaster.cpp index 75b1df3..a9e4bbf 100644 --- a/tinyraycaster.cpp +++ b/tinyraycaster.cpp @@ -32,7 +32,7 @@ void map_show_sprite(Sprite &sprite, FrameBuffer &fb, Map &map) { fb.draw_rectangle(sprite.x*rect_w-3, sprite.y*rect_h-3, 6, 6, pack_color(255, 0, 0)); } -void draw_sprite(Sprite &sprite, FrameBuffer &fb, Player &player, Texture &tex_sprites) { +void draw_sprite(Sprite &sprite, std::vector &depth_buffer, FrameBuffer &fb, Player &player, Texture &tex_sprites) { // absolute direction from the player to the sprite (in radians) float sprite_dir = atan2(sprite.y - player.y, sprite.x - player.x); while (sprite_dir - player.a > M_PI) sprite_dir -= 2*M_PI; // remove unncesessary periods from the relative direction @@ -44,10 +44,15 @@ void draw_sprite(Sprite &sprite, FrameBuffer &fb, Player &player, Texture &tex_s int v_offset = fb.h/2 - sprite_screen_size/2; for (size_t i=0; i=fb.w/2) continue; + if (h_offset+int(i)<0 || h_offset+i>=fb.w/2) continue; + if (depth_buffer[h_offset+i]=fb.h) continue; - fb.set_pixel(fb.w/2 + h_offset+i, v_offset+j, pack_color(0,0,0)); + if (v_offset+int(j)<0 || v_offset+j>=fb.h) continue; + uint32_t color = tex_sprites.get(i*tex_sprites.size/sprite_screen_size, j*tex_sprites.size/sprite_screen_size, sprite.tex_id); + uint8_t r,g,b,a; + unpack_color(color, r, g, b, a); + if (a>128) + fb.set_pixel(fb.w/2 + h_offset+i, v_offset+j, color); } } } @@ -68,6 +73,7 @@ void render(FrameBuffer &fb, Map &map, Player &player, std::vector &spri } } + std::vector depth_buffer(fb.w/2, 1e3); for (size_t i=0; i &spri size_t texid = map.get(x, y); // our ray touches a wall, so draw the vertical column to create an illusion of 3D assert(texid column = tex_walls.get_scaled_column(texid, x_texcoord, column_height); @@ -96,7 +103,7 @@ void render(FrameBuffer &fb, Map &map, Player &player, std::vector &spri for (size_t i=0; i