Load scripts...?

Started by
2 comments, last by Zakwayda 19 years, 6 months ago
In my engine I am organizing my materials in seperate files... for example:

// test.material
test {
	texture textures/test.tga
}


To read that file, I have something like this:

bool CMaterial::loadFromFile(const char* fileName, ...) {
		bool error = false;
		bool inBrackets = false;
		bool gotName = false;
		string currentRead;

		logger->newMsg("Loading material file '%s'", fileName); //! Logger Message

		ifstream materialFile(fileName);

		if(!materialFile.is_open()) {
			logger->newMsg("Loading material file failed"); //! Logger Message
			return false;
		}

		while(!materialFile.eof() && !error) {
			materialFile >> currentRead;
			//logger->newMsg("Syntax error: Unknown symbol '%s'", currentRead.c_str());
			
			if(!inBrackets) {
				if(currentRead == "{" && gotName){
					inBrackets = true;
				} else if(currentRead == "{" && !gotName){
					error = true;
					logger->newMsg("Syntax error: Unexpected '{' (name of material must be called first)");
				} else if(!gotName) { //! Assumes that this is the name of the material
					gotName = true;
					materialName = currentRead;
				}
				else if(gotName) { 
					error = true;
					logger->newMsg("Syntax error: Unknown symbol '%s'", currentRead.c_str());
				}
			}
			else {
				
				if(currentRead == "texture") {
					getline(materialFile, currentRead);
					loadTexture(currentRead.c_str());
				}
				
			}
			
		}

		if(error) {
			logger->newMsg("Loading material file failed"); //! Logger Message
			return false;
		}
		
		materialFile.close();
		logger->newMsg("Loading material file successful"); //! Logger Message

		return true;
	}


Is this the right idea for how to load a file like this? Also, does == work with std::string? Thanks
Advertisement
I didn't go through all the logic of your code, but this looks similar to my shader loading code, and to other similar code that I have seen.

std::string does overload ==, but AFAIK it's case-sensitive only. In my engine I want shader keywords to be case-insensitive, so I'm using !stricmp() instead.

I did do some digging around to find out if there was a way to make a case-insensitive std::string, but didn't find anything. If there is a way, I'd love to know about it.
Quote:Original post by jyk
I did do some digging around to find out if there was a way to make a case-insensitive std::string, but didn't find anything. If there is a way, I'd love to know about it.
Here. The code is a little shorter than I expected.
Cool - thanks for the link! I will definitely look into that.

This topic is closed to new replies.

Advertisement