Click to See Complete Forum and Search --> : 2D Tile based game help needed.
SapphireGreen
Mar 15th, 2002, 04:26 AM
*Cough*. Yes, I need some help.
I am making a 2D tile based game, and there are a few things that have eluded me so far.
I decided to make the maps I should create a map maker program. Then I figured I'd have to save them (how odd), but I can't figure out how to do that, since I have one type, typTile, and have coded:
Private Type typTile
Used as boolean
Walkable as boolean
DC as long
end type
Dim tile(999, 999) as typTile
And I don't know how to write it to a file. (I'm not sure if I should be doing it like this). So I've decided to ask you. :) Even a link to a theory website or a tutorial or SOMETHING to help me. That would be nice.
Thanks.
Nirces
Mar 15th, 2002, 06:31 AM
Well you cannot not, you need more infomation than that.
each tile needs to know its filename, not its DC (which changes)
have a look on how i did mine (the bit that is missing is the code to save the contents of SurfaceCollection, but it will work exactly the same as the code that saves the map.
if your willing to partly use DirectX, adding a single property to SurfaceCollectionItem - getDC will make it work with the gdi
PsychoMark
Mar 15th, 2002, 06:56 AM
Nirces is right, don't store the DC. Either store the filename (as suggested), or, if your tiles have been put in one (or more) larger bitmaps, store the X/Y-index of the tile.
As far as the actual writing is concerned, I still believe that this is the best way for storing that kind of data: http://www.x2software.net/viewarticle.php?id=2
Oh, and your current map will take over 1 MB, just so you know ;)
(999 x 999 ~ 1 MB, and that's only true if your record only takes one byte, and it doesn't :p. Use a dynamically sized array if the map can vary in size, it saves a lot of wasted diskspace and memory)
SapphireGreen
Mar 15th, 2002, 09:15 PM
Yeah, DC is actually a string in my project... I didn't copy correctly. I think I should change DC to probably FL (File location) and a string. And the 999 is just a place holder. I redim when the class is initialised to the height and width (49 or so).
Thanks for the link Psycho. I'll look at it now.
Arbiter
Mar 16th, 2002, 04:48 AM
If it helps, Sentience gives each map tile a reference number based on what it is (type, instance, coastline or not etc - without giving out too many trade secrets).
When games are loaded or generated the entire map graphics are loaded into DCs and the engine uses the map tile reference number to display the graphics.
I've just reread that and it's probably only going to cabbage your head - but I thought I'd add my tuppence worth anyway...
PS - when actually saving the file, save it as a binary file. Much smaller and easier to work with. ;)
SapphireGreen
Mar 18th, 2002, 05:06 AM
Thanks for the help. I now have a working map. :) Something along the lines of (this is from memory)
Public map(0) as udtMap
Public Type udtTile
' Where X and Y are locations inside the texture bmp (loaded in DC, so it grabs the right graphic)
X as long
Y as long
Walkable as Boolean
End Type
Public Type udtMap
tile(29, 29) as udtTile
texture as Long
' texture = a DC with all the tile graphics
End Type
That's the way I did it, reading and writing to Binary Files. Now I can start with the game engine. Thanks for the help.
SapphireGreen
Mar 21st, 2002, 09:33 PM
Ok. New problem. I have memory leakage. I think it's because I'm not deleting the DC's properly. If someone could tell me the API to kill DC's (I've tried DeleteDC and I still have memory leakage).
I basically use CreateCompatibleDC, then SelectObject something into it. I put the handle to the DC into an array, and then delete all of them, using DeleteDC.
If someone has a link to a tutorial to save them typing it out, that would help too. :)
PsychoMark
Mar 22nd, 2002, 02:03 AM
No tutorial, but with a little help from MS I figured out this should be the correct order: (in pseudo-code)
- DC = CreateCompatibleDC
- OldBMP = SelectObject(DC, CreateCompatibleBitmap)
- Do your stuff
- DeleteObject(SelectObject(DC, OldBMP))
- DeleteDC(DC)
SelectObject returns the previous object, you must restore that before your delete the DC. Since the last SelectObject call returns the Bitmap we selected into it in the first place, we can call DeleteObject on the return value to free the bitmap. Then delete the DC and everyone's happy :)
SapphireGreen
Mar 22nd, 2002, 05:34 AM
Great. Now I just have to run my app about, oh, 30 or so times to watch the memory leak, or not leak as I hope.
Thankyou. Hopefully when I feel that this game is ready for you all to look at, you won't laugh to hard. :)
PsychoMark
Mar 22nd, 2002, 05:38 AM
We've all been a newbie once. Just be proud of what you made and move on to the next level :)
And no, I won't laugh. It should probably be the other way around, I hardly ever finished a game I started. I solve the difficult parts, get bored and start another one :p
Fox
Mar 25th, 2002, 02:19 AM
This is crap. Thats what Id do:
Dim FileNumber As Integer
FileNumber = FreeFile
Open "map1.map" For Binary As FileNumber
'Size
Put FileNumber ,, TileCountX
Put FileNumber ,, TileCountY
'Data
Put FileNumber ,, Tile
Close FileNumber
And to load:
Dim FileNumber As Integer
FileNumber = FreeFile
Open "map1.map" For Binary As FileNumber
'Size
Get FileNumber ,, TileCountX
Get FileNumber ,, TileCountY
'Data
ReDim Tile(TileCountX, TileCountY)
Get FileNumber ,, Tile
Close FileNumber
'Code improved by vBulletin Tool 2.0 (http://fox.acky.net/downloads/vbtool.zip)
If you need a tutorial, heres one: http://fox.acky.net/vb/english/coding/tutorial/index.html
Arbiter
Mar 25th, 2002, 03:20 AM
I'm with Fox.
That's pretty much the same as the Sentience load/save routines...
Fox
Mar 25th, 2002, 03:30 AM
Of course you are :D
<< This is the master speaking *smile* :)
:D
Fox
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.