maps are more efficient to make and run on memory

This commit is contained in:
DarthMooMancer
2025-10-26 16:47:16 -04:00
parent 5e5f712a84
commit cd19e841a5
6 changed files with 135 additions and 105 deletions
+17 -49
View File
@@ -19,6 +19,7 @@
int main() {
Timer time;
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
int current_level = 0;
struct {
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
sf::Vector2<float> 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;
}
+100 -45
View File
@@ -1,4 +1,5 @@
#include "maps.hpp"
#include "elements.hpp"
#include "globals.hpp"
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
@@ -7,57 +8,111 @@
#include <ranges>
#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<float>(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<float>(merged_width), wall_list[i][start]->rect.getSize().y });
j = end;
}
}
int map_data[MAX_ROWS][MAX_COLS] = {
{ 1, 1, 1, 1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{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ '|', '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' },
};
+3 -2
View File
@@ -3,9 +3,10 @@
#include "scheduler.hpp"
#include <chrono>
#include <thread>
void Timer::update_timers(bool &running, float &dash_timer) {
void Timer::update_timers(bool &running, Times &times) {
while (running) {
std::this_thread::sleep_for(std::chrono::seconds(1));
dash_timer--;
times.dash_cooldown--;
times.level_switch_cooldown--;
}
}