Exemplo n.º 1
0
// contentDirName is an explicitely specified location for the content,
// or NULL if none was explicitely specified.
// execFile is the path to the uqm executable, as acquired through
// main()'s argv[0].
void
prepareContentDir (const char *contentDirName, const char* addonDirName, const char *execFile)
{
	const char *testFile = "version";
	const char *loc;

	if (contentDirName == NULL)
	{
		// Try the default content locations.
		const char *locs[] = {
			CONTENTDIR, /* defined in config.h */
			"",
			"content",
			"../../content" /* For running from MSVC */
		};
		loc = findFileInDirs (locs, sizeof locs / sizeof locs[0], testFile);

#ifdef __APPLE__
		/* On OSX, if the content can't be found in any of the static
		 * locations, attempt to look inside the application bundle,
		 * by looking relative to the location of the uqm executable. */
		if (loc == NULL)
		{
			char *tempDir = (char *) HMalloc (PATH_MAX);
			snprintf (tempDir, PATH_MAX, "%s/../Resources/content",
					dirname (execFile));
			loc = findFileInDirs ((const char **) &tempDir, 1, testFile);
			HFree (tempDir);
		}
#endif
	}
	else
	{
		// Only use the explicitely supplied content dir.
		loc = findFileInDirs (&contentDirName, 1, testFile);
	}
	if (loc == NULL)
	{
		log_add (log_Fatal, "Fatal error: Could not find content.");
		exit (EXIT_FAILURE);
	}

	if (expandPath (baseContentPath, sizeof baseContentPath, loc,
			EP_ALL_SYSTEM) == -1)
	{
		log_add (log_Fatal, "Fatal error: Could not expand path to content "
				"directory: %s", strerror (errno));
		exit (EXIT_FAILURE);
	}
	
	log_add (log_Debug, "Using '%s' as base content dir.", baseContentPath);
	contentMountHandle = mountContentDir (repository, baseContentPath);

	if (addonDirName)
		log_add (log_Debug, "Using '%s' as addon dir.", addonDirName);
	mountAddonDir (repository, contentMountHandle, addonDirName);

#ifndef __APPLE__
	(void) execFile;
#endif
}
Exemplo n.º 2
0
//------------------------------------------------------------------------------
ShaderAsset::ShaderAsset(Hub& hub, const std::string& filename, 
                         GLenum shaderType) :
    BaseAsset(filename),
    mHasValidShader(false),
    mHub(hub)
{
  std::string targetFilename = findFileInDirs(filename, hub.getShaderDirs(),
                                              false);
  const std::vector<std::string> dirs(hub.getShaderDirs());
  std::ifstream file(targetFilename, std::ios_base::in);
  if (file.is_open() == false)
  {
    Log::message() << "Failed to open shader " << filename << std::endl;
    throw NotFound("Failed to find shader.");
  }

  // Size std::string appropriately before reading file.
  std::string fileContents;
  file.seekg(0, std::ios::end);
  fileContents.resize(static_cast<unsigned int>(file.tellg()));
  file.seekg(0, std::ios::beg);

  // Extra parenthesis are essential to avoid the most vexing parse.
  fileContents.assign( (std::istreambuf_iterator<char>(file)), 
                        std::istreambuf_iterator<char>());
  
  // Now compile the shader file.
  GLuint  shader;
  GLint   compiled;

  // Create the shader object.
  shader = glCreateShader(shaderType);
  GL_CHECK();
  if (0 == shader)
  {
    Log::message() << "Failed to create shader of type: " << shaderType << "\n";
    throw GLError("Unable to construct shader.");
  }

#ifdef SPIRE_OPENGL_ES_2
  const size_t numShaderSources = 2;
  const char* cFileContents[numShaderSources] = 
    {"#define OPENGL_ES\n#define OPENGL_ES_2\n", fileContents.c_str()};
  GL(glShaderSource(shader, numShaderSources, cFileContents, NULL));
#else
  const char* cFileContents = fileContents.c_str();
  GL(glShaderSource(shader, 1, &cFileContents, NULL));
#endif

  GL(glCompileShader(shader));

  // Check the compile status
  GL(glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled));

  // Check compilation status.
  if (!compiled)
  {
    GLint infoLen = 0;
  
    GL(glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen));
    if (infoLen > 1)
    {
      char* infoLog = new char[infoLen];

      GL(glGetShaderInfoLog(shader, infoLen, NULL, infoLog));
      Log::error() << "Error compiling '" << filename << "':" << std::endl << infoLog 
                   << std::endl;

      delete[] infoLog;
    }

    GL(glDeleteShader(shader));
    throw GLError("Failed to compile shader.");
  }

  mHasValidShader = true;
  glID = shader;
}