gitignore no ha deixat versionar cap fitxer de core
afegida gestió de ratolí
This commit is contained in:
191
source/core/system/director.cpp
Normal file
191
source/core/system/director.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
#include "director.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "../../game/escenes/escena_joc.hpp"
|
||||
#include "../../game/escenes/escena_logo.hpp"
|
||||
#include "../../game/escenes/escena_titol.hpp"
|
||||
#include "../../game/options.hpp"
|
||||
#include "../audio/audio.hpp"
|
||||
#include "../defaults.hpp"
|
||||
#include "../rendering/sdl_manager.hpp"
|
||||
#include "gestor_escenes.hpp"
|
||||
#include "project.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Constructor
|
||||
Director::Director(std::vector<std::string> const& args) {
|
||||
std::cout << "Orni Attack - Inici\n";
|
||||
|
||||
// Inicialitzar opcions amb valors per defecte
|
||||
Options::init();
|
||||
|
||||
// Comprovar arguments del programa
|
||||
executable_path_ = checkProgramArguments(args);
|
||||
|
||||
// Crear carpetes del sistema
|
||||
createSystemFolder("jailgames");
|
||||
createSystemFolder(std::string("jailgames/") + Project::NAME);
|
||||
|
||||
// Establir ruta del fitxer de configuració
|
||||
Options::setConfigFile(system_folder_ + "/config.yaml");
|
||||
|
||||
// Carregar o crear configuració
|
||||
Options::loadFromFile();
|
||||
|
||||
if (Options::console) {
|
||||
std::cout << "Configuració carregada\n";
|
||||
std::cout << " Finestra: " << Options::window.width << "×"
|
||||
<< Options::window.height << '\n';
|
||||
std::cout << " Física: rotation=" << Options::physics.rotation_speed
|
||||
<< " rad/s\n";
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
Director::~Director() {
|
||||
// Guardar opcions
|
||||
Options::saveToFile();
|
||||
|
||||
// Cleanup audio
|
||||
Audio::destroy();
|
||||
|
||||
// Cleanup SDL
|
||||
SDL_Quit();
|
||||
|
||||
std::cout << "\nAdéu!\n";
|
||||
}
|
||||
|
||||
// Comprovar arguments del programa
|
||||
auto Director::checkProgramArguments(std::vector<std::string> const& args)
|
||||
-> std::string {
|
||||
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||
const std::string& argument = args[i];
|
||||
|
||||
if (argument == "--console") {
|
||||
Options::console = true;
|
||||
std::cout << "Mode consola activat\n";
|
||||
} else if (argument == "--reset-config") {
|
||||
Options::init();
|
||||
Options::saveToFile();
|
||||
std::cout << "Configuració restablida als valors per defecte\n";
|
||||
}
|
||||
}
|
||||
|
||||
return args[0]; // Retornar ruta de l'executable
|
||||
}
|
||||
|
||||
// Crear carpeta del sistema (específic per plataforma)
|
||||
void Director::createSystemFolder(const std::string& folder) {
|
||||
#ifdef _WIN32
|
||||
system_folder_ = std::string(getenv("APPDATA")) + "/" + folder;
|
||||
#elif __APPLE__
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
const char* homedir = pw->pw_dir;
|
||||
system_folder_ =
|
||||
std::string(homedir) + "/Library/Application Support/" + folder;
|
||||
#elif __linux__
|
||||
struct passwd* pw = getpwuid(getuid());
|
||||
const char* homedir = pw->pw_dir;
|
||||
system_folder_ = std::string(homedir) + "/.config/" + folder;
|
||||
|
||||
// CRÍTIC: Crear ~/.config si no existeix
|
||||
{
|
||||
std::string config_base_folder = std::string(homedir) + "/.config";
|
||||
int ret = mkdir(config_base_folder.c_str(), S_IRWXU);
|
||||
if (ret == -1 && errno != EEXIST) {
|
||||
printf("ERROR: No es pot crear la carpeta ~/.config\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Comprovar si la carpeta existeix
|
||||
struct stat st = {.st_dev = 0};
|
||||
if (stat(system_folder_.c_str(), &st) == -1) {
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
int ret = mkdir(system_folder_.c_str());
|
||||
#else
|
||||
int ret = mkdir(system_folder_.c_str(), S_IRWXU);
|
||||
#endif
|
||||
|
||||
if (ret == -1) {
|
||||
switch (errno) {
|
||||
case EACCES:
|
||||
printf("ERROR: Permisos denegats creant %s\n", system_folder_.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
case EEXIST:
|
||||
// La carpeta ja existeix (race condition), continuar
|
||||
break;
|
||||
|
||||
case ENAMETOOLONG:
|
||||
printf("ERROR: Ruta massa llarga: %s\n", system_folder_.c_str());
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
default:
|
||||
perror("mkdir");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Options::console) {
|
||||
std::cout << "Carpeta del sistema: " << system_folder_ << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// Bucle principal del joc
|
||||
auto Director::run() -> int {
|
||||
// Calculate initial size from saved zoom_factor
|
||||
int initial_width = static_cast<int>(std::round(
|
||||
Defaults::Window::WIDTH * Options::window.zoom_factor));
|
||||
int initial_height = static_cast<int>(std::round(
|
||||
Defaults::Window::HEIGHT * Options::window.zoom_factor));
|
||||
|
||||
// Crear gestor SDL amb configuració de Options
|
||||
SDLManager sdl(initial_width, initial_height, Options::window.fullscreen);
|
||||
|
||||
// Inicialitzar sistema d'audio
|
||||
Audio::init();
|
||||
|
||||
// Bucle principal de gestió d'escenes
|
||||
while (GestorEscenes::actual != GestorEscenes::Escena::EIXIR) {
|
||||
switch (GestorEscenes::actual) {
|
||||
case GestorEscenes::Escena::LOGO: {
|
||||
EscenaLogo logo(sdl);
|
||||
logo.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
case GestorEscenes::Escena::TITOL: {
|
||||
EscenaTitol titol(sdl);
|
||||
titol.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
case GestorEscenes::Escena::JOC: {
|
||||
EscenaJoc joc(sdl);
|
||||
joc.executar();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
source/core/system/director.hpp
Normal file
20
source/core/system/director.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Director {
|
||||
public:
|
||||
explicit Director(std::vector<std::string> const& args);
|
||||
~Director();
|
||||
|
||||
auto run() -> int; // Main game loop
|
||||
|
||||
private:
|
||||
std::string executable_path_;
|
||||
std::string system_folder_;
|
||||
|
||||
static auto checkProgramArguments(std::vector<std::string> const& args)
|
||||
-> std::string;
|
||||
void createSystemFolder(const std::string& folder);
|
||||
};
|
||||
17
source/core/system/gestor_escenes.hpp
Normal file
17
source/core/system/gestor_escenes.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// gestor_escenes.hpp - Sistema de gestió d'escenes
|
||||
// Basat en el patró del projecte "pollo"
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace GestorEscenes {
|
||||
enum class Escena {
|
||||
LOGO, // Pantalla d'inici (2 segons negre)
|
||||
TITOL, // Pantalla de títol amb menú
|
||||
JOC, // Joc principal
|
||||
EIXIR // Sortir del programa
|
||||
};
|
||||
|
||||
// Variable global inline per gestionar l'escena actual
|
||||
inline Escena actual = Escena::LOGO;
|
||||
} // namespace GestorEscenes
|
||||
48
source/core/system/global_events.cpp
Normal file
48
source/core/system/global_events.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
// global_events.cpp - Implementació dels events globals
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#include "global_events.hpp"
|
||||
|
||||
#include "../rendering/sdl_manager.hpp"
|
||||
#include "gestor_escenes.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
|
||||
namespace GlobalEvents {
|
||||
|
||||
bool handle(const SDL_Event& event, SDLManager& sdl) {
|
||||
// Tecles globals de finestra (F1/F2/F3)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (event.key.key) {
|
||||
case SDLK_F1:
|
||||
sdl.decreaseWindowSize();
|
||||
return true;
|
||||
case SDLK_F2:
|
||||
sdl.increaseWindowSize();
|
||||
return true;
|
||||
case SDLK_F3:
|
||||
sdl.toggleFullscreen();
|
||||
return true;
|
||||
case SDLK_F4:
|
||||
sdl.toggleVSync();
|
||||
return true;
|
||||
case SDLK_ESCAPE:
|
||||
GestorEscenes::actual = GestorEscenes::Escena::EIXIR;
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Tancar finestra
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
GestorEscenes::actual = GestorEscenes::Escena::EIXIR;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gestió del ratolí (auto-ocultar)
|
||||
Mouse::handleEvent(event);
|
||||
|
||||
return false; // Event no processat
|
||||
}
|
||||
|
||||
} // namespace GlobalEvents
|
||||
16
source/core/system/global_events.hpp
Normal file
16
source/core/system/global_events.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
// global_events.hpp - Events globals del joc
|
||||
// Basat en el patró del projecte "pollo"
|
||||
// © 2025 Port a C++20
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
// Forward declaration
|
||||
class SDLManager;
|
||||
|
||||
namespace GlobalEvents {
|
||||
// Processa events globals (F1/F2/F3/ESC/QUIT)
|
||||
// Retorna true si l'event ha estat processat i no cal seguir processant-lo
|
||||
bool handle(const SDL_Event& event, SDLManager& sdl);
|
||||
} // namespace GlobalEvents
|
||||
Reference in New Issue
Block a user