Page 1 of 2 12 LastLast
Results 1 to 40 of 52

Thread: Tile engine

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Tile engine

    I changed the topic to better show what this thread is all about...


    Old --------------
    I need ideas for games that are easy to program
    I have programed Pong and a game where you had to save the earth from falling meteors. I made pong using DirectDraw and the meteor thing using BitBlt...
    I thought about making a pacman clone but then I have to make up some kind of tileengine and I have no idea how to do that. Then I thoght about making a snake/nibbes kind of game but I dont know how to make the worm...
    Any ideas are welcome
    Last edited by McCain; May 7th, 2002 at 09:21 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well it seems like you got the basic technical aspects so I would really start to learn how to make tile engines since they will open a lot of new possibilites to you!
    I learned it at www.ur.co.nz it was kind of hard for me back than because I did not even know what API was but since you already know all of the Blitting and DirectX stuff it will take you about 5 mins to get it!
    If you have any further questions on it just post again.
    Sanity is a full time job

    Puh das war harter Stoff!

  3. #3

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    The only easy tutorial I found on the site you gave me was the pacman tutorial and it uses classes - is that what you guys use for tile engines?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    no not at all...
    well I guess you can but no need for it.. here the simple explanation:

    you have an array that stores the map. Typically a 2 dimensional one.
    Every number represents one tile
    now you go like
    Code:
    for x = 0 to 10
      for y = 0 to 10
      Destinatio.Draw Tile(map(x,y)), x*width, y * with
      next y
    next x
    of course this is pseudo code but it will give you a good idea what tile engines are all about... all you need is an array of sourceimages (the tiles) which one you will draw is stored in the map array. NOw you loop through every singe element and draw it.
    but why don'T you check out the good explanations on this in the tutorials section on www.ur.co.nz?
    Sanity is a full time job

    Puh das war harter Stoff!

  5. #5
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright that works and you got the basics but it has two things in it that I would change, it can be way easier than your attempt.
    the first one is just to save some time and make things a little nicer.

    Instead oj using a textfile to store the map youse a binary file. The textfile has the advantage that you don't need to program a map editor to create a map, but as easily as you can use notepad to create the map anyone will be able to alter the map. And it's also a waste of diskspace... (oh well) and it's also slower to laod it that way line by line.
    Look into the get and put statements in the MSDN you can just save a whole array with one line like put 1,,Array

    now the big one (actually it's too)
    instead of looping through strings and than manipulating them again to the get current one just use a two dimensional array you can declare one like this
    dim Map(0 to 10, 0 to 10)... now you got an array with two dimensions that has the width and height of 11.
    that makes the code much easier instead of all the string cutting you just go like
    Code:
    for x = 0 to 10
       for y = 0 to 10
       map(x,y)   'gives you the content of the field you want... it's kind of the same with you strings, just faster and less code like this.
      next y
    next x
    now you see all those case statements... It might be a way to do it but just imagine how huge your code will be when you have like 200 different tiles (if a game gets bigger that's nothing to special) well there must be some better way (it's also faster... of course )
    instead of having a letter for each tile you assign a number... now you store the tiles in an array. (you can also store them in one file but it's way easier this way for now, you can easily move on after you accomplished that). When they are in an array they all got their index as their number to indentify them so now you can just go like:
    draw Destination.hdc x*width,y*height,Tiles(map(x,y)).hdc

    alright have a look at that..
    instead of using the variable you called intX and intY I just use the x and y that where used in the foor next loop and take them times the width of the tiles.. will give you the same result 2 lines of code less and 2 variables less.
    than instead of having those case statements I put what I marked Bold... the map(x,y) will return the number/index of the tile to be drawn that way one line will work for all the tiles....

    I think that should help... (hey I nearly wrote a book)
    Sanity is a full time job

    Puh das war harter Stoff!

  6. #6

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Thanks a bunch for all the help! You have no idea how greatfull I am!

    But I don't quite get the binary thing...
    I know how to use the put and get statements and I can put and get simple variables but when it gets to 2d arrays I'm in a bit deep... Could you please explain that some more?

    This is my updated code
    VB Code:
    1. Private Sub cmdTile_Click()
    2.     Dim intLen As Integer
    3.     Dim intX As Integer
    4.     Dim strChar As String
    5.     Dim intChar As Integer
    6.     Dim intY As Integer
    7.     Dim strFileLine As String
    8.     Dim strFileArray(9) As String
    9.  
    10.     Open "c:\MyFile.txt" For Input As #1
    11.    
    12.     intY = 0
    13.    
    14.     Do Until EOF(1)
    15.         Line Input #1, strFileLine
    16.         strFileArray(intY) = strFileLine
    17.         intY = intY + 1
    18.     Loop
    19.    
    20.     Close #1
    21.  
    22.  
    23.     For intY = 0 To 9
    24.         intLen = Len(strFileArray(intY))
    25.         For intX = 1 To intLen
    26.             intChar = Mid$(strFileArray(intY), intX, 1)
    27.             lblLetters.Caption = lblLetters.Caption & strChar & " "
    28.             strMap(intY, intX - 1) = intChar
    29.             Label1.Caption = Label1.Caption & strMap(intY, intX - 1) & " "
    30.             Call BitBlt(picTiles.hDC, intX * udtTile(intChar).Width  - udtTile(intChar).Width, _
    31.                 intY * udtTile(intChar).Hight, 20, 20, udtTile(intChar).hSrcDC, udtTile(intChar).xSrc, _
    32.                 udtTile(intChar).ySrc, vbSrcCopy)
    33.         Next intX
    34.         lblLetters.Caption = lblLetters.Caption & vbNewLine
    35.         Label1.Caption = Label1.Caption & vbNewLine
    36.     Next intY
    37.    
    38.     picTiles.Refresh
    39.     blnPlay = True
    40.     Call GameLoop
    41. End Sub
    Last edited by McCain; May 5th, 2002 at 04:03 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well do you have a problem understanding 2 dimensional arrays or just saving them?
    VB Code:
    1. 'for loading
    2.   Open App.Path + "\Map1.map" For Binary As #1
    3.     Get 1, , Map
    4.   Close
    5.  
    6. 'for saving
    7. Open App.Path + "\Map1.map" For Binary As #1
    8.     Put #1, , map
    9. Close #1

    A two dimensional array is like a chess board...
    you can set a variable for every element like you'd say b3 on a chess board = pawn... well now in your array the difference is that you use numbers instead of letters for both coordinates so the same thing would be map(x,y) = pawn... well now you don't want to set pawns but tiles.. we represent every tile by a number it has in its array so it would be map(x,y) = 3 (or whatever tile would represent a pawn)
    I hope that made everything a little clearer if not I will try again tomorrow need to catch some Zzs now...
    Sanity is a full time job

    Puh das war harter Stoff!

  8. #8

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, thanks again
    I'll see what I can come up with...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  9. #9
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    I just saw something in your code..
    well you do not have the 2d array implemented, yet. It will clean the code up even more... but you are getting ahead.
    But you call the game loop after you are done drawing.
    The drawing will be inside of the game later on that is why it needs to be fast becaue you redraw all the time.
    (well in some cases you don't need to but most likely you will)
    later on when you might implement scrolling or something you will need to redraw anyways or when you draw actors on it.

    by the way what are you doing... a snake kind of game or something like that? Will it need scrolling?
    well I guess I am gonna post some old code of mine if I can find it tomorrow...
    Sanity is a full time job

    Puh das war harter Stoff!

  10. #10

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, I got it, I didn't realize I could just save the whole 2d array at once to the binary file, thought I could only save one row at a time...
    My code is so much shorter now!

    This is the tileplacing code...
    VB Code:
    1. For intY = 0 To 9
    2.     For intX = 0 To 9
    3.         intChar = intMap(intY, intX)
    4.         Call BitBlt(picTiles.hDC, intX * udtTile(intChar).Width, intY * udtTile(intChar).Hight, 20, 20, _
    5.             udtTile(intChar).hSrcDC, udtTile(intChar).xSrc, udtTile(intChar).ySrc, vbSrcCopy)
    6.     Next intX
    7. Next intY
    Last edited by McCain; May 5th, 2002 at 04:04 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  11. #11

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I dunno what I'm doing yet... I just thought I should get the basics of tiled game first...
    Next logical step will probably be to get somekind of sprite to move around...
    I have no idea how to do the collision detection so any pointers are very welcome...
    Please don't post any code - just keep doing what you have done so far - give me pointers in the right direction or post psuedo code.
    I feel I learn much more that way then if I just copy and paste code...
    Thanks

    Edit ----
    If we assume that the sprite moves with 20 pixels (the width and hight of the tiles) at a time. And then divide the x and y by 20 we should get the tile we're on right now and then it's just a matter about checking to see if the tile we're about to step on is a wall or not...
    The problem is that I can't get the sprite to move...
    I've tried Form_KeyDown and KeyPress - neither of them worked at all. Then I tried GetAsyncKeyState but it executes the moving code to fast, by the time I release the button the sprite is long gone from the screen... And I haven't been able to figure out how to just execute the code once...
    Last edited by McCain; May 5th, 2002 at 07:26 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  12. #12
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright doens't the code look so much nicer now?
    one more hint... normaly all tiles should be the same width so instead of saving the width for every tile just make it one constant in memory. That will save 2 longs pro tile-type.
    After this you can remove the line with the
    intChar = Map(x,y)
    and just put map(x,y) instead of it in the drawing code.
    You wont loose any time for the lookup because it will be only looked up one time so it's even faster and more memory efficient.
    Sanity is a full time job

    Puh das war harter Stoff!

  13. #13
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright I just saw the edited part..
    so: You are right about the collision thing.
    You can still use form_key down but as you are in a tight loop events wont be executed unless you put a 'doevents' in the loop somewhere... you should always do so.
    But still GetAsyncKeyState is the way to go (or just GetKeyState) as you saw it's very fast and it allows you to have more than one key pressed at a time.
    Now what you need to do is build in something that only executes the movement code if enough time passed. For that you should look up the GetTickcount API it is really simple you just use it like
    VB Code:
    1. static LastT as long
    2.   if LastT < Gettickcount then
    3.     'your code for moving
    4.     LastT = GetTickCount+1000
    5.   endif
    this code would wait one second until it can be executed again (1000ms = 1s)
    Sanity is a full time job

    Puh das war harter Stoff!

  14. #14

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    As allways, thanks for the hints. I'll try the code when I get home from school. And I do have a DoEvents in my loop, but the keyDown still doesn't work...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  15. #15
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    maybe the focus is set to some object on the form that recieves the key down events... I can remember things like this happening to me when I started off, but I haven't used the key down event for anything serious in a long time so I really don't know
    The GetKeyState will do a much better job anyways.

    Well you seem to learn really quick so here one more hint:
    for scrolling you just need two variables (well for pixelwise scrolling more but we will do this later on )
    lets call them xScroll and yScroll

    the drawline is going to change like this:
    Draw destination.hdc, ....., Source(map(x+xScroll, y + yScroll) that's how easy it is.
    Sanity is a full time job

    Puh das war harter Stoff!

  16. #16

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, a few questions that is more regular VB then tiling...
    In the GetTickCount thing you used a "static" variable, what exactly is a static variable?
    First when I moved my little sprite around it left a trail because I only drew the map once so I moved the code that draws the map into the gameloop but now it draws the map ontop of my little sprite so I can't see it at all... What should I do?
    How should I shut down my program? As it is now when I press the litte X in the corner the program becomes invisible but it is still runnig so I have to press the stop button in the VB GUI...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  17. #17
    Zaei
    Guest
    A Static variable is like a global variable, but can only be seen inside of the function it is declared in.

    The things you BitBlt last are on top. Blt your sprite last =).

    To make your game end, you need a global bRunning boolean. In your game loop, simply:
    Code:
    While bRunning
    ...
    Wend
    In the Form's QueryUnload Event, just set bRunning to False. On the next run of the game loop, it will fall out, and exit.

    Z.

  18. #18

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Nice of you to join Misanthrop and I, Zaei
    I have
    VB Code:
    1. Do Until blnPlay = False
    2.     'game loop in here
    3. Loop
    4.  
    5. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    6.     blnPlay = False
    7. End Sub
    Shouldn't that work just as well as While...Wend? If not, please explain why...

    I tried to put the map blitting code both before and after the sprite blitting code, nothing worked...

    Onther thing that is a bit confusig is how my map works...
    The map is in an intMap(9, 19) and looks like this:

    00000000000000000000
    00000000000000000000
    11110011113333003333
    11110011113333003333
    22220022222222002222
    22220022222222002222
    33330033331111001111
    33330033331111001111
    00000000000000000000
    00000000000000000000

    In math we learned that x was from left to right and that y was from top to bottom... So I thought you wold declare that map as intMap(19, 9) because it has 20 cols (x) and 10 rows (y).
    So to me it looks like intMap(9, 19) should have 10 cols and 20 rows... Please explain someone!
    Last edited by McCain; May 7th, 2002 at 12:18 AM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  19. #19
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright that seems wierd to me...
    well one thing about the math.. it's a good way to keep it like you learned in school but it is not neccesary you could also store it like
    map(y,x) but keep it map(x,y) so you don't all confused.

    How did you determine your map looks like this? the Array wont have any more elements than you declared... so it might be the way of how you found out about it....

    hm with the loop thing, the game should look something like this

    initialisation
    Main Loop containing:
    pretty much all of the game for now that would be
    the GetKey thing
    the drawing of the Graphics..
    and after the main loop the clean up /End...

    for the drawing you should first draw the map
    than maybe a second map onto it (for multiple layers kind of things) than the sprites and stuff, than the GUI (if you got one)

    now one thing that I belief you aren't using yet is a backbuffer.
    You do all the drawing to a picbox that is invisible and Autoredraw = true
    afterwards when all the drawing's done just blit it to the visible picbox you used to draw to. This will prevent flickering! (Gets even more neccessary when using masks)
    Later on you should replace the picbox by a DC but a picbox is good enough for now... after you got all the techniques you might as well redo it (in clean code) in DDraw, so you don't need any DCs anyways since you will be using surfaces then.
    Sanity is a full time job

    Puh das war harter Stoff!

  20. #20
    Zaei
    Guest
    I use While ... Wend, but its just a preference.

    Z.

  21. #21

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, I'll post all code tha's relevant to the creation of the map...
    I know I shouldn't use a txt file but I do so for now because I don't want to create a map editor...
    VB Code:
    1. Dim intMap(9, 19) As Integer
    2.  
    3. Private Sub Form_Load()
    4.     Dim intLen As Integer
    5.     Dim intX As Integer
    6.     Dim intChar As Integer
    7.     Dim intY As Integer
    8.     Dim strFileLine As String
    9.     Dim strFileArray(19) As String
    10.     Dim intMap2(9, 19) As Integer
    11.  
    12.     Open "c:\MyFile.txt" For Input As #1
    13.    
    14.     intY = 0
    15.    
    16.     Do Until EOF(1)
    17.         Line Input #1, strFileLine
    18.         strFileArray(intY) = strFileLine
    19.         intY = intY + 1
    20.     Loop
    21.    
    22.     Close #1
    23.  
    24.  
    25.     For intX = 0 To 19
    26.         intLen = Len(strFileArray(intX))
    27.         For intY = 1 To intLen
    28.             intChar = Mid$(strFileArray(intX), intY, 1)
    29.             intMap2(intX, intY - 1) = intChar
    30.         Next intY
    31.     Next intX
    32.    
    33.     Open "BinaryFile" For Binary As #1
    34.         Put #1, , intMap2
    35.     Close #1
    36.    
    37.     For intX = 0 To 9 ' ********This is where I see what the map look like...
    38.         For intY = 0 To 19
    39.             lbl.Caption = lbl.Caption & intMap2(intX, intY) & " "
    40.         Next intY
    41.         lbl.Caption = lbl.Caption & vbNewLine
    42.     Next intX
    43.    
    44.     blnPlay = False
    45.     intPX = 0 'player X
    46.     intPY = 0 'player y
    47. End Sub
    48.  
    49. Private Sub cmdTile_Click()
    50.     Dim intX As Integer
    51.     Dim intY As Integer
    52.    
    53.     Open "BinaryFile" For Binary As #1
    54.         Get #1, , intMap
    55.     Close #1
    56.  
    57.     lbl.Caption = ""
    58.     For intX = 0 To 9 '********And here again...
    59.         For intY = 0 To 19
    60.             lbl.Caption = lbl.Caption & intMap(intX, intY) & " "
    61.         Next intY
    62.         lbl.Caption = lbl.Caption & vbNewLine
    63.     Next intX
    64.    
    65.     blnPlay = True
    66.     Call GameLoop
    67. End Sub

    I thinkg I know what's wrong with my gameloop...
    I only draw my sprite when I click on a button (in the GetAsyncKeyState thingie)... I'll fix it when I get home so it draws the sprite all the time and just changes the X and Y coordinates when I click the buttons...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  22. #22
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    alright the code looks neat... since you already save your map as binary it will be no problem to switch to binary later...
    alright.. I really can't see how this code gives you more than 20 elements in one line since even the code to show it only loops 20 times...
    Sanity is a full time job

    Puh das war harter Stoff!

  23. #23
    Zaei
    Guest
    One thing to be aware of with VB arrays...

    When you dynamically allocate the array using Redim, and Redim Preserve (which you are not doing, at the moment), When you write the array to a file as binary, it also stores some extra data. When it does this, it makes it more difficult to read the data in another language, So I usually loop through and write out the actual integer data, as well as the width and height when saving the map. Just some advice =).

    Z.

  24. #24
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well Zaie that is true, but I have experienced that putting the data with no loops actually saves a lot of time! So when not using more languages I would stick to saving the whole thing...

    @McCain One more thing about the redim though. You probably used redim preserve before... it does not work on 2d arrays. It won't give you an error but it will just have the same effect as redim without preserve.
    One way to get around this is using a one dimensional array and go like this arrayMap(x+y*line) while line would be the number of how many tiles you have in each line...
    Sanity is a full time job

    Puh das war harter Stoff!

  25. #25
    Zaei
    Guest
    Yeah, but you should only be reading the map data in once anyway, so time shouldnt even be an issue =).

    Z.

  26. #26

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Hmmm, I dunno if you guys understood my question... (if you didn't it's probably b/c of my bad english... )
    So here comes my question again formulated different...
    2d Arrays are declared Dim array(x, y) As *whatever*, Right?
    But sometimes it looks as if the computer reads them array(y, x) (the wrong way)
    My map that looks like this:

    00000000000000000000
    00000000000000000000
    11110011113333003333
    11110011113333003333
    22220022222222002222
    22220022222222002222
    33330033331111001111
    33330033331111001111
    00000000000000000000
    00000000000000000000

    Looks like this:

    0011223300
    0011223300
    0011223300
    0011223300
    0000000000
    0000000000
    0011223300
    0011223300
    0011223300
    0011223300
    0033221100
    0033221100
    0033221100
    0033221100
    0000000000
    0000000000
    0033221100
    0033221100
    0033221100
    0033221100

    When I blitt it...
    What can be wrong?
    Here's the blitting code:
    VB Code:
    1. For intX = 0 To 9
    2.         For intY = 0 To 19
    3.             Call BitBlt(picTiles.hDC, intX * 20, intY * 20, 20, 20, _
    4.                 udtTile(intMap(intX + intScrollX, intY + intScrollY)).hSrcDC, _
    5.                 udtTile(intMap(intX + intScrollX, intY + intScrollY)).xSrc, _
    6.                 udtTile(intMap(intX + intScrollX, intY + intScrollY)).ySrc, vbSrcCopy)
    7.         Next intY
    8.     Next intX
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  27. #27
    Zaei
    Guest
    Code:
    For intX = 0 To 9
    Is this correct? For a square map, it seems like it should be 19...

    Z.

  28. #28

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    But it's not square =)
    The map is (supposed to be) 10 tiles high and 20 tiles long...
    Last edited by McCain; May 7th, 2002 at 09:59 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  29. #29

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, everything is working good now!
    Let's move on, shall we?
    I've increased the size of the map a bit - it's now 20 tiles high and 60 tiles long. I can scroll the map tile by tile using the keyboard but how should I do this with my sprite? Should I always try to keep the sprite in the middle or should I just scroll as the sprite gets close to the edges of the visible area?
    What is the next logical step in the learnig curve?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  30. #30
    Hyperactive Member
    Join Date
    Aug 1999
    Posts
    482
    I made a tile (Engine, basically)
    150 tiles wide, 100 tall..
    149 individual tiles, all together (Corner pieces, etc.)
    many different types of terrain

    it runs pretty fast(map editor), but it took me awhile to get it to it's current stage. of course, in-game, it'll run faster... because map editor has a lot of fail safes.

    Full screen, 1024x768, I get around 50-60 FPS.

    Start off small, trial and error, basically..
    find the best ways to do things, and you'll learn as you go.

    image (50% original quality, so i can upload)

    Just remember: everybody starts small, it's easy to get discouraged.
    Your first project may not get off the ground, but as you learn, you'll find what you're best at.

    Originally posted by McCain
    Ok, everything is working good now!
    Let's move on, shall we?
    I've increased the size of the map a bit - it's now 20 tiles high and 60 tiles long. I can scroll the map tile by tile using the keyboard but how should I do this with my sprite? Should I always try to keep the sprite in the middle or should I just scroll as the sprite gets close to the edges of the visible area?
    What is the next logical step in the learnig curve?
    Depends on what type of game you want. Keeping it in the center will be harder, IMHO.
    Attached Images Attached Images  

  31. #31
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    sorry for being unclear.
    I am not talking about global declaration of the variable but that they should specify a global postion

    this gets important when using scrolling let's say some sprite is at the point of (5,5).. right now you can just draw it where you draw sprite (5,5) but when you scroll away this guy is supposed to scroll away too...
    so you can't just draw it like draw x*tilewidth,y*tilewidht but more like
    draw (x-scrollx)*tilewidht, (y-scrolly)*tileheight

    (that wouldn't be pixel accurate but tile accurate... we can work on that later depending on what you want to do)
    for the snake thing and stuff... you learn pretty good from everything but I would recommend a all direction scrollable jump and run game since you get to do it all than!.. (snake will be more likely to be some fun game after you finished...)
    ... baiscally it's up to you every practise is good....
    Sanity is a full time job

    Puh das war harter Stoff!

  32. #32
    Zaei
    Guest
    If your map is not square, it wont Blt square (unless you are using un-square tiles =).

    As to scrolling, it depends on the type of game that you want. You can have a single character game, in which case, it is fine to keep that character in the center, or to scroll near the edges (you might also consider scrolling an entire screen, a la Original Zelda). If you are going to have a party oriented game, you might want to try a Baldur's Gate style, and detach the camera from the character completly. Also, You could center on the character, until you reach the edge of the map. At that point, the character moves off center until he reaches the edge of the map.

    Z.

  33. #33

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, let's go for a jump 'n' run game
    I'll keep the character in the middle of the screen until it reaches the edges. There the map will stop scrolling.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  34. #34
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    I prefer moving the map when the actor gets to close to the edge that way the map does not scroll around all the time when you are just moving back and forth for something... I don't know if it's just me but the other way I get a headache...
    what Zaei said about the detached camera is something I think is were slick. You can program the camera totally free from the game action and than attach it the way you want for effects and all of this. (for example when you press a button and somewhere there is a door opening or something you could have a move over there and stuff) that way you would be able to have maximum flexibility and also it would ensure good code (it needs to be good otherwise it wouldn't work that way)
    Sanity is a full time job

    Puh das war harter Stoff!

  35. #35
    Hyperactive Member
    Join Date
    Aug 1999
    Posts
    482
    Originally posted by /\/\isanThr0p
    keeping the sprite in the center is not a problem at all the only thing you got to remember than is that if you want to let the sprite go to the outermost pieces of the map that you engine must be able to recognize that it's not drawing anymore stuff otherwise you will get an out of bounds error. You can easily get around this by putting a wall around each map that is was wide as half a screen... the on error resume next would work to but it slows down things to much,.

    I think the harder way is to scroll when the sprite gets to close to the edge but it's also not to big of a problem.

    When you do all of this you need to start using global x and y positions that you convert to the screen x and y when drawing, so your collision detection does not get screwed up.
    The way mine does it, it wraps the map around...
    once your character gets to the edge, it centers the screen on your character, thus letting your character move more.

    This is a lot easier(and less error checking is needed) than keeping the guy in the center the whole time.

  36. #36

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ok, I've decided what kind of scrolling I want. I want the character to be centerd on the screen in the up/down direction and I want the map to scroll in the left/right direction when the character gets to 10 tiles from the edge of the viewable area of the map.
    To do this you obviously need to know where the character is on the screen, and as my tileengine is now this is very easy - the character moves exactly 20 pixels every time (my tiles are 20x20 pixels big). But that is not how I want my character to move... I want it to be able to move in different speeds (walk and run) and I want it to move smoother then 20 pixels jump... So my guess is that we have to get in to pixel collision detection...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  37. #37
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    well the collision with the map still works the same. But now I would store the position of the character in an x and y variable that represent pixels... before you draw it you just go like
    draw character.x-tilewidth*scrollx,....
    I think that should be clear...?
    than what do you think about letting the map scroll pixelwise? I think you need to do it otherwise everything will be jerking...
    so for pixelwise scrolling, here a way I did it (I don't think it's the smartest way, but since it worked for me I never thought about altering it...)
    so here it is, I added 2 more variables
    sclX, sclY
    and I draw like
    draw tile.x*tilewidth+sclx ,....
    alright now I add to the sclx (in pixels) whenever I want to scroll..
    whenever sclx (same goes for the scly) is equal to (or bigger than) 20 (your tilewidth) I set it to 0 and increase the scrollx by one...
    well of yourse you have to do the same for -20 for scrolling to the left...
    alright I hope you understood if not just post... (I will be home all day, so I will be around)
    Sanity is a full time job

    Puh das war harter Stoff!

  38. #38

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    I'm having major problems with the scrolling... I can get it to move but not as I want. And I can't get the collision detection to work at all... The scrolling works pretty good when I go right but when I go left the map doesn't update... just keeps tiling the same row again and again...

    Here's some rellevant code...
    VB Code:
    1. If GetAsyncKeyState(vbKeyRight) <> 0 Then
    2.     If intMap(intTmpY, intTmpX + 1) = 0 And intMap(intTmpY2, intTmpX + 1) = 0 Then
    3.         intPX = intPX + 20
    4.         If intTmpX >= 20 + intScrollX Then
    5.             intScrX = intScrX + 20
    6.             If intSclX < 20 Then
    7.                 intSclX = intSclX + 20
    8.             ElseIf intSclX >= 20 Then
    9.                 intScrollX = intScrollX + 1
    10.                 intSclX = 0
    11.             End If
    12.         End If
    13.     End If
    14. End If
    15. If GetAsyncKeyState(vbKeyLeft) <> 0 Then
    16.     If intMap(intTmpY, intTmpX - 1) = 0 And intMap(intTmpY2, intTmpX - 1) = 0 Then
    17.         intPX = intPX - 20
    18.         If intTmpX <= 10 + intScrollX Then
    19.             intScrX = intScrX - 20
    20.             If intSclX < 20 Then
    21.                 intSclX = intSclX - 20
    22.             ElseIf intSclX >= 20 Then
    23.                 intScrollX = intScrollX - 1
    24.                 intSclX = 0
    25.             End If
    26.         End If
    27.     End If
    28. End If
    29.  
    30. For intX = 0 To 9
    31.     For intY = 0 To 29
    32.         intChar = intMap(intX + intScrollY, intY + intScrollX)
    33.         Call BitBlt(backDC, intY * 20 - intSclX, intX * 20 - intSclY, 20, 20, udtTile(intChar).hSrcDC, udtTile(intChar).xSrc, udtTile(intChar).ySrc, vbSrcCopy)
    34.     Next intY
    35. Next intX
    36.  
    37. Call BitBlt(backDC, intPX - intScrX, intPY, 20, 20, udtTile(4).hSrcDC, udtTile(4).xSrc, udtTile(4).ySrc, vbSrcCopy)
    38. Call BitBlt(backDC, intPX - intScrX, intPY - 20, 20, 20, udtTile(5).hSrcDC, udtTile(5).xSrc, udtTile(5).ySrc, vbSrcCopy)
    39.  
    40. Call BitBlt(picMap.hDC, 0, 0, 600, 200, backDC, 0, 0, vbSrcCopy)

    FYI: The character is two tiles high...
    Last edited by McCain; May 9th, 2002 at 07:28 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  39. #39
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181
    you forgot to decrease the intScrollX when the intScrlX is getting smaller than -20 (you have 20 there which kind of confuses me this should cause your program to act all wierd)
    Sanity is a full time job

    Puh das war harter Stoff!

  40. #40

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Ohh, it's acting weird allright!
    And I have 20 because if I try to move it like 5 or something (to make it look smoother) the collision detecting (the little I have) messes up totally...
    This is driving me nuts, I can't for my life figure out what's wrong...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

Page 1 of 2 12 LastLast

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