Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Read Configuration Files Code

Author
Message
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 16th Feb 2010 23:38 Edited at: 16th Feb 2010 23:42
Hi everyone, i just wanted to contribute a little to everyone here. Yesterday i was checking my code to make it better and more understandable and all that, and i remember a lot of people having trouble with Dark GDKs integrated file functions. Contrary of what you may think, the Dark GDK In Built file system is not "crap" or "crappy" or whatever. Yes i know you can use fstream. people just dont know how it works. ive made a simple code to read a file, compare some strings and set some resolution/media folder settings. i will explain the code below:

first of all, declare some variables to work with.
next, call the dbOpenToRead command. this is obvious.
then check if the file was opened, or if it is empty, and set the default variables if no file is found, here, you can implement a file maker, which maybe ill discuss later in this same thread.
next, if the file is open, start by reading the first line, and tokenizing it, or " separating it" where a "=" is found with dbFirstToken. then compare the string you have using strncmp or strcmp, the only difference between those two is that in one you specify the number of letters or characters to compare. you can see it in the code.

Then, if comparing succeeds, say, at ResX, then use dbNextToken("="); to read what is next to ResX=. then convert it to whatever you need it, i used atoi to convert it to Int, and store it wherever you want. i used int resx. just add more ifs to compare the string to whatever you want.
when the end of file is reached, close the file. this code goes before the main loop. Oh! i almost forgot, i think this is only for version 7.4, since i dont remember having the tokenizer system integrated in Dark. i know this may not be the best place to post this but i know more people starting will read it here.

this is just some part of the code im using, as simple as it can get. i really hope this helps a lot of people as i know there are people who have asked for it.. well i investigated, tested and here it is for all of those who want to put it in your Apps, Games or program. hope i get into your game credits

hi
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Feb 2010 00:19
Quote: "the Dark GDK In Built file system is not "crap" or "crappy" or whatever"


Yes it is, you can't even append to files. I don't know why the file library was even included with GDK because of fstream.

The code you posted will leak memory because you don't handle deallocation of the string stored here: "char *ConfigFileLine=dbReadString(1)", nor here: "CheckConfigFileLine = dbFirstToken(ConfigFileLine,"=");" as well as the other places you retrieve strings. If the standard library does something GDK does then don't use the GDK one because it will do it worse.

Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 17th Feb 2010 00:57 Edited at: 17th Feb 2010 01:01
oh well its a start isnt it!? maybe you can explain how can i avoid this? by creating char ConfigFileLine[MAX_PATH] and char CheckConfigFileLine[MAX_PATH]? at least it does read files. maybe its not good for writing them and the code can be optimized cant it? Dark Coder, ive seen some if not all of your games, im impressed. oh well just trying to be helpfull. with 4 gb systems 1mb leaked memory isnt gonna hurt somebody is it?( for testing ).. with some code you can append two files... am i wrong? i know it wouldnt be so... optimized... but it would work wouldnt it?

hi
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 17th Feb 2010 02:00
It's not your usage of the strings that's the issue, but every GDK function that returns a char* requires deallocation by you because it's allocated on the heap(and then forgotten about). That is, something like 'char* plop = dbStr( 1337 );' requires a 'delete [] plop;' somewhere along the line. Also, unless you're writing low-level string operations you shouldn't really handle the char* variant because of this, it makes far more sense to use std::string or similar for anything related to strings.

Quote: "and the code can be optimized cant it?"


Other than limiting use of GDK, the most obvious one would be to reduce the complexity of your string lookup. This is the main weakness of C++ because it aims to keep things low level, so writing a simple routine to essentially do a string 'switch' statement isn't really possible. But for something that's most likely done at loading time it's not important.

Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 17th Feb 2010 02:51 Edited at: 17th Feb 2010 03:21
great, thanks for the tip then it does have some cra* in there
but it works! :p hehe i was just showing off Final Exodus to a friend of mine and he says you are the master XD... when i upgrade my veideocard i will try some shader tests... see if i can make my game look better thanks again

hi

Login to post a reply

Server time is: 2024-10-02 01:20:59
Your offset time is: 2024-10-02 01:20:59