Results 1 to 13 of 13

Thread: 2D Tile based game help needed.

  1. #1

    Thread Starter
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45

    2D Tile based game help needed.

    *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:
    Code:
    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.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  2. #2
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    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
    Some Days, i just get this feeling that i'm helping to write dozens of Viruses...

  3. #3
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    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 . Use a dynamically sized array if the map can vary in size, it saves a lot of wasted diskspace and memory)
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

  4. #4

    Thread Starter
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45
    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.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  5. #5
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    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.
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  6. #6

    Thread Starter
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45
    Thanks for the help. I now have a working map. Something along the lines of (this is from memory)

    Code:
    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.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  7. #7

    Thread Starter
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45
    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.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  8. #8
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    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
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

  9. #9

    Thread Starter
    Member SapphireGreen's Avatar
    Join Date
    Sep 2001
    Location
    I do not actually exist
    Posts
    45

    Thankyou

    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.
    On Error Give Up

    Mind over matter. Then if it doesn't matter, you lose your mind.

  10. #10
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    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
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

  11. #11
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    This is crap. Thats what Id do:

    Code:
    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:
    Code:
    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
    If you need a tutorial, heres one: http://fox.acky.net/vb/english/codin...ial/index.html
    Last edited by Fox; Mar 25th, 2002 at 03:28 AM.

  12. #12
    PowerPoster Arbiter's Avatar
    Join Date
    Sep 2000
    Location
    Manchester
    Posts
    2,276
    I'm with Fox.

    That's pretty much the same as the Sentience load/save routines...
    Gentile or Jew,
    O you who turn the wheel and look to windward,
    Consider Phlebas, who was once handsome and tall as you...

  13. #13
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Of course you are

    << This is the master speaking *smile*



    Fox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width