Remove some more junk from the texture thing.

master
Zed A. Shaw 1 month ago
parent d0d3c8bc04
commit 3519c73079
  1. 2
      constants.hpp
  2. 21
      raycaster.cpp
  3. 5
      texture.hpp

@ -10,6 +10,8 @@ constexpr const int SCREEN_HEIGHT=720;
constexpr const int SCREEN_WIDTH=1280; constexpr const int SCREEN_WIDTH=1280;
constexpr const bool VSYNC=false; constexpr const bool VSYNC=false;
constexpr const int FRAME_LIMIT=60; constexpr const int FRAME_LIMIT=60;
constexpr const int NUM_SPRITES=1;
#ifdef NDEBUG #ifdef NDEBUG
constexpr const bool DEBUG_BUILD=false; constexpr const bool DEBUG_BUILD=false;
#else #else

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

@ -16,14 +16,9 @@ struct Sprite {
double x; double x;
double y; double y;
SpriteTexture sprite; SpriteTexture sprite;
double elevation=0;
int uDiv=1;
int vDiv=1;
}; };
struct TexturePack { struct TexturePack {
int NUM_SPRITES=1;
std::vector<sf::Image> images; std::vector<sf::Image> images;
std::vector<Sprite> sprites; std::vector<Sprite> sprites;
std::unordered_map<std::string, SpriteTexture> sprite_textures; std::unordered_map<std::string, SpriteTexture> sprite_textures;

Loading…
Cancel
Save