Category Archives: XNA
If you are just starting with XNA game development, than you should probably read this post before you invest too much time in XNA.
Mind, this is no official statement from Microsoft, so there is still hope, but i agree with Promit. It is most likely, that we never see another big XNA update.
An inkcanvas can be serialized so that you have a byte array to send over a network.
For the client, in this case the canvas someone draws on, you create a TcpClient.
Fortunately this is handled by the .Net framework for you.
TcpClient client = new TcpClient(“192.168.0.1”, 42000);
Next you create a MemoryStream to save your StrokeCollection on the inkCanvas to. The Networkstream doesn’t support this. But you can send the byte array without problems.
MemoryStream memstream = new MemoryStream();
Get the Networkstream form the TcpClient.
NetworkStream netstream = client.GetStream();
In a real application you should put this in a separate thread, but for this example let us simply assume this is running in the Window with the inkCanvas. This saves the inkCanvas Stroke Collection to the memory as a byte array.
The byte array can be send to the networkstream and with that to the TcpServer on the other end of the connection.
netstream.Write(memstream.GetBuffer(), 0, memstream.GetBuffer().Length);
Some cleaning up and the client is complete.
Please note that you need to at least catch some exceptions before using this in any real application. #
The server looks pretty much the same.
Create the Server in the form of a TcpListener. The framework handles this for you.
TcpListener server = new TcpListener(42000);
Start the server. server.Start();
Tell the server to Accept a waiting connection from a TcpClient and save this client. This will freeze the Thread till a connection is established. So better put this in some other thread that is dedicated to communication.
TcpClient client = server.AcceptTcpClient();
Grab the Networkstream the client is writing to.
NetworkStream netstream = client.GetStream();
Fortunately the StrokeCollection Class has a constructor that take a stream as parameter. StrokeCollection p = new StrokeCollection(netstream) ;
The usual cleanup.
And don’t forget to pass the information somewhere. If you are sure, that nothing will happen with the object you can save the Clone(). But it is more secure to make a copy of the data for the gui. Otherwise you might get into troubles with the next update.
this.inkCanvas1.Strokes = p.Clone();
If you have big levels and/or a huge amount of 3d models you have to render you will find that the game gets slower and slower.
Before you try to optimize the draw codes, please use a profiler to make sure, that the problem is not somewhere else. Maybe the update logic is the problem?
You have some ways to tackle the problem:
- optimize everything in your draw code and shaders
- tell the user to buy a better computer
- remove objects
- lower the quality of models
Or you could scale the quality with the power of the computer displaying them.
To do so in a quick (and a bit dirty) way go to your class that holds the model and decides which model is to render (might be only one there without a LoD system or damage system).
- Add a second and third model.
- Make low poly versions of your model. One really low and one somewhere around the quality you can life with.
- Load these lower versions of the model in your class
- Add some code to check the distance to the camera
- Now simply make some if statements:
- If(distance >500.0f) do nothing
- If(distance > 300.0f) draw(lowest_quality_model)
- If(distance > 100.0f) draw(average_quality_model)
- else draw (high_quality_model)
- Play around till the numbers make sense in your game.
- You might also add a condition that a slow running game never renders the highest_quality_model
- As my LoD system not only draws the model but the shadows too i could use the lowest quality model to draw the shadow.
- I also added a condition that the game fist tries to stop drawing shadows and than lowers the quality of models even further till the point where only the lowest poly models really close to the camera a drawn and the rest is ignored.
- Be carefull with drastic measures. It is not always good to lower the overall quality of models just because the game missed one or two frames somewhere. The problem might not be the draw code at all.
Hope this helps you.
We will display some 2D stuff and move it around.
Possible reasons and fixes for the error message:
The type ‘Microsoft.Xna.Framework.Matrix’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.Xna.Framework, Version=22.214.171.124, Culture=neutral…
- You have references to different Versions of Microsoft.Xna.Framework in your projects. happens easily if you are like me and just import a library project and do not check the XNA version it was built against.
- Classic, you use some cool third party dll and they managed to build it against everything but your XNA version. … but tell you they did on the website. Thanks for that again.
- Content Pipeline Projects are alway, yes really, executed on the PC. Never set Visual Studio to build them for target platform Xbox360 or any CPU. You need x86. Sometimes this is changed automatically… aka you did something stupid and didn’t realise it.
- A C# class library doesn’t do so well as Xbox360 game library. Check your project types and settings.
- As a last resort. Throw all references out and set them new to one specific dll version and rebuild everything. Check where the error occurs and go from there on.
Hope this saves you some sleepless hours.
First thing you need to do is add a SpriteFont object to your code.
Done like this: SpriteFont myFont (or whatever name you want to give it).
Next you need an actual font you can load. For that you go to your solution explorer and right click your content project. Select add -> new item -> SpriteFont.
Maybe you should select a better name, that the default one. But it doesn’t really matter much.
You can edit how your font looks like in the .spritefont file. It is a XML like format. Most stuff you find in there should be pretty much self-explanatory. At the bottom you find tags specifying a character region. Please note, that not all fonts can display all characters. If you happen onto a bug resulting from some character display problem or exception, have a look here, if your font can display all your characters. The rest describes size, text decoration and stuff.
For this post, I leave everything like it is and go on to actually displaying something on the phone.
First we need to load our SpriteFont1 in the ContentLoad()-method of our game class.
All we have to do is display some text now.
We go to the Draw-method and beginn the spriteBatch draw with SpriteBatch.Begin(). The spriteBatch-Class offers us a DrawString-mehtod we use to display text. It first parameter is our SpriteFont. The secont the String you wish to display. This could of course come out of some Sting variable. The third is the position of the text on the screen and the last one, the color in the text is drawn in. We complete this with a spriteBatch.End() in the next line.
And see the result here: I made the Font bigger and red for this example. The size is controlled in the .spritefont file, the color can be selected in the DrawString-method.
I spend the last days hunting down cpu benchmarks and conducting my own tests for a customer. The aim was to build a computer with the maximum power for compiling C# source code with only minimal costs.
As XNA Games are written in C# (at least in the most cases) i thought this would be something i would like to share with you.
Basics about building a XNA Game Solution in Visual Studio 2008
First a few things you need to know when building a XNA Game Solution in Visual Studio 2008. I know Visual Studio 2010 is out there buu the XNA support is still missing. Only XNA 4.0 and the windows phone development tools run in Visual Studio 2010 Express. I did not yet upgrade my project from XNA 3.1 to 4.0 so it was clear that i needed to use Visual Studio 2008. Back to the point. Things you need to consider when building XNA Game Solutions in Visual Studio 2008.
- The build process runs only on one core. Usually you have dependencies between the projects in a solution and they have to wait on each other to finish building before the next one can be build.
- The build process runs through different stages and different stages challenge different parts of your system.
- The build process might run for a long time and you might wish to work on something else why building.
- You are probably not buying a dedicated build system.
1. Builds run only on one core:
This lets me believe that clock speed is more important than many cores. I let the build of my project run on a i7 920 and the CPU load stayed constantly at 11 to 14% for the devenv.exe process. A quick calculation reveals that the i7 with 4 real cores and 4 virtual cores thanks to hyperthreading has a total of 8 cores. So a 100% cpu load means that every core is running under full steam. But if the process can only use one core, the overall cpu load should be around one eighth. 100% divided on 8 cores gives you 12.5%. I think the short peaks of 13+ can be attributed to some Visual Studio background processes and measuring inaccuracy.
So clock speed is really all that matters? Yes of course … wait … sadly no, it isn’t. While clock speed is a big factor in this, it is not the only one. You have to consider the processor architecture. While a Pentium 4 with 3.6 GHz is not a bad choice for building a i7 will still be faster. But not by much.
I let my project build on:
Intel® Core™ i7–920 Processor (8M Cache, 2.66 GHz, 4.80 GT/s Intel® QPI) with 8 GB memory (DDR2) => 6 to 7 minutes (with a MySQL instance running but idle)
Intel® Pentium® 4 Processor supporting HT Technology 3.40 GHz, 512K Cache, 800 MHz FSB with 1.5 GB memory (DDR2) => 11 to 12 minutes
Intel® Core™ Duo Processor T2300 (2M Cache, 1.66 GHz, 667 MHz FSB) with 3 GB memory (DDR2 Notebook) => 10 to 12 minutes
Intel® Pentium® D Processor 820 (2M Cache, 2.80 GHz, 800 MHz FSB) with 2 GB memory (DDR2) => 11 to 13 minutes
The order is more or less what i expected. I think with a memory upgrade to 2 GB the Pentium 4 could beat the Pentium D and the Core Duo T2300 by a minute or two. And this is what really suprised me. The Pentium 4 is as good, sometimes even faster, than it much newer brothers. Actually only the Pentium 4 used all the available memory at some point. Please consider that the test is not completely scientific. These are some computers i have access to. They do not run clean installtions. On some of them other services are running. I tried to get them as idle as possible but it isn’t a completly objective test. They do not even run the same Windows versions. The T2300 and the Pentium D are running Windows XP Professional, the rest Windows 7 Professional or Enterprise.
So this test is useless? If you are looking for scientific data, than yes, it is useless for you. But i still think it shows something quite suprising. The difference between a $40 computer (i bought the Pentium 4 used from a business next door that gave up for that sum) and a $1500 computer (the i7, new) is not factor 1000 like the price suggests.
Just out of curiosity i visited some webshops selling used computers. You can get a used workstation with a 64-bit Intel® Xeon® Processor 3.60 GHz, 2M Cache, 800 MHz FSB with 2 to 3GB ECC memory and a 160GB hdd for just $190 plus shipping. Sometimes even with 2 cpus or 4 to 8GB ECC memory for just a little bit extra power. These things are build to run forever so you have your perfect build server. Just put some the code changes via a repository on it and let it build one solution after the other.
2. Builds challenge different parts of your system:
To simplify this i will break the building process down in three parts:
- Getting data from drive.
- Compiling source code.
- Writing output to drive.
The ressource manager of Windows 7 shows the following for each step:
Getting data from drive:
- CPU – mostly idle
- Memory – working some
- Harddisk – 100% load
Compiling source code:
- CPU – 100% load. At least the one core used.
- Memory – a nearly constant amound is used. In my case about 200MB. Not as much as i expected.
- Harddisk – idle.
Writing output to drive:
- CPU – mostly idle
- Memory – working some
- Harddisk – 100% load
So switching from a conversional harddisk drive to a ssd should give you a huge boost? Not really. I tried this with a SSD, a hybrid disk and a traditional hdd. Made next to no difference. The reason for this i quickly explained. Compared to the compile time the time spend reading and writing the data to the disk is so negligible that even a large performace boost in this area is not really noticeable.
So why do i read the advice to use a ssd for compiling so often? I don’t really know. I have a few theories about this. As we established above, the build process uses only one core, so when building in the background you should be bothered by it much… except for the few seconds large amounds of data are pushed from memory to disk. So what is the first thing you do? Look what ressources are taxed to the maximum by building you cool new awesome project. And suprise, you see the harddrive is the bottleneck. So of course you advice everybody to buy a ssd. Every reliable source told me the same. Visual Studio starts faster. The overall experience is faster, better, greater. But the build was running at the same speed as before. My own tests confirm this. A ssd ist great, but doesn’t help with build time. The hybrid disk is a cheap alternative and brings a slight boost but i in no way compareable to a ssd.
Please note that i am writing here about building a single XNA Game Solution in Visual Studio. There are of course cases where a fast harddisk is a huge speed boost.
3 + 4. You usually build in the background and do something else in the meantime if you have no dedicated build server / system.
What i want to tell you here ist, that it makes probably no sense to invest in a system you cannot use while building. For a dedicated build server it would be best to have one core (even hyperthreading is not really needed) with maximum speed, fast memory and a quick harddisk (or small ssd).
So our system should have at least decend power for stuff outside of coding games.
So what do i buy?
If you are one of those people who have their reserved parking lot at the local computer store, you can stop reading here. From here on i will focus on getting the best hardware for your money while staying under $500.
Let us start with the most important part, the CPU.
A quick glance at some benchmarks show us what a good cpu is for our problem. If you search for benchmarks on the internet be careful. Most benchmarks out there are for multithreaded applications. You need to look for ‘benchmark cpu single thread’. A good one is the ‘SPEC-Benchmark CPU2006‘ or the ‘Cinebench 11.5 (Single–threaded)‘.
At the time i am writing this the winner in the category under $300 is the Intel® Core™ i5-2500K Processor (6M Cache, 3.30 GHz) already on the new socket 1155. It costs today $255. If you have a few dollars more to spend on this get the Intel® Core™ i7-2600K Processor (8M Cache, 3.40 GHz) for $365.
Of course you need mainboard. This is where the flamewar usually starts. A good, reliable, expensive one with a long list of features and great driver support for the next decade ist the way to go here …. wait what? … No! Get a cheap one. You might need a new one in a few years because it broke down or doesn’t support something or whatever. Who cares? You are building a $500 computer not a $15000 server here. Let me tell you a secret. If you spend the whole $500 on a fancy mainboard, even that won’t do much without a cpu, power supply and memory. So we are simply getting a nice $85 entry level mainboard. If you can get SATA\600 support for a little bit more, do it.
That brings us to memory. Memory is important. Really important. Really, really important. Okay, so you got that? Good. I think 4GB is a good compromise. There is a recogniseable difference between cheap and expensive memory. But we are on a tight budget, so get some cheap one from a manufactor that gives you at least a one year warranty. Should cost you about $45 (2 x 2GB DDR3 – 1333). If you have the money you can go for 4 x 2GB or 2 x 4GB whatever your mainboard supports. Should be about $85.
That leaves us with $115 for the case, power supply, a DVD drive and the harddisk. Wait! And the graphics card if you have none on your board. The graphics card is a problem. You are developing games on the computer. Games require good graphics cards. The point is moot. You cannot get a good gaming graphics card in $500 computer. A acceptable solution would be a nVidia GeForce GT 240 or GT 430. But they cost you an addition $100. If you want to use the computer for the newest blockbuster games in full hd and maximum details you need to spend $300+ on a GeForce GTX 560.
A case and powersupply are available for $60. Don’t get the cheapest stuff out there. Get the dealer to show you some quite ones with a good efficiency factor.
A DVD drive is $25.
The last decision would now be what hdd to buy. We have $35 left. I have seen the ‘500GB Western Digital 7200rpm 16MB Cache Serial ATA/600’ for $48.
Plus keyboard and mice ($8.50 each). Get something good. Logitech, Microsoft, Cherry. They have good sturdy devices. Just forego the multimedia keys and stuff.
Now the moment of truth. To be honest i di not add all the stuff up beforehand.
It’s $535. So you just have to get a 7% discount and we are under the promised $500. Agreed, it’s without a graphics card.
If you are not already on your way to the computer parts dealer of your trust, subscribe to my blog and have a nice weekend.
You have a free weekend and a great game idea?
If you know some programming and want some fun developing a game, than paticipate here.
Prices to win!
Update: Theme is announced
In the immortal words of MC Hammer, “U Can’t Touch This”. Take it literally, figuratively, or sideways