Skip to content

Commit

Permalink
fix: absolute resource path is loaded at runtime and not absolute pat…
Browse files Browse the repository at this point in the history
…h of the directory from that the executable is called
  • Loading branch information
Ipagaxi committed Jul 19, 2024
1 parent 6dcd3c9 commit 62275f5
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 47 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ add_executable (
${SOURCES}
)

configure_file(
${CMAKE_SOURCE_DIR}/config.hpp.in
${CMAKE_SOURCE_DIR}/config.hpp
)

target_include_directories(${PROJECT_NAME} PRIVATE "src" "${CMAKE_SOURCE_DIR}")
target_link_libraries(${PROJECT_NAME} PUBLIC sfml-graphics sfml-audio)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
Expand Down
3 changes: 0 additions & 3 deletions config.hpp.in

This file was deleted.

2 changes: 1 addition & 1 deletion src/Activities/CharacterActivity/CharacterActivity.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "CharacterActivity.hpp"

CharacterActivity::CharacterActivity(): Activity() {
this->backgroundTX.loadFromFile(RESOURCES + "backgrounds/backgroundMenu.png");
this->backgroundTX.loadFromFile(RESOURCE_PATH + "backgrounds/backgroundMenu.png");
this->backgroundSP.setTexture(this->backgroundTX);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Activities/FightActivity/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::
this->fightEnv.enemyOverview = std::make_unique<UIEnemyOverview>(this->enemy);
this->fightEnv.playerOverview = std::make_unique<UIPlayerOverview>(Game::getInstance().player);

this->fightEnv.backgroundTX.loadFromFile(RESOURCES + "backgrounds/background_fight.png");
this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH + "backgrounds/background_fight.png");
this->fightEnv.backgroundSP.setTexture(this->fightEnv.backgroundTX);

this->fightEnv.backgroundMusic.openFromFile(RESOURCES + "music/fight_background_music.wav");
this->fightEnv.backgroundMusic.openFromFile(RESOURCE_PATH + "music/fight_background_music.wav");
this->fightEnv.backgroundMusic.setLoop(true);
this->fightEnv.backgroundMusic.play();

Expand All @@ -25,8 +25,8 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::
std::uniform_int_distribution<int> dist(0, 1);
this->fightEnv.isPlayersTurn = dist(gen);

this->fightEnv.playersTurnTX.loadFromFile(RESOURCES + "combat/turn_status_player.png");
this->fightEnv.enemiesTurnTX.loadFromFile(RESOURCES + "combat/turn_status_enemy.png");
this->fightEnv.playersTurnTX.loadFromFile(RESOURCE_PATH + "combat/turn_status_player.png");
this->fightEnv.enemiesTurnTX.loadFromFile(RESOURCE_PATH + "combat/turn_status_enemy.png");

if (this->fightEnv.isPlayersTurn) {
this->fightEnv.turnSP.setTexture(this->fightEnv.playersTurnTX);
Expand Down
2 changes: 1 addition & 1 deletion src/Activities/FightActivity/FightStates/PlayersTurn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

PlayersTurn::PlayersTurn(FightEnv &fightEnv) {
generateTexture();
this->newColorImage.loadFromFile(RESOURCES + "color_textures/colorPIC_new.png");
this->newColorImage.loadFromFile(RESOURCE_PATH + "color_textures/colorPIC_new.png");
this->oldColorImage = fightEnv.enemyOverview->colorPicker.colorIMG;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Activities/FightActivity/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ UIEnemyOverview::UIEnemyOverview(std::shared_ptr<Enemy> _enemy): enemyBorderedIm
sf::Vector2f creatureFramePos = sf::Vector2f(overviewPos.x + creatureFrameMargin, overviewPos.y + creatureFrameMargin);
this->enemyBorderedImage.setPosition(creatureFramePos.x, creatureFramePos.y);

this->enemyIconBackgroundTX.loadFromFile(RESOURCES + "actor_landscape_backgrounds/forest.png");
this->enemyIconBackgroundTX.loadFromFile(RESOURCE_PATH + "actor_landscape_backgrounds/forest.png");
this->enemyIconBackgroundSP.setTexture(this->enemyIconBackgroundTX);
sf::FloatRect creatureBackgroundRect = this->enemyIconBackgroundSP.getGlobalBounds();
this->enemyIconBackgroundSP.setOrigin(creatureBackgroundRect.width / 2.f, creatureBackgroundRect.height / 2.f);
Expand Down
4 changes: 2 additions & 2 deletions src/Activities/FightActivity/UIPlayerOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ UIPlayerOverview::UIPlayerOverview(std::shared_ptr<Player> _player): uiPlayerSta
this->playerFrame.setPosition(statsPos.x + statsSize.width*0.5 - playerFrameRect.width/2.f, windowSize.y * 0.14);

sf::Vector2f playerFramePos = this->playerFrame.getPosition();
this->playerBackgroundTX.loadFromFile(RESOURCES + "actor_landscape_backgrounds/forest.png");
this->playerBackgroundTX.loadFromFile(RESOURCE_PATH + "actor_landscape_backgrounds/forest.png");
this->playerBackgroundSP.setTexture(this->playerBackgroundTX);
sf::FloatRect playerBackgroundRect = this->playerBackgroundSP.getGlobalBounds();
this->playerBackgroundSP.setOrigin(playerBackgroundRect.width/2.f, playerBackgroundRect.height/2.f);
Expand All @@ -46,7 +46,7 @@ void UIPlayerOverview::init() {
this->playerFrame.setPosition(statsPos.x + statsSize.width*0.5 - playerFrameRect.width/2.f, windowSize.y * 0.14);

sf::Vector2f playerFramePos = this->playerFrame.getPosition();
this->playerBackgroundTX.loadFromFile(RESOURCES + "actor_landscape_backgrounds/forest.png");
this->playerBackgroundTX.loadFromFile(RESOURCE_PATH + "actor_landscape_backgrounds/forest.png");
this->playerBackgroundSP.setTexture(this->playerBackgroundTX);
sf::FloatRect playerBackgroundRect = this->playerBackgroundSP.getGlobalBounds();
this->playerBackgroundSP.setOrigin(playerBackgroundRect.width/2.f, playerBackgroundRect.height/2.f);
Expand Down
6 changes: 3 additions & 3 deletions src/Activities/MenuActivity/MenuActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

MenuActivity::MenuActivity(): Activity() {
Game& game = Game::getInstance();
this->backgroundTX.loadFromFile(RESOURCES + "backgrounds/backgroundMenu.png");
this->backgroundTX.loadFromFile(RESOURCE_PATH + "backgrounds/backgroundMenu.png");
this->backgroundSP.setTexture(this->backgroundTX);

this->backgroundMusic.openFromFile(RESOURCES + "music/menu_background_music.wav");
this->backgroundMusic.openFromFile(RESOURCE_PATH + "music/menu_background_music.wav");
this->backgroundMusic.setLoop(true);
this->backgroundMusic.play();

this->buttonsBackgroundTX.loadFromFile(RESOURCES + "box_backgrounds/menu_border_with_name.png");
this->buttonsBackgroundTX.loadFromFile(RESOURCE_PATH + "box_backgrounds/menu_border_with_name.png");
this->buttonsBackgroundSP.setTexture(this->buttonsBackgroundTX);

sf::Vector2f windowSize = static_cast<sf::Vector2f>(game.gameWindow.getSize());
Expand Down
2 changes: 1 addition & 1 deletion src/Animations/TextFadingManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Animations/TextFadingManager.hpp"

TextFading::TextFading() {
this->font.loadFromFile(RESOURCES + "fonts/Avara-Bold.otf");
this->font.loadFromFile(RESOURCE_PATH + "fonts/Avara-Bold.otf");
this->text.setFont(this->font);
this->text.setString("0");
this->text.setCharacterSize(20);
Expand Down
2 changes: 1 addition & 1 deletion src/Global/GenerateColorIMG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ void generateTexture() {
//int fileNum = std::rand() % 10;
sf::Image colorIMG;
colorIMG.create(GEN_IMG_WIDTH, GEN_IMG_HEIGHT, pixels);
colorIMG.saveToFile(RESOURCES + "color_textures/colorPIC_new.png");
colorIMG.saveToFile(RESOURCE_PATH + "color_textures/colorPIC_new.png");
delete[] pixels;
}
37 changes: 32 additions & 5 deletions src/Global/Utility.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
#include "Utility.hpp"

std::string getResourcePath() {
std::filesystem::path relativeResourcePath = "resources/";
std::filesystem::path absoluteResourcePath = std::filesystem::absolute(relativeResourcePath);
return absoluteResourcePath.string();
}
#ifdef _WIN32
#include <windows.h>

std::string getResourcesDirPath() {
char path[MAX_PATH];
HMODULE hModule = GetModuleHandle(NULL);
GetModuleFileName(hModule, path, MAX_PATH);
return std::filesystem::path(path).remove_filename().string() + "resources/";
}

#elif __linux__
#include <unistd.h>

std::string getResourcesDirPath() {
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::filesystem::path(std::string(result, (count > 0) ? count : 0)).remove_filename().string() + "resources/";
}

#elif __APPLE__
#include <mach-o/dyld.h>

std::string getResourcesDirPath() {
char path[PATH_MAX];
uint32_t size = sizeof(path);
_NSGetExecutablePath(path, &size);
return std::filesystem::path(path).remove_filename().string() + "resources/";
}

#else
#error "Unsupported platform"
#endif
4 changes: 2 additions & 2 deletions src/Global/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <filesystem>
#include <iostream>

std::string getResourcePath();
std::string const RESOURCES = getResourcePath();
std::string getResourcesDirPath();
std::string const RESOURCE_PATH = getResourcesDirPath();

#endif
2 changes: 1 addition & 1 deletion src/System/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Game &Game::getInstance() {
Game::Game(): gameWindow(*(new sf::RenderWindow(sf::VideoMode::getDesktopMode(), "Road of Ogden", sf::Style::Fullscreen))) {
//sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "Road of Ogden", sf::Style::Fullscreen);
this->gameWindow.setFramerateLimit(60);
this->mainFont.loadFromFile(RESOURCES + "fonts/Avara-Bold.otf");
this->mainFont.loadFromFile(RESOURCE_PATH + "fonts/Avara-Bold.otf");
}

Game::~Game() {
Expand Down
10 changes: 5 additions & 5 deletions src/UIElements/UIBorderedImage.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "UIElements/UIBorderedImage.hpp"

UIBorderedImage::UIBorderedImage(std::string imageFilePath, std::string borderFilePath) {
this->imageTX.loadFromFile(RESOURCES + imageFilePath);
this->imageTX.loadFromFile(RESOURCE_PATH + imageFilePath);
this->imageSP.setTexture(this->imageTX);
this->borderTX.loadFromFile(RESOURCES + borderFilePath);
this->borderTX.loadFromFile(RESOURCE_PATH + borderFilePath);
this->borderSP.setTexture(this->borderTX);

sf::FloatRect imageRect = this->imageSP.getGlobalBounds();
Expand All @@ -18,9 +18,9 @@ UIBorderedImage::UIBorderedImage(std::string imageFilePath, std::string borderFi


void UIBorderedImage::init(std::string imageFilePath, std::string borderFilePath) {
this->imageTX.loadFromFile(RESOURCES + imageFilePath);
this->imageTX.loadFromFile(RESOURCE_PATH + imageFilePath);
this->imageSP.setTexture(this->imageTX);
this->borderTX.loadFromFile(RESOURCES + borderFilePath);
this->borderTX.loadFromFile(RESOURCE_PATH + borderFilePath);
this->borderSP.setTexture(this->borderTX);

sf::FloatRect imageRect = this->imageSP.getGlobalBounds();
Expand All @@ -34,7 +34,7 @@ void UIBorderedImage::init(std::string imageFilePath, std::string borderFilePath
}

void UIBorderedImage::setImage(std::string imagePath) {
this->imageTX.loadFromFile(RESOURCES + imagePath);
this->imageTX.loadFromFile(RESOURCE_PATH + imagePath);
this->imageSP.setTexture(this->imageTX);
}

Expand Down
2 changes: 1 addition & 1 deletion src/UIElements/UIBox.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "UIElements/UIBox.hpp"

UIBox::UIBox(sf::Color fillColor, std::string borderFilePath) {
this->borderTX.loadFromFile(RESOURCES + borderFilePath);
this->borderTX.loadFromFile(RESOURCE_PATH + borderFilePath);

this->borderSP.setTexture(this->borderTX);

Expand Down
6 changes: 3 additions & 3 deletions src/UIElements/UIButton.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "UIElements/UIButton.hpp"

void UIButton::init(std::string filePath) {
std::string buttonsPath = RESOURCES + "buttons/";
std::string buttonsPath = RESOURCE_PATH + "buttons/";
int sep_pos = filePath.find(".");
std::string fileName = filePath.substr(0, sep_pos);
std::string fileType = filePath.substr(sep_pos + 1);
Expand All @@ -14,7 +14,7 @@ void UIButton::init(std::string filePath) {
this->hovered = false;
this->pressed = false;

this->pressSoundBuffer.loadFromFile(RESOURCES + "test_sounds/tick.wav");
this->pressSoundBuffer.loadFromFile(RESOURCE_PATH + "test_sounds/tick.wav");
//this->pressSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/buttonAlpha2.wav");
this->pressSound.setBuffer(this->pressSoundBuffer);
//this->releaseSound.setBuffer(this->releaseSoundBuffer);
Expand All @@ -30,7 +30,7 @@ UIButton::UIButton(std::string labelText, std::string filePath) {
this->init(filePath);

sf::Vector2u buttonSize = this->basicTX.getSize();
this->font.loadFromFile(RESOURCES + "fonts/Avara-Bold.otf");
this->font.loadFromFile(RESOURCE_PATH + "fonts/Avara-Bold.otf");
this->label.setFont(this->font);
this->label.setString(labelText);
this->label.setCharacterSize(buttonSize.y * 0.5);
Expand Down
14 changes: 7 additions & 7 deletions src/UIElements/UIColorPicker.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "UIElements/UIColorPicker.hpp"

UIColorPicker::UIColorPicker(std::string imagePath, std::string borderPath) {
this->colorIMG.loadFromFile(RESOURCES + "color_textures/" + imagePath);
this->colorIMG.loadFromFile(RESOURCE_PATH + "color_textures/" + imagePath);
this->colorTX.loadFromImage(this->colorIMG);
this->colorSP.setTexture(this->colorTX);
this->borderTX.loadFromFile(RESOURCES + "borders/" + borderPath);
this->borderTX.loadFromFile(RESOURCE_PATH + "borders/" + borderPath);
this->borderSP.setTexture(this->borderTX);

sf::FloatRect colorRect = this->colorSP.getGlobalBounds();
Expand All @@ -16,7 +16,7 @@ UIColorPicker::UIColorPicker(std::string imagePath, std::string borderPath) {
// For a save small overlap
this->borderSP.scale(0.97, 0.97);

this->releaseSoundBuffer.loadFromFile(RESOURCES + "test_sounds/softair.wav");
this->releaseSoundBuffer.loadFromFile(RESOURCE_PATH + "test_sounds/softair.wav");
this->releaseSound.setBuffer(this->releaseSoundBuffer);
this->releaseSound.setPitch(1.5);
}
Expand All @@ -25,7 +25,7 @@ UIColorPicker::UIColorPicker(sf::Image image, std::string borderPath) {
this->colorTX.loadFromImage(image);
this->colorSP.setTexture(this->colorTX);

this->borderTX.loadFromFile(RESOURCES + "borders/" + borderPath);
this->borderTX.loadFromFile(RESOURCE_PATH + "borders/" + borderPath);
this->borderSP.setTexture(this->borderTX);

sf::FloatRect colorRect = this->colorSP.getGlobalBounds();
Expand Down Expand Up @@ -96,13 +96,13 @@ bool UIColorPicker::clickListener(sf::Vector2f &clickedPos) {
}

void UIColorPicker::setColorBox(std::string picPath, std::string borderPath) {
this->colorIMG.loadFromFile(RESOURCES + "color_textures/" + picPath);
this->colorIMG.loadFromFile(RESOURCE_PATH + "color_textures/" + picPath);
this->colorTX.loadFromImage(this->colorIMG);
this->borderTX.loadFromFile(RESOURCES + "borders/" + borderPath);
this->borderTX.loadFromFile(RESOURCE_PATH + "borders/" + borderPath);
}

void UIColorPicker::setColorImage(std::string picPath) {
this->colorIMG.loadFromFile(RESOURCES + "color_textures/" + picPath);
this->colorIMG.loadFromFile(RESOURCE_PATH + "color_textures/" + picPath);
this->colorTX.loadFromImage(this->colorIMG);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Game* Game::instance = nullptr;

int main()
{

std::cout << getResourcesDirPath() << std::endl;
Game& game = Game::getInstance();

sf::Vector2i mousePos;
Expand Down

0 comments on commit 62275f5

Please sign in to comment.