Thursday, September 9, 2010

SDL Conversion Complete

Since I have two or three more days before the expected update, I'll spend the extra time adding stuff. On my end, an object to handle the generation of creatures separate from the creatures themselves. This will save processing power and speed by reducing irrelevant data. On the user end, creatures will be individually generated with their own quirks, and a stat system with growth should be coming in.

But yes, the SDL conversion is complete. Everything is done and behaves exactly as it did. The advantage? Come next update, you will be able to run it right out of the box, pretty much regardless of your system layout. Heck, theoretically you can run it in Mono on non-Windows systems. I'll need more people to play test it though. I've only got one download on last week's update. :/

I faced several problems during the conversion. The first of which is that SDL is written in C, and I have to get at it with Tao. Which means lots and lots of pointers. Like I said in the last blog post, I was able to sort that out in safe code with IntPtr and Marshalling the pointer into accessible data. The second was that SDL alone does not come with some of what you think would be pretty critical commands, like drawing text or a rectangle. You have to import SDL_TTF and SDL_Gfx respectively. Which it turns out Tao included, and it took me forever to find.

And the third time-waster was looking up how to change the color of an image. You know, I have a white @ sign, I want to turn it green because it's a goblin. Well, SDL doesn't have a command for that. SDL_Gfx doesn't have a command for that. In fact, there are no commands for it whatsoever. The things I found about color key switching were all theoretical and did not work for me. No one had working code anywhere. That's when I decided to check out if anyone on the roguelike development chatroom #RGRD could give me any ideas. I will say right now that if it weren't for them, Adventurer may very well have taken two weeks to convert and still be in black and white.

#RGRD is your hero, guys. It turns out that there really is no easy way to do it, but someone helped guide me through how to make it happen. So I had the data, and the color I wanted to change it to. I have to be able to get at the data. Not normal C# stuff, I had to get at the ^raw^ data. So I needed a pointer. And not just an IntPtr. I needed an honest C style pointer. So I had to flip it to unsafe mode. Got a pointer to the raw data. Now, how to mess with it?

You can get at pointer data by saying pointerWhatever[somenumber]. That finds a block of data in there. In this case, a pixel stored in RGBA format. Or maybe it was ARGB. I forget. I then had to convert a color into its raw ARGB data. Thankfully, there was a command for that. Then I had to put it in an appropriate pixel. Well, at first I just picked one at random. White dots started appearing in images, which meant that it worked.

Next, I had to get at not just one, but all the pixels. Pixels are not stored as [x,y] coordinates in an image. They're all one dimension. Which meant I had to write a function to make that happen. Which I did with the help of the #RGRD folks. All this with the help of the #RGRD folks. Once I had access to all the pixels, I had to work out criteria for changing the color, or else I'd end up with solid color blocks. Color multiplying was brought up, and I tried to implement that. Basically, R100 * R50 = R75. Well, it didn't work like I wanted. But then I figured something else.

I simply looked for any pixel that was not transparent (Alpha > 0), and changed only those. It suddenly worked. From misshapen color garbles to the old Adventurer. And from there, it was smooth sailing. Well, except for a little kink with the shift key that I just today finished working out. After all that, the final thing was just a logic slip on my part. So it's all working, and expect an update in two or three days. I can't seem to find the chat log, which has a link to a Pastebin post which would have given you all the code. Nor can I properly thank the person who showed me how to do it. So to all my players, all of #RGRD, all of the roguelike community, and in particular that one #RGRD guy, thank you!

No comments:

Post a Comment