proper start to map editor

This commit is contained in:
DarthMooMancer
2025-10-31 15:09:24 -04:00
parent bdabd5a0bc
commit 72961ebf6d
5 changed files with 178 additions and 72 deletions
+3 -3
View File
@@ -3,7 +3,7 @@ set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(SFML_game_engine) project(engine)
include_directories(include) include_directories(include)
set(SOURCES set(SOURCES
@@ -37,8 +37,8 @@ if(APPLE)
include_directories(${range-v3_SOURCE_DIR}/include) include_directories(${range-v3_SOURCE_DIR}/include)
endif() endif()
add_executable(SFML_game_engine ${SOURCES}) add_executable(engine ${SOURCES})
target_link_libraries(SFML_game_engine target_link_libraries(engine
PRIVATE PRIVATE
SFML::Graphics SFML::Graphics
SFML::Window SFML::Window
+1
View File
@@ -10,5 +10,6 @@ extern char test_level_2[MAX_ROWS][MAX_COLS];
void clear_map(Entity* wall_list[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 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 #endif
+2 -1
View File
@@ -49,7 +49,6 @@ CollisionDir Entity::find_intersection(Entity &entity) {
float overlapW = overlap.size.x; float overlapW = overlap.size.x;
float overlapH = overlap.size.y; float overlapH = overlap.size.y;
// Compare which axis overlap is smaller
if (overlapW < overlapH) { if (overlapW < overlapH) {
// Horizontal collision // Horizontal collision
return (a.position.x < b.position.x) 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::None) continue;
if(collision == CollisionDir::Left) { if(collision == CollisionDir::Left) {
rect.setPosition({ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y }); rect.setPosition({ wall_list[i][j]->rect.getPosition().x - rect.getSize().x, rect.getPosition().y });
return true;
} }
if(collision == CollisionDir::Right) { if(collision == CollisionDir::Right) {
rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, rect.getPosition().y }); rect.setPosition({ wall_list[i][j]->rect.getPosition().x + wall_list[i][j]->rect.getSize().x, rect.getPosition().y });
return true;
} }
} }
return false; return false;
+117 -52
View File
@@ -1,10 +1,15 @@
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cmath>
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <thread> #include <thread>
#include <iostream>
#ifdef __APPLE__ #ifdef __APPLE__
#include <range/v3/view/iota.hpp> #include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp> #include <range/v3/view/cartesian_product.hpp>
@@ -16,87 +21,147 @@
#include "maps.hpp" #include "maps.hpp"
#include "scheduler.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() { int main() {
Timer time; Timer time;
Entity *wall_list[MAX_ROWS][MAX_COLS] = {}; Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
int current_level = 0; sf::Vector2i local_position;
struct { sf::Vector2i start_mouse_pos;
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f }; sf::Vector2i end_mouse_pos;
sf::Vector2<float> pos { TILESIZE, TILESIZE }; bool dragging = false;
float max_velocity_y { 15.0f }; bool prev_dragging = false;
float border_size { -1.0f }; // int current_level = 0;
float dash_distance { 20.0f }; // struct {
sf::Vector2f velocity { 3.0f, 0 }; // sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
} player_data; // sf::Vector2<float> 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); load_map(wall_list, test_level_0);
Player player { // Player player {
player_data.size, player_data.pos, player_data.velocity, // player_data.size, player_data.pos, player_data.velocity,
player_data.border_size, GRAVITY, player_data.max_velocity_y, // player_data.border_size, GRAVITY, player_data.max_velocity_y,
player_data.dash_distance, sf::Color::Black, sf::Color::White // 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); window.setFramerateLimit(FPS);
#ifdef __APPLE__ #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 #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 #endif
sf::Text text(font, "0, 0", 16); sf::Text text(font, "0, 0", 16);
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); } ); // std::thread player_dash_cooldown([&] { time.update_timers(running, player.timers); } );
while (window.isOpen()) { while (window.isOpen()) {
local_position = sf::Mouse::getPosition(window);
window.clear(sf::Color::Black); 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()) { while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) { if (event->is<sf::Event::Closed>()) {
running = false; // running = false;
window.close(); window.close();
} }
} }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
running = false; // running = false;
window.close(); window.close();
} }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::B)) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) {
clear_map(wall_list); 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;
} }
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);
player.handle_movment(); // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && player.timers.level_switch_cooldown <= 0) {
player.get_horizontal_collision(wall_list); // clear_map(wall_list);
// if(current_level == 0) {
player.handle_gravity(); // load_map(wall_list, test_level_1);
if(player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY)) { // current_level = 1;
player.set_velocity_y((TILESIZE / 2) - GRAVITY); // player.timers.level_switch_cooldown = player.timers.level_switch_cooldown_max;
} // }
if(!player.get_vertical_bottom_collision(wall_list)) { // else if(current_level == 1) {
player.get_vertical_top_collision(wall_list); // 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__ #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))) {
if(wall_list[i][j] == nullptr) continue; if(wall_list[i][j] == nullptr) continue;
@@ -108,11 +173,11 @@ int main() {
window.draw(wall_list[i][j]->rect); window.draw(wall_list[i][j]->rect);
} }
#endif #endif
window.draw(player.rect); // window.draw(player.rect);
window.draw(text); window.draw(text);
window.display(); window.display();
} }
clear_map(wall_list); clear_map(wall_list);
player_dash_cooldown.join(); // player_dash_cooldown.join();
return 0; return 0;
} }
+54 -15
View File
@@ -22,9 +22,48 @@ void clear_map(Entity* wall_list[MAX_ROWS][MAX_COLS]) {
#endif #endif
} }
void create_map() { // void create_map() {
char output[MAX_ROWS][MAX_COLS] = {}; // 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<float>(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<float>(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]) { 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] = { char test_level_0[MAX_ROWS][MAX_COLS] = {
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '1' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '2' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '1', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '2' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
}; };
char test_level_1[MAX_ROWS][MAX_COLS] = { char test_level_1[MAX_ROWS][MAX_COLS] = {