Lead Developer, Stardock Entertainment
Improving Framerates in Dark Avatar by re-writing the Minimap
Published on April 27, 2007 By CariElf In GalCiv Journals

I'll admit, I'm rather ashamed by how long it's been since the last time that I wrote a dev journal. The trouble is, I usually feel that what I'm working on is not interesting to end users (particularly the ones who aren't programmers). The stuff that I work on is usually not visible, unless it's a screen. It's usually something like data manipulation or i/o or something like that. I've been told I'm wrong, but I don't really expect many people to get as geeked as I do about generic xml parsers. Also, I know that I spend too much time trying to write a good article when I could be spending that time coding, doing the second level tech support, or going home on time instead of working late like the workaholic I am.

However, the last few weeks I've actually been working with graphics code: I've been re-writing the minimap. I had to deal with the minimap code in GalCiv1, and it was neither efficient nor that good looking and I was sick of trying to get the 2D drawing to look a little nicer. So I made one of the other coders do the minimap for GalCiv2, and he used GDI which, to be fair, looked nicer than the GalCiv1 minimap code. GDI has always been inefficient, but 2D used to have hardware acceleration support before 3D became the be-all, end-all of the game industry. (Note: I really like 3D and it makes a lot of things easier that would be horribly inefficient in 2D. But I'm not a fan of throwing out the baby with the bathwater.)

Anyway, in Windows Vista, GDI is horribly slow. So whenever the minimap was drawing, it was dragging down the framerate, particularly when scrolling because that causes the minimap to re-draw a lot. Not to mention that the code to draw the fog of war, ship ranges, and zone of control on the minimap was really inefficient. Basically, it was setting the color of each pixel in the minimap one at a time. The ship ranges code was even worse in that it was simulating alpha-blending by checking to see what the current color of the pixel was, multiplying it by 0.5, and then setting it again. I wasn't aware of just how bad this code was until I had to get in and start re-writing it, or I might have done this sooner. Also, it had seemed that the scrolling slowdown was an NVidia bug because ATI cards don't seem to have as much of a problem.

Last week, I put up a link to a test build where I replaced the calls to the GDI functions with the GalCiv1 drawing code. I tried this first because I was able to get it done fairly quickly, and it was code that I was already familiar with. I was hoping that since it used assembly code for copying the pixel colors that it would be some kind of improvement over GDI. Unfortunately, the people who tried it reported no change, so it was back to the drawing board. Our graphics gurus were working on other things that I didn't want to pull them off of, which meant that I was going to have to do it myself.

This time, I knew that I had to use Direct3D. For me, graphics code is not fun to write. I can do it, but since I don't do it that often, I have to look up a lot of stuff, usually several times. I wrote the code that displays the user inteface in GalCiv2, but that was fairly simple and only involved one kind of primitive: triangle strips. (For those of you who don't know what that means, it means that all of the graphics that make up the interface are displayed as triangles that are connected so that they form rectangles called quads. Just picture a square with a line drawn from the bottom left corner of the square to the top right corner of the square.) That was also quite awhile ago, because we used the code first for Political Machine which came out in 2004. Now I had to become familiar with the other primitive types so that I could learn how to draw things like circles.

I finally got the new code compiling and running without crashing yesterday late in the day, but it wasn't displaying correctly. I finally left an hour after I had been planning to leave and spent most of today debugging it. I now have it to the point where almost everything displays correctly. The starbases are practically invisible even on tiny galaxies, so I'll need to work on that. There's also this bug where the minimap shows up (before it's rotated) at the upper left hand corner of the screen briefly when the game first starts, instead of where it's supposed to be. Furthermore, there are a few more things I can do to make the minimap draw more efficiently. But it was good enough to try it out on my Vista test box, and I'm getting far better framerates than I was before. I'm also getting better framerates on XP. Yay! Next week, I'll also fix the line graphs to be more efficient.

The best thing about all of this is that we'll be able to re-use this new 2D drawing code for our next games, so my time was not entirely wasted.

Edit: I've added a link to an updated test build if any of you want to try it. But make sure that you back up your existing GC2DarkAvatar.exe and Lib3D.dll files. You should have the latest version off Stardock Central (1.59.078) installed before extracting the files in this zip.

Also, I made the text blue in the hopes that it would show up better on the galciv2 forums and journals page.

Link


Comments (Page 2)
2 Pages1 2 
on Apr 30, 2007
Will these optimizations appear in DA 1.6 Beta 2?
on Apr 30, 2007
Question: I don't like looking at my mini-map at a tilted angle. Do you think you can have it displayed at a none tilted angle?



Umm, you might be able to do it with a mod but I'm not sure if it's hardcoded to be at that angle or not. I haven't changed any of the math behind the minimap, just the drawing code. I'll have to check.


Thanks for the reply. I'll see if I can mod the user interface.

By the way while I'm still thinking about this, is there already a mod that untilts the minimap, or will I be the first to attempt this?

on Apr 30, 2007
By the way while I'm still thinking about this, is there already a mod that untilts the minimap


There isn't. At least as it was before Cari's changes, it seems to be rotated in the code, so the most you can do is stretch or squash it in the dxpack.
on May 01, 2007

Will these optimizations appear in DA 1.6 Beta 2?

Yes.

Thanks for the reply. I'll see if I can mod the user interface.

By the way while I'm still thinking about this, is there already a mod that untilts the minimap, or will I be the first to attempt this?

The rotation is hard-coded, unfortunately.  If I have time, I might be able to put in logic for it to detect if it should be rotated or not, but I'm still trying to figure out why it shows up in the upper left hand corner for a second when the game first starts up. 

on May 01, 2007
The rotation is hard-coded, unfortunately.


I noticed. The dxpacks shows up as gibberish when I try to open them up in notepad. Also, all the mods I looked at that modded the user interface didn't provide much insight in the matter. The no glare minimap mod is an .exe file that replaces the existing dxpack, and the Dread Lords example mod used dxpacks that were also gibberish.

If I have time, I might be able to put in logic for it to detect if it should be rotated or not, but I'm still trying to figure out why it shows up in the upper left hand corner for a second when the game first starts up.


I'll appreciate any effort you are able to make.

Please note, you don't have to make something complex such as making the minimap rotate as you rotate the main screen. An option in the options screen that simply untilts the minimap will suffice.

on May 01, 2007
The dxpacks shows up as gibberish when I try to open them up in notepad.


That's because they're zip files. Open them in a zip program and you'll see all the images used for the interface bits, and the .ini files that control them all.
on May 02, 2007
That's because they're zip files.


Hmm... you're right. They are zip files. This will keep me busy for a few days.

Thanks.
on May 02, 2007
Blue is much better now but still far from optimal.

I just selected all the text so it was highlighted. Then it showed up fine.

Usually it would work. But not for this text. Because the font is so small and there is very little line feed between the lines.

I have to increase font size to read it. But when I do that and highlight the text the lines overlap. It's very ugly to read. Other texts (even in the Journals) don't have this problem.

This font is definitely not accessible.

on May 02, 2007
"Will these optimizations appear in DA 1.6 Beta 2?"
CariElf: Yes.

Any ETA on DA 1.6.Beta 2?
on May 02, 2007
Also, I made the text blue in the hopes that it would show up better on the galciv2 forums and journals page.


Well, it seems to have backfired - I find dark blue on black to be hard to read. Usually white and yellow work better on dark backgrounds.

Perhaps you now understand why a growing number of people call Vista 'ME2'. Beware of the Programs area of Vista (aka program files), cause any attempt to update programs will be seen by Vista as malware, and Vista will trash all files on all partitions. Everything under Programs is Protected Content.


It has its problems, yes, but I don't think they're quite as bad as ME.

I've never seen Vista mistake an installer or update as malware - can you elaborate?

Everything under Programs is Protected Content.


Incorrect. Everything under Program Files requires administrative privileges for write permissions. "Protected Content" has to do with DRM, not file permissions - you are confusing the two; they are not the same! You can easily override the file permissions by elevating to admin privileges.


Anyways, back to the subject:

It's very nice to know what's happening behind the scenes . As a Vista user myself, I am very interested in seeing improvements that improves the game's compatibility with Vista. Thanks for the info!
on May 04, 2007
Any idea when the next patch will be?
on May 05, 2007
Any idea when the next patch will be?


Cari posted 1.6b2 in the Beta Reports forum, see the sticky there.
2 Pages1 2