Amit's code mostly converted to use the new texture.hpp but there's an error on line amt/pixel.hpp:472

This commit is contained in:
Zed A. Shaw 2025-01-17 11:36:03 -05:00
parent c91e8fc543
commit 4d31a4daf2
9 changed files with 98 additions and 92 deletions

View file

@ -1,42 +1,10 @@
#include "amt/raycaster.hpp"
#include "pixel.hpp"
#include "amt/texture.hpp"
#include "amt/pixel.hpp"
using namespace fmt;
using std::make_unique;
#define rgba_color(r,g,b,a) (r<<(0*8))|(g<<(1*8))|(b<<(2*8))|(a<<(3*8))
#define gray_color(c) rgba_color(c, c, c, 255)
amt::PixelBuf TexturePack::load_image(const char *filename) {
sf::Image img;
bool good = img.loadFromFile(filename);
dbc::check(good, format("failed to load {}", filename));
return amt::PixelBuf(img.getPixelsPtr(), TEXTURE_HEIGHT, TEXTURE_WIDTH);
}
void TexturePack::load_textures() {
images.emplace_back(load_image("assets/tile16.png"));
images.emplace_back(load_image("assets/tile02.png"));
images.emplace_back(load_image("assets/tile03.png"));
images.emplace_back(load_image("assets/tile32.png"));
images.emplace_back(load_image("assets/tile05.png"));
images.emplace_back(load_image("assets/tile17.png"));
images.emplace_back(load_image("assets/tile10.png"));
images.emplace_back(load_image("assets/tile01.png"));
images.emplace_back(load_image("assets/portal.png"));
}
amt::PixelBuf& TexturePack::get(size_t num) {
return images[num];
}
Sprite &TexturePack::get_sprite(size_t sprite_num) {
return SPRITE[sprite_num];
}
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
$width(width), $height(height),
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)),
@ -80,12 +48,13 @@ void Raycaster::sprite_casting() {
// sort sprites from far to close
for(int i = 0; i < textures.NUM_SPRITES; i++) {
auto& sprite = textures.get_sprite(i);
spriteOrder[i] = i;
// this is just the distance calculation
spriteDistance[i] = ((posX - textures.SPRITE[i].x) *
(posX - textures.SPRITE[i].x) +
(posY - textures.SPRITE[i].y) *
(posY - textures.SPRITE[i].y));
spriteDistance[i] = ((posX - sprite.x) *
(posX - sprite.x) +
(posY - sprite.y) *
(posY - sprite.y));
}
sort_sprites(spriteOrder, spriteDistance, textures.NUM_SPRITES);
@ -94,9 +63,9 @@ void Raycaster::sprite_casting() {
for(int i = 0; i < textures.NUM_SPRITES; i++) {
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;
auto& sprite_texture = textures.get(sprite_rec.texture);
//transform sprite with the inverse camera matrix
// [ planeX dirX ] -1 [ dirY -dirX ]
@ -229,7 +198,7 @@ void Raycaster::cast_rays() {
int drawEnd = lineHeight / 2 + $height / 2 + PITCH;
if(drawEnd >= $height) drawEnd = $height - 1;
auto &texture = textures.get($map[mapY][mapX] - 1);
auto &texture = textures.get_texture($map[mapY][mapX] - 1);
// calculate value of wallX
double wallX; // where exactly the wall was hit
@ -267,9 +236,6 @@ void Raycaster::draw_ceiling_floor() {
const size_t textureWidth = textures.TEXTURE_WIDTH;
const size_t textureHeight = textures.TEXTURE_HEIGHT;
auto& floorTexture = textures.get(textures.floor);
auto& ceilingTexture = textures.get(textures.ceiling);
for(int y = $height / 2 + 1; y < $height; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w)
float rayDirX0 = dirX - planeX;
@ -320,10 +286,10 @@ void Raycaster::draw_ceiling_floor() {
// floorX cellX to find the texture x/y. How?
// FLOOR
pixels[y][x] = floorTexture[ty][tx];
pixels[y][x] = textures.floor[ty][tx];
// CEILING
pixels[$height - y - 1][x] = ceilingTexture[ty][tx];
pixels[$height - y - 1][x] = textures.ceiling[ty][tx];
}
}