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.
314 lines
8.2 KiB
314 lines
8.2 KiB
#include <fmt/core.h>
|
|
#include <numbers>
|
|
#include <cmath>
|
|
#include "matrix.hpp"
|
|
#include <cstdlib>
|
|
#include "fenster/fenster.h"
|
|
#include "dbc.hpp"
|
|
|
|
using matrix::Matrix;
|
|
using namespace fmt;
|
|
|
|
Matrix MAP{
|
|
{1,1,1,1,1,1,1,1,1},
|
|
{1,0,1,0,0,0,0,0,1},
|
|
{1,0,1,0,0,1,1,0,1},
|
|
{1,0,0,0,0,0,0,0,1},
|
|
{1,1,0,0,0,0,0,0,1},
|
|
{1,0,0,1,1,1,0,0,1},
|
|
{1,0,0,0,1,0,0,0,1},
|
|
{1,0,0,0,0,0,1,1,1},
|
|
{1,1,1,1,1,1,1,1,1}
|
|
};
|
|
|
|
const int SCREEN_HEIGHT=480;
|
|
const int SCREEN_WIDTH=SCREEN_HEIGHT * 2;
|
|
|
|
const int THREED_VIEW_WIDTH=480;
|
|
const int THREED_VIEW_HEIGHT=480;
|
|
const int MAP_SIZE=matrix::width(MAP);
|
|
const int TILE_SIZE=(SCREEN_WIDTH/2) / MAP_SIZE;
|
|
const float FOV = std::numbers::pi / 3.0;
|
|
const float HALF_FOV = FOV / 2;
|
|
const int CASTED_RAYS=120;
|
|
const float STEP_ANGLE = FOV / CASTED_RAYS;
|
|
const int MAX_DEPTH = MAP_SIZE * TILE_SIZE;
|
|
const float SCALE = (SCREEN_WIDTH / 2) / CASTED_RAYS;
|
|
|
|
float player_x = SCREEN_WIDTH / 4;
|
|
float player_y = SCREEN_WIDTH / 4;
|
|
float player_angle = std::numbers::pi;
|
|
|
|
#define rgba_color(r,g,b,a) (b<<(0*8))|(g<<(1*8))|(r<<(2*8))|(a<<(3*8))
|
|
#define gray_color(c) rgba_color(c, c, c, 255)
|
|
|
|
void draw_rect(Fenster &window, Point pos, Point size, uint32_t color) {
|
|
size_t x_start = size_t(pos.x);
|
|
size_t y_start = size_t(pos.y);
|
|
size_t width = size_t(size.x);
|
|
size_t height = size_t(size.y);
|
|
|
|
dbc::check(x_start <= size_t(window.f.width), format("pos.x {} is greater than width {}", x_start, window.f.width));
|
|
dbc::check(y_start <= size_t(window.f.height), format("pos.y {} is greater than height {}", y_start, window.f.height));
|
|
dbc::check(x_start + width <= size_t(window.f.width), format("size width {} is greater than width {}", x_start + width, window.f.width));
|
|
dbc::check(y_start + height <= size_t(window.f.height), format("size height {} is greater than height {}", y_start + height, window.f.height));
|
|
|
|
for(size_t y = y_start; y < y_start + height; y++) {
|
|
for(size_t x = x_start; x < x_start + width; x++) {
|
|
window.px(x, y) = color;
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw_map_rect(Fenster &window, int x, int y, uint32_t color) {
|
|
draw_rect(window,
|
|
{size_t(x * TILE_SIZE), size_t(y * TILE_SIZE)},
|
|
{size_t(TILE_SIZE-1), size_t(TILE_SIZE-1)},
|
|
color);
|
|
}
|
|
|
|
void draw_map(Fenster &window, Matrix &map) {
|
|
uint32_t light_grey = gray_color(191);
|
|
uint32_t dark_grey = gray_color(65);
|
|
|
|
for(size_t y = 0; y < matrix::height(map); y++) {
|
|
for(size_t x = 0; x < matrix::width(map); x++) {
|
|
draw_map_rect(window, x, y, map[y][x] == 1 ? light_grey : dark_grey);
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw_line(Fenster &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;
|
|
}
|
|
|
|
window.px(x, y) = color;
|
|
}
|
|
}
|
|
|
|
void clear(Fenster &window) {
|
|
for(int y = 0; y < window.f.height; y++) {
|
|
for(int x = 0; x < window.f.width; x++) {
|
|
window.px(x, y) = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw_map_rays(Fenster &window, int col, int row, Point target) {
|
|
draw_map_rect(window, col, row, rgba_color(100, 20, 20, 255));
|
|
draw_line(window, {size_t(player_x), size_t(player_y)}, target, rgba_color(200, 20, 20, 255));
|
|
}
|
|
|
|
void ray_casting(Fenster &window, Matrix& map) {
|
|
int w = THREED_VIEW_WIDTH;
|
|
int h = THREED_VIEW_HEIGHT;
|
|
|
|
// x and y start position
|
|
double posX = player_x / TILE_SIZE;
|
|
double posY = player_y / TILE_SIZE;
|
|
|
|
// initial direction vector
|
|
double dirX = std::cos(player_angle);
|
|
double dirY = 0;
|
|
|
|
// the 2d raycaster version of camera plane
|
|
double planeX = 0;
|
|
double planeY = 0.66;
|
|
|
|
// time of current frame
|
|
double time = 0;
|
|
// time of previous frame
|
|
double oldTime = 0;
|
|
|
|
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 = (rayDirX == 0) ? 1e30 : std::abs(1.0 / rayDirX);
|
|
double deltaDistY = (rayDirY == 0) ? 1e30 : std::abs(1.0 / rayDirY);
|
|
double perpWallDist;
|
|
|
|
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;
|
|
if(drawStart < 0) drawStart = 0;
|
|
|
|
int drawEnd = lineHeight / 2 + h / 2;
|
|
if(drawEnd >= h) drawEnd = h - 1;
|
|
|
|
uint32_t color = gray_color(std::min(lineHeight, 255));
|
|
|
|
draw_line(window,
|
|
{size_t(x + THREED_VIEW_WIDTH), size_t(drawStart)},
|
|
{size_t(x + THREED_VIEW_WIDTH), size_t(drawEnd)}, color);
|
|
}
|
|
}
|
|
|
|
|
|
void map_view_casting(Fenster &window, Matrix& map) {
|
|
float start_angle = player_angle - HALF_FOV;
|
|
|
|
for(int ray = 0; ray < CASTED_RAYS; ray++, start_angle += STEP_ANGLE)
|
|
{
|
|
for(int depth = 1; depth < MAX_DEPTH; depth++) {
|
|
float target_x = player_x - std::sin(start_angle) * depth;
|
|
float target_y = player_y + std::cos(start_angle) * depth;
|
|
|
|
int col = int(target_x / TILE_SIZE);
|
|
int row = int(target_y / TILE_SIZE);
|
|
|
|
if(map[row][col] == 1) {
|
|
draw_map_rays(window, col, row, {size_t(target_x), size_t(target_y)});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void draw_ceiling_floor(Fenster &window) {
|
|
draw_rect(window,
|
|
{size_t(window.width() / 2), 0},
|
|
{size_t(window.height()), size_t(window.height() / 2)},
|
|
gray_color(100));
|
|
draw_rect(window,
|
|
{size_t(window.width() / 2), size_t(window.height() / 2)},
|
|
{size_t(window.width() / 2), size_t(window.height() / 2)},
|
|
gray_color(200));
|
|
}
|
|
|
|
void draw_everything(Fenster &window) {
|
|
clear(window);
|
|
draw_map(window, MAP);
|
|
draw_ceiling_floor(window);
|
|
map_view_casting(window, MAP);
|
|
ray_casting(window, MAP);
|
|
}
|
|
|
|
bool collision(float x, float y) {
|
|
int col = int(x / TILE_SIZE);
|
|
int row = int(y / TILE_SIZE);
|
|
|
|
return MAP[row][col] == 1;
|
|
}
|
|
|
|
int main() {
|
|
Fenster window(SCREEN_WIDTH, SCREEN_HEIGHT, "Fenscaster");
|
|
|
|
while(window.loop(60)) {
|
|
draw_everything(window);
|
|
|
|
float x = player_x;
|
|
float y = player_y;
|
|
|
|
if(window.key('Q')) {
|
|
player_angle -= 0.1;
|
|
} else if(window.key('E')) {
|
|
player_angle += 0.1;
|
|
}
|
|
|
|
if(window.key('W')) {
|
|
x += -1 * std::sin(player_angle) * 5;
|
|
y += std::cos(player_angle) * 5;
|
|
} else if(window.key('S')) {
|
|
x -= -1 * std::sin(player_angle) * 5;
|
|
y -= std::cos(player_angle) * 5;
|
|
}
|
|
|
|
if(window.key('D')) {
|
|
x += -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
|
|
y += std::cos(player_angle + std::numbers::pi * 0.5) * 5;
|
|
} else if(window.key('A')) {
|
|
x -= -1 * std::sin(player_angle + std::numbers::pi * 0.5) * 5;
|
|
y -= std::cos(player_angle + std::numbers::pi * 0.5) * 5;
|
|
}
|
|
|
|
if(!collision(x, y)) {
|
|
player_x = x;
|
|
player_y = y;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
|
|
int nCmdShow) {
|
|
(void)hInstance, (void)hPrevInstance, (void)pCmdLine, (void)nCmdShow;
|
|
return main();
|
|
}
|
|
#endif
|
|
|