Now the sprites are rendered in the 3d scene with just SFML sprites.

This commit is contained in:
Zed A. Shaw 2025-01-23 11:24:06 -05:00
parent da7075864b
commit ad38f575a3
5 changed files with 49 additions and 41 deletions

View file

@ -41,6 +41,7 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
$view_sprite.setPosition({0, 0});
$pixels = make_unique<RGBA[]>($width * $height);
$textures.load_textures();
$view_texture.setSmooth(false);
}
void Raycaster::set_position(int x, int y) {
@ -111,11 +112,21 @@ void Raycaster::sprite_casting() {
// 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 drawStartX = -spriteWidth / 2 + spriteScreenX;
if(drawStartX < 0) drawStartX = 0;
int drawEndX = spriteWidth / 2 + spriteScreenX;
if(drawEndX > $width) drawEndX = $width;
int stripe = drawStartX;
for(; stripe < drawEndX; stripe++) {
//the conditions in the if are:
//1) it's in front of camera plane so you don't see things behind you
//2) ZBuffer, with perpendicular distance
if(!(transformY > 0 && transformY < ZBuffer[stripe])) break;
}
int texX_end = int(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
if(drawStartX < drawEndX && transformY > 0 && transformY < ZBuffer[drawStartX]) {
int vMoveScreen = int(sprite_rec.elevation * -1 / transformY);
@ -125,18 +136,18 @@ void Raycaster::sprite_casting() {
int drawEndY = spriteHeight / 2 + $height / 2 + vMoveScreen;
if(drawEndY >= $height) drawEndY = $height - 1;
int texX = int(256 * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
float x = float(drawStartX + RAY_VIEW_X);
float y = float(drawStartY + RAY_VIEW_Y);
float sprite_w = float(spriteWidth) / float(textureWidth);
float sprite_h = float(spriteHeight) / float(textureHeight);
int texX = int(256 * (drawStartX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
int texX_end = int(256 * (drawEndX - (-spriteWidth / 2 + spriteScreenX)) * textureWidth / spriteWidth) / 256;
int d = (y - vMoveScreen) * 256 - $height * 128 + spriteHeight * 128;
int texY = ((d * textureHeight) / spriteHeight) / 256;
sf_sprite->setScale({sprite_h, sprite_w});
sf_sprite->setTextureRect(sf::IntRect({texX, texY}, {texX_end, textureHeight}));
sf_sprite->setScale({sprite_w, sprite_h});
sf_sprite->setTextureRect(sf::IntRect({texX, texY}, {texX_end - texX, textureHeight}));
sf_sprite->setPosition({x, y});
$window.draw(*sf_sprite);
}