Hello. My game is running fairly slowly. I checked it over with VisualVM and it looks like that’s mostly due to the fact that I’m redrawing all the SVG shapes every frame with the CPU. You can see my source code here: https://gitlab.com/AngularAngel/Crovasshun
Is there a good way to optimize this? Hand it off to the GPU, or only redraw when things change, or whatever? Should I take all the ASCII characters from being svg to being an actual texture and just stencil it onto the shapes? Also, on another note, should I have my game code run in a separate thread from graphics? I’m not very knowledgeable on this subject. Any good tutorials, preferably ones relating to processing?
I suggest you modified settings() in
src/crovasshun/Game.java so it uses the OpenGL renderer. That might relieve some the graphics overloading that you are experiencing. This will be my first attempt before considering actual optimization.
Actually, for 2D graphics, renderer FX2D is generally much faster.
Hmm. I need to link libraries to use either of those. Can you tell me where to find them? I’m guessing the Open GL ones are next to the processing Core, but there are a bunch to choose from and all the ones I’ve tried don’t work, and I can’t find the ones for FX2D at all.
EDIT: Okay, I managed to get P2D working. FX2D still gives me exceptions, though. I’ll see how P2D treats me and go from there.
I’m guessing from a quick look that this is a top-down 2D open world, and that a static background layer of grass / stone / etc. is rendered from map data every frame? Is that right, that the main load is your backgrounds?
Yeah, exactly. Thats almost the entirety of the load right now.
In general, creating a collection of large map tiles – e.g. 4x4 = 16 – that cover an area larger than the screen viewport. Render them to PGraphics once when they get close to the viewport, and overwrite them when they get far away and are replaced. This way the maximum amount of space you are drawing in a single frame is 7 tiles, but that is a very rare event, and you are mostly sliding pre-rendered images around (fast), not rendering them (slow).
You can write your own – it is fun and educational – or you can use an existing implementation, like the PTMX library for Processing.
Here is are two old examples of one-off tilemap renderers, each implemented in a single Processing sketch:
Here is a recent Processing game framework, PixelPie, that I believe works with Tiled maps – I haven’t looked closely:
If you were working primarily with image data (e.g. maps) rather than sprite-drawing, then you would use Unfolding Maps, which also uses this tile logic:
For one extended discussion of tilemaps and Tiled (in actionscript, not processing) see for example: https://gamedevelopment.tutsplus.com/tutorials/parsing-and-rendering-tiled-tmx-format-maps-in-your-own-game-engine--gamedev-3104
It’s not tiles, it various shapes. And I want to be able to zoom in and out. I was thinking of just re rendering when the player moves the view, instead of every screen. I’ll look over those links though, they sound promising.
Sure – you don’t have to draw small tiled shapes or use the Tiled data format, you can draw whatever you want. You don’t have to use PTMX or PixelPie – it is the approach that is important: a generic approach to buffering pre-rendered scrolling content as it approaches the viewport. Within that approach you can have a tile-based and non-tile-based “objects” layer – as in several of the examples linked above – or you could have no tile-based layer at all, just objects layer(s).
This is a pretty widely used approach – for example there are hundreds of related posts on gamedev.stackexchange, including discussion of how the buffering relates to 1D and 2D scrolls, zooming, parallax, rect vs hex grids, layering, using the painters algorithm for isometric content, etc.
AH, now that sounds perfect I’ll have to look into that, thank you.