|
|
|
@ -5,7 +5,31 @@ |
|
|
|
|
using namespace fmt; |
|
|
|
|
using std::make_unique; |
|
|
|
|
|
|
|
|
|
union ColorConv { |
|
|
|
|
struct { |
|
|
|
|
uint8_t r; |
|
|
|
|
uint8_t g; |
|
|
|
|
uint8_t b; |
|
|
|
|
uint8_t a; |
|
|
|
|
} as_color; |
|
|
|
|
uint32_t as_int; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
inline uint32_t dumb_lighting(uint32_t pixel, double distance) { |
|
|
|
|
if(distance < 0.9) return pixel; |
|
|
|
|
|
|
|
|
|
ColorConv conv{.as_int=pixel}; |
|
|
|
|
conv.as_color.r /= distance; |
|
|
|
|
conv.as_color.g /= distance; |
|
|
|
|
conv.as_color.b /= distance; |
|
|
|
|
|
|
|
|
|
return conv.as_int; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) : |
|
|
|
|
view_texture({(unsigned int)width, (unsigned int)height}), |
|
|
|
|
view_sprite(view_texture), |
|
|
|
|
$width(width), $height(height), |
|
|
|
|
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)), |
|
|
|
|
$window(window), |
|
|
|
@ -15,14 +39,12 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh |
|
|
|
|
ZBuffer(width) |
|
|
|
|
{ |
|
|
|
|
$window.setVerticalSyncEnabled(true); |
|
|
|
|
view_texture.create($width, $height); |
|
|
|
|
view_sprite.setTexture(view_texture); |
|
|
|
|
view_sprite.setPosition(0, 0); |
|
|
|
|
view_sprite.setPosition({0, 0}); |
|
|
|
|
textures.load_textures(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Raycaster::set_position(int x, int y) { |
|
|
|
|
view_sprite.setPosition(x, y); |
|
|
|
|
view_sprite.setPosition({(float)x, (float)y}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Raycaster::position_camera(float player_x, float player_y) { |
|
|
|
@ -32,7 +54,7 @@ void Raycaster::position_camera(float player_x, float player_y) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Raycaster::draw_pixel_buffer() { |
|
|
|
|
view_texture.update(pixels.to_raw_buf(), $width, $height, 0, 0); |
|
|
|
|
view_texture.update(pixels.to_raw_buf(), {(unsigned int)$width, (unsigned int)$height}, {0, 0}); |
|
|
|
|
// BUG: can I do this once and just update it?
|
|
|
|
|
$window.draw(view_sprite); |
|
|
|
|
} |
|
|
|
@ -64,6 +86,7 @@ void Raycaster::sprite_casting() { |
|
|
|
|
int sprite_index = spriteOrder[i]; |
|
|
|
|
Sprite& sprite_rec = textures.get_sprite(sprite_index); |
|
|
|
|
auto& sprite_texture = textures.get_texture(sprite_rec.texture); |
|
|
|
|
|
|
|
|
|
double spriteX = sprite_rec.x - posX; |
|
|
|
|
double spriteY = sprite_rec.y - posY; |
|
|
|
|
|
|
|
|
|