Files
engine/src/main.cpp
T
2025-10-24 14:37:31 -04:00

142 lines
4.7 KiB
C++

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <cstdlib>
#include <string>
#include <thread>
#ifdef __APPLE__
#include <range/v3/view/iota.hpp>
#include <range/v3/view/cartesian_product.hpp>
#else
#include <ranges>
#endif
#include "globals.hpp"
#include "elements.hpp"
#include "maps.hpp"
#include "scheduler.hpp"
int main() {
Timer time;
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
struct {
sf::Vector2<float> size { TILESIZE / 2.0f, TILESIZE / 2.0f };
sf::Vector2<float> pos { TILESIZE, TILESIZE };
float acceleration { 1.0f };
float max_velocity_y { 20.0f };
float border_size { -1.0f };
float dash_distance { 20.0f };
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 };
}
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_data.size, player_data.pos, player_data.velocity,
player_data.border_size, player_data.acceleration, 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);
window.setFramerateLimit(60);
#ifdef __APPLE__
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
#else
sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_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.dash_cooldown); } );
while (window.isOpen()) {
window.clear(sf::Color::Black);
text.setString(std::to_string(player.timers.dash_cooldown));
while (const std::optional event = window.pollEvent()) {
if (event->is<sf::Event::Closed>()) {
running = false;
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
running = false;
window.close();
}
// 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_vertical_bottom_collision(wall_list)) {
player.get_vertical_top_collision(wall_list);
}
// Drawing Everything to window
#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;
window.draw(wall_list[i][j]->rect);
}
#else
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;
window.draw(wall_list[i][j]->rect);
}
#endif
window.draw(player.rect);
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
player_dash_cooldown.join();
return 0;
}