diff --git a/include/maps.hpp b/include/maps.hpp index 4d1d908..aa37350 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -1,6 +1,7 @@ #ifndef MAPS_HPP #define MAPS_HPP +#include #include "globals.hpp" #include "elements.hpp" @@ -10,6 +11,13 @@ extern char test_level_2[MAX_ROWS][MAX_COLS]; void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]); void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]); -void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]); +// void create_map(sf::RenderWindow &window, sf::Vector2i &local_position, sf::Vector2i &start_mouse_pos, sf::Vector2i &end_mouse_pos, bool &dragging, bool &prev_dragging, Entity *wall_list[MAX_ROWS][MAX_COLS]); +std::array, MAX_ROWS> create_map(sf::RenderWindow &window, + sf::Vector2i &local_position, + sf::Vector2i &start_mouse_pos, + sf::Vector2i &end_mouse_pos, + bool &dragging, + bool &prev_dragging, + Entity *wall_list[MAX_ROWS][MAX_COLS]); #endif diff --git a/src/main.cpp b/src/main.cpp index dd6ced8..5e7389d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,51 +10,19 @@ #include #include #include + #ifdef __APPLE__ #include #include #else #include #endif + #include "globals.hpp" #include "elements.hpp" #include "maps.hpp" #include "scheduler.hpp" -void draw_wall(sf::RenderWindow &window, sf::Vector2i &local_position, sf::Vector2i &start_mouse_pos, sf::Vector2i &end_mouse_pos, bool &dragging, bool &prev_dragging, Entity *wall_list[MAX_ROWS][MAX_COLS]) { - if(local_position.x > 0 && local_position.x < WIDTH && local_position.y > 0 && local_position.y < HEIGHT) { - if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !dragging) { - dragging = true; - start_mouse_pos = local_position; - } - if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging) { - prev_dragging = true; - end_mouse_pos = local_position; - sf::RectangleShape rect { { (float) (end_mouse_pos.x - start_mouse_pos.x), 1.0f } }; - rect.setPosition({ (float)start_mouse_pos.x, (float)(std::floor(start_mouse_pos.y / 32) * TILESIZE + ((float)TILESIZE / 2))} ); - window.draw(rect); - } - if(!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging && prev_dragging) { - sf::Vector2i start_integers = { (int)std::floor(start_mouse_pos.x / 32), (int)std::floor(start_mouse_pos.y / 32) }; - sf::Vector2i end_integers = { (int)std::floor(end_mouse_pos.x / 32), (int)std::floor(end_mouse_pos.y / 32) }; - - if(end_integers.x - start_integers.x > 0) { - if(wall_list[start_integers.x][start_integers.y] == nullptr) { - wall_list[start_integers.x][start_integers.y] = new Entity { - { float(end_integers.x - start_integers.x + 1) * TILESIZE, TILESIZE }, - { (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) }, - { 0, 0 }, - -2.0f, - sf::Color::Black - }; - } - } - prev_dragging = false; - dragging = false; - } - } -} - int main() { Timer time; Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; @@ -99,7 +67,6 @@ int main() { local_position = sf::Mouse::getPosition(window); window.clear(sf::Color::Black); text.setString(std::to_string((int)std::floor(local_position.x / 32)) + ", " + std::to_string((int)std::floor(local_position.y / 32))); - std::cout << dragging << "\n"; while (const std::optional event = window.pollEvent()) { if (event->is()) { // running = false; @@ -117,7 +84,7 @@ int main() { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { dragging = false; } - draw_wall(window, local_position, start_mouse_pos, end_mouse_pos, dragging, prev_dragging, wall_list); + create_map(window, local_position, start_mouse_pos, end_mouse_pos, dragging, prev_dragging, wall_list); // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) { // clear_map(wall_list); diff --git a/src/maps.cpp b/src/maps.cpp index 8cc767b..723adff 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,6 +1,8 @@ #include "maps.hpp" #include "elements.hpp" #include "globals.hpp" +#include +#include #ifdef __APPLE__ #include #include @@ -10,68 +12,109 @@ void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) { #ifdef __APPLE__ - for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { - delete wall_list[i][j]; - wall_list[i][j] = nullptr; - } -#else +for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { + #else for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { + #endif delete wall_list[i][j]; wall_list[i][j] = nullptr; } -#endif } -// void create_map() { -// char output[MAX_ROWS][MAX_COLS] = {}; -// -// } - -void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) { -#ifdef __APPLE__ -for(auto[j, i] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { - #else - for(auto[j, i] : std::views::cartesian_product(std::views::iota(0, MAX_COLS), std::views::iota(0, MAX_ROWS))) { - #endif - if(wall_list[i][j] == nullptr) continue; - if(wall_list[i][j]->rect.getSize().x != TILESIZE) continue; - int start = i, end = i; - - while(end + 1 < MAX_ROWS && wall_list[end + 1][j] != nullptr) { - delete wall_list[end + 1][j]; - wall_list[end + 1][j] = nullptr; - end++; - } - int merged_height = (end - start + 1) * TILESIZE; - wall_list[start][j]->rect.setSize({ wall_list[start][j]->rect.getSize().x, static_cast(merged_height) }); - i = end; +bool is_tiles_clear_x(std::array, MAX_ROWS> draw_list, int y_level, int start_x, int end_x) { + for(int i : std::views::iota(start_x, end_x)) { + if(draw_list[i][y_level] == ' ') continue; + return false; } + return true; +} + +bool is_tiles_clear_y(std::array, MAX_ROWS> draw_list, int x_level, int start_y, int end_y) { + for(int i : std::views::iota(start_y, end_y)) { + if(draw_list[i][x_level] == ' ') continue; + return false; + } + return true; +} + +std::array, MAX_ROWS> create_map(sf::RenderWindow &window, sf::Vector2i &local_position, sf::Vector2i &start_mouse_pos, sf::Vector2i &end_mouse_pos, bool &dragging, bool &prev_dragging, Entity *wall_list[MAX_ROWS][MAX_COLS]) { + std::array, MAX_ROWS> draw_list {}; + + for(auto &row : draw_list) { + row.fill(' '); + } + static bool vertical = false; + if(local_position.x > 0 && local_position.x < WIDTH && local_position.y > 0 && local_position.y < HEIGHT) { + if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !dragging) { + dragging = true; + start_mouse_pos = local_position; + } + if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging) { + prev_dragging = true; + end_mouse_pos = local_position; + sf::RectangleShape rect; + if(end_mouse_pos.x - start_mouse_pos.x > end_mouse_pos.y - start_mouse_pos.y) { + rect.setSize({ (float) (end_mouse_pos.x - start_mouse_pos.x), 1.0f }); + rect.setPosition({ (float)start_mouse_pos.x, (float)(std::floor(start_mouse_pos.y / 32) * TILESIZE + ((float)TILESIZE / 2))} ); + vertical = false; + } else { + rect.setSize({ 1.0f, (float) (end_mouse_pos.y - start_mouse_pos.y),}); + rect.setPosition({ (float)(std::floor(start_mouse_pos.x / 32) * TILESIZE + ((float)TILESIZE / 2)), (float)start_mouse_pos.y} ); + vertical = true; + } + window.draw(rect); + } + if(!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && dragging && prev_dragging) { + sf::Vector2i start_integers = { (int)std::floor(start_mouse_pos.x / 32), (int)std::floor(start_mouse_pos.y / 32) }; + sf::Vector2i end_integers = { (int)std::floor(end_mouse_pos.x / 32), (int)std::floor(end_mouse_pos.y / 32) }; + + if(!vertical) { + if(wall_list[start_integers.y][start_integers.x] == nullptr) { + wall_list[start_integers.y][start_integers.x] = new Entity { + { float(end_integers.x - start_integers.x + 1) * TILESIZE, TILESIZE }, + { (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + draw_list[start_integers.y][start_integers.x] = '1'; + draw_list[end_integers.y][end_integers.x] = '2'; + for(int i : std::views::iota(start_integers.x, end_integers.x)) { + draw_list[start_integers.y][i] = '-'; + } + } + } else if(vertical) { + if(wall_list[start_integers.y][start_integers.x] == nullptr) { + // if(is_tiles_clear_y(draw_list, start_integers.x, start_integers.y, end_integers.y)) { + wall_list[start_integers.y][start_integers.x] = new Entity { + { TILESIZE, float(end_integers.y - start_integers.y + 1) * TILESIZE }, + { (float)(start_integers.x * TILESIZE), (float)(start_integers.y * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + draw_list[start_integers.y][start_integers.x] = '1'; + draw_list[end_integers.y][end_integers.x] = '2'; + for(int i : std::views::iota(start_integers.y, end_integers.y)) { + draw_list[i][start_integers.x] = '|'; + } + // } + } + // } + prev_dragging = false; + dragging = false; + } + } + } + return draw_list; +} + +void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]) { #ifdef __APPLE__ for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { #else for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { #endif - if(wall_list[i][j] == nullptr) continue; - if(wall_list[i][j]->rect.getSize().y != TILESIZE) continue; - int start = j, end = j; - - while(end + 1 < MAX_COLS && wall_list[i][end + 1] != nullptr && wall_list[i][end + 1]->rect.getSize().y == TILESIZE) { - delete wall_list[i][end + 1]; - wall_list[i][end + 1] = nullptr; - end++; - } - int merged_width = (end - start + 1) * TILESIZE; - wall_list[i][start]->rect.setSize({ static_cast(merged_width), wall_list[i][start]->rect.getSize().y }); - j = end; - } -} - -void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]) { -#ifdef __APPLE__ - for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { -#else - for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { -#endif if(map_data[i][j] == ' ') continue; if(map_data[i][j] == '0') { wall_list[i][j] = new Entity {