Exemple #1
0
/* Include search order is intended to be compatible with C/C++ compilers
 * and is as follows:
 *
 * 1) For absolute pathnames, try only that pathname and nothing else
 *
 * 2) For includes in double quotes only, search current directory
 *
 * 3) For includes in double quotes only, search the directory
 *    of including file
 *
 * 4) Search include directories specified by IncludePath1 (usually command
 *    line -I argument(s)
 *
 * 5) Search include directories specified by IncludePath2 (usualy INCLUDE path)
 *
 * 6) Directory 'h' adjacent to current directory (../h)
 *
 * Note that some of these steps will be skipped if PPFLAG_IGNORE_CWD and/or
 * PPFLAG_IGNORE_INCLUDE is set.
 */
int PP_FindInclude( const char *filename, size_t len, char *fullfilename, int incl_type )
{
    int         rc = -1;
    char        drivebuf[_MAX_DRIVE];
    char        dirbuf[_MAX_DIR];

    memcpy( fullfilename, filename, len );
    fullfilename[len] = '\0';
    if( HAS_PATH( fullfilename ) ) {
        rc = access( fullfilename, R_OK );
    } else {
        if( rc == -1 && incl_type != PPINCLUDE_SYS && (PPFlags & PPFLAG_IGNORE_CWD) == 0 ) {
            rc = access( fullfilename, R_OK );
        }
        if( rc == -1 && incl_type == PPINCLUDE_USR && PP_File != NULL ) {
            size_t  len1;

            _splitpath( PP_File->filename, drivebuf, dirbuf, NULL, NULL );
            _makepath( fullfilename, drivebuf, dirbuf, NULL, NULL );
            len1 = strlen( fullfilename );
            if( len1 > 0 ) {
                char c = fullfilename[len1 - 1];
                if( !IS_PATH_SEP( c ) ) {
                    fullfilename[len1++] = DIR_SEP;
                }
            }
            memcpy( fullfilename + len1, filename, len );
            fullfilename[len1 + len] = '\0';
            rc = access( fullfilename, R_OK );
        }
        if( rc == -1 && IncludePath1 != NULL ) {
            rc = findInclude( IncludePath1, filename, len, fullfilename );
        }
        if( rc == -1 && IncludePath2 != NULL ) {
            rc = findInclude( IncludePath2, filename, len, fullfilename );
        }
        if( rc == -1 && incl_type == PPINCLUDE_USR && (PPFlags & PPFLAG_IGNORE_DEFDIRS) == 0 ) {
            memcpy( fullfilename, H_DIR, sizeof( H_DIR ) - 1 );
            memcpy( fullfilename + sizeof( H_DIR ) - 1, filename, len );
            fullfilename[sizeof( H_DIR ) - 1 + len] = '\0';
            rc = access( fullfilename, R_OK );
        }
    }
    return( rc );
}
Exemple #2
0
/* Include search order is intended to be compatible with C/C++ compilers
 * and is as follows:
 *
 * 1) For absolute pathnames, try only that pathname and nothing else
 *
 * 2) For includes in double quotes only, search current directory
 *
 * 3) For includes in double quotes only, search the directory
 *    of including file
 *
 * 4) Search include directories specified by IncludePath1 (usually command
 *    line -I argument(s)
 *
 * 5) Search include directories specified by IncludePath2 (usualy INCLUDE path)
 *
 * 6) Directory 'h' adjacent to current directory (../h)
 *
 * Note that some of these steps will be skipped if PPFLAG_IGNORE_CWD and/or
 * PPFLAG_IGNORE_INCLUDE is set.
 */
int PP_FindInclude( const char *filename, char *fullfilename, int incl_type )
{
    int         rc = -1;
    char        drivebuf[ _MAX_DRIVE ];
    char        dirbuf[ _MAX_DIR ];

    if( HAS_PATH( filename ) ) {
        if( (rc = access( filename, R_OK )) == 0 ) {
            strcpy( fullfilename, filename );
        }
    } else {
        if( rc == -1 && incl_type != PPINCLUDE_SYS && (PPFlags & PPFLAG_IGNORE_CWD) == 0 ) {
            if( (rc = access( filename, R_OK )) == 0 ) {
                strcpy( fullfilename, filename );
            }
        }
        if( rc == -1 && incl_type == PPINCLUDE_USR && PP_File != NULL ) {
            size_t  len;

            _splitpath( PP_File->filename, drivebuf, dirbuf, NULL, NULL );
            _makepath( fullfilename, drivebuf, dirbuf, NULL, NULL );
            len = strlen( fullfilename );
            if( len > 0 ) {
                char c = fullfilename[len - 1];
                if( !IS_PATH_SEP( c ) ) {
                    fullfilename[len++] = c;
                }
            }
            strcpy( fullfilename + len, filename );
            rc = access( fullfilename, R_OK );
        }
        if( rc == -1 && IncludePath1 != NULL ) {
            rc = findInclude( IncludePath1, filename, fullfilename );
        }
        if( rc == -1 && IncludePath2 != NULL ) {
            rc = findInclude( IncludePath2, filename, fullfilename );
        }
        if( rc == -1 && incl_type == PPINCLUDE_USR && (PPFlags & PPFLAG_IGNORE_DEFDIRS) == 0 ) {
            sprintf( fullfilename, H_DIR "%s", filename );
            rc = access( fullfilename, R_OK );
        }
    }
    return( rc );
}
Exemple #3
0
  Shader::Shader(const string& filename, ShaderType type) {
    filename_ = jtil::string_util::ToWideString(filename);
    compiled_ = false;

    // Read in the raw shader source
    shader_source_ = readFileToBuffer(filename);

    // Append the include code: We need to scan through the code looking for
    // lines that have "#include" and replace them with the correct text.
    // This could be much more efficient, but is fast enough for just startup
    int include_pos = findInclude(shader_source_);
    while (include_pos != -1) {
      string f_include = extractIncludeFilename(shader_source_, include_pos);
      GLchar* inc_source = readFileToBuffer(f_include);
      shader_source_ = insertIncludeSource(shader_source_, inc_source, 
        include_pos);
      free(inc_source);

      // There might be more, so find the next one
      include_pos = findInclude(shader_source_);
    }

    // Assign a shader handle
    switch (type) {
    case VERTEX_SHADER:
      shader_ = GLState::glsCreateShader(GL_VERTEX_SHADER);
      break;
    case FRAGMENT_SHADER:
      shader_ = GLState::glsCreateShader(GL_FRAGMENT_SHADER);
      break;
    default:
      throw std::runtime_error("Shader::Shader() - unrecognized shader type!");
    }

    // Send the shader source code to OpenGL
    // Note that the source code is NULL character terminated.
    // GL will automatically detect that therefore the length info can be 0 
    // in this case (the last parameter)
    GLState::glsShaderSource(shader_, 1, const_cast<const GLchar**>(&shader_source_), 0);

    // Compile the shader
    GLState::glsCompileShader(shader_);

    // Check that everything compiled OK
    int is_compiled;
    GLState::glsGetShaderiv(shader_, GL_COMPILE_STATUS, &is_compiled);
    if(is_compiled == 0) {
      int info_length;
      GLState::glsGetShaderiv(shader_, GL_INFO_LOG_LENGTH, &info_length);

      // The maxLength includes the NULL character
      char* shader_info_log;
      shader_info_log = new char[info_length];

      GLState::glsGetShaderInfoLog(shader_, info_length, &info_length, shader_info_log);
      ERROR_CHECK;
      wstring err_log = jtil::string_util::ToWideString(shader_info_log);
      delete[] shader_info_log;
      
      std::cout << std::endl;
      std::cout << "Cannot compile the following shader code: ";
      std::cout << jtil::string_util::ToNarrowString(filename_).c_str() << std::endl;
      std::cout << "  --> Compilation error: " << std::endl;
      std::cout << jtil::string_util::ToNarrowString(err_log).c_str();
      std::cout << "  --> For the code:" << std::endl;
      std::cout << "*********************************************************";
      std::cout << std::endl;
      printToStdOut();
      std::cout << "*********************************************************";
      std::cout << std::endl;

      throw std::runtime_error(string("Shader::Shader() - ERROR compiling") +
        string(" shader from file: ") + jtil::string_util::ToNarrowString(filename_) + string(": ") + 
        jtil::string_util::ToNarrowString(err_log));
    } else {
      compiled_ = true;
    }
  }