From cd19e841a5019adb1d276da6fb4e6c31c9c0015e Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Sun, 26 Oct 2025 16:47:16 -0400 Subject: [PATCH] maps are more efficient to make and run on memory --- include/elements.hpp | 8 +-- include/maps.hpp | 7 +- include/scheduler.hpp | 9 ++- src/main.cpp | 66 +++++-------------- src/maps.cpp | 145 +++++++++++++++++++++++++++++------------- src/scheduler.cpp | 5 +- 6 files changed, 135 insertions(+), 105 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index ecba2b3..a7d8530 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -4,6 +4,7 @@ #include #include #include +#include "scheduler.hpp" #include "globals.hpp" enum class CollisionDir { None, Left, Right, Top, Bottom }; @@ -24,11 +25,6 @@ protected: float _border_thickness; }; -struct Player_timers { - float dash_cooldown { 0 }; - float dash_cooldown_max { 5 }; -}; - class Player : public Entity { public: Player(sf::Vector2 size, sf::Vector2 pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, sf::Color border_color); @@ -37,7 +33,7 @@ public: float m_dash_distance; bool m_jumping; bool m_dash; - Player_timers timers; + Times timers; void handle_movment(); void handle_gravity(); bool get_jumping(); diff --git a/include/maps.hpp b/include/maps.hpp index 1b5a7be..b671001 100644 --- a/include/maps.hpp +++ b/include/maps.hpp @@ -4,7 +4,10 @@ #include "globals.hpp" #include "elements.hpp" -extern int map_data[MAX_ROWS][MAX_COLS]; -void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]); +extern char test_level_0[MAX_ROWS][MAX_COLS]; +extern char test_level_1[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]); #endif diff --git a/include/scheduler.hpp b/include/scheduler.hpp index 0d43c19..fb0648e 100644 --- a/include/scheduler.hpp +++ b/include/scheduler.hpp @@ -1,7 +1,14 @@ #ifndef SCHEDULER_HPP #define SCHEDULER_HPP +struct Times { + float dash_cooldown { 0 }; + float dash_cooldown_max { 5 }; + float level_switch_cooldown { 0 }; + float level_switch_cooldown_max { 1 }; +}; + struct Timer { - void update_timers(bool &running, float &dash_timer); + void update_timers(bool &running, Times ×); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 5883444..da4cf2a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ 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 }; @@ -29,37 +30,7 @@ int main() { sf::Vector2f velocity { 4.0f, 0 }; } player_data; - struct { - sf::Vector2f size { TILESIZE, TILESIZE }; - sf::Vector2f pos { 0, 0 }; - sf::Vector2f velocity { 0, 0 }; - float border_size { -2.0f }; - } tile_data; - -#ifdef __APPLE__ - for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { - if(map_data[i][j] != 1) continue; - wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }; - } - - 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; - wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); - } - -#else - for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { - if(map_data[i][j] != 1) continue; - wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black }; - } - - merged_walls(wall_list); - - for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { - if(wall_list[i][j] == nullptr) continue; - wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) }); - } -#endif + load_map(wall_list, test_level_0); Player player { player_data.size, player_data.pos, player_data.velocity, @@ -80,7 +51,7 @@ int main() { 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.dash_cooldown); } ); + std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } ); while (window.isOpen()) { window.clear(sf::Color::Black); text.setString(std::to_string(player.timers.dash_cooldown)); @@ -94,19 +65,24 @@ int main() { running = false; window.close(); } + 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_0); + current_level = 0; + player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max; + } + } // Handle Horizontal movement and collisions player.handle_movment(); player.get_horizontal_collision(wall_list); - // if(player.rect.getPosition().y > HEIGHT) { - // player.rect.setPosition({ player.rect.getPosition().x, HEIGHT - TILESIZE - player.rect.getSize().y }); - // player.set_velocity_y(0); - // while(player.get_vertical_bottom_collision(wall_list) || player.get_vertical_top_collision(wall_list)) { - // player.rect.setPosition({ player.rect.getPosition().x, player.rect.getPosition().y - TILESIZE - player.rect.getSize().y }); - // } - // } - player.handle_gravity(); if(player.get_velocity().y > ((float)TILESIZE / 2)) { player.set_velocity_y(TILESIZE / 2); @@ -132,15 +108,7 @@ int main() { window.draw(text); window.display(); } -#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]; - } -#else - for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) { - delete wall_list[i][j]; - } -#endif + clear_map(wall_list); player_dash_cooldown.join(); return 0; } diff --git a/src/maps.cpp b/src/maps.cpp index 6f219f3..e55cebc 100644 --- a/src/maps.cpp +++ b/src/maps.cpp @@ -1,4 +1,5 @@ #include "maps.hpp" +#include "elements.hpp" #include "globals.hpp" #ifdef __APPLE__ #include @@ -7,57 +8,111 @@ #include #endif -void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) { +void clear_map(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; + 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; } -#ifdef __APPLE__ -for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { - #else +#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; + delete wall_list[i][j]; + wall_list[i][j] = nullptr; + } +#endif +} - 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++; +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 { + { TILESIZE, TILESIZE }, + { float(j * TILESIZE), float(i * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + } + if(map_data[i][j] == '1' && map_data[i][j + 1] == '2') { + int start = j, end = j + 1; + wall_list[i][j] = new Entity { + { float(end - start + 1) * TILESIZE, TILESIZE }, + { float(j * TILESIZE), float(i * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + } + if(map_data[i][j] == '1' && map_data[i + 1][j] == '2') { + int start = i, end = i + 1; + wall_list[i][j] = new Entity { + { TILESIZE, float(end - start + 1) * TILESIZE }, + { float(j * TILESIZE), float(i * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + } + if(map_data[i][j] == '1' && map_data[i][j + 1] == '-') { + int start = j, end = j; + while(end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++; + if(map_data[i][end + 1] == '2') end++; + wall_list[i][j] = new Entity { + { float(end - start + 1) * TILESIZE, TILESIZE }, + { float(j * TILESIZE), float(i * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; + } + if(map_data[i][j] == '1' && map_data[i + 1][j] == '|') { + int start = i, end = i; + while(end + 1 < MAX_ROWS && map_data[end + 1][j] != '2') end++; + if(map_data[end + 1][j] == '2') end++; + wall_list[i][j] = new Entity { + { TILESIZE, float(end - start + 1) * TILESIZE }, + { float(j * TILESIZE), float(i * TILESIZE) }, + { 0, 0 }, + -2.0f, + sf::Color::Black + }; } - 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; } } -int map_data[MAX_ROWS][MAX_COLS] = { - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, - { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } +char test_level_0[MAX_ROWS][MAX_COLS] = { + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, + { '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '2', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, +}; + +char test_level_1[MAX_ROWS][MAX_COLS] = { + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, + { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', '1', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, + { '|', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' }, + { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' }, + { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, }; diff --git a/src/scheduler.cpp b/src/scheduler.cpp index cf8b63e..0c58811 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -3,9 +3,10 @@ #include "scheduler.hpp" #include #include -void Timer::update_timers(bool &running, float &dash_timer) { +void Timer::update_timers(bool &running, Times ×) { while (running) { std::this_thread::sleep_for(std::chrono::seconds(1)); - dash_timer--; + times.dash_cooldown--; + times.level_switch_cooldown--; } }