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

93 lines
2.0 KiB

#include "lel.hpp"
#include <fmt/core.h>
#include "dbc.hpp"
#include "lel_parser.cpp"
LELParser::LELParser(int x, int y, int width, int height) :
grid_x(x),
grid_y(y),
grid_w(width),
grid_h(height),
cur(0, 0)
{
}
void LELParser::ltab() {
cur.row = rows;
}
void LELParser::col() {
}
void LELParser::valign(char dir) {
cur.bottom = dir == '.';
}
void LELParser::align(char dir) {
cur.right = dir == '>';
}
void LELParser::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 LELParser::row() {
rows++;
columns = std::max(columns, cur.col);
cur.col = 0;
}
void LELParser::setwidth(int width) {
cur.max_w = width;
}
void LELParser::setheight(int height) {
cur.max_h = height;
}
void LELParser::expand() {
cur.expand = true;
}
void LELParser::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;
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);
if(cell.right) {
cell.x += cell_width - cell.w;
}
if(cell.bottom) {
cell.y += cell_height - cell.h;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
}
}
void LELParser::reset() {
rows = 0;
columns = 0;
cur = {0, 0};
}