So I’m working on a 2D project that I’ve spent almost a year on now with some friends. We just started coding recently and I had to design a shader and logic that would allow me to blend tiles together to get a smooth change of textures. It’s a pretty complicated pile of code but basically it converts some arrays of integers that represent the tile data into actual parts to draw (the goal being that we can rotate the view eventually even though it’s 2D, by being able to render it while facing any one of the cardinal directions (i.e. the top of the screen can be West if we rotate it clockwise, and all buildings and heights will be drawn from the new perspective).
Anyway, because of all the computation the framerate isn’t perfect, and I’m looking for ways to make it better. I have reduced the amount of new objects I’m defining each Update() to as little as possible, choosing instead to move existing rectangles and adjust existing Vectors.
I think this has helped, but I’m looking for any kind of ideas that will help me make it quicker. The tiles in our game are 64x64 but each one is made up of nine parts (plus additional parts for cliffs, fauna, etc). So if you can imagine, there are about 30x17 tiles on a 1920x1080 monitor, and I’m drawing averagely ten parts for each one of them. Plus characters, trees, particles, etc. The world is sent to the player from the server in chunks of 64x64 tiles.
My process (in pseudocode) works like this:
- Build an array of only the tiles that will be drawn (retrieved from the world’s chunk data)
- Calculate edge types for all cliffs (lots of ifs here, but they only go 3 ifs deep)
- Based on the edge types determined, determine drawing rectangle sizes and locations (accounting for height and things being behind each other)
- Drawing data is saved in rows, along with a List of heights that appear in the row.
- Each row’s tiles are drawn in order of height, lowest to highest, so everything shows up covered correctly by things in front of them (sometimes I am drawing tiles that get drawn over if they are hidden behind a higher tile that’s closer to the bottom of the screen). This is done in a for loop that starts at the lowest indexed height in the list, loops through the row and draws those tiles, then chooses the next higher height in the list, repeat, until the row is done.
My drawing loop is pretty tight at this point so I don’t think I can save processor time here:
Any ideas? I would love some random suggestions for this, or any straws I can grasp at.