How to handle resources in a C ++ library

I am creating a very lightweight library / shell for OpenGL and I am having a little problem. Let's say I have a directory structure that looks something like this:

GraphicsProject |GraphicsApp ||main.cpp |GraphicsLib ||include |||fileA.h |||fileB.h ||library |||fileA.cpp |||fileB.cpp ||shaders |||shader1.txt |||shader2.txt || build ||| bunch of build stuff 

What is the best way to get the path to shader1.txt at runtime? Or else, to make sure that the path does not change regardless of who is using this project or where it is located on the user machine?

Some options that I considered: 1) Get the current working directory and use this path to work to where I need it. My main concern with this solution is that I'm not sure how much the current directory will change based on how the user builds the project or other factors out of control. It is also not very elegant. 2) Save the source code for the shaders as char arrays / lines in the .h file, which is located in the include directory. This seems like a better solution, except that it will make recording shaders a bit more cumbersome.

Is there a standard way to do this? I use CMake to create a project if that changes anything.

+4
source share
1 answer

I assume that you are talking about making the "shader" folder accessible to other developers who are building your project, because if you meant end users (which would include install components), you would not talk about assembling folders.

I think the easiest way to provide a fixed location in the shader folder at runtime is to copy it from the source tree to the assembly tree. Thus, other developers can place their root folder anywhere outside of your project (or even inside if you want), and the application will still have one fixed relative path for accessing the folder of copied shaders.

There are other options if you do not want to copy (for example, you could write a CMake configuration file in the assembly tree, you can specify the path to the shader folder in the configuration file), but I think they are more complex and probably more fragile. Despite this, I think that the main task is to copy information from the source tree to the assembly tree at the configure time (when CMake is running), so the runtime code does not require a complex search for a potentially very remote source tree.

Example root CMakeLists.txt:

 cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(example) add_executable(GraphicsApp GraphicsApp/main.cpp) add_library(GraphicsLib GraphicsLib/library/fileA.cpp GraphicsLib/library/fileB.cpp GraphicsLib/include/fileA.h GraphicsLib/include/fileB.h GraphicsLib/shaders/shader1.txt GraphicsLib/shaders/shader2.txt ) include_directories(GraphicsLib/include) target_link_libraries(GraphicsApp GraphicsLib) add_custom_command(TARGET GraphicsApp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/GraphicsLib/shaders $<TARGET_FILE_DIR:GraphicsApp>/shaders) 

To expand add_custom_command , this basically causes the shaders subdirectory to be copied to the folder into which GraphicsApp will be built. The command is executed at any time by GraphicsApp . If GraphicsApp built and updated, and you are trying to rebuild it, the user command will not be executed.

The user command actually executes

 cmake -E copy_directory <path to shaders source> <path to shaders copy> 

using CMake -E cross-platform mode command.

The "target" part of the command ("copy to") uses a generator expression to infer the location where GraphicsApp will be built: $<TARGET_FILE_DIR:GraphicsApp> . This is probably the most reliable way to achieve this; it will be correct regardless of the type of configuration (where, for example, MSVC inserts the "Debug /" or "Release /" folder into the build path).

So, this, I hope, will give you most of the way to your goal. I assume that you should still get the full path to the executable exe , and then output the full path to the copied shader folder. However, this should be much simpler and more reliable than searching from the current working directory. Even calculating the current working directory is nontrivial.

+5
source

Source: https://habr.com/ru/post/1495148/


All Articles