Sign in to follow this  

Load scripts...?

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this