2021年5月5日星期三

How to render images in a exe file created with SDL2, C++ and Visual Studio Installer projects?

I'm trying to create an executable file to run a game I'm making with C++ and SDL2 in Visual Studio Community 2019. When I run the program from within Visual Studio (build and run) it renders all of the images correctly. However, After I use Microsoft Visual Studio Installer projects to create a exe file it does not load any of the images and just renders a black screen. I have tried using global filepaths for the images, relative paths, and pasting the images into the working directory of the exe file. All of these result in the console printing "failed to load image ...".

Code:

SDL_Texture* TextureManager::LoadTexture(const char* texture)  {      std::cout << "loading " << texture << std::endl;        SDL_Surface* tempSurface = nullptr;      tempSurface = IMG_Load(texture);      if (!tempSurface)          std::cout << "failed to load image "<<texture<<"\n";        SDL_Texture* tex = nullptr;      tex = SDL_CreateTextureFromSurface(Game::renderer, tempSurface);      if (!tex)          std::cout << "failed to load texture" << texture << "\n";        SDL_FreeSurface(tempSurface);        return tex;  }  

Here are some examples of filepaths I have tried:

    "D:/Dev/Asteroid_old/GameEngine/GameEngine/assets/stars.png"      "D:\\Dev\\Asteroid_old\\GameEngine\\GameEngine\\assets\\stars.png"      "D/Dev/Asteroid_old/GameEngine/GameEngine/assets/stars.png" (also with \\)  

I have also used GetModuleFileName to get the current working directory and insert that at the beginning of the filename.

Any suggestions on what it is looking for in a filepath for the images?

EDIT: My original searches about this led me to believe it was a filepath problem but after using IMG_GetError I found out visual studio project installer was not correctly detecting all dependencies (libpng16-16.dll was not found). I found a different tutorial for project installer that includes a step for manually adding dependencies. Thank you for the help. https://docs.microsoft.com/en-us/cpp/windows/walkthrough-deploying-a-visual-cpp-application-by-using-a-setup-project?view=msvc-160

https://stackoverflow.com/questions/67410242/how-to-render-images-in-a-exe-file-created-with-sdl2-c-and-visual-studio-inst May 06, 2021 at 08:05AM

没有评论:

发表评论