|
|
|
#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"
|
|
|
|
|
|
|
|
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;
|
|
|
|
double elevation;
|
|
|
|
int texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define RAY_VIEW_WIDTH 960
|
|
|
|
#define RAY_VIEW_HEIGHT 720
|
|
|
|
#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;
|
|
|
|
|
|
|
|
uint32_t out;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// initial direction vector
|
|
|
|
double dirX = -1;
|
|
|
|
double dirY = 0;
|
|
|
|
|
|
|
|
// the 2d raycaster version of camera plane
|
|
|
|
double planeX = 0;
|
|
|
|
double planeY = 0.66;
|
|
|
|
|
|
|
|
std::array<double, RAY_VIEW_WIDTH> ZBuffer;
|
|
|
|
RGBA *pixels = nullptr;
|
|
|
|
|
|
|
|
int spriteOrder[NUM_SPRITES];
|
|
|
|
double spriteDistance[NUM_SPRITES];
|
|
|
|
|
|
|
|
sf::Texture view_texture;
|
|
|
|
sf::Sprite view_sprite;
|
|
|
|
|
|
|
|
sf::RenderWindow& $window;
|
|
|
|
int TILE_SIZE;
|
|
|
|
|
|
|
|
Raycaster(sf::RenderWindow& window);
|
|
|
|
|
|
|
|
void load_image(std::vector<uint32_t>& texture, const char *filename);
|
|
|
|
void load_textures();
|
|
|
|
|
|
|
|
void draw_pixel_buffer();
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
void cast_rays(Matrix& map);
|
|
|
|
|
|
|
|
void draw_ceiling_floor();
|
|
|
|
void render();
|
|
|
|
bool empty_space(int new_x, int new_y);
|
|
|
|
void sort_sprites(int* order, double* dist, int amount);
|
|
|
|
void move_forward(double moveSpeed);
|
|
|
|
void move_backward(double moveSpeed);
|
|
|
|
void rotate_right(double rotSpeed);
|
|
|
|
void rotate_left(double rotSpeed);
|
|
|
|
};
|