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

master
Zed A. Shaw 2 months ago
parent da7075864b
commit ad38f575a3
  1. BIN
      assets/armored_knight_1-256.png
  2. BIN
      assets/evil_eye_test-256.png
  3. 19
      raycaster.cpp
  4. 22
      scratchpad/raycaster_sprites.cpp
  5. 3
      texture.cpp

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 59 KiB

@ -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);
}

@ -135,14 +135,14 @@ int main(int /*argc*/, char */*argv*/[])
//load some textures
unsigned long tw, th, error = 0;
error |= loadImage(texture[0], tw, th, "assets/eagle.png");
error |= loadImage(texture[1], tw, th, "assets/redbrick.png");
error |= loadImage(texture[2], tw, th, "assets/purplestone.png");
error |= loadImage(texture[3], tw, th, "assets/greystone.png");
error |= loadImage(texture[4], tw, th, "assets/bluestone.png");
error |= loadImage(texture[5], tw, th, "assets/mossy.png");
error |= loadImage(texture[6], tw, th, "assets/wood.png");
error |= loadImage(texture[7], tw, th, "assets/colorstone.png");
error |= loadImage(texture[0], tw, th, "pics/eagle.png");
error |= loadImage(texture[1], tw, th, "pics/redbrick.png");
error |= loadImage(texture[2], tw, th, "pics/purplestone.png");
error |= loadImage(texture[3], tw, th, "pics/greystone.png");
error |= loadImage(texture[4], tw, th, "pics/bluestone.png");
error |= loadImage(texture[5], tw, th, "pics/mossy.png");
error |= loadImage(texture[6], tw, th, "pics/wood.png");
error |= loadImage(texture[7], tw, th, "pics/colorstone.png");
if(error) { std::cout << "error loading images" << std::endl; return 1; }
//load some sprite textures
@ -335,11 +335,7 @@ int main(int /*argc*/, char */*argv*/[])
for(int i = 0; i < numSprites; i++)
{
spriteOrder[i] = i;
spriteDistance[i] =
((posX - sprite[i].x) *
(posX - sprite[i].x) +
(posY - sprite[i].y) *
(posY - sprite[i].y)); //sqrt not taken, unneeded
spriteDistance[i] = ((posX - sprite[i].x) * (posX - sprite[i].x) + (posY - sprite[i].y) * (posY - sprite[i].y)); //sqrt not taken, unneeded
}
sortSprites(spriteOrder, spriteDistance, numSprites);

@ -29,7 +29,8 @@ void TexturePack::load_textures() {
floor = load_image(assets["floor"]);
ceiling = load_image(assets["ceiling"]);
sf::Texture* sprite_texture = new sf::Texture("assets/undead_peasant-256.png");
sf::Texture* sprite_texture = new sf::Texture("assets/evil_eye_test-256.png");
sprite_texture->setSmooth(false);
sf::Sprite* sf_sprite = new sf::Sprite(*sprite_texture);
sprites.push_back({4.0, 3.55, 6, sf_sprite, sprite_texture});

Loading…
Cancel
Save