This converts it to SFML 3.0 but the build only runs on Windows at the moment.
This commit is contained in:
parent
cdbd83ded7
commit
105c974f1c
7 changed files with 62 additions and 630 deletions
34
main.cpp
34
main.cpp
|
@ -24,10 +24,15 @@ Matrix MAP{
|
|||
{1,1,1,1,1,1,1,1,1}
|
||||
};
|
||||
|
||||
int main() {
|
||||
using KB = sf::Keyboard;
|
||||
void draw_gui(sf::RenderWindow &window) {
|
||||
sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, 300});
|
||||
rect.setPosition({0,0});
|
||||
rect.setFillColor({100, 100, 100});
|
||||
window.draw(rect);
|
||||
}
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing");
|
||||
int main() {
|
||||
sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing");
|
||||
|
||||
//ZED this should set with a function
|
||||
float player_x = matrix::width(MAP) / 2;
|
||||
|
@ -40,30 +45,29 @@ int main() {
|
|||
double moveSpeed = 0.1;
|
||||
double rotSpeed = 0.1;
|
||||
|
||||
const auto onClose = [&window](const sf::Event::Closed&)
|
||||
{
|
||||
window.close();
|
||||
};
|
||||
|
||||
while(window.isOpen()) {
|
||||
rayview.render();
|
||||
|
||||
// DRAW GUI
|
||||
draw_gui(window);
|
||||
window.display();
|
||||
|
||||
if(KB::isKeyPressed(KB::W)) {
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
|
||||
rayview.run(moveSpeed, 1);
|
||||
} else if(KB::isKeyPressed(KB::S)) {
|
||||
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
|
||||
rayview.run(moveSpeed, -1);
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::D)) {
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
|
||||
rayview.rotate(rotSpeed, -1);
|
||||
} else if(KB::isKeyPressed(KB::A)) {
|
||||
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
|
||||
rayview.rotate(rotSpeed, 1);
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while(window.pollEvent(event)) {
|
||||
if(event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
window.handleEvents(onClose);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
43
meson.build
43
meson.build
|
@ -1,12 +1,27 @@
|
|||
project('raycaster', 'cpp',
|
||||
default_options: ['cpp_std=c++20'])
|
||||
|
||||
cc = meson.get_compiler('cpp')
|
||||
|
||||
catch2 = dependency('catch2-with-main')
|
||||
fmt = dependency('fmt')
|
||||
json = dependency('nlohmann_json')
|
||||
sfml = dependency('sfml')
|
||||
opengl32 = cc.find_library('opengl32', required: true)
|
||||
winmm = cc.find_library('winmm', required: true)
|
||||
gdi32 = cc.find_library('gdi32', required: true)
|
||||
sfml_audio = dependency('sfml_audio')
|
||||
sfml_graphics = dependency('sfml_graphics')
|
||||
sfml_main = dependency('sfml_main')
|
||||
sfml_network = dependency('sfml_network')
|
||||
sfml_system = dependency('sfml_system')
|
||||
sfml_window = dependency('sfml_window')
|
||||
|
||||
dependencies = [fmt, json, sfml]
|
||||
dependencies = [
|
||||
fmt, json, opengl32,
|
||||
winmm, gdi32, sfml_audio, sfml_graphics,
|
||||
sfml_main, sfml_network, sfml_system,
|
||||
sfml_window
|
||||
]
|
||||
|
||||
executable('runtests', [
|
||||
'dbc.cpp',
|
||||
|
@ -15,12 +30,6 @@ executable('runtests', [
|
|||
],
|
||||
dependencies: dependencies + [catch2])
|
||||
|
||||
executable('sfmlcaster', [
|
||||
'dbc.cpp',
|
||||
'sfmlcaster.cpp'
|
||||
],
|
||||
dependencies: dependencies)
|
||||
|
||||
executable('zedcaster', [
|
||||
'dbc.cpp',
|
||||
'matrix.cpp',
|
||||
|
@ -31,12 +40,12 @@ executable('zedcaster', [
|
|||
],
|
||||
dependencies: dependencies)
|
||||
|
||||
executable('amtcaster', [
|
||||
'dbc.cpp',
|
||||
'config.cpp',
|
||||
'amt/texture.cpp',
|
||||
'amt/raycaster.cpp',
|
||||
'amt/main.cpp'
|
||||
],
|
||||
cpp_args: ['-std=c++23'],
|
||||
dependencies: dependencies)
|
||||
# executable('amtcaster', [
|
||||
# 'dbc.cpp',
|
||||
# 'config.cpp',
|
||||
# 'amt/texture.cpp',
|
||||
# 'amt/raycaster.cpp',
|
||||
# 'amt/main.cpp'
|
||||
# ],
|
||||
# cpp_args: ['-std=c++23'],
|
||||
# dependencies: dependencies)
|
||||
|
|
|
@ -28,6 +28,8 @@ 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),
|
||||
$width(width), $height(height),
|
||||
$window(window),
|
||||
$map(map),
|
||||
|
@ -36,15 +38,13 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh
|
|||
ZBuffer(width)
|
||||
{
|
||||
$window.setVerticalSyncEnabled(true);
|
||||
view_texture.create($width, $height);
|
||||
view_sprite.setTexture(view_texture);
|
||||
view_sprite.setPosition(0, 0);
|
||||
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(x, y);
|
||||
view_sprite.setPosition({(float)x, (float)y});
|
||||
}
|
||||
|
||||
void Raycaster::position_camera(float player_x, float player_y) {
|
||||
|
@ -54,8 +54,7 @@ void Raycaster::position_camera(float player_x, float player_y) {
|
|||
}
|
||||
|
||||
void Raycaster::draw_pixel_buffer() {
|
||||
view_texture.update((uint8_t *)pixels.get(), $width, $height, 0, 0);
|
||||
// BUG: can I do this once and just update it?
|
||||
view_texture.update((uint8_t *)pixels.get(), {(unsigned int)$width, (unsigned int)$height}, {0, 0});
|
||||
$window.draw(view_sprite);
|
||||
}
|
||||
|
||||
|
|
553
sfmlcaster.cpp
553
sfmlcaster.cpp
|
@ -1,553 +0,0 @@
|
|||
#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 "dbc.hpp"
|
||||
|
||||
using matrix::Matrix;
|
||||
using namespace fmt;
|
||||
|
||||
#define texWidth 256 // must be power of two
|
||||
#define texHeight 256 // must be power of two
|
||||
|
||||
#define numSprites 1
|
||||
#define numTextures 11
|
||||
|
||||
struct Sprite {
|
||||
double x;
|
||||
double y;
|
||||
double elevation;
|
||||
int texture;
|
||||
};
|
||||
|
||||
//parameters for scaling and moving the sprites
|
||||
#define uDiv 1
|
||||
#define vDiv 1
|
||||
|
||||
const int RAY_VIEW_WIDTH=960;
|
||||
const int RAY_VIEW_HEIGHT=720;
|
||||
const int RAY_VIEW_X=1280 - RAY_VIEW_WIDTH;
|
||||
const int RAY_VIEW_Y=0;
|
||||
|
||||
const int SCREEN_HEIGHT=RAY_VIEW_HEIGHT;
|
||||
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}
|
||||
};
|
||||
|
||||
const int MAP_SIZE=matrix::width(MAP);
|
||||
const int TILE_SIZE=RAY_VIEW_HEIGHT / MAP_SIZE;
|
||||
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 = player_x / TILE_SIZE;
|
||||
double posY = player_y / TILE_SIZE;
|
||||
|
||||
// initial direction vector
|
||||
double dirX = -1;
|
||||
double dirY = 0;
|
||||
|
||||
// the 2d raycaster version of camera plane
|
||||
double planeX = 0;
|
||||
double planeY = 0.66;
|
||||
|
||||
#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)
|
||||
|
||||
Sprite SPRITE[numSprites] = {
|
||||
{4.0, 3.55, 0, 8},
|
||||
// {3.4, 1.95, 9},
|
||||
// {7.34, 5.5, 10}
|
||||
};
|
||||
|
||||
double ZBuffer[RAY_VIEW_WIDTH];
|
||||
int spriteOrder[numSprites];
|
||||
double spriteDistance[numSprites];
|
||||
|
||||
std::vector<uint32_t> texture[numTextures];
|
||||
|
||||
union RGBA {
|
||||
struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t a;
|
||||
} color;
|
||||
|
||||
uint32_t out;
|
||||
};
|
||||
|
||||
inline void RGBA_brightness(RGBA& pixel, double distance) {
|
||||
pixel.color.r /= distance;
|
||||
pixel.color.g /= distance;
|
||||
pixel.color.b /= distance;
|
||||
}
|
||||
|
||||
#define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X)
|
||||
RGBA pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {{.out=0}};
|
||||
|
||||
sf::Texture view_texture;
|
||||
sf::Sprite view_sprite;
|
||||
|
||||
void sortSprites(int *order, double *dist, int amount);
|
||||
|
||||
void loadImage(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 load_textures() {
|
||||
for(int i = 0; i < numTextures; i++) {
|
||||
texture[i].resize(texWidth * texHeight);
|
||||
}
|
||||
|
||||
loadImage(texture[0], "assets/tile16.png");
|
||||
loadImage(texture[1], "assets/tile02.png");
|
||||
loadImage(texture[2], "assets/tile03.png");
|
||||
loadImage(texture[3], "assets/tile32.png");
|
||||
loadImage(texture[4], "assets/tile05.png");
|
||||
loadImage(texture[5], "assets/tile17.png");
|
||||
loadImage(texture[6], "assets/tile10.png");
|
||||
loadImage(texture[7], "assets/tile01.png");
|
||||
loadImage(texture[8], "assets/portal.png");
|
||||
}
|
||||
|
||||
void draw_sfml_rect(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, uint8_t color) {
|
||||
sf::RectangleShape rect(size);
|
||||
rect.setFillColor({color, color, color});
|
||||
rect.setPosition(pos);
|
||||
window.draw(rect);
|
||||
}
|
||||
|
||||
void draw_pixel_buffer(sf::RenderWindow &window) {
|
||||
view_texture.update((uint8_t *)pixels, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT, 0, 0);
|
||||
// BUG: can I do this once and just update it?
|
||||
window.draw(view_sprite);
|
||||
}
|
||||
|
||||
void draw_gui(sf::RenderWindow &window, Matrix &map) {
|
||||
draw_sfml_rect(window,
|
||||
{0.0, 0.0},
|
||||
{SCREEN_WIDTH-RAY_VIEW_WIDTH, SCREEN_HEIGHT / 2},
|
||||
100);
|
||||
|
||||
draw_sfml_rect(window,
|
||||
{0.0, SCREEN_HEIGHT / 2},
|
||||
{SCREEN_WIDTH-RAY_VIEW_WIDTH, SCREEN_HEIGHT / 2},
|
||||
150);
|
||||
}
|
||||
|
||||
void draw_line(sf::RenderWindow &window, Point start, Point end, uint32_t color) {
|
||||
int x = int(start.x);
|
||||
int y = int(start.y);
|
||||
int x1 = int(end.x);
|
||||
int y1 = int(end.y);
|
||||
int dx = std::abs(x1 - x);
|
||||
int sx = x < x1 ? 1 : -1;
|
||||
int dy = std::abs(y1 - y) * -1;
|
||||
int sy = y < y1 ? 1 : -1;
|
||||
int error = dx + dy;
|
||||
|
||||
while(x != x1 || y != y1) {
|
||||
int e2 = 2 * error;
|
||||
|
||||
if(e2 >= dy) {
|
||||
error = error + dy;
|
||||
x = x + sx;
|
||||
}
|
||||
|
||||
if(e2 <= dx) {
|
||||
error = error + dx;
|
||||
y = y + sy;
|
||||
}
|
||||
|
||||
pixels[pixcoord(x,y)].out = color;
|
||||
}
|
||||
}
|
||||
|
||||
void clear(sf::RenderWindow &window) {
|
||||
// std::fill_n((uint32_t *)pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0);
|
||||
// window.clear();
|
||||
}
|
||||
|
||||
void ray_casting(sf::RenderWindow &window, Matrix& map) {
|
||||
int w = RAY_VIEW_WIDTH;
|
||||
int h = RAY_VIEW_HEIGHT;
|
||||
double perpWallDist;
|
||||
|
||||
// WALL CASTING
|
||||
for(int x = 0; x < w; x++) {
|
||||
// calculate ray position and direction
|
||||
double cameraX = 2 * x / double(w) - 1; // x-coord in camera space
|
||||
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);
|
||||
|
||||
// length of ray from current pos to next x or y-side
|
||||
double sideDistX;
|
||||
double sideDistY;
|
||||
|
||||
// length of ray from one x or y-side to next x or y-side
|
||||
double deltaDistX = std::abs(1.0 / rayDirX);
|
||||
double deltaDistY = std::abs(1.0 / rayDirY);
|
||||
|
||||
int stepX = 0;
|
||||
int stepY = 0;
|
||||
int hit = 0;
|
||||
int side = 0;
|
||||
|
||||
// calculate step and initial sideDist
|
||||
if(rayDirX < 0) {
|
||||
stepX = -1;
|
||||
sideDistX = (posX - mapX) * deltaDistX;
|
||||
} else {
|
||||
stepX = 1;
|
||||
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
|
||||
}
|
||||
|
||||
if(rayDirY < 0) {
|
||||
stepY = -1;
|
||||
sideDistY = (posY - mapY) * deltaDistY;
|
||||
} else {
|
||||
stepY = 1;
|
||||
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
|
||||
}
|
||||
|
||||
// perform DDA
|
||||
while(hit == 0) {
|
||||
if(sideDistX < sideDistY) {
|
||||
sideDistX += deltaDistX;
|
||||
mapX += stepX;
|
||||
side = 0;
|
||||
} else {
|
||||
sideDistY += deltaDistY;
|
||||
mapY += stepY;
|
||||
side = 1;
|
||||
}
|
||||
|
||||
if(map[mapY][mapX] > 0) hit = 1;
|
||||
}
|
||||
|
||||
if(side == 0) {
|
||||
perpWallDist = (sideDistX - deltaDistX);
|
||||
} else {
|
||||
perpWallDist = (sideDistY - deltaDistY);
|
||||
}
|
||||
|
||||
int lineHeight = int(h / perpWallDist);
|
||||
|
||||
int drawStart = -lineHeight / 2 + h / 2 + PITCH;
|
||||
if(drawStart < 0) drawStart = 0;
|
||||
|
||||
int drawEnd = lineHeight / 2 + h / 2 + PITCH;
|
||||
if(drawEnd >= h) drawEnd = h - 1;
|
||||
|
||||
int texNum = MAP[mapY][mapX] - 1;
|
||||
|
||||
// calculate value of wallX
|
||||
double wallX; // where exactly the wall was hit
|
||||
if(side == 0) {
|
||||
wallX = posY + perpWallDist * rayDirY;
|
||||
} else {
|
||||
wallX = posX + perpWallDist * rayDirX;
|
||||
}
|
||||
wallX -= floor((wallX));
|
||||
|
||||
// x coorindate on the texture
|
||||
int texX = int(wallX * double(texWidth));
|
||||
if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
|
||||
if(side == 1 && rayDirY < 0) texX = texWidth - 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 * texHeight / lineHeight;
|
||||
// Starting texture coordinate
|
||||
double texPos = (drawStart - PITCH - h / 2 + lineHeight / 2) * step;
|
||||
|
||||
for(int y = drawStart; y < drawEnd; y++) {
|
||||
int texY = (int)texPos & (texHeight - 1);
|
||||
texPos += step;
|
||||
RGBA pixel{.out=texture[texNum][texHeight * texY + texX]};
|
||||
RGBA_brightness(pixel, perpWallDist);
|
||||
pixels[pixcoord(x, y)] = pixel;
|
||||
}
|
||||
|
||||
// SET THE ZBUFFER FOR THE SPRITE CASTING
|
||||
ZBuffer[x] = perpWallDist;
|
||||
}
|
||||
|
||||
// SPRITE CASTING
|
||||
// sort sprites from far to close
|
||||
for(int i = 0; i < numSprites; i++) {
|
||||
spriteOrder[i] = i;
|
||||
// this is just the distance calculation
|
||||
spriteDistance[i] = ((posX - SPRITE[i].x) *
|
||||
(posX - SPRITE[i].x) +
|
||||
(posY - SPRITE[i].y) *
|
||||
(posY - SPRITE[i].y));
|
||||
}
|
||||
|
||||
sortSprites(spriteOrder, spriteDistance, numSprites);
|
||||
|
||||
// after sorting the sprites, do the projection
|
||||
for(int i = 0; i < numSprites; i++) {
|
||||
int sprite_index = spriteOrder[i];
|
||||
Sprite& sprite_rec = SPRITE[sprite_index];
|
||||
double spriteX = sprite_rec.x - posX;
|
||||
double spriteY = sprite_rec.y - posY;
|
||||
int sprite_texture_number = sprite_rec.texture;
|
||||
auto sprite_texture = texture[sprite_texture_number];
|
||||
|
||||
//transform sprite with the inverse camera matrix
|
||||
// [ 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 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);
|
||||
|
||||
int spriteScreenX = int((w / 2) * (1 + transformX / transformY));
|
||||
|
||||
int vMoveScreen = int(sprite_rec.elevation * -1 / transformY);
|
||||
|
||||
// calculate the height of the sprite on screen
|
||||
//using "transformY" instead of the real distance prevents fisheye
|
||||
int spriteHeight = abs(int(h / transformY)) / vDiv;
|
||||
|
||||
//calculate lowest and highest pixel to fill in current stripe
|
||||
int drawStartY = -spriteHeight / 2 + h / 2 + vMoveScreen;
|
||||
if(drawStartY < 0) drawStartY = 0;
|
||||
int drawEndY = spriteHeight / 2 + h / 2 + vMoveScreen;
|
||||
if(drawEndY >= h) drawEndY = h - 1;
|
||||
|
||||
// calculate width the the sprite
|
||||
// same as height of sprite, given that it's square
|
||||
int spriteWidth = abs(int(h / transformY)) / uDiv;
|
||||
int drawStartX = -spriteWidth / 2 + spriteScreenX;
|
||||
if(drawStartX < 0) drawStartX = 0;
|
||||
int drawEndX = spriteWidth / 2 + spriteScreenX;
|
||||
if(drawEndX > w) drawEndX = w;
|
||||
|
||||
//loop through every vertical stripe of the sprite on screen
|
||||
for(int stripe = drawStartX; stripe < drawEndX; stripe++) {
|
||||
int texX = int(256 * (stripe - (-spriteWidth / 2 + spriteScreenX)) * texWidth / spriteWidth) / 256;
|
||||
// the conditions in the if are:
|
||||
// 1) it's in front of the camera plane so you don't see things behind you
|
||||
// 2) ZBuffer, with perpendicular distance
|
||||
if(transformY > 0 && transformY < ZBuffer[stripe]) {
|
||||
for(int y = drawStartY; y < drawEndY; y++) {
|
||||
//256 and 128 factors to avoid floats
|
||||
int d = (y - vMoveScreen) * 256 - h * 128 + spriteHeight * 128;
|
||||
int texY = ((d * texHeight) / spriteHeight) / 256;
|
||||
//get current color from the texture
|
||||
uint32_t color = sprite_texture[texWidth * texY + texX];
|
||||
// poor person's transparency, get current color from the texture
|
||||
if((color & 0x00FFFFFF) != 0) {
|
||||
RGBA pixel{.out=color};
|
||||
RGBA_brightness(pixel, perpWallDist);
|
||||
pixels[pixcoord(stripe, y)] = pixel;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_ceiling_floor(sf::RenderWindow &window) {
|
||||
int screenHeight = RAY_VIEW_HEIGHT;
|
||||
int screenWidth = RAY_VIEW_WIDTH;
|
||||
|
||||
for(int y = screenHeight / 2 + 1; y < screenHeight; ++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;
|
||||
|
||||
// current y position compared to the horizon
|
||||
int p = y - screenHeight / 2;
|
||||
|
||||
// vertical position of the camera
|
||||
// 0.5 will the camera at the center horizon. For a
|
||||
// different value you need a separate loop for ceiling
|
||||
// and floor since they're no longer symmetrical.
|
||||
float posZ = 0.5 * screenHeight;
|
||||
|
||||
// horizontal distance from the camera to the floor for the current row
|
||||
// 0.5 is the z position exactly in the middle between floor and ceiling
|
||||
// See NOTE in Lode's code for more.
|
||||
float rowDistance = posZ / p;
|
||||
|
||||
// calculate the real world step vector we have to add for each x (parallel to camera plane)
|
||||
// adding step by step avoids multiplications with a wight in the inner loop
|
||||
float floorStepX = rowDistance * (rayDirX1 - rayDirX0) / screenWidth;
|
||||
float floorStepY = rowDistance * (rayDirY1 - rayDirY0) / screenWidth;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
for(int x = 0; x < screenWidth; ++x) {
|
||||
// the cell coord is simply taken from the int parts of
|
||||
// floorX and floorY.
|
||||
int cellX = int(floorX);
|
||||
int cellY = int(floorY);
|
||||
|
||||
// get the texture coordinat from the fractional part
|
||||
int tx = int(texWidth * (floorX - cellX)) & (texWidth - 1);
|
||||
int ty = int(texWidth * (floorY - cellY)) & (texHeight - 1);
|
||||
|
||||
floorX += floorStepX;
|
||||
floorY += floorStepY;
|
||||
|
||||
// now get the pixel from the texture
|
||||
uint32_t color;
|
||||
// this uses the previous ty/tx fractional parts of
|
||||
// floorX cellX to find the texture x/y. How?
|
||||
|
||||
// FLOOR
|
||||
color = texture[floorTexture][texWidth * ty + tx];
|
||||
pixels[pixcoord(x, y)].out = color;
|
||||
|
||||
// CEILING
|
||||
color = texture[ceilingTexture][texWidth * ty + tx];
|
||||
pixels[pixcoord(x, screenHeight - y - 1)].out = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_everything(sf::RenderWindow &window) {
|
||||
clear(window);
|
||||
draw_gui(window, MAP);
|
||||
draw_ceiling_floor(window);
|
||||
ray_casting(window, MAP);
|
||||
draw_pixel_buffer(window);
|
||||
window.display();
|
||||
}
|
||||
|
||||
bool empty_space(int new_x, int new_y) {
|
||||
dbc::check((size_t)new_x < matrix::width(MAP),
|
||||
format("x={} too wide={}", new_x, matrix::width(MAP)));
|
||||
dbc::check((size_t)new_y < matrix::height(MAP),
|
||||
format("y={} too high={}", new_y, matrix::height(MAP)));
|
||||
|
||||
return MAP[new_y][new_x] == 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
using KB = sf::Keyboard;
|
||||
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFMLCaster");
|
||||
|
||||
window.setVerticalSyncEnabled(true);
|
||||
view_texture.create(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
|
||||
view_sprite.setTexture(view_texture);
|
||||
view_sprite.setPosition(RAY_VIEW_X, 0);
|
||||
|
||||
load_textures();
|
||||
|
||||
double moveSpeed = 0.1;
|
||||
double rotSpeed = 0.1;
|
||||
|
||||
while(window.isOpen()) {
|
||||
draw_everything(window);
|
||||
|
||||
if(KB::isKeyPressed(KB::X)) {
|
||||
println("player position: {},{}",
|
||||
posX, posY);
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::W)) {
|
||||
if(empty_space(int(posX + dirX * moveSpeed), int(posY))) posX += dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY + dirY * moveSpeed))) posY += dirY * moveSpeed;
|
||||
} else if(KB::isKeyPressed(KB::S)) {
|
||||
if(empty_space(int(posX - dirX * moveSpeed), int(posY))) posX -= dirX * moveSpeed;
|
||||
if(empty_space(int(posX), int(posY - dirY * moveSpeed))) posY -= dirY * moveSpeed;
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::D)) {
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||
} else if(KB::isKeyPressed(KB::A)) {
|
||||
double oldDirX = dirX;
|
||||
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
||||
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
||||
|
||||
double oldPlaneX = planeX;
|
||||
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||
}
|
||||
|
||||
if(KB::isKeyPressed(KB::E)) {
|
||||
println("PITCH DISABLED");
|
||||
// PITCH = std::clamp(PITCH + 10, -60, 240);
|
||||
} else if(KB::isKeyPressed(KB::Q)) {
|
||||
println("PITCH DISABLED");
|
||||
// PITCH = std::clamp(PITCH - 10, -60, 240);
|
||||
}
|
||||
|
||||
sf::Event event;
|
||||
while(window.pollEvent(event)) {
|
||||
if(event.type == sf::Event::Closed) {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sortSprites(int* order, double* dist, int amount)
|
||||
{
|
||||
std::vector<std::pair<double, int>> sprites(amount);
|
||||
|
||||
for(int i = 0; i < amount; i++) {
|
||||
sprites[i].first = dist[i];
|
||||
sprites[i].second = order[i];
|
||||
}
|
||||
|
||||
std::sort(sprites.begin(), sprites.end());
|
||||
|
||||
// restore in reverse order
|
||||
for(int i = 0; i < amount; i++) {
|
||||
dist[i] = sprites[amount - i - 1].first;
|
||||
order[i] = sprites[amount - i - 1].second;
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
[wrap-file]
|
||||
directory = openal-soft-1.23.1
|
||||
source_url = https://github.com/kcat/openal-soft/archive/refs/tags/1.23.1.tar.gz
|
||||
source_filename = openal-soft-1.23.1.tar.gz
|
||||
source_hash = dfddf3a1f61059853c625b7bb03de8433b455f2f79f89548cbcbd5edca3d4a4a
|
||||
patch_filename = openal-soft_1.23.1-2_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/openal-soft_1.23.1-2/get_patch
|
||||
patch_hash = e03c3afe0bb40a931d25d41d92a08b90e3c33b217d1b47210b26ca6627eb3aa3
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/openal-soft_1.23.1-2/openal-soft-1.23.1.tar.gz
|
||||
wrapdb_version = 1.23.1-2
|
||||
|
||||
[provide]
|
||||
openal = openal_dep
|
|
@ -1,15 +0,0 @@
|
|||
[wrap-file]
|
||||
directory = SDL2-2.30.6
|
||||
source_url = https://github.com/libsdl-org/SDL/releases/download/release-2.30.6/SDL2-2.30.6.tar.gz
|
||||
source_filename = SDL2-2.30.6.tar.gz
|
||||
source_hash = c6ef64ca18a19d13df6eb22df9aff19fb0db65610a74cc81dae33a82235cacd4
|
||||
patch_filename = sdl2_2.30.6-2_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/sdl2_2.30.6-2/get_patch
|
||||
patch_hash = aa9f6a4947b07510c2ea84fb457e965bebe5a5deeb9f5059fbcf10dfe6b76d1f
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/sdl2_2.30.6-2/SDL2-2.30.6.tar.gz
|
||||
wrapdb_version = 2.30.6-2
|
||||
|
||||
[provide]
|
||||
sdl2 = sdl2_dep
|
||||
sdl2main = sdl2main_dep
|
||||
sdl2_test = sdl2_test_dep
|
|
@ -1,13 +1,14 @@
|
|||
[wrap-file]
|
||||
directory = SFML-2.6.2
|
||||
source_url = https://github.com/SFML/SFML/archive/refs/tags/2.6.2.tar.gz
|
||||
source_filename = 2.6.2.tar.gz
|
||||
source_hash = 15ff4d608a018f287c6a885db0a2da86ea389e516d2323629e4d4407a7ce047f
|
||||
patch_filename = sfml_2.6.2-1_patch.zip
|
||||
patch_url = https://wrapdb.mesonbuild.com/v2/sfml_2.6.2-1/get_patch
|
||||
patch_hash = 36737f7fc6d616be791c6901b15414315b3a77df82dabc80b151d628e5d48386
|
||||
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/sfml_2.6.2-1/2.6.2.tar.gz
|
||||
wrapdb_version = 2.6.2-1
|
||||
[wrap-git]
|
||||
directory=SFML-3.0.0
|
||||
url=https://github.com/SFML/SFML.git
|
||||
revision=3.0.0
|
||||
depth=1
|
||||
method=cmake
|
||||
|
||||
[provide]
|
||||
sfml = sfml_dep
|
||||
sfml_audio = sfml_audio_dep
|
||||
sfml_graphics = sfml_graphics_dep
|
||||
sfml_main = sfml_main_dep
|
||||
sfml_network = sfml_network_dep
|
||||
sfml_system = sfml_system_dep
|
||||
sfml_window = sfml_window_dep
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue