Thursday, December 15, 2005

Christmas, Santa is an anagram of Satan

I'm off to the UK. I won't be back in Japan till the New Year.
Everyone have a nice Christmas.

EDIT: Finally fixed the graphics bug in my main code. After christmas I'll have a chunk of free time and I want to get as far as possible into stage three.


Tuesday, December 13, 2005

Working hard under the decimal point : What to do if when you move your camera the border pixels of your textures disappear or get stretched

It seems as thought I've solved the graphics problem, which goes to prove that if you keep bashing your head against it the wall will eventually break (provided you don't die from a brain heamorrage that is). After asking on every message board I could think of and even a IRC chat room or two I managed to stumble on to what seems to be the possible solution by myself. Finding a solution is one thing ... a reason is quite another. I can make guesses why my fix works but I don't think I'll ever go digging for the truth, though it would be nice if some body would tell me :D I have ideas, hypothesis' - yes - but nothing verifiable.

Anyway enough ranting here's the solution I worked on, thus titled: "What to do if when you move your camera the border pixels of your textures disappear or get stretched."

1. Read the mapping pixels to texels thing.
2. Don't translate your camera by a very small number. (I was doing 0.003)

I fixed this by scaling everything up so my view port is currently 800.0f by 600.0f and my textures are some equally large number (I may try and half this, or at least get some nice balance). I move by a full 1f instead of some dodgy fraction. And that's it, the mystery of the missing borders solved. It must be something to do with rounding error, something that's happening far far away in the arch-under-sewer in the sixhundred and sixty sixth level of the direct x API.

Anyway thank you to the following places:

GameDev Help Me Post
The Game Wiki Help Me Post
Usenet Help Me Post
MSDN Help Me Post
Mr Gamemaker Post

Saturday, December 10, 2005

Unit Testing IO

A little tricky no?

It's bad form in unit testing to write to files outside of the code ... but you still want to make sure you loaders and writers work. If, like me, you make use of serialization you can serialize to memory then unserailize and den-duhr nicely unit tested - check the object equals the original one.

This requires:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

Here's an example of one my unit tests.

public void SerializesDataOkay()
TurnManager tm = new TurnManager();
tm.TurnNumber = 2;

BinaryFormatter formatter = new BinaryFormatter();

MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, tm);
stream.Seek(0, 0); // return to start of stream

TurnManager newTm = (TurnManager)formatter.Deserialize(stream);

Assert.IsTrue(newTm.TurnNumber == 2);

Remember the stream.Seek(0,0) or you'll be at the end of the stream and when you try and load you'll get a crash with a exception that's something like: "end of stream encountered before serialization finished".

I fixed up a bug in the clock code that had problems with repeated pausing. I wrote a unit test first and then fixed, yay for me.

For now I've let my graphics problem rest.

Some kind soul on The Game Programming Wiki message boards suggested clamping. And this cleared up a little of my graphics bug but something still remains.

The bottom red line of the texture dissapears when I move the camera to a certain position. I have the feeling this bug isn't probably quite obvious to someone who knows what they're doing and is something to do with my wonky camera setup.

I ripped the graphics out and made a very simple form demo to demonstrate the bug. Here it is

EDIT: Places problem posted now numbering 3:

(about a day ago)

2. The Game Development Wiki
(this morning, very fast helpful response but still no resolution)

3. USENET microsoft.public.win32.programmer.directx.managed
(Just now, fingers crossed)

EDIT 4: Microsfot MSDN help forums

I've finished my other todo and I've now deep in the heart of my map class hacking away dead things and redoing others. This time I'm adding unit-test plating as I go. In a round about way it's all to refactor one function that's comically bad and could probably be seen on The Daily WTF.

Friday, December 09, 2005

Under the water but in the boat

It seem I've finally dealt the killing blow to a bug that caused me extreme pain. I actually noted down a small fraction of my hunt for it here's the fraction.

Wait first let me explain the bug.

I'm making, like many others a simple tilebased game in Direct3D. I'm using Direct3D because we all know direct draw is the devil. Microsoft makes this rather hard. If I was doing it again I'd definetly go for allegro or some wonderful preexisting C# 2D engine.

Anyway in the 3D world there's the pesky extra z co-ordinate and there are cameras looking on to your 2D world. Also texturing isn't straight forward - it's not pixels anymore it's texels, the pixels evil cousin, he starts from the middle of all pixels and was designed by a crack team of scientists to make you cry. So you have to construct your tiles from vertices and rarara

I did this and at some point I got black lines, gaps, between my tiles. I hacked this away in what was now in retrospect a terrible bodge. My rendering code was like a dark jungle where one dared not venture. The black lines dissapeared and their was much dancing and rejoicing - I have no idea what I did but it was something hideous where the vertices spacing of the quad was changing depending on the size of the window. All I know is it wasn't a solution by pre-determined design, rather it was by pure bloody bruteforce.

Anyway the lines dissapeared but soon there where some overlaps and they appeared randomly every few tiles. Overlaps - why that's obviously something wrong with the tile plotter, one would think. Well it wasn't and after lots of confusion I decided it must be crazy 3D camera and floating point rounding errors creating gaps. Far too hard for me to bother win - I'll deal with it later.

Some months later on a monday was later. I decided I'd pull the graphics system out, simplify it and discover exactly where the problems came from. Now here's the log:

O Pulled out the graphics subsystem and madea simpler seperate program
-Revealed a drawing bug.
+Bug was fixed

O Fix is add to main code
-Problem remains

O A new game state was created in the main code to demonstrate the tile plot
+One tile plots fine, as the seperate program
-Using the RenderSubSection plots there's a bug
O Some pixels lines are stretched or clipped ~> a texture bug?
O It happens in the plotting funcion ~> a plotting bug?

Tests to run through

Plot a number of tiles behind hand PREDICT: No Problem

A tile rendered next to the original seems to over lap it! By one pixel.
Even when rendered apart the second tile seems to be missing, it's left most pixel column!
(turn off all other tile rendering and do it again)x
This does nothing

Thoughts For Prediction 1

Are the same tile render functions being used?
Trace through render function
Rewrite the plotting function so that's it's more efficent.
Check a certain conversion function ... currently I'm thinking it's floating point error.

O Remove the +0.03f patch
+horizontal overlap dissapears from both test and example
+all top row squares are the same
-two rows down on the top subsection render a tile seems to overlap another

O Test this in full screen
-no top row or bottom row
-green pixels are twice as thick as blue lines
-top rows are missing from the first two rows.

Editing the textures results in uniform one pixel gapped tiles but with a single errant pixel.

This seems to shout texture problem.
Fiddle with UV textures?

O Fiddle with textures applying different corrections to different axis
+Seems to work on hand plot
-Not tested in full screen
+Hand plot works in full screen
-Doesn't work on long plot.
-Both vert and horizon problems.

-> See if automantic plot can be recreated by hand and if similar problems appear.

O Surrounded texture with pink
+A new pink line appers under tiles
+Obviously current settings require adjustment.

O Adjusted single tile plot seems fine
-Auto plot has same problems
-Not tested full screen

O Added tiles below and above
+Hand plot no problems
-Seems to be an autplot problem.
O A little hard to say but full screen also seems good.
-Full screen has double pixel of the leftmost side (;_;)
+On the sunny side seems like the problems becoming isolated and a fix is within reach.
+It's hacky but if it works it's wonderful.

O Write own render
+Have pink lines from over sampling texture
+Has to be some function in the render function that doesn't occur with hand placement.
O Do hand placement in the exact same place to ensure it's something in the rendering function.

O Moving the render function around proves it's LOCATION as well as a rendering error.
+If location matters than its something to do with the camera
+So I go into camera (;_;) I've been avoid this.
+I change the screen coordinates so that they're ratios of the actual window
+This *uniforms the problem*
+I also hard code the tile size. This wasn't hard coded as I was hoping bigger windows would have more tiles ... it seemed this never worked.
~> Potentially there's only one problem to solve

O First let's test full screen.
:o fullscreen actually seems to work now :o

My code is like an unexplored jungle; there are scary parts you just don't venture into.

O Let's check the actual game ... preparing self to cry.
+Same problems as before pretty much, just looks better now.
+I think now it must be texture problems.

(Solved earlier code mystery - characters where never scaled but tiles where, that's why characters didn't look awful)

Anyway so you see at the end of here things where starting to look hopefuly. But I was once again on the wrong track closer examinings proved there where still graphical glitches.

The seperate program that was suppose to locate the bugs was curiously bug free it worked a treat. So I pulled in some of the cleaner structures - bugs remained. So I pain stakingly ensure the camera and device where all set up exactly alike. The same.

Some lines where a little thicker than others in certain places. There seemed to be less and less places where there could be a problem.

Drumroll please: Eventually I looked to the form. The form itself is a bit of trickyness if you're using it as your device canvas. Let's say you want your game to have an 800x600 view - pretty standard for old school style.

Well you don't set the form to that no because the form includes the little lines of GUI niceness at the sizes and the size of the header bar. All this I knew and thought I had accounted for. Obviously I was somehat mistaken. The only difference between the two programs at this point seemed to be that I was running the main code directly upon the form while the test program be rendered on to a panel control. So I threw in a new control set it 800x600, perfectly fitting the form, and what do you know sharp crisp prefectly aligned graphics. Wonderful.

I found these two links helpful in solving parts of my graphics bug:

Directly Mapping Texels To Pixels (MSDN)

2D Tile Engine Problems, a post on the message board of The Game Programming Wiki.

I'm quite happy, but I'll be happier once the codes all locked down again and performed as expected.

EDIT: Increasing desperation prompts GameDev posting.

EDIT2: ...and now Game Dev Wiki too

Wednesday, December 07, 2005

Why monkeys fight

I found this an excellent and interesting read.

I have a whole spiel somewhere about whatsup with my progress but there's not much at the moment. I'm not willing to move on to the next stage until certain bugs are squashed. I could also do with bolstering my work ethic I think :D Unless I have at least a nice three hours or so of free time I'm reluctant to sink into my main code.

This next weekend is my last before I return to the UK. I'll be back in Japan in the newyear and will have some hacking time them. My new years resoultion will be to be well into stage three before I restart my job :D

When I go to school I often have snippets of free time - I read stuff that I've scrapbooked from various webpages. This morning I read something like 43 things where you write down your goals and see who shares them - sounded quite intereting perhaps it will motivate me more.