yellowstonegames / SquidLib

Useful tools for roguelike, role-playing, strategy, and other grid-based games in Java. Feedback is welcome!
Other
454 stars 46 forks source link

SparseLayers.draw doesn't reset batch.shader #203

Closed olberg closed 5 years ago

olberg commented 5 years ago

It seems that SparseLayers's draw-method sets a shader and doesn't reset it. I had to manually set it to null, before I could draw anything correctly with the SpriteBatch.

tommyettinger commented 5 years ago

When you use a distance field font (including MSDF), SquidLib needs the shader set to the correct one for that font. Most of the time, text-based games that would benefit from distance field fonts don't need to change from this shader, and there's a performance hit for changing the shader when it isn't needed. If you're using a non-distance-field font, there shouldn't be any change to the shader, but the text also won't resize nicely. LibGDX has a DistanceFieldFont class that resets and flushes the batch every time text is drawn, which is slow, and I wanted to avoid that here. Now, the TextCellFactory.configureShader(Batch) method doesn't set the shader if the current one is identical (this was just changed in the last commit, thanks for bringing it to my attention), but it will set it in any other case. Because some drawing may happen after SparseLayers.draw(Batch, float), such as for special-case Glyphs, SparseLayers shouldn't reset the shader those Glyphs need inside draw(). I've documented the draw() methods that change the shader with notes about setting the shader to null when you want to draw full-color art. I hope this helps future users, and it seems like your approach works already, I think? I think having control over when to reset the Batch should be good for unusual cases, which seems to be most of the time in game development.

Feel free to reopen if this isn't addressed suitably, but I think you have the right approach, setting the Batch.shader to null when you want to draw normal art, and letting SquidLib change the shader to the one it needs for distance field fonts when it draws those.