Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
raycaster/raycaster.hpp

99 lines
2.2 KiB

#pragma once
#include <fmt/core.h>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Image.hpp>
#include <numbers>
#include <algorithm>
#include <cmath>
#include "matrix.hpp"
#include <cstdlib>
#include <array>
#include "dbc.hpp"
#include <memory>
using matrix::Matrix;
#define TEXTURE_WIDTH 256 // must be power of two
#define TEXTURE_HEIGHT 256 // must be power of two
#define NUM_SPRITES 1
#define NUM_TEXTURES 11
struct Sprite {
double x;
double y;
int texture;
// ZED: this should be a separate transform parameter
double elevation=0;
int uDiv=1;
int vDiv=1;
};
#define RAY_VIEW_WIDTH 960
#define RAY_VIEW_HEIGHT 720
#define RAY_VIEW_X (1280 - RAY_VIEW_WIDTH)
#define RAY_VIEW_Y 0
using RGBA = uint32_t;
struct TexturePack {
std::vector<uint32_t> texture[NUM_TEXTURES];
std::vector<Sprite> SPRITE{{4.0, 3.55, 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 {
int $width=RAY_VIEW_WIDTH;
int $height=RAY_VIEW_HEIGHT;
TexturePack textures;
double posX = 0;
double posY = 0;
// initial direction vector
double dirX = -1;
double dirY = 0;
// the 2d raycaster version of camera plane
double planeX = 0;
double planeY = 0.66;
//ZED allocate this on the heap
std::array<double, RAY_VIEW_WIDTH> ZBuffer;
//ZED: USE smart pointer for this
std::unique_ptr<RGBA[]> pixels = nullptr;
//ZED: heap too?
int spriteOrder[NUM_SPRITES];
double spriteDistance[NUM_SPRITES];
sf::Texture view_texture;
sf::Sprite view_sprite;
sf::RenderWindow& $window;
Matrix& $map;
int PITCH=0;
Raycaster(sf::RenderWindow& window, Matrix &map);
void draw_pixel_buffer();
void clear();
void cast_rays();
void sprite_casting();
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 run(double speed, int dir);
void rotate(double speed, int dir);
void position_camera(float player_x, float player_y);
};