Way better in the structure but still a lot of work to do. However, just by moving variables to better locations, taking things out of loops that don't need to be recalulated, etc. this is already 50% of the CPU/GPU usage as the previous version.

This commit is contained in:
Zed A. Shaw 2025-01-15 15:32:54 -05:00
parent e379bcd5ec
commit 113df851af
3 changed files with 115 additions and 106 deletions

View file

@ -10,6 +10,7 @@
#include <cstdlib>
#include <array>
#include "dbc.hpp"
#include <memory>
using matrix::Matrix;
@ -31,32 +32,24 @@ struct Sprite {
#define RAY_VIEW_X (1280 - RAY_VIEW_WIDTH)
#define RAY_VIEW_Y 0
union RGBA {
struct {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
} color;
using RGBA = uint32_t;
uint32_t out;
struct TexturePack {
std::vector<uint32_t> texture[NUM_TEXTURES];
std::vector<Sprite> SPRITE{{4.0, 3.55, 0, 8}};
const int floor = 3;
const int ceiling = 6;
void load_textures();
void load_image(std::vector<uint32_t>& texture, const char *filename);
Sprite &get_sprite(size_t sprite_num);
std::vector<uint32_t>& get(size_t num);
};
struct Raycaster {
std::vector<Sprite> SPRITE;
std::vector<uint32_t> texture[NUM_TEXTURES];
Matrix MAP;
int PITCH=0;
// I chose fixed textures for this instead
const int floorTexture = 3;
const int ceilingTexture = 6;
float player_x = RAY_VIEW_HEIGHT / 2;
float player_y = RAY_VIEW_HEIGHT / 2;
// x and y start position
double posX;
double posY;
TexturePack textures;
double posX = 0;
double posY = 0;
// initial direction vector
double dirX = -1;
@ -66,9 +59,12 @@ struct Raycaster {
double planeX = 0;
double planeY = 0.66;
//ZED allocate this on the heap
std::array<double, RAY_VIEW_WIDTH> ZBuffer;
RGBA *pixels = nullptr;
//ZED: USE smart pointer for this
std::unique_ptr<RGBA[]> pixels = nullptr;
//ZED: heap too?
int spriteOrder[NUM_SPRITES];
double spriteDistance[NUM_SPRITES];
@ -76,25 +72,23 @@ struct Raycaster {
sf::Sprite view_sprite;
sf::RenderWindow& $window;
int TILE_SIZE;
Matrix& $map;
int PITCH=0;
Raycaster(sf::RenderWindow& window);
void load_image(std::vector<uint32_t>& texture, const char *filename);
void load_textures();
Raycaster(sf::RenderWindow& window, Matrix &map);
void draw_pixel_buffer();
void clear();
void cast_rays(Matrix& map);
void cast_rays();
void draw_ceiling_floor();
void render();
bool empty_space(int new_x, int new_y);
void sort_sprites(int* order, double* dist, int amount);
// ZED these can be one or two functions
void move_forward(double moveSpeed);
void move_backward(double moveSpeed);
void rotate_right(double rotSpeed);
void rotate_left(double rotSpeed);
void position_camera(float player_x, float player_y, int tile_size);
};