C# Game progress

What’s up? It’s been a little while since I’ve updated again, but that’s because I’ve been busy with the latest project. I’ve been working on a GUI system, using XNA only so far, and it’s been an interesting experience.  I’ll include some screen shots, but remember it’s super basic.

So what you’re seeing there is two buttons, complete with basic hover and clicking ability. It took a while for me to understand how to pass functions in to it easily. I ended up using lambdas, so something like “button.command = () => AMethodToBindToButton()” works nicely. It’s not perfect though  because neither is my understanding of the `Func` concept. I think I’ve written that only functions that return an `int` is valid as a command for the Button class. For now though, it works.

On the bottom half of the screen, you’ll see a MessageBox stretched over the entire width of the screen. That, my friends, is where the games text will be shown. I’ve got it so you can pause  the stream, which I really found clever. All it is is showing a certain range of messages, say from  the last item to the seven before it. All I have to do to scroll then, is the change the value of the ‘last item’ and the message box will appear to scroll. For now, it’s only a simple List<string> but eventually I’ll have to write a special class that allows for  word wrapping and text formatting, such as bolding and coloring, so as to achieve a Zelda-like effect where names and places are coloured.

Above that MessageBox is a test one, just floating in the air. Again,  I was proud of myself, because in order to do that, I only needed to create a new instance of the MessageBox class. This wasn’t something I expected to have happen so easily, because I simply hadn’t thought about writing the code in that way yet. I’m still learning so it’s taking time to do the simplest things.

With getting the second MsgArea set up, there was one catch though: I could only see one box on the screen at a time, and I couldn’t figure out why. This was a frustrating issue, because it was a case of me not fully understanding what goes on behind the scenes in XNA, and I didn’t know what I was doing wrong, in order to start looking in my code for a visible bug. So I did what any good programmer does, and asked for help.

I turned to Stack Overflow and asked the question with a simplified version of my code. A user there, Cole Campbell helped me out a great deal, and found the solution: In XNA, every time the RenderTarget is reset to the Back Buffer, or null, like so: `graphicsDevice.SetRenderTarget(null)`, the back buffer clears itself, so I had to rewrite the code, and split it into two parts. The first, where I draw the MessageBox onto a RenderTarget2D and then second, draw that RT2D to the backbuffer.  So it’s no longer ‘draw on RT, reset the render target to null, draw on another RT and then reset the backbuffer again`.

Oh, and lastly, once I got the drawing sorted out, I quickly added dragging the messageboxes, which was only a few lines, where I’d find the difference between the current mouse position and the last, and I’d add that to the widgets current position, and draw the widget there instead. Worked pretty nicely, but still a bit buggy, due to the way I search serially through the list of widgets: If a widget was created before another, it’ll always catch the mouses attention before the ones that came after, meaning if you drag an older widget over a newer one, you’ll always start to drag the newer one, leaving the older one behind.

That’s it for now. Cheers!

Leave a Reply

Your email address will not be published.