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.

DarkBASIC Discussion / Performance hits when making a tile-based game

Author
Message
Shrimpalimpa
7
Years of Service
User Offline
Joined: 7th Mar 2017
Location: Idaho
Posted: 20th Jul 2017 04:25
So recently I've been working on a project to create a 2d platformer while making use of a tile map. However, I'm running into some performance problems. After adding around 30 tiles to the map, the fps begins to drop. After 200 tiles the fps absolutely tanks. This isn't exactly the best news, since I was hoping to use multiple layers of tilemaps to create parallax scrolling later on. Maybe someone who knows what they're doing can cue me in on whats happening. I have my suspicion that the sprite command is to blame - since it must be called every frame to adjust position (essential for when I add a camera offset later). I'll post the code for the editor that I slapped together. It's not very user friendly, but hopefully it will help a little.

Also commenting out the sprite command returns the fps to a sold 60.




NOTES:
Grid is commented out because it messed up the fps even more. (despite it being pretty simple)


Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Jul 2017 00:43 Edited at: 21st Jul 2017 00:45
Hello,

It's been a while since I've looked at DBC code and I've only eyeballed your code quickly. Off the bat, I can see a couple of things that might hit performance. In your function inspect(), it looks like you are repeatedly calling internal functions for the mouse position. Unless you need an updated mouse position for each calculation, you can store the mouse X and Y in a variable, pass that to your function, and then use that variable inside the inspect function instead with offsets instead of repeated calling mousex() and mousey().

Also, avoid calling the ink and rgb() commands and functions inside inspect() if you don't have to. Create a series of variable that you store your colors in:

yellow = rgb(255,255,0)
white = rgb(255,255,255)
red = rgb(255,0,0)

etc.

Then pass the color to your function so you don't have to continually call the rgb() function in your code. Even the built in functions take resources.

If I get a chance to actually try out the code, I may find somewhere that a bottle kneck is occurring - but at first glance, the above is kinda what jumped out at me.

Oj yeah, one huge performance eater is the use of sprites. Best to paste images instead or paste sprites as images keeping them always hidden.

Enjoy your day.
Shrimpalimpa
7
Years of Service
User Offline
Joined: 7th Mar 2017
Location: Idaho
Posted: 21st Jul 2017 01:30 Edited at: 21st Jul 2017 01:31


Thank you!

I had completely forgot about the paste image feature! This basically fixed all the fps problems, but I also aim to optimize those other things you told me about. I'm going to have to find out how to do hit detection once I actually get to the platforming physics, but for the time being, this is a major breakthrough. I also have to rewrite the rescale feature (though it's not entirely necessary).
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Jul 2017 19:35
Shrimpalimpa,

I dug around the forums and found an old post I made that includes a quick example of how to make a tile make maker and layer different features. It may be helpful and it also starts to lay the foundation for collision based on the current visibile feature in the tile layer.


Posted: 21st Apr 2009 03:22 Edited at: 21st Apr 2009 15:16
Link

From: Latch
[EDIT]
To start, I'm not a newbie. I think this is great thread because you guys see things through so I thought I'd throw a couple of ideas in the mix. I don't want to scare any newbies away if what I post seems too difficult; these are only some ideas. You can take them or leave them. I'm not trying to tell you how to do anything. If I post some code, look through it and see if it makes sense. If I make a mistake or seem to be doing something the hard way, feel free to give me what for!

Also, I keep talking about 16 layers. It's really just an arbitrary number. It just to show it can be done with little overhead in the map design. It could as easily be 4.

@Obese

Quote: "So how are we going to store all this data? For each tile we need to store several images (layers)"

You don't actually have to store several images on a single map square. The master level tile set holds the images that can be used on the map. A map array, holds a reference to a particular image/tile. The layering only holds the reference to the tile as well. The master level tile set is always numbered the same, so you know what the tile type is by the number that is referenced in the map file. The picture (image) is in the master level tile set. If tiles 1 - 16 are passable tiles, those pictures could be anything, blank spaces, a road, grass, shallow water - anything.

The tile number from the master level tileset is stored in the map array. The actual in game map can be drawn on the fly - tile by tile, or predrawn on an offscreen bitmap, or partially predrawn and updated as movement occurs, or drawn and saved as a a picture file (though that may be huge depending on the map size - jpg, bmp, png, etc.) - any number of methods could be applied (I like having the initial visible area predrawn offscreen and then just updated as the map scrolls into or out of view). The only visible image is the last one drawn, so if you had a stack/layers of 16 images, there would ultimately be only 1 displayed (using transparency - a composite of all 16 images).

I knocked together a quick example. I didn't create a nice neat layout for a master tile set, I just created 3 images in memory that can be used as tiles. The map editor will allow you to stack 16 layers on a single tile. It won't allow you to have 2 of the exact same tile overlap because that would waste layers. However, you could alternate between 2 images and fill up all 16 layers if you felt so inclined.

If you point at a position on the screen, it will tell you the x,y tile position, the number of layers used on that tile and which tiles/images from the master tile set are assigned to that layer. The highest layer number is the one on top for the map. So you could lay down some grass, then put a road or two on top of it.

Using this method, you could check through the layers to see if there was an action tile (by master tile number) and then trigger whatever is necessary based on the x,y tile position.

Feel free to put tiles anywhere on the screen. The information text will still display on top.

Here's the example:



Hopefully it's helpful.
Enjoy your day.
Shrimpalimpa
7
Years of Service
User Offline
Joined: 7th Mar 2017
Location: Idaho
Posted: 22nd Jul 2017 01:36
That's actually a really great way to handle layers - using a three dimensional array. I'm really impressed with the coding conventions too: everything ran quite well. Thanks for posting this example here. I'm still trying to figure out where to take my current code, but I feel like seeing it done from another person's perspective / coding is valuable.

In any case, thank you!

Login to post a reply

Server time is: 2024-03-28 15:31:14
Your offset time is: 2024-03-28 15:31:14