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 / Saving tile maps as files

Author
Message
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 05:09
I'm fairly new to programming. I've read the entire "Beginner's Guide to C++" that Microsoft handed out and know most of the sprite commands in the DarkGDK quite well. I also have a good understanding of how the language works ( well, programming languages in general; the process of building and such ).

I have started a tile-based system for a 2D game. The tile grid is stored in a vector of vectors. I particularly like this approach.



sTile is defined as such:



I would like to store this tile grid in a file ( possibly the standard .map or .grid or something ). I've looked into C++ I/O a bit, and I have found a few tutorials that teach basic text file writing and similiar. I can save the map data into a file, but I can't read it back into the vector. How should I save and load the map? The tutorials I found weren't very video game-centered.
aersixb9
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 10th Jun 2008 06:18
You can only store primitives in files for some reason...they haven't written a good object-storage-mechanism in files? Ironically you can't store objects in *@%&#*&%@# databases either...

Basically you'll need some custom code to save all the variables inside the vector into a text file, and then to read all the variables out...does that make any sense?

Alternatively you could serialize the vector, but that's probably even more hassle...although I'm a serialization noob?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 06:20
Is there any variable-to-string conversion process I need to know about?
aersixb9
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 10th Jun 2008 07:43
LOLZ! NO! IT'S C++ SO YOU HAVE TO USE atoi AND itoa TWO OF THE *EASIEST* FUNCITONS EVER!!!!!

Whoops. Sorry for yelling, but I've been programming in C# (Which fixed strings...but broke D3D!) and, uh, anyways you'll have to use atoi and itoa...and they have the parameters from hell...

Probably have to google up pages of documentation just to get those two working! GL!
aersixb9
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 10th Jun 2008 07:45
Oh, wait, I just remembered the easier way.

Include fstream.h (it's c++)

Then make an ifstream object to read files and an ofstream object to write files...then you can use << and >> to extract different types of data! Much easier...alternatively you can use fprintf to convert your ints to strings...heh...or you could do it the hard way...
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 07:45
Sorry, that's the one thing I don't know about: casting.
aersixb9
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 10th Jun 2008 07:45
On a side note, don't drink and post.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 07:53
XD

I was using fstream, actually. It just didn't work right. I'll try some different ways, though. Thanks for the help.
aersixb9
16
Years of Service
User Offline
Joined: 29th Nov 2007
Location:
Posted: 10th Jun 2008 07:59
Here, I'll write some code!



Please note there's only a 10% chance this code works. I'm all hopped up on beverages right now. Really strong upper beverages. Not meth or anything (that's illegal)...
}
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 08:31
I'll test it out in the morning. Thanks for the help. I appreciate it.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 10th Jun 2008 08:35
I found this: http://irc.essex.ac.uk/www.iota-six.co.uk/c/g3_atoi_itoa_sprintf_sscanf.asp

I always wanted a function that did that. Thanks!
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 11th Jun 2008 19:29
Hey. I'm just coming back to ask if I'm doing this right. Here is my function to save the map:



I << a c into the file to mark in between the two grids. Then, I have a dummy loading function for demonstration:



My idea is this: what if I load the contents of the saved file into a character array ( or vector ) and process that? For instance, each character until a "c" is found is put into the type of the appropriate sTile. Then, after a "c" is found, all are put into the same sTiles. Would that be a start?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 11th Jun 2008 19:35
Wait. Another brainstorm. Could I make a loop like this?



Something to that effect?
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 11th Jun 2008 19:41
You can use DarkGDK if you want.

To conver a number to a string use dbStr().

To open a file to write in it, dbOpenToWrite( FileNum, Name )

to close it, dbCloseFile( 1 )

to write something, dbWriteString( FileNum, String )


Of course, you could cut out the middle man and write the number directly:
dbWriteByte( FileNum, Data ) <- I THINK

Don't you just hate that Zotoaster guy?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 11th Jun 2008 19:43
I'd much prefer to use standard C++. But, I will look into those functions. They may be a good temporary fill in.

What do you think of the code I posted? Is it a step in the right direction?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 12th Jun 2008 04:51
Can anyone give me more details on the dbWrite commands? The documentation is lacking.
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 14th Jun 2008 11:23
What is it that you need to know about dbWrite?

I also like the vector container alot...it fills my dynamic array needs nicely. I use it in plug-ins to expose the same sort of object number system that GDK/DBPro do.

I was going to post in here a few days ago, but I figured that you were fine doing things this way. Now that you've directed me here from your other thread, I feel compelled to mention that GDK has a whole section of commands dedicated to solving this problem for you without using file I/O, or having to process definitions, etc.

There is an object in GDK called a matrix, which is a 2D plane that is divided into a grid. There are associated commands to process an image for use in setting the image to the matrix by tile number.

You make an image with all of the tiles, arranged as a grid. Then you create a matrix, and prepare the loaded image for use in the matrix. Then you set the image bits to the tiles in the matrix. There are alot of commands for manipulating the matrix.

Check out the Matrix commands in the help. There is a sample that demonstrates the use of a matrix in GDK, too. It is a water matrix which does not ply the tiling feature in the way that you want, but...it shows how to create and use a matrix. Maybe the other samples use a matrix. They are popular for scrollers and also terrain use.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 14th Jun 2008 15:31
I already figured out what to do on my own. I went with an osfstream. I save the file with this:



Then load it with this:



It works great. I can't believe that I couldn't get it working on my own. I almost feel stupid for asking. I'm just glad I can now move on. Thanks for the help that you've given.

P.S. I'll take a look at the matrix thing.
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 14th Jun 2008 17:15 Edited at: 14th Jun 2008 17:17
No problem, I am glad to see that you went straight to the best one, anyway. You can or [|] attributes with streams, too...so, you could overwrite an existing file using:
ios_base::binary | ios_base::trunc

The matrix commands could still work well for you...your file would have the tile x and y pairs for a given map. It would go a long way toward making your own map system using the matrix object.

After that, you can make lots of different scrollers very easily, and if you have a decent paint program (TextureMaker is very nice, imo.), you've got a pipeline in place.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 14th Jun 2008 18:27
I changed the output stream declaration to this:



I was hoping to use standard C++ anyway.

I took a quick look at the matrix section of the documentation. To be quite honest, I don't understand its usage as it applies to me. I honestly don't know if I ever mentioned, but I'm using this for a grid of 2D tile data. So, if you really think it could help me, that's great. But, I'm hoping to make the tile grid "engine" myself as practice. I'm really not all that great at this. I've only just started learning, really.

Also, I was worried that I didn't do too wonderful a job at writing this particular section:



I got the basic idea of finding the size in bytes from MSDN's library, then added my own ideas for using that information to find the size of the saved grid. I think I did alright, but you know more than I do.
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 16th Jun 2008 21:45 Edited at: 16th Jun 2008 21:47
Hey. I've gotten a little more work on this done ( finally ). I haven't had too much time to put into it, though. So, this may be a disappointing amount of progress.

Right now, I'm stuck with making an editor to at least edit the vector so I can start working on some scrolling techniques and such. I just wanted to make sure I'm doing this right. I'm very new to this.

This is the class file for the tile map:



Then, divided among a few .cpp files, are the definitions:

Constructor definition:


Definition of size_map:


EDIT: I know that the method I used for handling the size error is not the best way to do it. Just temporary.

Drawing code:


And, finally, saving and loading code:


Of course, there are a few debugging functions and such that I removed, but that is it so far. I was just updating all of you that helped me on where I was and, as a beginner, hoping to get some pointers as to where and how I sucked. Thanks.

Login to post a reply

Server time is: 2024-09-29 23:36:39
Your offset time is: 2024-09-29 23:36:39