Exploring raycasters and possibly make a little "doom like" game based on it.
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.
 
 
 
 
 
 
raycaster/lel.cpp

82 lines
2.2 KiB

#include "lel.hpp"
#include <fmt/core.h>
#include "dbc.hpp"
#include <numeric>
#include "lel_parser.cpp"
namespace lel {
Parser::Parser(int x, int y, int width, int height) :
grid_x(x),
grid_y(y),
grid_w(width),
grid_h(height),
cur(0, 0)
{
}
void Parser::id(std::string name) {
if(name != "_") {
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
cells.insert_or_assign(name, cur);
}
cur = {cur.col + 1, cur.row};
}
void Parser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
int cell_width = grid_w / columns;
int cell_height = grid_h / rows;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
for(auto& [name, cell] : cells) {
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
cell.max_w = cell.max_w == 0 ? cell_width : cell.max_w;
cell.max_h = cell.max_h == 0 ? cell_height : cell.max_h;
if(cell.percent) {
cell.max_w = cell.max_w * 0.01 * cell_width;
cell.max_h = cell.max_h * 0.01 * cell_height;
}
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
cell.mid_x = std::midpoint(cell.x, cell.x + cell_width);
cell.mid_y = std::midpoint(cell.y, cell.y + cell_height);
if(cell.right) cell.x += cell_width - cell.w;
if(cell.bottom) cell.y += cell_height - cell.h;
if(cell.center) {
cell.x = cell.mid_x - cell.w / 2;
cell.y = cell.mid_y - cell.h / 2;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
}
}
void Parser::reset() {
rows = 0;
columns = 0;
cur = {0, 0};
}
Cell center(int width, int height, Cell &parent) {
Cell copy = parent;
copy.x = parent.mid_x - width / 2;
copy.y = parent.mid_y - height / 2;
copy.w = width;
copy.h = height;
return copy;
}
}