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 1)
2 Pages1 2 
on Apr 28, 2007
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.

I feel your pain, Cari. Programming can be a nightmare at times.
on Apr 28, 2007
Interesting how something like 2D graphics in a mini map can have such an effect on framerates in the game.
on Apr 28, 2007
An interesting, very well written journal.

Keep up the good work (both types, coding and blogging )
on Apr 28, 2007
If you can read this article, you don't need glasses.
on Apr 28, 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?
on Apr 28, 2007
These kinds of journals are enlightening to me. It gives a good insight into the processes involved, and as I hope to be in the industry in a few years, that's always pretty useful.

Your list of bug after bug reminds me of the time when I was writing my AI pathfinder...
on Apr 28, 2007
I just don't like that glare on it. It makes the map harder to read at the top. Please make that thing optional.  
on Apr 28, 2007
I just don't like that glare on it. It makes the map harder to read at the top. Please make that thing optional.


There's a mod in the library that removes it.
on Apr 28, 2007

I just don't like that glare on it. It makes the map harder to read at the top. Please make that thing optional.


There's a mod in the library that removes it.


Great, first mod for me to install
on Apr 28, 2007
There's a mod in the library that removes it.

But it only works for DA, not DL.
I would like to get rid of the glare in DL.

on Apr 28, 2007
Awesome, shame I had to highlight it to read it. (the text is in black)
on Apr 29, 2007

I enjoyed the update, and I *do* find it insightful to hear your perspective about the graphics tools available, which you've tried, and which ones worked. I appreciate the work that you and the everyone at Stardock continue to do on GC2DL/DA.
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.

I wrote the article on JoeUser, so that must be why it showed up as being black. I'll report it to the web team.
on Apr 30, 2007
Very nice reading, thank you.

Awesome, shame I had to highlight it to read it. (the text is in black)


Yes, black on black is a hard read

Luckily I have the Preference Toolbar PrefBar installed. One click on "Colors" and one refresh and the black background is gone. Now I can read it black on white what is much better
on Apr 30, 2007
I just selected all the text so it was highlighted. Then it showed up fine.
2 Pages1 2