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.

master
Zed A. Shaw 2 months ago
parent e379bcd5ec
commit 113df851af
  1. 25
      main.cpp
  2. 138
      raycaster.cpp
  3. 58
      raycaster.hpp

@ -3,19 +3,38 @@
static const int SCREEN_HEIGHT=720; static const int SCREEN_HEIGHT=720;
static const int SCREEN_WIDTH=1280; static const int SCREEN_WIDTH=1280;
Matrix MAP{{8,8,8,8,8,8,8,8,8},
{8,0,2,0,0,0,0,0,8},
{8,0,7,0,0,5,6,0,8},
{8,0,0,0,0,0,0,0,8},
{8,8,0,0,0,0,0,8,8},
{8,0,0,1,3,4,0,0,8},
{8,0,0,0,0,0,8,8,8},
{8,0,0,0,0,0,0,0,8},
{8,8,8,8,8,8,8,8,8}
};
int main() { int main() {
using KB = sf::Keyboard;
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing"); sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing");
Raycaster rayview(window); int TILE_SIZE = RAY_VIEW_HEIGHT / matrix::width(MAP);
using KB = sf::Keyboard; //ZED this should set with a function
rayview.load_textures(); float player_x = RAY_VIEW_HEIGHT / 2;
float player_y = RAY_VIEW_HEIGHT / 2;
Raycaster rayview(window, MAP);
rayview.position_camera(player_x, player_y, TILE_SIZE);
double moveSpeed = 0.1; double moveSpeed = 0.1;
double rotSpeed = 0.1; double rotSpeed = 0.1;
while(window.isOpen()) { while(window.isOpen()) {
rayview.render(); rayview.render();
// DRAW GUI
window.display();
if(KB::isKeyPressed(KB::W)) { if(KB::isKeyPressed(KB::W)) {
rayview.move_forward(moveSpeed); rayview.move_forward(moveSpeed);

@ -1,6 +1,7 @@
#include "raycaster.hpp" #include "raycaster.hpp"
using namespace fmt; 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 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) #define gray_color(c) rgba_color(c, c, c, 255)
@ -9,54 +10,14 @@ using namespace fmt;
#define uDiv 1 #define uDiv 1
#define vDiv 1 #define vDiv 1
inline void RGBA_brightness(RGBA& pixel, double distance) {
pixel.color.r /= distance;
pixel.color.g /= distance;
pixel.color.b /= distance;
}
inline size_t pixcoord(int x, int y) { inline size_t pixcoord(int x, int y) {
return ((y) * RAY_VIEW_WIDTH) + (x); return ((y) * RAY_VIEW_WIDTH) + (x);
} }
Raycaster::Raycaster(sf::RenderWindow& window) : void TexturePack::load_textures() {
$window(window) // ZED: this needs to determine sprite vs. other textures
{ // so it can put the texture into the sprite rec immediately
$window.setVerticalSyncEnabled(true); // or....maybe just use SFML's sprite stuff?
view_texture.create(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
view_sprite.setTexture(view_texture);
view_sprite.setPosition(RAY_VIEW_X, 0);
pixels = new RGBA[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT];
SPRITE = {{4.0, 3.55, 0, 8}};
MAP = {{8,8,8,8,8,8,8,8,8},
{8,0,2,0,0,0,0,0,8},
{8,0,7,0,0,5,6,0,8},
{8,0,0,0,0,0,0,0,8},
{8,8,0,0,0,0,0,8,8},
{8,0,0,1,3,4,0,0,8},
{8,0,0,0,0,0,8,8,8},
{8,0,0,0,0,0,0,0,8},
{8,8,8,8,8,8,8,8,8}
};
TILE_SIZE = RAY_VIEW_HEIGHT / matrix::width(MAP);
// x and y start position
posX = player_x / TILE_SIZE;
posY = player_y / TILE_SIZE;
}
void Raycaster::load_image(std::vector<uint32_t>& texture, const char *filename) {
sf::Image img;
bool good = img.loadFromFile(filename);
dbc::check(good, format("failed to load {}", filename));
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
std::copy_n(pixbuf, texture.size(), texture.begin());
}
void Raycaster::load_textures() {
for(int i = 0; i < NUM_TEXTURES; i++) { for(int i = 0; i < NUM_TEXTURES; i++) {
texture[i].resize(TEXTURE_WIDTH * TEXTURE_HEIGHT); texture[i].resize(TEXTURE_WIDTH * TEXTURE_HEIGHT);
} }
@ -72,18 +33,54 @@ void Raycaster::load_textures() {
load_image(texture[8], "assets/portal.png"); load_image(texture[8], "assets/portal.png");
} }
std::vector<uint32_t>& TexturePack::get(size_t num) {
return texture[num];
}
void TexturePack::load_image(std::vector<uint32_t>& texture, const char *filename) {
sf::Image img;
bool good = img.loadFromFile(filename);
dbc::check(good, format("failed to load {}", filename));
uint32_t *pixbuf = (uint32_t *)img.getPixelsPtr();
std::copy_n(pixbuf, texture.size(), texture.begin());
}
Sprite &TexturePack::get_sprite(size_t sprite_num) {
return SPRITE[sprite_num];
}
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map) :
$window(window),
$map(map)
{
$window.setVerticalSyncEnabled(true);
view_texture.create(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
view_sprite.setTexture(view_texture);
view_sprite.setPosition(RAY_VIEW_X, 0);
pixels = make_unique<RGBA[]>(RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT);
textures.load_textures();
}
void Raycaster::position_camera(float player_x, float player_y, int tile_size) {
// x and y start position
posX = player_x / tile_size;
posY = player_y / tile_size;
}
void Raycaster::draw_pixel_buffer() { void Raycaster::draw_pixel_buffer() {
view_texture.update((uint8_t *)pixels, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT, 0, 0); view_texture.update((uint8_t *)pixels.get(), RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT, 0, 0);
// BUG: can I do this once and just update it? // BUG: can I do this once and just update it?
$window.draw(view_sprite); $window.draw(view_sprite);
} }
void Raycaster::clear() { void Raycaster::clear() {
std::fill_n((uint32_t *)pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0); std::fill_n(pixels.get(), RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0);
$window.clear(); $window.clear();
} }
void Raycaster::cast_rays(Matrix& map) { void Raycaster::cast_rays() {
int w = RAY_VIEW_WIDTH; int w = RAY_VIEW_WIDTH;
int h = RAY_VIEW_HEIGHT; int h = RAY_VIEW_HEIGHT;
double perpWallDist; double perpWallDist;
@ -141,7 +138,7 @@ void Raycaster::cast_rays(Matrix& map) {
side = 1; side = 1;
} }
if(map[mapY][mapX] > 0) hit = 1; if($map[mapY][mapX] > 0) hit = 1;
} }
if(side == 0) { if(side == 0) {
@ -158,7 +155,7 @@ void Raycaster::cast_rays(Matrix& map) {
int drawEnd = lineHeight / 2 + h / 2 + PITCH; int drawEnd = lineHeight / 2 + h / 2 + PITCH;
if(drawEnd >= h) drawEnd = h - 1; if(drawEnd >= h) drawEnd = h - 1;
int texNum = MAP[mapY][mapX] - 1; auto &texture = textures.get($map[mapY][mapX] - 1);
// calculate value of wallX // calculate value of wallX
double wallX; // where exactly the wall was hit double wallX; // where exactly the wall was hit
@ -184,8 +181,7 @@ void Raycaster::cast_rays(Matrix& map) {
for(int y = drawStart; y < drawEnd; y++) { for(int y = drawStart; y < drawEnd; y++) {
int texY = (int)texPos & (TEXTURE_HEIGHT - 1); int texY = (int)texPos & (TEXTURE_HEIGHT - 1);
texPos += step; texPos += step;
RGBA pixel{.out=texture[texNum][TEXTURE_HEIGHT * texY + texX]}; RGBA pixel = texture[TEXTURE_HEIGHT * texY + texX];
RGBA_brightness(pixel, perpWallDist);
pixels[pixcoord(x, y)] = pixel; pixels[pixcoord(x, y)] = pixel;
} }
@ -198,10 +194,10 @@ void Raycaster::cast_rays(Matrix& map) {
for(int i = 0; i < NUM_SPRITES; i++) { for(int i = 0; i < NUM_SPRITES; i++) {
spriteOrder[i] = i; spriteOrder[i] = i;
// this is just the distance calculation // this is just the distance calculation
spriteDistance[i] = ((posX - SPRITE[i].x) * spriteDistance[i] = ((posX - textures.SPRITE[i].x) *
(posX - SPRITE[i].x) + (posX - textures.SPRITE[i].x) +
(posY - SPRITE[i].y) * (posY - textures.SPRITE[i].y) *
(posY - SPRITE[i].y)); (posY - textures.SPRITE[i].y));
} }
sort_sprites(spriteOrder, spriteDistance, NUM_SPRITES); sort_sprites(spriteOrder, spriteDistance, NUM_SPRITES);
@ -209,11 +205,10 @@ void Raycaster::cast_rays(Matrix& map) {
// after sorting the sprites, do the projection // after sorting the sprites, do the projection
for(int i = 0; i < NUM_SPRITES; i++) { for(int i = 0; i < NUM_SPRITES; i++) {
int sprite_index = spriteOrder[i]; int sprite_index = spriteOrder[i];
Sprite& sprite_rec = SPRITE[sprite_index]; Sprite& sprite_rec = textures.get_sprite(sprite_index);
double spriteX = sprite_rec.x - posX; double spriteX = sprite_rec.x - posX;
double spriteY = sprite_rec.y - posY; double spriteY = sprite_rec.y - posY;
int sprite_texture_number = sprite_rec.texture; auto& sprite_texture = textures.get(sprite_rec.texture);
auto sprite_texture = texture[sprite_texture_number];
//transform sprite with the inverse camera matrix //transform sprite with the inverse camera matrix
// [ planeX dirX ] -1 [ dirY -dirX ] // [ planeX dirX ] -1 [ dirY -dirX ]
@ -261,11 +256,11 @@ void Raycaster::cast_rays(Matrix& map) {
int d = (y - vMoveScreen) * 256 - h * 128 + spriteHeight * 128; int d = (y - vMoveScreen) * 256 - h * 128 + spriteHeight * 128;
int texY = ((d * TEXTURE_HEIGHT) / spriteHeight) / 256; int texY = ((d * TEXTURE_HEIGHT) / spriteHeight) / 256;
//get current color from the texture //get current color from the texture
// BUG: this crashes sometimes when the math goes out of bounds
uint32_t color = sprite_texture[TEXTURE_WIDTH * texY + texX]; uint32_t color = sprite_texture[TEXTURE_WIDTH * texY + texX];
// poor person's transparency, get current color from the texture // poor person's transparency, get current color from the texture
if((color & 0x00FFFFFF) != 0) { if((color & 0x00FFFFFF) != 0) {
RGBA pixel{.out=color}; RGBA pixel = color;
RGBA_brightness(pixel, perpWallDist);
pixels[pixcoord(stripe, y)] = pixel; pixels[pixcoord(stripe, y)] = pixel;
} }
} }
@ -277,6 +272,8 @@ void Raycaster::cast_rays(Matrix& map) {
void Raycaster::draw_ceiling_floor() { void Raycaster::draw_ceiling_floor() {
int screenHeight = RAY_VIEW_HEIGHT; int screenHeight = RAY_VIEW_HEIGHT;
int screenWidth = RAY_VIEW_WIDTH; int screenWidth = RAY_VIEW_WIDTH;
auto& floorTexture = textures.get(textures.floor);
auto& ceilingTexture = textures.get(textures.ceiling);
for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) { for(int y = screenHeight / 2 + 1; y < screenHeight; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w) // rayDir for leftmost ray (x=0) and rightmost (x = w)
@ -329,30 +326,29 @@ void Raycaster::draw_ceiling_floor() {
// floorX cellX to find the texture x/y. How? // floorX cellX to find the texture x/y. How?
// FLOOR // FLOOR
color = texture[floorTexture][TEXTURE_WIDTH * ty + tx]; color = floorTexture[TEXTURE_WIDTH * ty + tx];
pixels[pixcoord(x, y)].out = color; pixels[pixcoord(x, y)] = color;
// CEILING // CEILING
color = texture[ceilingTexture][TEXTURE_WIDTH * ty + tx]; color = ceilingTexture[TEXTURE_WIDTH * ty + tx];
pixels[pixcoord(x, screenHeight - y - 1)].out = color; pixels[pixcoord(x, screenHeight - y - 1)] = color;
} }
} }
} }
void Raycaster::render() { void Raycaster::render() {
draw_ceiling_floor(); draw_ceiling_floor();
cast_rays(MAP); cast_rays();
draw_pixel_buffer(); draw_pixel_buffer();
$window.display();
} }
bool Raycaster::empty_space(int new_x, int new_y) { bool Raycaster::empty_space(int new_x, int new_y) {
dbc::check((size_t)new_x < matrix::width(MAP), dbc::check((size_t)new_x < matrix::width($map),
format("x={} too wide={}", new_x, matrix::width(MAP))); format("x={} too wide={}", new_x, matrix::width($map)));
dbc::check((size_t)new_y < matrix::height(MAP), dbc::check((size_t)new_y < matrix::height($map),
format("y={} too high={}", new_y, matrix::height(MAP))); format("y={} too high={}", new_y, matrix::height($map)));
return MAP[new_y][new_x] == 0; return $map[new_y][new_x] == 0;
} }

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

Loading…
Cancel
Save