diff --git a/CMakeLists.txt b/CMakeLists.txt index ee836e7..0418c8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -project(SFML_game_engine) +project(engine) include_directories(include) set(SOURCES @@ -37,8 +37,8 @@ if(APPLE) include_directories(${range-v3_SOURCE_DIR}/include) endif() -add_executable(SFML_game_engine ${SOURCES}) -target_link_libraries(SFML_game_engine +add_executable(engine ${SOURCES}) +target_link_libraries(engine PRIVATE SFML::Graphics SFML::Window diff --git a/include/maps.hpp b/include/maps.hpp index 11cfc05..4d1d908 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -10,5 +10,6 @@ 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]); #endif diff --git a/src/elements.cpp b/src/elements.cpp index 3dcfdc0..f050b43 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -49,7 +49,6 @@ CollisionDir Entity::find_intersection(Entity &entity) { float overlapW = overlap.size.x; float overlapH = overlap.size.y; - // Compare which axis overlap is smaller if (overlapW < overlapH) { // Horizontal collision return (a.position.x < b.position.x) @@ -126,9 +125,11 @@ for(auto [i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_RO if(collision == CollisionDir::None) continue; if(collision == CollisionDir::Left) { rect.setPosition({ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y }); + return true; } if(collision == CollisionDir::Right) { rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, rect.getPosition().y }); + return true; } } return false; diff --git a/src/main.cpp b/src/main.cpp index 1096360..dd6ced8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,15 @@ #include #include +#include +#include #include #include +#include +#include #include #include #include +#include #ifdef __APPLE__ #include #include @@ -16,87 +21,147 @@ #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] = {}; - int current_level = 0; - struct { - sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; - sf::Vector2 pos { TILESIZE, TILESIZE }; - float max_velocity_y { 15.0f }; - float border_size { -1.0f }; - float dash_distance { 20.0f }; - sf::Vector2f velocity { 3.0f, 0 }; - } player_data; + sf::Vector2i local_position; + sf::Vector2i start_mouse_pos; + sf::Vector2i end_mouse_pos; + bool dragging = false; + bool prev_dragging = false; + // int current_level = 0; + // struct { + // sf::Vector2 size { TILESIZE / 2.0f, TILESIZE / 2.0f }; + // sf::Vector2 pos { TILESIZE, TILESIZE }; + // float max_velocity_y { 15.0f }; + // float border_size { -1.0f }; + // float dash_distance { 20.0f }; + // sf::Vector2f velocity { 3.0f, 0 }; + // } player_data; load_map(wall_list, test_level_0); - Player player { - player_data.size, player_data.pos, player_data.velocity, - player_data.border_size, GRAVITY, player_data.max_velocity_y, - player_data.dash_distance, sf::Color::Black, sf::Color::White - }; + // Player player { + // player_data.size, player_data.pos, player_data.velocity, + // player_data.border_size, GRAVITY, player_data.max_velocity_y, + // player_data.dash_distance, sf::Color::Black, sf::Color::White + // }; - sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default); + sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "engine", sf::Style::Default); window.setFramerateLimit(FPS); #ifdef __APPLE__ - sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf"); + sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/engine/assets/pixel.ttf"); #else - sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf"); + sf::Font font("/home/andrew/personal/Projects/CPP/engine/assets/pixel.ttf"); #endif sf::Text text(font, "0, 0", 16); text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)}); - bool running = true; - std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } ); - + // bool running = true; + // std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } ); while (window.isOpen()) { + local_position = sf::Mouse::getPosition(window); window.clear(sf::Color::Black); - text.setString(std::to_string(player.timers.dash_cooldown)); + 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; + // running = false; window.close(); } } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { - running = false; + // running = false; window.close(); } - - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) { + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) { clear_map(wall_list); - if(current_level == 0) { - load_map(wall_list, test_level_1); - current_level = 1; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } - else if(current_level == 1) { - load_map(wall_list, test_level_2); - current_level = 2; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } - else if(current_level == 2) { - load_map(wall_list, test_level_0); - current_level = 0; - player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; - } } - - player.handle_movment(); - player.get_horizontal_collision(wall_list); - - player.handle_gravity(); - if(player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { - player.set_velocity_y((TILESIZE / 2) - GRAVITY); - } - if(!player.get_vertical_bottom_collision(wall_list)) { - player.get_vertical_top_collision(wall_list); + 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); + // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) { + // clear_map(wall_list); + // if(current_level == 0) { + // load_map(wall_list, test_level_1); + // current_level = 1; + // player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; + // } + // else if(current_level == 1) { + // load_map(wall_list, test_level_2); + // current_level = 2; + // player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; + // } + // else if(current_level == 2) { + // load_map(wall_list, test_level_0); + // current_level = 0; + // player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; + // } + // } + // + // player.handle_movment(); + // if(!player.get_horizontal_collision(wall_list)) { + // std::cout << "\n"; + // } + // else { + // std::cout << "Yes horizontal collision\n"; + // } + // + // player.handle_gravity(); + // if(player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { + // player.set_velocity_y((TILESIZE / 2) - GRAVITY); + // } + // if(!player.get_vertical_bottom_collision(wall_list)) { + // std::cout << "\n"; + // if(!player.get_vertical_top_collision(wall_list)) { + // std::cout << "\n"; + // } else { + // std::cout << "Yes vertical top collision\n"; + // } + // } + // else { + // std::cout << "Yes vertical bottom collision\n"; + // } + // #ifdef __APPLE__ for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { if(wall_list[i][j] == nullptr) continue; @@ -108,11 +173,11 @@ int main() { window.draw(wall_list[i][j]->rect); } #endif - window.draw(player.rect); + // window.draw(player.rect); window.draw(text); window.display(); } clear_map(wall_list); - player_dash_cooldown.join(); + // player_dash_cooldown.join(); return 0; } diff --git a/src/maps.cpp b/src/maps.cpp index e142a36..8cc767b 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -22,9 +22,48 @@ void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) { #endif } -void create_map() { - char output[MAX_ROWS][MAX_COLS] = {}; - +// 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; + } +#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]) { @@ -91,19 +130,19 @@ void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROW } char test_level_0[MAX_ROWS][MAX_COLS] = { - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, - { '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, - { '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '2' }, - { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, + { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }, }; char test_level_1[MAX_ROWS][MAX_COLS] = {