Fixed that crash and cleaned up more variables for some study next. I might also try out my debug macros.

master
Zed A. Shaw 2 months ago
parent 7fb2d5cf26
commit 7a74877849
  1. 8
      constants.hpp
  2. 14
      dbc.cpp
  3. 39
      dbg.h
  4. 12
      main.cpp
  5. 2
      miniaudio.cpp
  6. 92621
      miniaudio.h
  7. 160
      raycaster.cpp
  8. 31
      raycaster.hpp
  9. 4
      texture.hpp

@ -0,0 +1,8 @@
#pragma once
constexpr const int RAY_VIEW_WIDTH=960;
constexpr const int RAY_VIEW_HEIGHT=720;
constexpr const int RAY_VIEW_X=(1280 - RAY_VIEW_WIDTH);
constexpr const int RAY_VIEW_Y=0;
constexpr const int SCREEN_HEIGHT=720;
constexpr const int SCREEN_WIDTH=1280;

@ -1,17 +1,20 @@
#include "dbc.hpp"
#include <iostream>
void dbc::log(const string &message) {
fmt::print("{}\n", message);
std::cerr << "!!!!!!!!!!" << message << std::endl;
}
void dbc::sentinel(const string &message) {
string err = fmt::format("[SENTINEL!] {}\n", message);
string err = fmt::format("[SENTINEL!] {}", message);
dbc::log(err);
throw dbc::SentinelError{err};
}
void dbc::pre(const string &message, bool test) {
if(!test) {
string err = fmt::format("[PRE!] {}\n", message);
string err = fmt::format("[PRE!] {}", message);
dbc::log(err);
throw dbc::PreCondError{err};
}
}
@ -22,7 +25,8 @@ void dbc::pre(const string &message, std::function<bool()> tester) {
void dbc::post(const string &message, bool test) {
if(!test) {
string err = fmt::format("[POST!] {}\n", message);
string err = fmt::format("[POST!] {}", message);
dbc::log(err);
throw dbc::PostCondError{err};
}
}
@ -34,7 +38,7 @@ void dbc::post(const string &message, std::function<bool()> tester) {
void dbc::check(bool test, const string &message) {
if(!test) {
string err = fmt::format("[CHECK!] {}\n", message);
fmt::println("{}", err);
dbc::log(err);
throw dbc::CheckError{err};
}
}

39
dbg.h

@ -0,0 +1,39 @@
#ifndef __dbg_h__
#define __dbg_h__
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef NDEBUG
#define debug(M, ...)
#else
#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n",\
__FILE__, __LINE__, ##__VA_ARGS__)
#endif
#define clean_errno() (errno == 0 ? "None" : strerror(errno))
#define log_err(M, ...) fprintf(stderr,\
"[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__,\
clean_errno(), ##__VA_ARGS__)
#define log_warn(M, ...) fprintf(stderr,\
"[WARN] (%s:%d: errno: %s) " M "\n",\
__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
#define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n",\
__FILE__, __LINE__, ##__VA_ARGS__)
#define check(A, M, ...) if(!(A)) {\
log_err(M, ##__VA_ARGS__); errno=0; goto error; }
#define sentinel(M, ...) { log_err(M, ##__VA_ARGS__);\
errno=0; goto error; }
#define check_mem(A) check((A), "Out of memory.")
#define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__);\
errno=0; goto error; }
#endif

@ -3,14 +3,7 @@
#include <chrono>
#include <numeric>
#include <functional>
#define RAY_VIEW_WIDTH 960
#define RAY_VIEW_HEIGHT 720
#define RAY_VIEW_X (1280 - RAY_VIEW_WIDTH)
#define RAY_VIEW_Y 0
static const int SCREEN_HEIGHT=720;
static const int SCREEN_WIDTH=1280;
#include "constants.hpp"
Matrix MAP{
{1,1,1,1,1,1,1,1,1},
@ -25,7 +18,7 @@ Matrix MAP{
};
void draw_gui(sf::RenderWindow &window) {
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, 300});
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, SCREEN_HEIGHT});
rect.setPosition({0,0});
rect.setFillColor({100, 100, 100});
window.draw(rect);
@ -54,6 +47,7 @@ int main() {
rayview.render();
draw_gui(window);
window.display();
rayview.rotate(rotSpeed, -1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
rayview.run(moveSpeed, 1);

@ -1,2 +0,0 @@
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"

File diff suppressed because it is too large Load Diff

@ -28,78 +28,78 @@ inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
view_texture({(unsigned int)width, (unsigned int)height}),
view_sprite(view_texture),
$view_texture({(unsigned int)width, (unsigned int)height}),
$view_sprite($view_texture),
$width(width), $height(height),
$window(window),
$map(map),
spriteOrder(textures.NUM_SPRITES),
spriteDistance(textures.NUM_SPRITES),
spriteOrder($textures.NUM_SPRITES),
spriteDistance($textures.NUM_SPRITES),
ZBuffer(width)
{
$window.setVerticalSyncEnabled(true);
view_sprite.setPosition({0, 0});
pixels = make_unique<RGBA[]>($width * $height);
textures.load_textures();
$view_sprite.setPosition({0, 0});
$pixels = make_unique<RGBA[]>($width * $height);
$textures.load_textures();
}
void Raycaster::set_position(int x, int y) {
view_sprite.setPosition({(float)x, (float)y});
$view_sprite.setPosition({(float)x, (float)y});
}
void Raycaster::position_camera(float player_x, float player_y) {
// x and y start position
posX = player_x;
posY = player_y;
$posX = player_x;
$posY = player_y;
}
void Raycaster::draw_pixel_buffer() {
view_texture.update((uint8_t *)pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
$window.draw(view_sprite);
$view_texture.update((uint8_t *)$pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
$window.draw($view_sprite);
}
void Raycaster::clear() {
std::fill_n(pixels.get(), $width * $height, 0);
std::fill_n($pixels.get(), $width * $height, 0);
$window.clear();
}
void Raycaster::sprite_casting() {
const int textureWidth = textures.TEXTURE_WIDTH;
const int textureHeight = textures.TEXTURE_HEIGHT;
const int textureWidth = $textures.TEXTURE_WIDTH;
const int textureHeight = $textures.TEXTURE_HEIGHT;
// sort sprites from far to close
for(int i = 0; i < textures.NUM_SPRITES; i++) {
auto& sprite = textures.get_sprite(i);
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 - sprite.x) *
(posX - sprite.x) +
(posY - sprite.y) *
(posY - sprite.y));
spriteDistance[i] = (($posX - sprite.x) *
($posX - sprite.x) +
($posY - sprite.y) *
($posY - sprite.y));
}
sort_sprites(spriteOrder, spriteDistance, textures.NUM_SPRITES);
sort_sprites(spriteOrder, spriteDistance, $textures.NUM_SPRITES);
// after sorting the sprites, do the projection
for(int i = 0; i < textures.NUM_SPRITES; i++) {
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);
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;
double spriteX = sprite_rec.x - $posX;
double spriteY = sprite_rec.y - $posY;
//transform sprite with the inverse camera matrix
// [ planeX dirX ] -1 [ dirY -dirX ]
// [ ] = 1/(planeX*dirY-dirX*planeY) * [ ]
// [ planeY dirY ] [ -planeY planeX ]
// [ $planeX $dirX ] -1 [ $dirY -$dirX ]
// [ ] = 1/($planeX*$dirY-$dirX*$planeY) * [ ]
// [ $planeY $dirY ] [ -$planeY $planeX ]
double invDet = 1.0 / (planeX * dirY - dirX * planeY); // required for correct matrix multiplication
double invDet = 1.0 / ($planeX * $dirY - $dirX * $planeY); // required for correct matrix multiplication
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
double transformX = invDet * ($dirY * spriteX - $dirX * spriteY);
//this is actually the depth inside the screen, that what Z is in 3D, the distance of sprite to player, matching sqrt(spriteDistance[i])
double transformY = invDet * (-planeY * spriteX + planeX * spriteY);
double transformY = invDet * (-$planeY * spriteX + $planeX * spriteY);
int spriteScreenX = int(($width / 2) * (1 + transformX / transformY));
@ -136,11 +136,13 @@ void Raycaster::sprite_casting() {
int texY = ((d * textureHeight) / spriteHeight) / 256;
//get current color from the texture
// BUG: this crashes sometimes when the math goes out of bounds
uint32_t color = sprite_texture[textureWidth * texY + texX];
size_t index = textureWidth * texY + texX;
if(index < 0 || index >= sprite_texture.size()) continue;
uint32_t color = sprite_texture[index];
// poor person's transparency, get current color from the texture
if((color & 0x00FFFFFF) != 0) {
RGBA pixel = color;
pixels[pixcoord(stripe, y)] = pixel;
$pixels[pixcoord(stripe, y)] = pixel;
}
}
}
@ -149,18 +151,20 @@ void Raycaster::sprite_casting() {
}
void Raycaster::cast_rays() {
constexpr static const int textureWidth = $textures.TEXTURE_WIDTH;
constexpr static const int textureHeight = $textures.TEXTURE_HEIGHT;
double perpWallDist;
// WALL CASTING
for(int x = 0; x < $width; x++) {
// calculate ray position and direction
double cameraX = 2 * x / double($width) - 1; // x-coord in camera space
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
double rayDirX = $dirX + $planeX * cameraX;
double rayDirY = $dirY + $planeY * cameraX;
// which box of the map we're in
int mapX = int(posX);
int mapY = int(posY);
int mapX = int($posX);
int mapY = int($posY);
// length of ray from current pos to next x or y-side
double sideDistX;
@ -178,18 +182,18 @@ void Raycaster::cast_rays() {
// calculate step and initial sideDist
if(rayDirX < 0) {
stepX = -1;
sideDistX = (posX - mapX) * deltaDistX;
sideDistX = ($posX - mapX) * deltaDistX;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
sideDistX = (mapX + 1.0 - $posX) * deltaDistX;
}
if(rayDirY < 0) {
stepY = -1;
sideDistY = (posY - mapY) * deltaDistY;
sideDistY = ($posY - mapY) * deltaDistY;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
sideDistY = (mapY + 1.0 - $posY) * deltaDistY;
}
// perform DDA
@ -215,40 +219,40 @@ void Raycaster::cast_rays() {
int lineHeight = int($height / perpWallDist);
int drawStart = -lineHeight / 2 + $height / 2 + PITCH;
int drawStart = -lineHeight / 2 + $height / 2 + $pitch;
if(drawStart < 0) drawStart = 0;
int drawEnd = lineHeight / 2 + $height / 2 + PITCH;
int drawEnd = lineHeight / 2 + $height / 2 + $pitch;
if(drawEnd >= $height) drawEnd = $height - 1;
auto &texture = textures.get_texture($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
if(side == 0) {
wallX = posY + perpWallDist * rayDirY;
wallX = $posY + perpWallDist * rayDirY;
} else {
wallX = posX + perpWallDist * rayDirX;
wallX = $posX + perpWallDist * rayDirX;
}
wallX -= floor((wallX));
// x coorindate on the texture
int texX = int(wallX * double(textures.TEXTURE_WIDTH));
if(side == 0 && rayDirX > 0) texX = textures.TEXTURE_WIDTH - texX - 1;
if(side == 1 && rayDirY < 0) texX = textures.TEXTURE_WIDTH - texX - 1;
int texX = int(wallX * double(textureWidth));
if(side == 0 && rayDirX > 0) texX = textureWidth - texX - 1;
if(side == 1 && rayDirY < 0) texX = textureWidth - texX - 1;
// LODE: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster
// How much to increase the texture coordinate per screen pixel
double step = 1.0 * textures.TEXTURE_HEIGHT / lineHeight;
double step = 1.0 * textureHeight / lineHeight;
// Starting texture coordinate
double texPos = (drawStart - PITCH - $height / 2 + lineHeight / 2) * step;
double texPos = (drawStart - $pitch - $height / 2 + lineHeight / 2) * step;
for(int y = drawStart; y < drawEnd; y++) {
int texY = (int)texPos & (textures.TEXTURE_HEIGHT - 1);
int texY = (int)texPos & (textureHeight - 1);
texPos += step;
RGBA pixel = texture[textures.TEXTURE_HEIGHT * texY + texX];
pixels[pixcoord(x, y)] = dumb_lighting(pixel, perpWallDist);
RGBA pixel = texture[textureHeight * texY + texX];
$pixels[pixcoord(x, y)] = dumb_lighting(pixel, perpWallDist);
}
// SET THE ZBUFFER FOR THE SPRITE CASTING
@ -257,15 +261,15 @@ void Raycaster::cast_rays() {
}
void Raycaster::draw_ceiling_floor() {
const int textureWidth = textures.TEXTURE_WIDTH;
const int textureHeight = textures.TEXTURE_HEIGHT;
constexpr static const int textureWidth = $textures.TEXTURE_WIDTH;
constexpr static const int textureHeight = $textures.TEXTURE_HEIGHT;
for(int y = $height / 2 + 1; y < $height; ++y) {
// rayDir for leftmost ray (x=0) and rightmost (x = w)
float rayDirX0 = dirX - planeX;
float rayDirY0 = dirY - planeY;
float rayDirX1 = dirX + planeX;
float rayDirY1 = dirY + planeY;
float rayDirX0 = $dirX - $planeX;
float rayDirY0 = $dirY - $planeY;
float rayDirX1 = $dirX + $planeX;
float rayDirY1 = $dirY + $planeY;
// current y position compared to the horizon
int p = y - $height / 2;
@ -289,8 +293,8 @@ void Raycaster::draw_ceiling_floor() {
// real world coordinates of the leftmost column.
// This will be updated as we step to the right
float floorX = posX + rowDistance * rayDirX0;
float floorY = posY + rowDistance * rayDirY0;
float floorX = $posX + rowDistance * rayDirX0;
float floorY = $posY + rowDistance * rayDirY0;
for(int x = 0; x < $width; ++x) {
// the cell coord is simply taken from the int parts of
@ -311,12 +315,12 @@ void Raycaster::draw_ceiling_floor() {
// floorX cellX to find the texture x/y. How?
// FLOOR
color = textures.floor[textureWidth * ty + tx];
pixels[pixcoord(x, y)] = color;
color = $textures.floor[textureWidth * ty + tx];
$pixels[pixcoord(x, y)] = color;
// CEILING
color = textures.ceiling[textureWidth * ty + tx];
pixels[pixcoord(x, $height - y - 1)] = color;
color = $textures.ceiling[textureWidth * ty + tx];
$pixels[pixcoord(x, $height - y - 1)] = color;
}
}
}
@ -358,22 +362,22 @@ void Raycaster::sort_sprites(std::vector<int>& order, std::vector<double>& dist,
void Raycaster::run(double speed, int dir) {
double speed_and_dir = speed * dir;
if(empty_space(int(posX + dirX * speed_and_dir), int(posY))) {
posX += dirX * speed_and_dir;
if(empty_space(int($posX + $dirX * speed_and_dir), int($posY))) {
$posX += $dirX * speed_and_dir;
}
if(empty_space(int(posX), int(posY + dirY * speed_and_dir))) {
posY += dirY * speed_and_dir;
if(empty_space(int($posX), int($posY + $dirY * speed_and_dir))) {
$posY += $dirY * speed_and_dir;
}
}
void Raycaster::rotate(double speed, int dir) {
double speed_and_dir = speed * dir;
double oldDirX = dirX;
dirX = dirX * cos(speed_and_dir) - dirY * sin(speed_and_dir);
dirY = oldDirX * sin(speed_and_dir) + dirY * cos(speed_and_dir);
double oldDirX = $dirX;
$dirX = $dirX * cos(speed_and_dir) - $dirY * sin(speed_and_dir);
$dirY = oldDirX * sin(speed_and_dir) + $dirY * cos(speed_and_dir);
double oldPlaneX = planeX;
planeX = planeX * cos(speed_and_dir) - planeY * sin(speed_and_dir);
planeY = oldPlaneX * sin(speed_and_dir) + planeY * cos(speed_and_dir);
double oldPlaneX = $planeX;
$planeX = $planeX * cos(speed_and_dir) - $planeY * sin(speed_and_dir);
$planeY = oldPlaneX * sin(speed_and_dir) + $planeY * cos(speed_and_dir);
}

@ -16,24 +16,23 @@ using matrix::Matrix;
using RGBA = uint32_t;
struct Raycaster {
int PITCH=0;
int $pitch=0;
TexturePack textures;
double posX = 0;
double posY = 0;
TexturePack $textures;
double $posX = 0;
double $posY = 0;
// initial direction vector
double dirX = -1;
double dirY = 0;
double $dirX = -1;
double $dirY = 0;
// the 2d raycaster version of camera plane
double planeX = 0;
double planeY = 0.66;
sf::Texture view_texture;
sf::Sprite view_sprite;
double $planeX = 0;
double $planeY = 0.66;
sf::Texture $view_texture;
sf::Sprite $view_sprite;
//ZED: USE smart pointer for this
std::unique_ptr<RGBA[]> pixels = nullptr;
std::unique_ptr<RGBA[]> $pixels = nullptr;
int $width;
int $height;
@ -61,6 +60,14 @@ struct Raycaster {
void set_position(int x, int y);
inline size_t pixcoord(int x, int y) {
if(!(x >=0 && x < $width)) {
dbc::sentinel(fmt::format("pixcoord x={} but $width={}", x, $width));
}
if(!(y >= 0 && y < $height)) {
dbc::sentinel(fmt::format("pixcoord y={} but $height={}", y, $height));
}
return ((y) * $width) + (x);
}

@ -18,8 +18,8 @@ using Image = std::vector<uint32_t>;
struct TexturePack {
int NUM_SPRITES=1;
static const int TEXTURE_WIDTH=256; // must be power of two
static const int TEXTURE_HEIGHT=256; // must be power of two
constexpr static const int TEXTURE_WIDTH=256; // must be power of two
constexpr static const int TEXTURE_HEIGHT=256; // must be power of two
std::vector<Image> images;
std::vector<Sprite> sprites{{4.0, 3.55, 6}};

Loading…
Cancel
Save