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
+2 -6
View File
@@ -4,6 +4,7 @@
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include "scheduler.hpp"
#include "globals.hpp" #include "globals.hpp"
enum class CollisionDir { None, Left, Right, Top, Bottom }; enum class CollisionDir { None, Left, Right, Top, Bottom };
@@ -24,11 +25,6 @@ protected:
float _border_thickness; float _border_thickness;
}; };
struct Player_timers {
float dash_cooldown { 0 };
float dash_cooldown_max { 5 };
};
class Player : public Entity { class Player : public Entity {
public: public:
Player(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, float gravity, float max_acceleration, float dash_distance, sf::Color inside_color, sf::Color border_color); Player(sf::Vector2<float> size, sf::Vector2<float> 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; float m_dash_distance;
bool m_jumping; bool m_jumping;
bool m_dash; bool m_dash;
Player_timers timers; Times timers;
void handle_movment(); void handle_movment();
void handle_gravity(); void handle_gravity();
bool get_jumping(); bool get_jumping();
+5 -2
View File
@@ -4,7 +4,10 @@
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp" #include "elements.hpp"
extern int map_data[MAX_ROWS][MAX_COLS]; extern char test_level_0[MAX_ROWS][MAX_COLS];
void merged_walls(Entity* wall_list[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 #endif
+8 -1
View File
@@ -1,7 +1,14 @@
#ifndef SCHEDULER_HPP #ifndef SCHEDULER_HPP
#define 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 { struct Timer {
void update_timers(bool &running, float &dash_timer); void update_timers(bool &running, Times &times);
}; };
#endif #endif
+17 -49
View File
@@ -19,6 +19,7 @@
int main() { int main() {
Timer time; Timer time;
Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
int current_level = 0;
struct { struct {
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f }; sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
sf::Vector2<float> pos { TILESIZE, TILESIZE }; sf::Vector2<float> pos { TILESIZE, TILESIZE };
@@ -29,37 +30,7 @@ int main() {
sf::Vector2f velocity { 4.0f, 0 }; sf::Vector2f velocity { 4.0f, 0 };
} player_data; } player_data;
struct { load_map(wall_list, test_level_0);
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
Player player { Player player {
player_data.size, player_data.pos, player_data.velocity, 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)}); text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)});
bool running = true; 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()) { while (window.isOpen()) {
window.clear(sf::Color::Black); window.clear(sf::Color::Black);
text.setString(std::to_string(player.timers.dash_cooldown)); text.setString(std::to_string(player.timers.dash_cooldown));
@@ -94,19 +65,24 @@ int main() {
running = false; running = false;
window.close(); 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 // Handle Horizontal movement and collisions
player.handle_movment(); player.handle_movment();
player.get_horizontal_collision(wall_list); 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(); player.handle_gravity();
if(player.get_velocity().y > ((float)TILESIZE / 2)) { if(player.get_velocity().y > ((float)TILESIZE / 2)) {
player.set_velocity_y(TILESIZE / 2); player.set_velocity_y(TILESIZE / 2);
@@ -132,15 +108,7 @@ int main() {
window.draw(text); window.draw(text);
window.display(); window.display();
} }
#ifdef __APPLE__ clear_map(wall_list);
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
player_dash_cooldown.join(); player_dash_cooldown.join();
return 0; return 0;
} }
+94 -39
View File
@@ -1,4 +1,5 @@
#include "maps.hpp" #include "maps.hpp"
#include "elements.hpp"
#include "globals.hpp" #include "globals.hpp"
#ifdef __APPLE__ #ifdef __APPLE__
#include <range/v3/view/iota.hpp> #include <range/v3/view/iota.hpp>
@@ -7,57 +8,111 @@
#include <ranges> #include <ranges>
#endif #endif
void merged_walls(Entity* wall_list[MAX_ROWS][MAX_COLS]) { void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
#ifdef __APPLE__ #ifdef __APPLE__
for(auto[j, i] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { 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 #else
for(auto[j, i] : std::views::cartesian_product(std::views::iota(0, MAX_COLS), std::views::iota(0, MAX_ROWS))) { 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];
wall_list[i][j] = nullptr;
}
#endif #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) { void load_map(Entity* wall_list[MAX_ROWS][MAX_COLS], const char map_data[MAX_ROWS][MAX_COLS]) {
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;
}
#ifdef __APPLE__ #ifdef __APPLE__
for(auto[i, j] : ranges::views::cartesian_product(ranges::views::iota(0, MAX_ROWS), ranges::views::iota(0, MAX_COLS))) { 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))) { for(auto[i, j] : std::views::cartesian_product(std::views::iota(0, MAX_ROWS), std::views::iota(0, MAX_COLS))) {
#endif #endif
if(wall_list[i][j] == nullptr) continue; if(map_data[i][j] == ' ') continue;
if(wall_list[i][j]->rect.getSize().y != TILESIZE) 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; int start = j, end = j;
while(end + 1 < MAX_COLS && map_data[i][end + 1] != '2') end++;
while(end + 1 < MAX_COLS && wall_list[i][end + 1] != nullptr && wall_list[i][end + 1]->rect.getSize().y == TILESIZE) { if(map_data[i][end + 1] == '2') end++;
delete wall_list[i][end + 1]; wall_list[i][j] = new Entity {
wall_list[i][end + 1] = nullptr; { float(end - start + 1) * TILESIZE, TILESIZE },
end++; { 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;
} }
} }
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' },
};
int map_data[MAX_ROWS][MAX_COLS] = { char test_level_1[MAX_ROWS][MAX_COLS] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { '1', '-', '-', '-', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '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', '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1 }, { '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', '-', '-', '-', '-', '2', ' ', ' ', ' ', '|' },
{ 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', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', '|' },
{ 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }, { '2', ' ', ' ', ' ', ' ', '2', '0', ' ', ' ', ' ', ' ', ' ', ' ', '2', ' ', ' ', ' ', ' ', '2' },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } { '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' },
}; };
+3 -2
View File
@@ -3,9 +3,10 @@
#include "scheduler.hpp" #include "scheduler.hpp"
#include <chrono> #include <chrono>
#include <thread> #include <thread>
void Timer::update_timers(bool &running, float &dash_timer) { void Timer::update_timers(bool &running, Times &times) {
while (running) { while (running) {
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
dash_timer--; times.dash_cooldown--;
times.level_switch_cooldown--;
} }
} }