Devlog : Shaders and other stuff

Hum, that was a busy week code wise, but I can’t say it really shows in-game, but I have done a lot of code changes I postponed for too long. I rewrote the way objects are stored and deleted from memory, which massively improved performances under some situations, but that is code spread around my whole source code and debugging isn’t finished yet. I also rewrote the rendering engine. It’s better and can use shading effects from your GPU. And as usual some bug fixes. So yeah, fairly technical post ahead.

Rendering Engine

Before the recent change, I was using a buffer rendering the whole sector 1:1 in size while culling objects that were out of vision range, then I was cropping and resizing the result to the game window. While it was a fairly straightforward method, this buffer was the size of a whole sector, 5000×5000 pixels, eating a whooping 100mb or RAM all by himself. Also applying any graphical effect to such an area would have been difficult.

Now, the buffer is the same size as your game window and all visible objects are resized directly. It’s a bit faster, and only a bit, because resizing a lot of small objects is only marginally faster than the global cropping/resizing method used before. But the main improvement doesn’t lie in speed gain for once:

Being resized individually, each object loose much less detail when the game is zoomed in or out. The image as a whole looks sharper. Also, selection boxes and floating text around ships and stations aren’t dependent on the zoom level anymore, they are always readable now.

Last but not least, I can use all sorts of “pixel shader” effects on the image to improve quality. I am still experimenting with it and I have issues with my GPU taking its sweet time to render things properly, but having the suns actually casting lights in the surrounding area is not out of the question now (it will be optional and require a correct graphic card).

Object and Memory Management

That one is a though one to explain in layman’s terms. Let’s say that before my objects (ships, bullets, stations, everything…) were stored in simple lists: One global list for the game, and sector based lists to quickly access objects in specific sectors. What you have to know, is that resizing lists (adding or removing elements) is a very costly operation for a CPU, which doesn’t really resize but instead make a new copy of the list (taking changes into account) and delete the old one. So of course, I avoided resizing lists when deleting objects, just clearing the spot used by the deleted object and using it when needed. The issue is that in large battles, several hundred bullets and laser beams can be created, then deleted every game tick, leaving gaping holes in the list when the fight dies down. And as I still have to go through the whole list when i need to find a specific element inside it (hint, all the time), navigating lists that look like that

1: Drath Ship
2: Core Ship
3..400: empty space from deleted bullets
401: ship that entered the sector when the bullets were still alive

can be quite wasteful CPU wise. Not even mentioning how error prone such things can be in a multi-threaded game.

So I switched to something I knew I should have been using in the first place but didn’t because I am stupid, I guess, Linked Lists. Simply put, each object has a reference to the next object in the list, when you want to delete an object, you simply change the reference of the object before him and to the object after him. Same goes for adding stuff. The downside is that it takes marginally more RAM (a few octets per object and I just saved 100Mb in the rendering process, so that’s not a concern here), and that if I want for some reason the object n°10 in a list I will have to go through it, instead of picking the element. As I do very little of that, and very rarely, it’s not a concern either.

The final result is that during large combats or in extremely busy sectors, things go more smoothly as the “list” is resized in real time. Collision detection (each bullet has to check the whole list of ships every 50ms) especially benefit from this change. It has other more technical implications, but no need to go into too much details.

This being said, that was a big change, every source file, in multiple parts had to be modified. I am now squashing the resulting bugs.

Bug Fixes !

What’s good when you have to carefully re-read your whole code is that you spot bugs you don’t see when playing the game because the more you play the more you get habits that may cause you to ignore the elephant in the room. So here’s the list of obvious bugs I missed completely and fixed (hopefully) this week:

  • It was possible to “target” explosions.
  • It was possible to have something in another sector targeted, causing all kinds of trouble with missile and homing mines.
  • Being in a sector where an invasion take place could cause a nasty crash
  • Laser beams from destroyed ships tracing random lines across the screen (oopsie)
  • Flak Missile causing AI crashes (that were not necessarily visible to the end user)
  • Cargo window crash when the selected item has no image
  • Memory leak when restarting a game (sector background not deleted from memory)
  • Threads not quitting correctly (another memory leak when starting a second game)

That’s all folks!

I hope to finish those relatively boring changes during the week and go back to missions and pirates the weekend, with maybe a release early next week if everything goes fine.

Cheers,

SK.