Author Topic: Tilemap advice  (Read 6390 times)

0 Members and 1 Guest are viewing this topic.

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Tilemap advice
« on: August 25, 2016, 05:57:01 pm »
Hey,

So, I'm wanting to program a game where I draw a tilemap for the background and have masked sprites for the character and objects/enemies, etc. It'd be a side-scrolling game

I read here that the quickest way to get a tilemap to shift is by using the Horizontal or Vertical commands. You then redraw the side the direction the map shifted. Like, if the map moves down, you redraw that side, etc. This avoids drawing the whole map over again.

If the screen has masked sprites on it, will they shift along with everything else when I use the Horizontal/Vertical commands? How could I avoid it?

It probably has to do with the front and back buffers, but I dont know too much about them, so an explanation on buffers could be helpful! :D

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #1 on: August 25, 2016, 06:02:43 pm »
Hey,

So, I'm wanting to program a game where I draw a tilemap for the background and have masked sprites for the character and objects/enemies, etc. It'd be a side-scrolling game

I read here that the quickest way to get a tilemap to shift is by using the Horizontal or Vertical commands. You then redraw the side the direction the map shifted. Like, if the map moves down, you redraw that side, etc. This avoids drawing the whole map over again.
Sounds right
Quote
If the screen has masked sprites on it, will they shift along with everything else when I use the Horizontal/Vertical commands? How could I avoid it?
Yes, the masked sprite would also shift along
Quote
It probably has to do with the front and back buffers, but I dont know too much about them, so an explanation on buffers could be helpful! :D
There are multiple ways in solving this problem, as you already suggested back and front buffers, i'll explain that one.
The following is assuming you don't use grayscale, only black/white.

So, basically you draw your tilemap to the back buffer (you know, with the ^r command), and then also shift the back-buffer (whith Horizontal+^r etc.)

Then, when drawing the screen, you copy the back buffer to the front buffer: Copy(L3)

After that, you draw your masked sprite to the front buffer (with your normal commands)

And then with DispGraph the screen gets updated.

I hope that made sense somehow :3

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #2 on: August 25, 2016, 06:14:04 pm »
I think so...It typicallly makes more sense in practice, lol.

I'm making the DrawMap command right now, and I'm drawing everything to the back buffer like you said. but, nothing's showing up. Is there a command to display the back buffer?

Also, should I use Pt-On( or Pt-Off? Xor or Overwrite logic?

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #3 on: August 25, 2016, 06:15:35 pm »
Well, it's not showing up as you have to copy it to the front buffer first, with Copy(L3)

In case you want to, for whatever reason, display the back buffer directly to the screen (where you actually don't want to have the masked sprite on), you do DispGraph(L3)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #4 on: August 25, 2016, 06:32:58 pm »
ahh, thanks! that makes a lot more sense!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #5 on: August 26, 2016, 07:17:23 pm »
Alright. So I took Sorunome's advice. I draw the map onto the back buffer and wrote a routine to shift it with the horizontal and vertical commands. I then use Copy(L3) to send it to the front. It works great, however, I'm kindof stumped at this minor problem.

The routine uses the Horizontal/Vertical command first, then redraws the appropriate area. In the attachments, you can see that every 16 pixels the redraw method isn't working, and I'm left with a line of pixels that isn't drawn correctly. This only happens on the Horizontal+ and Vertical+ commands.

You can see it most notably in the horizontal screenshot, but it's the same problem with the vertical one as well.

Any advice?

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #6 on: August 26, 2016, 07:26:41 pm »
it's very hard to tell without any code, do you have some to share?

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #7 on: August 26, 2016, 07:38:32 pm »
Gotcha! Here's what I got, it's totally not optimized! lol. I have a 16x16 tilemap. Each tile is 16x16 pixels. {GDB0} refers to the map's overall x and y corrdinates

Code: [Select]
//Shifts Map background 1 pixel
Lbl Shift

//Tell which Tile is on the top left of the screen
!If {GDB0}/2/2/2/2→A=0:15-A→A:End
!If {1+GDB0}/2/2/2/2→B=0:15-B→B:End

If r₁=1
Vertical-ʳ
16*B→C
For(J,A,A+7)
C+J+64→N
Draw(16*J+{GDB0},C+64+{1+GDB0},{N+GDB0MAP}*32+Pic0Tile
End
ElseIf r₁=2
Horizontal+ʳ
For(I,B,B+5)
16*I+A→N
Draw(16*A+{GDB0},16*I+{1+GDB0},{N+GDB0MAP}*32+Pic0Tile 
End
ElseIf r₁=3
Horizontal-ʳ
For(I,B,B+5)
16*I+A+6→N
Draw(16*A+96+{GDB0},16*I+{1+GDB0},{N+GDB0MAP}*32+Pic0Tile 
End
ElseIf r₁=4
Vertical+ʳ
16*B→C
For(J,A,A+7)
C+J→N
Draw(16*J+{GDB0},C+{1+GDB0},{N+GDB0MAP}*32+Pic0Tile
End
End
Copy(L₃)
Return
--------------------------
Here's the action key that calls shift. The map cannot scroll past the boundaries.

Lbl ActionKey
{GDB0}→X
{1+GDB0}→Y
If getKey(1)
If Y≠64
{1+GDB0}--
Shift(1)
End
End
If getKey(2)
If X≠0
{GDB0}++
Shift(2)
End
End
If getKey(3)
If X≠96
{GDB0}--
Shift(3)
End
End
If getKey(4)
If Y≠0
{1+GDB0}++
Shift(4)
End
End
Return
Hope that explains enough!

Edit Sorunome: Added code tags
« Last Edit: August 26, 2016, 07:40:27 pm by Sorunome »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #8 on: August 26, 2016, 07:59:34 pm »
How does your method "Draw" look like? (More importantly: what kind of arguments does it take / what do they do)

From the look of the screenshot it looks like when it is shifting out a whole tile it isn't working as expected, so my guess right now is that something with
Code: [Select]
!If {GDB0}/2/2/2/2→A=0:15-A→A:End
!If {1+GDB0}/2/2/2/2→B=0:15-B→B:End
is wrong. Without really knowing what the code does, why 15-A and 15-B and not 16? (since your tiles are 16x16)

Again, I don't really understand your code yet, i might be just plain up wrong, lol
« Last Edit: August 26, 2016, 08:07:19 pm by Sorunome »

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #9 on: August 26, 2016, 08:09:03 pm »
Code: [Select]
!If {GDB0}/2/2/2/2→A=0:15-A→A:End
!If {1+GDB0}/2/2/2/2→B=0:15-B→B:End

This returns the reference number to the tile in the upper right hand of the screen. A is the number of tiles to the right, B is the number of tiles down. It's pretty accurate. What's weird is this is working when usingt he Vertical- and Horizontal- commands, the shift method works fine...

Draw is very simple: takes an x, y and pointer. I chose to use overwrite logic

Code: [Select]
Lbl Draw
  Pt-Off(r₁,r₂,r₃)ʳ
  Pt-Off(r₁+8,r₂,r₃+8)ʳ
  Pt-Off(r₁+8,r₂+8,r₃+16)ʳ
  Pt-Off(r₁,r₂+8,r₃+24)ʳ
Return

ActionKey doesn't recieve any parameters.

Shift takes 1 parameter: 1 - 4, associated with the four directional keys.

Here's the main loop:

Code: [Select]
Lbl Main
Repeat getKey(15)
//ClrDraw
ActionKey()
//redraw the map by pressing enter
If getKey(9)
DrawMap({GDB0},{1+GDB0})
End
DispGraph
End
Return
« Last Edit: August 26, 2016, 08:18:59 pm by coops »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #10 on: August 26, 2016, 08:24:46 pm »
Hang on, you said {GDB0} and {GDB0+1} hold the x- and y-coordinates of the map, right?

I don't see that changed at all in your Shift routine, keep in mind that you need to grab the tiles to re-draw from the new coordinates, not from the old ones

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline coops

  • LV3 Member (Next: 100)
  • ***
  • Posts: 58
  • Rating: +3/-0
    • View Profile
Re: Tilemap advice
« Reply #11 on: August 26, 2016, 09:45:26 pm »
Yeah, I have the coordinates changed in the actionKey routine, right before the shift routine is called, but you're right. it should be in the shift routine. i'll make that change.

I'll try fiddling around with them and see where I get. Its so weird cause when I use the DrawMap command to redraw the map, everything matches except for the missing lines...

As a side note, i'm using wabbitemu to emulate, and i'm not sure how to record video on it...

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Tilemap advice
« Reply #12 on: August 27, 2016, 06:06:43 am »
[...]
As a side note, i'm using wabbitemu to emulate, and i'm not sure how to record video on it...
Either with the backspace key or with "File" --> "Record GIF"

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!