1

I'm watching the tutorial for adding image in SDL game, but author uses Visual Studio. I'm on the XCode and I'm not sure where to add the assets folder. I tried with right click on root of application > Add files to "Project" > destination of the assets folder but image is not showing in the window.

main.cpp

#include "Game.hpp"

Game *game = nullptr;

int main() {

    game = new Game();

    game->init("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);

    while (game->running()) {
        game->handleEvents();
        game->update();
        game->render();
    }

    game->clean();

    return 0;
}

This is the initializing method where I add image at the end of the function:

Game.cpp

void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
    int flags = 0;
    if (fullscreen) {
        flags = SDL_WINDOW_FULLSCREEN;
    }

    if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
        std::cout << "Subsystems initalized." << std::endl;

        window = SDL_CreateWindow(title, xpos, ypos, width, height, fullscreen);

        if (window)
        {
            std::cout << "Window created!" << std::endl;
        }

        renderer = SDL_CreateRenderer(window, -1, 0);

        if (renderer)
        {
            SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
            std::cout << "Renderer created!" << std::endl;
        }

        isRunning = true;
    } else {
        isRunning = false;
    }

    SDL_Surface *tempSurface = IMG_Load("assets/player.png");
    playerTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
    SDL_FreeSurface(tempSurface);
}

And this is the render method:

Game.cpp

void Game::render()
{
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, playerTexture, NULL, NULL);
    SDL_RenderPresent(renderer);
}
Nikola Stojaković
  • 2,257
  • 4
  • 27
  • 49
  • Does the working directory for your Xcode run target point to your source tree? – Botje May 28 '18 at 11:09
  • @Botje Sorry, I'm not sure I understand you. How can I check it? – Nikola Stojaković May 28 '18 at 11:10
  • The path you give is a relative path, so SDL tries to load it starting from the current working directory. I know from previous experience that Xcode does not set the working directory equal to your source directory. Maybe https://meandmark.com/blog/2013/12/setting-the-current-working-directory-for-xcode-command-line-projects/ still applies to modern-day Xcode? – Botje May 28 '18 at 11:14
  • @Botje I understand now. I found it in `Product` > `Scheme` > `Edit Scheme` > `Options`. – Nikola Stojaković May 28 '18 at 11:40

1 Answers1

2

Thanks to the help of Botje's comment and this answer I solved the problem. I had to set up working directory to point to my source folder.

If you have the same problem just go to Product > Scheme > Edit Scheme... > Options, check Use custom working directory, click on the small folder icon and add your source directory.

Nikola Stojaković
  • 2,257
  • 4
  • 27
  • 49