Archive for category Programming
Linq is probably one of my favoriate features of C# and to show you why here is a quick code demonstration.
Quite often you want to write some code that has to iterate over the collection; For example: Finding an element in a list of to see if it is present.
For a program I’m working on at my day job, I decided to implement a rule that says each account must have at least one user who has admin rights for the account, and here is some code that checks that condition.
(Note to self: Find good code formatting plug-in for wordpress)
bool AtLeastOneAdmin = false;
foreach(User u in Users)
AtLeastOneAdmin = true;
Pretty straightforward, Now lets write the same code using Linq:
Bool AtLeastOneAdmin = (from u in users where u.isAdmin == true select u).Count() > 0;
That does the exact same thing and only takes up one line of code!
I spent the morning creating a random name generator which will be added to the ‘create character’ section of Caverns of Underkeep.
I’ll explain how it works in a minute. But for now, here is an example list of names:
So not bad, sorta could be real-made-up fantasy character names maybe. This is how the algorithm works:
First of all, I did a little bit of research reading about syllables on wikipedia
I decided to create two different name types – Those that start with a vowel, and those that don’t. For the names that start with a vowel I use the form VCC-VCV
Where C = consonant and V = Vowel
For names that start with a consonant I use the form CVC-CVC
In each case I randomly determine if the name length is 5 or 6 characters long. If the length is 5 characters, then I just leave off the last letter in the string.
Obviously it’s very simple and I don’t do any weightings against uncommon letters like x or z or anything like that. But it gets the job done
If anyone is interested the java source code is avalible here
I usually hate these silly little tests that are all over the web. But I found this one pretty interesting.
It’s sorta losely based on the Meyer’s-Briggs personality test – It has been a while since I took the Meyer’s-Briggs, but I think I was an INTJ? Or something like that, which is apparently quite a common type for people who like to program computers.
Anyway, here were my results…
Your programmer personality type is:
You’re a Doer.
You are very quick at getting tasks done. You believe the outcome is the most important part of a task and the faster you can reach that outcome the better. After all, time is money.
You like coding at a High level.
The world is made up of objects and components, you should create your programs in the same way.
You work best in a Solo situation.
The best way to program is by yourself. There’s no communication problems, you know every part of the code allowing you to write the best programs possible.
You are a liBeral programmer.
Programming is a complex task and you should use white space and comments as freely as possible to help simplify the task. We’re not writing on paper anymore so we can take up as much room as we need.
Yesterday I spent the entire day refactoring code for my upcoming game.
Refactoring is a funny thing because essentially you have to break your code, rearrange it, clean it up, put it back togeather, test it, and then hunt down all of the new bugs you’ve introduced into the system – By which time you should end up with a program that resembles exactly the same program you had before.
And your line count doesnt even go up.
However, I do think its very important to do refactoring as you go and I’m going to try to spend one day a week where instead of writing new functionality I’m going to take a look at the existing codebase and try tidy it up. This will hopefully reduce the probablity of errors popping up as any assumptions I made to get something ‘to just work for now’ will be removed.
The other goal is to start to make the code more re-usable and not so specific to my current project.
Meaning I can reuse code for the next game I work on. The obvious target for this is the GUI library and Finite State Machine code which is turning out just peachy. Of course one should be very careful – I’m working on a game, not a gui library. Which is an important distinction to keep in mind. And although it’s my opinon that its a good idea to refactor, one should only do what is necessary.
One of the principal reasons for the refactoring yesterday was because I need to implement modal dialog boxes and the way the GUI library was structured it would have been a very nasty hack™ – In my case refactoring was nessesary in order to move forward – what Jay from Rampant Games calls a Local Maxima Problem.
I haven’t read any technical books on refactoring code. But I’m going to add a few to my reading list and perhaps sprinkle a few tidbits about how to improve code as you go in a later blog post.
In my earlier post Is Crossplatform Development just a Myth? I mentioned the difficulty of creating truly cross-platform games. (And when we’re talking just Apple and Windows here, one could argue to what degree is that cross-platform anyway?) And I just covered a few api’s and programming languages. What I didn’t mention was game engines. There are definately cross-platform game engines out there, SDL, PTK, Irrlicht and Torque to name what appear to be the most popular engines avalible. Each one comes baggaged with it’s own pro’s and cons, and may or may not suit your particular needs as an indie game developer.
As I am looking into doing 2D games, but wanting to take advantage of 3D hardware acceleration PTK has recently struck my interest. I’ve yet to have a play with it myself, it’s probably going to be a couple of weeks before I get some free time to really assess it’s featureset. But it does come with a long list of finished indie titles that have used the engine. And in my opinion there is no better litmus test for a game engine than real shipped products.
While I’m at it, I’d like to point out that each of those engines have OpenGL/DirectX rendering support, which I’m now sure is a requirement if you want to have games that behave nicely on both PCs and Macs.
I’ve been experimenting with a couple of different APIs/languages so I can become more informed when it’s time to choose my Apple/Windows cross-platform strategy. And I’ve come to the conclusion that there really is no such thing as a truely cross-platform API.
One of the chief selling points of Java is that it’s supposed to be cross-platform. But if I want to design a UI using swing, Ok sure, its going to run on multiple platforms. But its going to be just as ugly on each of those platforms. I’ve yet to see a program written in java with a nice UI. Most of the time when I open up an app written in java, I just want to close it immediately, it just doesn’t look like it belongs on my system.
OpenGL is also supposed to be cross-platform, but its Windows support is severly lacking, requiring all kinds of extensions to get functionallity that is also dependant on having the appropriate drivers and hardware. It’s just a pain, always remember your target audience, and in this case I can’t be sure they have their drivers up to date.
SDL is a nice cross-platform library, but unfortunately it doesn’t have access to some 3D acceleration features that my project requires. If I was using straight 2D I could use it, But unfortunately it’s just not for me.
Then I thought about writing my own API abstraction layer and use DirectX for rendering on windows and OpenGL on the mac. But this would complicate the code greatly by having that extra layer in there. Then I decided, “Screw it, I’ll write the code for windows using DirectX 8.0″ (which comes installed with Windows XP) and just port it over to the Mac when it’s done. And replace the DX code with OpenGL code. That way I can assume different hardware specs and requirements on each system and program for each target machine rather than being reduced to the lowest common denominator for both platforms.
I’m still looking into it, but this seems to be the best bet so far.
More correctly what programming languages am *I* going to learn.
The domain of software development is huge and it’s next to impossible to learn everything. It’s even more impossible (if such a thing is possible to be more than impossible) to become a master at everything. In fact, the domain is so huge that its impossible to learn even 1% of all the new technologies / methodologies and programming languages that are out there.
So if I want to make games (and I do) this is my little list of technologies to focus on. Disclaimer: I am currently interested in writting cross-platform games for personal computers and my choice of languages and technologies reflects this. I also may be interested in programming for consoles at a later date. This list is completely different to someone who wants to do web 2.0 stuff, which just goes to show the kinds of disjoint bubbles even developer geeks live in.
C++ and OOP
Defacto standard for computer games. Even though C++ is loosing ground in other areas like the web and business apps. It remains the market leader for the bulk of commercial game dev. This is a huge language, a monster that will take many, many years to master, and it’s even bigger than that, it’s a way of thinking.
Total number of C++ books on my amazon wishlist : 7
When I finish university I’m going to look for a job working in C#. If C++ is going to be the language that computer games are written in I’m predicting C# is going to be the language that the tools are written in. C# has many very nice features and is evolving nicely, the .net framework and windows forms gives programmers a large chunk of easy to use managed code to get apps up and running quickly. It’s also nice to use with DirectX and not to mention the fact the Microsoft’s XNA gamedev toolkit for the Xbox 360 uses C# as its development language.
Total number of C# books on my amazon wishlist : 6
C and Assembly
I’ve been thinking about doing some homebrew programming for the Nintendo DS and while these days if you’re programming for the PC there’s little incentive to write C code or assembly code. Modern compilers take care of all the nitty gritty for you. But if you wanna get low level with the hardware then you need to know all about your bits and bytes.
Total number of C/ASM books on my amazon wishlist : 3
Small, portable – written in ansi c, powerful, extensible and commercially proven (World of Warcraft, Baulders Gate) scripting language. I’m using lua for my current project and its just really nice. If you dont use a scripting language for your game projects you have to get one. It allows seperation of game logic with game engine code. Which makes life easier and allows people to program the game without touching the game engine (and potentially breaking it.) To program your own scripting language is not a trivial task, that’s why I recommend LUA.
Total number of LUA books on my amazon wishlist : 2
Graphics APIs : OpenGL and DirectX
You really should learn both, they are more similar than dissimilar. OpenGL is of course portible, but DX has its places – Xbox and windows, C#.
Total number of DX and OpenGL books on my amazon wishlist : 4
So thats my list, and that’s not including general computer science stuff that a game programmer should know. Data-structures, AI, Network programming etc…
C# is sexy.
I’m quite slow to move on to new technologies. Infact I even thought that Microsofts .net platform sounded like a dumb idea. But I suppose theres a lesson there, “Don’t knock it till you’ve tried it” as the saying goes.
My inital adversion to .net was largely caused by having the end-user need the framework installed on their machine. But with Vista now rolling off the shelves we can be more confident in users actually having the framework. Which is good for small companies who do the majority of their distribution online. Getting someone to download and try your product is one thing. Convincing them to install the .net framework after running your program and finding that it doesn’t work straight away is another.
But anyway, C# is sexy. I used to use VB6.0 for many of my homegrown-inhouse gamedev tools. But now I’m going to be using C#
The power of C++ with the nice drag and drop functionality of VB for creating windows forms apps – Its a good thing to have.