|
|
|
@ -34,8 +34,8 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh |
|
|
|
|
$width(width), $height(height), |
|
|
|
|
$window(window), |
|
|
|
|
$map(map), |
|
|
|
|
spriteOrder($textures.NUM_SPRITES), |
|
|
|
|
spriteDistance($textures.NUM_SPRITES), |
|
|
|
|
spriteOrder(NUM_SPRITES), |
|
|
|
|
spriteDistance(NUM_SPRITES), |
|
|
|
|
ZBuffer(width) |
|
|
|
|
{ |
|
|
|
|
$view_sprite.setPosition({0, 0}); |
|
|
|
@ -72,7 +72,7 @@ void Raycaster::sprite_casting() { |
|
|
|
|
const int halfHeight = TEXTURE_HEIGHT / 2; |
|
|
|
|
|
|
|
|
|
// sort sprites from far to close
|
|
|
|
|
for(int i = 0; i < $textures.NUM_SPRITES; i++) { |
|
|
|
|
for(int i = 0; i < NUM_SPRITES; i++) { |
|
|
|
|
auto& sprite = $textures.get_sprite(i); |
|
|
|
|
spriteOrder[i] = i; |
|
|
|
|
// this is just the distance calculation
|
|
|
|
@ -82,10 +82,10 @@ void Raycaster::sprite_casting() { |
|
|
|
|
($posY - sprite.y)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sort_sprites(spriteOrder, spriteDistance, $textures.NUM_SPRITES); |
|
|
|
|
sort_sprites(spriteOrder, spriteDistance, NUM_SPRITES); |
|
|
|
|
|
|
|
|
|
// after sorting the sprites, do the projection
|
|
|
|
|
for(int i = 0; i < $textures.NUM_SPRITES; i++) { |
|
|
|
|
for(int i = 0; i < NUM_SPRITES; i++) { |
|
|
|
|
int sprite_index = spriteOrder[i]; |
|
|
|
|
Sprite& sprite_rec = $textures.get_sprite(sprite_index); |
|
|
|
|
// TODO: this must die
|
|
|
|
@ -110,11 +110,11 @@ void Raycaster::sprite_casting() { |
|
|
|
|
|
|
|
|
|
// calculate the height of the sprite on screen
|
|
|
|
|
//using "transformY" instead of the real distance prevents fisheye
|
|
|
|
|
int spriteHeight = abs(int($height / transformY)) / sprite_rec.vDiv; |
|
|
|
|
int spriteHeight = abs(int($height / transformY)); |
|
|
|
|
|
|
|
|
|
// calculate width the the sprite
|
|
|
|
|
// same as height of sprite, given that it's square
|
|
|
|
|
int spriteWidth = abs(int($height / transformY)) / sprite_rec.uDiv; |
|
|
|
|
int spriteWidth = abs(int($height / transformY)); |
|
|
|
|
|
|
|
|
|
int drawStartX = -spriteWidth / 2 + spriteScreenX; |
|
|
|
|
if(drawStartX < 0) drawStartX = 0; |
|
|
|
@ -132,11 +132,10 @@ void Raycaster::sprite_casting() { |
|
|
|
|
int texX_end = int(textureWidth * (stripe - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / textureWidth; |
|
|
|
|
|
|
|
|
|
if(drawStartX < drawEndX && transformY > 0 && transformY < ZBuffer[drawStartX]) { |
|
|
|
|
int vMoveScreen = int(sprite_rec.elevation * -1 / transformY); |
|
|
|
|
//calculate lowest and highest pixel to fill in current stripe
|
|
|
|
|
int drawStartY = -spriteHeight / 2 + $height / 2 + vMoveScreen; |
|
|
|
|
int drawStartY = -spriteHeight / 2 + $height / 2; |
|
|
|
|
if(drawStartY < 0) drawStartY = 0; |
|
|
|
|
int drawEndY = spriteHeight / 2 + $height / 2 + vMoveScreen; |
|
|
|
|
int drawEndY = spriteHeight / 2 + $height / 2; |
|
|
|
|
if(drawEndY >= $height) drawEndY = $height - 1; |
|
|
|
|
|
|
|
|
|
int texX = int(textureWidth * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / textureWidth; |
|
|
|
@ -146,7 +145,7 @@ void Raycaster::sprite_casting() { |
|
|
|
|
float sprite_w = float(spriteWidth) / float(textureWidth); |
|
|
|
|
float sprite_h = float(spriteHeight) / float(textureHeight); |
|
|
|
|
|
|
|
|
|
int d = (y - vMoveScreen) * textureHeight - $height * halfHeight + spriteHeight * halfHeight; |
|
|
|
|
int d = y * textureHeight - $height * halfHeight + spriteHeight * halfHeight; |
|
|
|
|
int texY = ((d * textureHeight) / spriteHeight) / textureHeight; |
|
|
|
|
|
|
|
|
|
sf_sprite->setScale({sprite_w, sprite_h}); |
|
|
|
|