示例#1
0
static void loadShader(const char* fname, gl::Shader& shader) {
	std::vector<std::string> filenames;
	std::string source;

	preprocessFile(source, fname, filenames);
	shader.setSource(source.c_str());
	shader.compile();
	if (!shader.compileSuccess()) {
		for (unsigned int i = 0; i < filenames.size(); ++i) {
			std::cerr << '(' << i << "): " << filenames[i] << '\n';
		}
		shader.printInfoLog(std::cerr);
	}
}
示例#2
0
static void preprocessFile(std::string& source, const std::string& fname, std::vector<std::string>& file_list) {
	static const std::string shader_path("data/shaders/");
	std::ifstream f(shader_path + fname);
	if (!f) {
		source.append("#error File not found: ");
		source.append(fname);
		source.append(".\n");
	}

	unsigned int file_n = file_list.size();
	file_list.push_back(fname);

	if (file_n > 0) {
		source.append("#line 1 ");
		source.append(std::to_string((unsigned long long)file_n));
		source.push_back('\n');
	}

	std::string line;
	unsigned int line_n = 1;
	while (std::getline(f, line)) {
		line_n += 1;
		if (line.substr(0, 8) == "#include") {
			std::smatch results;
			if (std::regex_match(line, results, include_re)) {
				preprocessFile(source, results[1].str(), file_list);
				
				source.append("#line ");
				source.append(std::to_string((unsigned long long)line_n));
				source.push_back(' ');
				source.append(std::to_string((unsigned long long)file_n));
				source.push_back('\n');
			} else {
				source.append("#error Malformed include directive.\n");
			}
		} else {
			source.append(line);
			source.push_back('\n');
		}
	}
}
/** 
 * Reads the style template info stored in the given (XML) file and creates style templates (OpenGL display lists).
 * Existing templates remain where they are but are replaced if a new definition with an existing name is found.
 *
 * @param filename The name of the file to load.
 * @param variables A set of name/value pairs to be replaced in the xml text.
 * @return Returns GC_NO_ERROR if everything was ok, otherwise an error code.
 */
TGCError CGenericCanvas::addStylesFromFile(const char* filename, map<wstring, wstring>& variables)
{
  TGCError result = GC_NO_ERROR;
  xmlNodePtr root, current;
  CSVGParser parser;
  xmlDocPtr document = NULL;
  string currentDir = getCurrentDir();
  
  if (!variables.empty())
  {
    char *data;
    size_t length;

    data= preprocessFile(filename, variables, length);
    if (data)
    {
      document = xmlParseMemory(data, (int) length);
      free(data);
    }
  }
  else
    document = xmlParseFile(utf8ToANSI(filename).c_str());

  if (document == NULL)
    return GC_XML_PARSE_ERROR;                       

  root = xmlDocGetRootElement(document);

  if (root == NULL)
  {
    xmlFreeDoc(document);
    error(this, "XML Error: Template file is empty.");
    return GC_XML_EMPTY_DOCUMENT;
  }
  
  if (!XML_IS(root, "gc-styles"))
  {
    xmlFreeDoc(document);
    error(this, "XML Error: Template file invalid.");
    return GC_XML_INVALID_DOCUMENT;
  }

  // Switch to the directory of the given file. This is necessary to make relative file names working.
  string path = extractFilePath(filename);
  setCurrentDir(path);

  // Parse description elements.
  glMatrixMode(GL_MODELVIEW);
  current = root->children;
  while (current != NULL)
  {
    // Be flexible, ignore any unknown layout entries.
    if (XML_IS(current, "style-definition"))
    {
      parser.parseDefinition(current, FModel);
    }
    else
      if (XML_IS(current, "texture"))
      {
        // Adds a new texture to the texture list.
        parseTextureEntry(current, FModel);
        checkError();
      };
    current = current->next;
  }

  setCurrentDir(currentDir);
  xmlFreeDoc(document);

  return result;
}