XNA easy and efficient primitive rendering (PrimitiveBatch)

Recently, I got into the situation where I had to render a simple line in a 2D view, in XNA. As the fixed func. pipeline is no longer available, in order to do so you would have to deal with a shader. Probably a BasicEffect or another one created to render 2D lines.

I just googled up for "xna 2d rendering" and I´ve found this article in ZiggyWare (a must for any XNA developer). It shows how to do some basic collision detection between polygons, but as far as it also renders those polygons with lines, I was interested on it.

To draw all the lines there, he uses the PrimitiveBatch class, a very helpful class developed in one of the XNA samples available here, that I honestly didn´t take the time to look at. It behaves in a similar way as the SpriteBatch, basically:

batch.Begin( primitiveType );

batch.AddVertex( Vector2D);
...
batch.AddVertex( Vector2D);

batch.End();

And that´s it. Easy, ain´t it?

Cheers!

Tetris a.k.a "Cuadrados de Rusia"

Y es que así es como parece que conocen en China al Tetris.


Yo ya sabía que era un juego adictivo, porque me dejaba casi todas las monedas de 25 pelas que componían mi paga en la jodía maquinilla, pero es que esta noticia me ha dejado "pa'llá":


Una anciana china de 72 años, adicta a los vídeojuegos desde hace dos décadas

La colega (se llama Liu Zhi), que se inició en este aciago mundo con el Tetris, empieza a jugar a las 9 de la mañana y termina a las 10 de la noche, y si no la vigilan, se olvida hasta de comer.


No me quiero imaginar qué nivel de experiencia tendrá en el WOW ! Debe ser la portadora de la "espada de las mil verdades".


Juas !....

TechEd 2007. More C# 3 features. Object and collection initializers

Another great feature of C# version 3, shown in the TechEd today and yesterday is Object Initializers.

Working as expressions, we can create and initialize objects in a single statement. Imagine we have a class Provider, with properties Name and email. We can do the following:

Provider prv = new Provider() { Name="Microsoft", email = "microsoft@microsoft.com" }

That will create the object and initialize every property we include between the brackets.

In a very similar way work the Collection Initializers, where we can add new items to a collection in the same statement we create it:

List list = new List()
{
new Provider(),
new Provider(),
....
}

And those two features can be nested in the following way:

List list = new List()
{
new Provider() { Name="Microsoft", email = "microsoft@microsoft.com" },
new Provider() { Name="AMD", email = "amd@amd.com" },
....
}

Cheers!

TechEd 2007. .NET Language Integrated Query (LINQ)

The last session I attended on monday was:

The .NET Language Integrated Query (LINQ) Framework

by Luca Bolognese (Senior Program Manager, Microsoft).

For anyone thas has worked in data access components, software, or have struggled at anytime with databases, this represents a huge step forward in terms of productivity, robustness, understandability, and efficiency.

I´m no expert in such issues but, to my eyes, it´s just as if you could write SQL sentences right in the middle of your C# code, combined with the power of .net of course. In addition to that, LINQ can work against almost any kind of data source (SQL data bases, xml, arrays, collections, etc), and serve as well as a datasource for dataBinding.

An example:

[Assume "names" is a collection of strings, an array or whatever]

List<string> query = from s in names
where s.Length == 5
orderby s
select s.ToUpper();

The full LINQ sentence is evaluated as a single expression, but is executed in steps. First, every string in "names" is selected and returned to the second sentence. This one applies a filter selecting those with length == 5 and returns another collection, which is ordered in the third sentence. And so on... as complex as you may need.

I asked Luka how the debugger behaves in this sentences, and he said that LINQ sentences are watched in a special window which allows to see each step of the execution. This is something I´m definately waiting to try.

Just as a last note, LINQ doesn´t always behave this way, as this would be very inefficient. If the collection it´s parsing is just that, something that implements iEnumerable, it behaves so, parsing each element in the collection, iterating through the full array.

BUT, the interesting thing is: if the collection being parsed also implements iQueryable, it behaves differently, building a full query from the LINQ sentences and executing it, as once, at the end. This obviously would make debuggin more difficult, but is the efficient way to execute certain types of queries.

Cheers!

TechEd 2007. VStudio 2008, C# 3, Automatic Properties

Just following the previous post, I keep talking about the session by Daniel Moth (http://www.danielmoth.com/Blog ), yesterday in the TechEd.

He showed another C# 3 new feature that is extremely useful: Automatic Properties.

You not tired about writing something like this?

private int mNumCustomers;
public int NumCustomers
{
get{return mNumCustomers;}
set{mNumCustomers = value;}
}

This is a pain in the ass... You are supposed to use properties, to be clear, clean, robust, elegant, and a good christian, but writing them is painful. Yes, refactoring helped quite a bit, but it´s still something... don´t know... slow and uncomfortable.

Well, Microsoft says that is there to make us more productive, as there it is:

Automatic Properties a.k.a JustWriteThis:

public int NumCustomers{get; set;}

The language will take care of creating the private equivalent variable, just for us. We don´t need to write it. Just specify if you want the get and set, the get only, etc, and there it is!

Again, more productivity...

TechEd 2007. VisualStudio 2008, C# 3, MultiTargeting and Anonymous Types

Yesterday, from 16 to 17:15, Daniel Moth (http://www.danielmoth.com/Blog ) introduced the new features of VStudio 2008 and .Net Framework 3.5. It was a very impressive session, fun and interesting.

Specially exciting the new multi-targeting feature of VStudio 2008. It´s a way to tell the environment which .Net Framework version your project will target, through the project´s properties window. Just selecting in a comboBox if it targets version 2.0, 3.0, or 3.5. By selecting one of them, all of the version features are enabled or disabled for your project, and it´s possible to switch between them without loosing work. Your project will just adapt to that perfectly.

Just as a tip, VStudio 2008 finally has the option "Open folder in windows explorer" in the context menu of the solution explorer... Yeeeeeaaahhhh ! Cannot imagine why something that simple didn´t appear before. No more navigating to bin\debug folder....

Also found quite interesting some of the new C# 3 features, like anonymous types. Didn´t you find redundant something like this before?

Form f = new Form();

Why do I have to say that "f" is a windows form twice? No more, just write something like this:

var f = new Form();

And the compiler will infer that "f" is a windows form just reading the code on the right part of the sentence. It´s important to mark that this has no performance impact, as it´s just a compiler feature. Each time you compile, the MSIL code generated will definately contain the declaration in the old way. It´s just a helper right there for you, just to be more productive. And of course it works with anything:

var a = 5;
var a = 5f;
var a = new Class1();
var a = "";

Great!

TechEd 2007. KeyNote by S. Somasegar

Yesterday, at 14:00 we attended the keynote of the TechEd2007, by Microsoft´s developer division corporate vice-president, Mr. S. Somasegar.



He presented the new key features of Microsoft developments plattform: VisualStudio 2008, .Net Framework 3.5, SilverLight, the Expression Studio tools, etc.


To summarize, he presented an important part of what we are going to see in deeper detail through this next days.

More later...

TechEd Barcelona. 2007


Tomorrow I´m traveling to Barcelona to attend the Microsoft TechEd Developers 2007. I´d be glad to meet you there if you also attend.
I´ll try to post session comments here too, at the end of each day.
Cheers!
-----
Mañana viajo a Barcelona para asistir al TechEd Developers 2007 que Microsoft organiza esta semana.
Estaré encantado de veros por allí si estáis, y también trataré de postear comentarios, impresiones o resúmenes de las sesiones a las que vaya asistiendo.
Saludos!


Great master of chess trying Simax

Several months ago, we had the pleasure to receive at our facilities to the great master of chess Miguel Illescas.

He was trying one of the first prototypes of the simax simulator. Although it was in a very primitive stage, he liked it and enjoyed a ride with a Volkswagen Polo.

I include this post now, because I´ve received this link with some info about it (Basque Language)

http://www.xake.net/6Gogoratzen/2007_EUS/2007-05-29%20Miguel%20Illescas,%20xakelaria%20eta%20informatikaria%20-Berria-n.htm

MSFT Madrid

Today, I had the chance to visit Microsoft facilities in Madrid. It´s a beautiful place as you can see:


Good to work here, specially after you see several XBox360 at the entrance ;)

Thank you guys!

Photoshop CS3 Layers. Select all pixels & rasterize effects

I´ve been messing around with Photoshop CS3 and I´ve found a couple of things I´d like to share with you.

In versions of PS prior to the CS2, only one layer in the layer palette could be selected at a time. Now, there´s the possibility to select more than one layer and work with them alltogether, in the typical ctrl+click way to select and de-select layers.

This is great, but that combination (ctrl+click) was previously used to select all layer´s visible pixels. So now, how is this thing done? Well, after 15 minutes trying, searching, and desperating, I´ve realized it´s the very same thing, but clicking in the layer´s thumbnail instead of any part of layers entrance.

Second trick I´ve found here:

http://graphicssoft.about.com/cs/photoshop/qt/flatlayereffect.htm

HOWTO flatten, or rasterize all layer´s effects:

If you have applied some effects to a layer, all of them keep editable at all the time. This is also great but if you need at some time to "apply" all that effects, rasterize them in the layer and reset all the effects properties, so you can work again with the modified layer "from scratch", there is no obvious option to do so.

A very easy way to do this, is to create an empty layer and place it just below the layer you want to flatten. Then merge that layer down (ctrl+E) to the empty layer you just created and voilá! all the effects get applied and disappeared.

Cheers!