Friendly F#: fun with game programming and XNA

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
malvoria
Posts: 2
Joined: Thu Aug 18, 2011 9:55 am

Friendly F#: fun with game programming and XNA

Post by malvoria »

Hi! I hope my message is not inappropriate here and I apologize in advance if it is.

I just coauthored a book on F# and game programming. The main subject of the book is the F# language and its various constructs, but every single chapter is centered around a game-related problem. Each one of the first 5 chapters describes a problem, shows and discusses its solution and then discusses in depth the F# constructs used. The book has a (relatively unique) "problem-solution" approach where everything is explained because of how well it works in solving the problem, and not just "because". The 5 problems we present are:
- a bouncing ball
- the Saturn V rocket
- an asteroid field
- a large asteroid field optimized with quad trees
- a police starship that must fight off a pirate ship attacking a cargo freighter

In the last two chapters we use XNA to build a 2D and 3D renderer for two of the samples we have seen. We show the basics of the SpriteBatch class, the Model class, input management and audio with this powerful framework. Basically, we cover the most important aspects of XNA in a simple and succint way.

The book is recommended for programmers who are already familiar with an imperative programming language; a little bit of knowledge of object-orientation may help in the latest chapters, but it is by no means required. The book may also be read by complete beginners to programming, but in that case the reader should expect to have to *study* the book and not just read it.

If you want to take a look at the samples, they can all be downloaded freely at: http://fsharpgamedev.codeplex.com/

Let me add that we have written this book mostly for fun, and as such the book has two important aspects: it is short and it is cheap (7,49$). The book can be found here http://www.amazon.com/Friendly-Fun-game ... 51&sr=1-12 or here http://www.smashwords.com/books/view/81765

Thank you, kind regards

Giulia
XXChester
Posts: 95
Joined: Thu Oct 04, 2007 5:41 pm
Location: Ontario, Canada

Re: Friendly F#: fun with game programming and XNA

Post by XXChester »

Why would anyone want a book on F# and XNA......they are totally different languages/frameworks, opposed to a pure XNA book or C# and XNA? XNA is a framework written in C#....
malvoria
Posts: 2
Joined: Thu Aug 18, 2011 9:55 am

Re: Friendly F#: fun with game programming and XNA

Post by malvoria »

Hi!

Sounds like you could benefit from reading our book, then :P

Let's start with a bit of background. :)

F# and C# are .Net languages; in theory (and in practice) they can both accomplish the same tasks and access the same libraries. A .Net application can make use of libraries written in various .Net languages, and in fact we are currently building a commercial game in both C# and F#: most rendering is done in C#, while most game logic is done in F#, but it is just because we have found this particular division convenient and nothing stopped us from doing the opposite.

XNA on the other hand is a set of .Net libraries for making games and an accompanying framework (a series of tools for deploying, debugging and selling games), not a language.

Any .Net language can access the XNA libraries and take advantage of the framework. F#, in particular, has built-in a series of scientific computing helpers such as units of measure; F# also excels at algorithmic computing. All of this put together means that F# can be a great language for game development.

Let me give you a tiny practical example of some of the advantages that F# offers, when writing games, with respect to C# (mind you: C# is a very good language as well, but F# has some interesting strong points). In F# we can declare two vectors as having a unit of measure; for example, we could have:

position : Vector2<m>
velocity : Vector2<m/s>
dt : float<s>

and if we write:

position + velocity

then we get a compiler error, but if we write:

position + velocity * dt

the dimensional analysis performed by the compiler allows this code to pass. :wink: This particular feature has *no performance overhead*, since it is just a series of compile-time checks.

Another example is building a quadtree, which in F# is declared very simply as:

type QuadTree<'T> = Leaf of 'T | Node of  QuadTree<'T> * QuadTree<'T> *  QuadTree<'T> * QuadTree<'T>

A final example is F# support for constructs like coroutines, which are heavily used in scripting languages (such as LUA) in commercial games for building AIs and state machines:

let prince_ai =
co{
   let! princess=nearest_princess
   do! reach princess
   do! save princess
  do! go_back_to_castle
 }

Notice that F# is not a scripting language, it is statically typed and very aggressively optimized by its compiler, and its performance profile is the same as C#. For this reason F# beats languages such as LUA of about 10x (and in some cases even up to 100x).

In conclusion, F# is a powerful, expressive and reasonably high-performance language and many of its features can be of great help during game development. :D

I hope this answer helps clear some of the confusion about .Net languages and frameworks. :oops:

Best regards, Giulia

PS: XNA is written in C++, not C# :)
Post Reply