Forseti
Forseti

Reputation: 21

.exe wont compile but does when run in visual studio

the problem only comes up when the .exe file it says its trying to use the file reader method to find the shaders I'm using but when I run it in visual studio it works fine. This is the file reading method

    static std::string read_file(const char* filepath)
    {
       FILE* file = fopen(filepath, "rt");
       fseek(file, 0, SEEK_END);
       unsigned long length = ftell(file);
       char* data = new char[length + 1];
       memset(data, 0, length + 1);
       fseek(file, 0, SEEK_SET);
       fread(data, 1, length, file);
       fclose(file);

       std::string result(data);
       delete[] data;
       return result;
    }

specifically it creakes a break on the fseek(file, 0, SEEK_END) part the line that calls it is

shader = new Shader("basic.vert", "basic.frag");

the files are in the same folder as the .cpp and in the .exe folder. the shader is defined here

Shader::Shader(const char* vertexPath, const char* fragPath)
    : m_VertexPath(vertexPath), m_FragPath(fragPath)
{
        m_ShaderID = load();
}

GLuint Shader::load()
{
    GLuint program = glCreateProgram();
    GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);

    std::string vertexSourceString = read_file(m_VertexPath).c_str();
    std::string fragSourceString = read_file(m_FragPath).c_str();

    const char* vertexSource = vertexSourceString.c_str();
    const char* fragSource = fragSourceString.c_str();



    glShaderSource(vertex, 1, &vertexSource, NULL);
    glCompileShader(vertex);

    GLint result;
    glGetShaderiv(vertex, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE)
    {
        GLint length;
        glGetShaderiv(vertex, GL_INFO_LOG_LENGTH, &length);
        std::vector<char> error(length);
        glGetShaderInfoLog(vertex, length, &length, &error[0]);
        std::cout << "Failed to compile vertex shader :(" << std::endl << &error[0] << std::endl;
        glDeleteShader(vertex);
        return 0;
    }

    glShaderSource(fragment, 1, &fragSource, NULL);
    glCompileShader(fragment);


    glGetShaderiv(fragment, GL_COMPILE_STATUS, &result);
    if (result == GL_FALSE)
    {
        GLint length;
        glGetShaderiv(fragment, GL_INFO_LOG_LENGTH, &length);
        std::vector<char> error(length);
        glGetShaderInfoLog(fragment, length, &length, &error[0]);
        std::cout << "Failed to compile fragment shader :(" << std::endl << &error[0] << std::endl;
        glDeleteShader(fragment);
        return 0;
    }

    glAttachShader(program, vertex);
    glAttachShader(program, fragment);

    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vertex);
    glDeleteShader(fragment);

    return program;
}

sorry if this is a bit long but I wanted to be thorough and I wasnt sure which bit may be causing the problem.

Upvotes: 0

Views: 780

Answers (1)

Xirema
Xirema

Reputation: 20396

You need to make sure that the basic.vert and basic.frag files are in the same directory as the executable. OR, you need to specify Absolute Paths when identifying where the files are. OR, you need to invest development into a "Resource Loader" type object which will dynamically query for and load external resources into your program; as an example, I'd write a program that recursively searches through all the subdirectories of the folder the program is located in, and maps each file to an addressable resource in your program.

This isn't a problem when you run the program in Visual Studio because the files that have to be read in are located in the source directory of your project, and by default, when Visual Studio is debugging your program, it uses the source directory as the Working Directory for your program, whereas when you run the .exe on its own, it uses the directory that the executable is located in (or the directory the script running it is located in).

Upvotes: 2

Related Questions