From 872cedc8e1cf08c633ecf48ec7082f1c22b91e3f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 15 Feb 2025 01:31:57 -0500 Subject: [PATCH] LEL is working at a basic grid level, able to render boxes where I want. --- combat_ui.cpp | 22 +++++++++++- combat_ui.hpp | 4 +++ gui.cpp | 5 --- lel.cpp | 58 ++++++++++++++++---------------- lel.hpp | 6 ++-- lel_parser.cpp | 90 +++++++++++++++++++++----------------------------- lel_parser.rl | 11 ++++-- tests/lel.cpp | 34 ++++++++++++++----- 8 files changed, 129 insertions(+), 101 deletions(-) diff --git a/combat_ui.cpp b/combat_ui.cpp index 4ce8e87..73807dc 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -4,11 +4,31 @@ namespace gui { CombatUI::CombatUI(GameLevel level) : + $layout(RAY_VIEW_X, RAY_VIEW_HEIGHT, + RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT), $level(level) { + bool good = $layout.parse( + "[attack1 | attack2 | attack3 | heal]" + "[move1 | move2 | move3 | move4]" + ); + + dbc::check(good, "failed to parse combat layout"); + + for(auto& [name, cell] : $layout.cells) { + sf::RectangleShape button; + button.setPosition({float(cell.x + 10), float(cell.y + 10)}); + button.setSize({float(cell.w - 20), float(cell.h - 20)}); + button.setFillColor({uint8_t(cell.col * 75), 100, 100}); + button.setOutlineColor({200, 200, 200}); + button.setOutlineThickness(5); + $shapes[name] = button; + } } void CombatUI::draw(sf::RenderWindow& window) { - (void)window; + for(auto& [name, shape] : $shapes) { + window.draw(shape); + } } } diff --git a/combat_ui.hpp b/combat_ui.hpp index 1f3b7ae..3d8c5eb 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -2,11 +2,15 @@ #include "panel.hpp" #include "levelmanager.hpp" #include +#include +#include "lel.hpp" namespace gui { class CombatUI { public: + LELParser $layout; GameLevel $level; + std::unordered_map $shapes; CombatUI(GameLevel level); void draw(sf::RenderWindow& window); diff --git a/gui.cpp b/gui.cpp index 3f2f185..3fba3e0 100644 --- a/gui.cpp +++ b/gui.cpp @@ -316,11 +316,6 @@ namespace gui { left_gui->setPosition({0,0}); $window.draw(*left_gui); - sf::RectangleShape lower({RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT}); - lower.setPosition({RAY_VIEW_X,RAY_VIEW_HEIGHT}); - lower.setFillColor({30, 30, 30}); - $window.draw(lower); - $status_view.render(); $renderer.draw($status_view); diff --git a/lel.cpp b/lel.cpp index f4b4d72..52f8f67 100644 --- a/lel.cpp +++ b/lel.cpp @@ -1,6 +1,6 @@ #include "lel.hpp" #include - +#include "dbc.hpp" #include "lel_parser.cpp" LELParser::LELParser(int x, int y, int width, int height) : @@ -10,12 +10,10 @@ LELParser::LELParser(int x, int y, int width, int height) : grid_h(height), cur(0, 0) { - } void LELParser::ltab() { - cur.row = row_count; - fmt::println("ltab: rows {}", row_count); + cur.row = rows; } void LELParser::col() { @@ -23,60 +21,62 @@ void LELParser::col() { void LELParser::valign(char dir) { cur.top = dir == '^'; - fmt::println("valign: {}", dir); } void LELParser::align(char dir) { cur.left = dir == '<'; - fmt::println("align {}", dir); } void LELParser::id(std::string name) { - fmt::println("id: {}", 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() { - row_count++; - max_columns = std::max(max_columns, cur.col); + rows++; + columns = std::max(columns, cur.col); cur.col = 0; - fmt::println("row end: cols {}", cur.col); } void LELParser::setwidth(int width) { - fmt::println("setwidth: {}", width); - cur.w = width; + cur.max_w = width; } void LELParser::setheight(int height) { - fmt::println("setheight: {}", height); - cur.h = height; + cur.max_h = height; } void LELParser::expand() { - fmt::println("expand"); cur.expand = true; } void LELParser::finalize() { - int cell_width = grid_w / max_columns; - int cell_height = grid_h / row_count; - fmt::println("FINALIZE: cell w/h: {},{}", cell_width, cell_height); - - for(auto [name, cell] : cells) { - cell.x = cell.col * cell_width; - cell.y = cell.row * cell_height; - if(cell.w == 0) cell.w = cell_width; - if(cell.x == 0) cell.h = cell_height; - - fmt::println("name={}; col/row={},{}; x/y={},{} w/h={},{}; left={}, top={}, expand={}", - name, cell.col, cell.row, cell.x, cell.y, cell.w, cell.h, cell.left, cell.top, cell.expand); + 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 ? cell.max_w : std::min(cell_width, cell.max_w); + cell.h = cell.expand ? cell.max_h : std::min(cell_height, cell.max_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() { - row_count = 0; - max_columns = 0; + rows = 0; + columns = 0; cur = {0, 0}; } diff --git a/lel.hpp b/lel.hpp index 9baf0a7..484b4a7 100644 --- a/lel.hpp +++ b/lel.hpp @@ -7,6 +7,8 @@ struct Cell { int y = 0; int w = 0; int h = 0; + int max_w = 0; + int max_h = 0; int col = 0; int row = 0; bool left = true; @@ -21,8 +23,8 @@ struct LELParser { int grid_y = 0; int grid_w = 0; int grid_h = 0; - int row_count = 0; - int max_columns = 0; + int rows = 0; + int columns = 0; Cell cur; std::unordered_map cells; diff --git a/lel_parser.cpp b/lel_parser.cpp index c5e284b..b3ad35b 100644 --- a/lel_parser.cpp +++ b/lel_parser.cpp @@ -2,13 +2,14 @@ #line 1 "lel_parser.rl" #include "lel.hpp" #include +#include -#line 33 "lel_parser.rl" +#line 34 "lel_parser.rl" -#line 7 "lel_parser.cpp" +#line 8 "lel_parser.cpp" static const char _LELParser_actions[] = { 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 9, 1, @@ -18,8 +19,7 @@ static const char _LELParser_actions[] = { static const char _LELParser_key_offsets[] = { 0, 0, 4, 18, 20, 24, 35, 47, - 52, 66, 68, 72, 83, 95, 99, 101, - 104, 106, 109 + 52, 54, 57 }; static const char _LELParser_trans_keys[] = { @@ -29,33 +29,23 @@ static const char _LELParser_trans_keys[] = { 40, 42, 46, 60, 62, 94, 95, 65, 90, 97, 122, 32, 93, 95, 124, 9, 13, 48, 57, 65, 90, 97, 122, 32, - 93, 124, 9, 13, 32, 40, 42, 46, - 60, 62, 94, 95, 9, 13, 65, 90, - 97, 122, 48, 57, 41, 44, 48, 57, - 40, 42, 46, 60, 62, 94, 95, 65, - 90, 97, 122, 32, 93, 95, 124, 9, - 13, 48, 57, 65, 90, 97, 122, 32, - 93, 9, 13, 48, 57, 41, 48, 57, - 48, 57, 41, 48, 57, 32, 91, 9, - 13, 0 + 93, 124, 9, 13, 48, 57, 41, 48, + 57, 32, 91, 9, 13, 0 }; static const char _LELParser_single_lengths[] = { 0, 2, 8, 0, 2, 7, 4, 3, - 8, 0, 2, 7, 4, 2, 0, 1, 0, 1, 2 }; static const char _LELParser_range_lengths[] = { 0, 1, 3, 1, 1, 2, 4, 1, - 3, 1, 1, 2, 4, 1, 1, 1, 1, 1, 1 }; static const char _LELParser_index_offsets[] = { 0, 0, 4, 16, 18, 22, 32, 41, - 46, 58, 60, 64, 74, 83, 87, 89, - 92, 94, 97 + 46, 48, 51 }; static const char _LELParser_indicies[] = { @@ -64,40 +54,30 @@ static const char _LELParser_indicies[] = { 9, 1, 10, 11, 12, 1, 4, 5, 6, 7, 7, 6, 8, 8, 8, 1, 13, 15, 14, 16, 13, 14, 14, 14, - 1, 17, 18, 19, 17, 1, 20, 21, - 22, 23, 24, 24, 23, 25, 20, 25, - 25, 1, 26, 1, 27, 28, 29, 1, - 21, 22, 23, 24, 24, 23, 25, 25, - 25, 1, 30, 15, 31, 16, 30, 31, - 31, 31, 1, 32, 18, 32, 1, 33, - 1, 34, 35, 1, 36, 1, 37, 38, - 1, 39, 2, 39, 1, 0 + 1, 17, 18, 19, 17, 1, 20, 1, + 21, 22, 1, 23, 2, 23, 1, 0 }; static const char _LELParser_trans_targs[] = { 1, 0, 2, 2, 3, 5, 5, 5, - 6, 4, 5, 16, 4, 7, 6, 18, - 8, 7, 18, 8, 8, 9, 11, 11, - 11, 12, 10, 11, 14, 10, 13, 12, - 13, 15, 11, 15, 17, 5, 17, 18 + 6, 4, 5, 8, 4, 7, 6, 10, + 2, 7, 10, 2, 9, 5, 9, 10 }; static const char _LELParser_trans_actions[] = { 0, 0, 3, 0, 0, 13, 5, 11, 17, 15, 19, 19, 0, 7, 0, 28, - 25, 0, 9, 1, 0, 0, 13, 5, - 11, 17, 15, 19, 19, 0, 7, 0, - 0, 15, 22, 0, 15, 22, 0, 0 + 25, 0, 9, 1, 15, 22, 0, 0 }; static const int LELParser_start = 1; -static const int LELParser_first_final = 18; +static const int LELParser_first_final = 10; static const int LELParser_error = 0; static const int LELParser_en_main = 1; -#line 36 "lel_parser.rl" +#line 37 "lel_parser.rl" bool LELParser::parse(std::string input) { reset(); @@ -109,14 +89,14 @@ bool LELParser::parse(std::string input) { std::string tk; -#line 104 "lel_parser.cpp" +#line 84 "lel_parser.cpp" { cs = LELParser_start; } -#line 47 "lel_parser.rl" +#line 48 "lel_parser.rl" -#line 107 "lel_parser.cpp" +#line 87 "lel_parser.cpp" { int _klen; unsigned int _trans; @@ -191,54 +171,54 @@ _match: switch ( *_acts++ ) { case 0: -#line 8 "lel_parser.rl" +#line 9 "lel_parser.rl" {tk = input.substr(start - begin, p - start); } break; case 1: -#line 10 "lel_parser.rl" +#line 11 "lel_parser.rl" { col(); } break; case 2: -#line 11 "lel_parser.rl" +#line 12 "lel_parser.rl" { ltab(); } break; case 3: -#line 12 "lel_parser.rl" +#line 13 "lel_parser.rl" { valign((*p)); } break; case 4: -#line 13 "lel_parser.rl" +#line 14 "lel_parser.rl" { id(input.substr(start - begin, p - start)); } break; case 5: -#line 14 "lel_parser.rl" +#line 15 "lel_parser.rl" { row(); } break; case 6: -#line 15 "lel_parser.rl" +#line 16 "lel_parser.rl" { align((*p)); } break; case 7: -#line 16 "lel_parser.rl" +#line 17 "lel_parser.rl" { setwidth(std::stoi(tk)); } break; case 8: -#line 17 "lel_parser.rl" +#line 18 "lel_parser.rl" { setheight(std::stoi(tk)); } break; case 9: -#line 18 "lel_parser.rl" +#line 19 "lel_parser.rl" { expand(); } break; case 10: -#line 26 "lel_parser.rl" +#line 27 "lel_parser.rl" { start = p; } break; case 11: -#line 29 "lel_parser.rl" +#line 30 "lel_parser.rl" {start = p;} break; -#line 216 "lel_parser.cpp" +#line 196 "lel_parser.cpp" } } @@ -251,10 +231,14 @@ _again: _out: {} } -#line 48 "lel_parser.rl" +#line 49 "lel_parser.rl" bool good = pe - p == 0; - finalize(); - + if(good) { + finalize(); + } else { + fmt::println("error at"); + std::cout << p; + } return good; } diff --git a/lel_parser.rl b/lel_parser.rl index 8bc89d9..a297dfe 100644 --- a/lel_parser.rl +++ b/lel_parser.rl @@ -1,5 +1,6 @@ #include "lel.hpp" #include +#include %%{ machine LELParser; @@ -27,7 +28,7 @@ setw = ("(" number %setwidth ("," number %setheight)? ")") ; modifiers = (expand | valign | halign | setw); id = modifiers* ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; - row = space* ltab space* id space* (col space* id)* space* rtab space*; + row = space* ltab space* id space* (col space* id space*)* space* rtab space*; main := row+; }%% @@ -47,7 +48,11 @@ bool LELParser::parse(std::string input) { %% write exec; bool good = pe - p == 0; - finalize(); - + if(good) { + finalize(); + } else { + fmt::println("error at"); + std::cout << p; + } return good; } diff --git a/tests/lel.cpp b/tests/lel.cpp index cae1056..49c8fb3 100644 --- a/tests/lel.cpp +++ b/tests/lel.cpp @@ -5,23 +5,41 @@ #include "ansi_parser.hpp" #include #include +#include TEST_CASE("test basic ops", "[lel]") { LELParser parser(0, 0, 500, 500); + std::vector labels{ + "label_1", "label3", "text1", "people", + "label2", "_", "message", "buttons"}; bool good = parser.parse( - "[ label_1 | *label3 ]" - "[ (300,300)text1 | (150)people ]" - "[ >label2 | _ ]" - "[ message | buttons ]"); + "[ label_1 | label3 | test1]" + "[ *(300,300)text1 | (150)people | test2]" + "[ >label2 | _ | test3]" + "[ message | buttons | test4]"); REQUIRE(good); - REQUIRE(parser.row_count == 4); - REQUIRE(parser.max_columns == 2); - REQUIRE(parser.cells.size() == 8); + REQUIRE(parser.rows == 4); + REQUIRE(parser.columns == 3); + REQUIRE(parser.cells.size() == 12); REQUIRE(parser.cells.at("label2").left == false); - REQUIRE(parser.cells.at("label3").expand == true); + REQUIRE(parser.cells.at("text1").expand == true); + REQUIRE(parser.cells.at("text1").w == 300); + REQUIRE(parser.cells.at("text1").h == 300); REQUIRE(parser.cells.at("people").expand == false); REQUIRE(parser.cells.at("message").expand == false); + + for(auto name : labels) { + auto& cell = parser.cells.at(name); + + fmt::println("name={}; col/row={},{}; x/y={},{} w/h={},{}; left={}, top={}, expand={}", + name, cell.col, cell.row, cell.x, cell.y, cell.w, cell.h, cell.left, cell.top, cell.expand); + + REQUIRE(cell.w > 0); + REQUIRE(cell.h > 0); + REQUIRE(cell.row < parser.rows); + REQUIRE(cell.col < parser.columns); + } }