blog.vi-kan.net One thought after another

1st particle-challenge: Getting something unto the screen

No firework, but its somethingA couple of days ago, I took a challenge to show that making a particle engine is not that hard. I also stated that the first part of the challenge would be to get something unto the screen, and that is as far as I have come. I have enough code to emit particles unto the screen, beautifully rendered as small triangles in various colors.

Well – its no firework, but it’s a start…

For those who are interested in the code, it’s available for  download here. The lib-folder contains two libraries that I use. First of all, there is the Free Pascal OpenGL Headers used for rendering. Second, there is a folder called ‘SDL’, which is a older version of the Delphi Container and Algorithm Library (DeCAL).  I started using it for many, many years ago, back when it was a commercial product. It was called Standard Delphi Library, but was renamed to avoid confusion with ‘Simple DirectMedia Library’. I guess it’s time to find something else, and when I start using Delphi 2009, I will for sure.

So, how did I put something unto the screen?

I started off with a simple TParticle class with information about position, velocity, color, time to live and age.

 TParticle = class(TObject)
  public
    constructor Create;

    procedure Update(DeltaFrame: int64);

    property Position: TVector3D read FPosition write FPosition;
    property Velocity: TVector3D read FVelocity write FVelocity;

    property Color: TColorFA read FColor write FColor;

    property TimeToLive: int64 read FTimeToLive write FTimeToLive;
    property Age: int64 read GetAge;
    property Dead: boolean read FDead write FDead;
  end;

Then followed a TParticleManager to hold all the particles.

 TParticleManager = class(TObject)
  public
    constructor Create;
    destructor Destroy; override;

    procedure AddEmitter(Emitter: TParticleEmitter);

    procedure ProcessFrame;

    property Particles: DArray read FPArticles;
  end;

For each ‘game loop’, the particle manager will loop through all particles, destroying those that have lived their age, and telling the others to update them self. When calling Update( ) on the particle, it pass in the tickcount since update. This is to ensure a relatively smooth and natural movement even if the fps is not stable.

 procedure TParticleManager.ProcessFrame;
  var
    CurrentFrame: int64;
    FrameDelta: int64;
  begin
    CurrentFrame := GetTickCount;
    FrameDelta := CurrentFrame - FLastFrame;

    RemoveDeadParticles;
    EmittNewParticles(FrameDelta);
    ProcessParticles(FrameDelta);

    FLastFrame := CurrentFrame;
  end;

It will also contain a list of TParticleEmitters. The particle emitter will get it’s chance to emit new particles once every loop. It is the emitters responsibility to give the new particles a initial position and velocity.

 TParticleEmitter = class(TObject)
  public
    constructor Create;
    procedure EmittParticles(TimeSinceLastFrame: int64; Particles: DArray);

    property ParticlesPrTick: extended read FParticlesPrTick write FParticlesPrTick;
  end;

The rendering is separated into it’s own class, TRendrer. This class will to all the OpenGL initialization-stuff. It is given a list of objects to render, where each item must implement the IRenderItem interface. In this way, the rendrer do not need to know anything about what it renders. It just setup a place for the rendering to happend, and asks the item to draw it self.

 TRendrer = class(TObject)
  public
    constructor Create(AHandle: THandle);
    destructor Destroy; override;

    procedure Render(ItemsToRender: DArray);

    procedure HandleResize(NewSize: TRect);
  end;

To make the particle able to render it self, I have made a sub class, TVisualParticle, which implements IRenderItem. For now, it just draws a small triangle in the particles color.

procedure TVisualParticle.Render;
begin
  glPushMatrix();
  glTranslatef(Position.x, Position.y, Position.z);

  glBegin(GL_TRIANGLES);
    glColor4f(Color.Red, Color.Green, Color.Blue, Color.Alpha);
    glVertex3f(-0.1,-0.1, 0);
    glVertex3f( 0.1,-0.1, 0);
    glVertex3f( 0.0, 0.1, 0);
  glEnd;

  glPopMatrix;
end;

From here, there are a couple of things that needs improvements. I need to explore OpenGL a bit more to get control over ‘the world’ that the particles lives in. Maybe a camera-model or something is needed. I don’t know. I also need to look into ways of rendering a little more interesting particles then triangles… And then there are physics – particles should be able to react to gravity and other forces as well. I’m not sure where I will start though. Time will show…

The “Particle-engines-ain’t-that-hard’-challenge

particle firework The other day, a colleague and I where looking at a cool application, Machine Flow. It’s a visual development environment and interpreter for the programming language Machine Flow. The language it self is defined by small lua-scripts, which you visually connect together. The running of the program is illustrated by small marbles carrying data around. It’s quite fun to play with, and the source is open, so there should be one or two things to look at there to.

What caught my colleagues interest, was a small firework-effect when the development environment was cleared. So we started discussing it, and soon I was determined to show that it’s really not that hard to make such effects. Now, I have never before done anything like this before. I lack experience with both graphics programming and the necessary math/physics. But it can’t be that hard, can it?

Well, we’ll just have to wait and see, then. The first challenge will be to get something unto the screen, and than I can start look at how to make it look nice. I will try to steer away from available libraries and tutorials, and rather come up with something completely on my own. The visual part will require  some reading though…

Norwegian Developers Conference 2009

So, for three days now, I have been attending #ndc09. One thing that I really like about it, is that it’s not owned by any single platform vendor or any thing like that. This is not Microsoft trying to sell their tools or technology. It’s just some cool gays (and a couple of ladies) putting together some of the worlds best speakers and inviting all of Norway to come listening. It’s not like you want see any Microsoft banners at all, and most of the technology spoken of is MS specific, though. It’s hard to change the fact that most developers tend to use MS technology already, so that’s what they would want to hear about.

Now, you can read more about #ndc09 at http://ndc2009.no/, and you can also find information about last years event there. We have been promised that all sessions will be published on that site as soon as possible. I’m looking forward to that. There where quite a few sessions that I would love to see, but obviously you have to make some choices. So when the sessions get available online, I can catch up with all the sessions that I missed. I will also take a second look at some of the sessions that I did attend. It’s easy to forget things after eight hours in uncomfortable chairs…

1st day

Keynote: “Are You a Professional?” with Robert C. Martin

The keynote will always be the grand opening of the ball. It will set the context for the rest of the conference. The agenda. R. Martin, or Uncle Bob as he calls him self, started out with the question ‘Are you a professional?’. He followed up with a set of criteria’s, or principles, that he thinks you have to fulfill to give an positive answer.

I really liked this session, and found it very inspiring. It was a given as an challenge to start act as an professional. To take both your paid work and your profession seriously, even after paid hours. He finished it off with the sentence “I’m a doctor; I don’t have time for washing hands.” as a clear parallel to developers not taking the time to do tests.

“Deep Tour of .NET 4” with Scott Hanselman

I have to admit, that I partly choose this session because it was held by Hanselman. Of all the speakers, he was the one that I knew most about. I’m a regular reader of his blog, and an occasional listener to his podcasts. I like his style, and couldn’t wait to experience him live.

“Making Your Blog Suck Less: Social Networking and Your Personal Brand Online” with Scott Hanselman

The next session that I attended was also a Hanselman-show. This time he shared some of his own experience with blogging and becoming ‘a brand’. Actually, the talk was mostly a repetition of a post he did to his blog back in 2007, but hearing it live with the opportunity to ask question is still valuable. One of the things he said, was that you probably would just be writing for your self a couple of years. No one will be reading your blog, until you suddenly blog a topic that some way manage to show up on google. Hopefully this one post will pull some more readers to your other posts – if they’re interesting enough.

“Declarative UI Programming with XAML: Windows Presentation Foundation, Silverlight & Surface” with Tim Huckaby

For the third lesson, I choose to move over to the WPF-track to see what was going on there. I find WPF quite fascinating, and wanted to pick up some tips. There is not much to say about this lesson. It was obviously that I had chosen wrongly when leaving track 2, and missing out on Phil Haack and the MVC-ninja-thing. Actually there was two other sessions at the same time that I would love to see. Both Michael Feathers and Robert C. Martin was talking at the time, and according to twitter-messages there was a lot more going on there than where I was. As soon as it finished, I went back where I came from.

“ASP.NET MVC AJAX = meant for each other” with Phil Haack

I have been curious on the ASP.NET MVC framework, but I have never taken the time to look at it. Phil Haack gave a interesting session that, even though it focused on using AJAX with MVC, gave some shallow insight into what MVC looks like. I’m not a web developer, so it’s not that relevant for me. It made me curious, though. It seems to a more tidy way of doing asp.net then what I have seen before.

“The Haacked and Hanselman Show” with Phil Haack & Scott Hanselman

Now, this session scared me a little bit. Scott made some simple web sites, and Phil showed how to exploit some of the weaknesses that he found; Calling javascript and posting values from other sites, making use of  javascript injection. Scott showed how he could secure his site from Phils attacks, but how do you know before you have experienced such an attack? I think I would felt uncomfortable doing web development, but I guess you get confident with whatever you do after some time.

I ended up buying the ASP.NET MVC-book, though, and I’m looking forward to read it.

“How to scare C# developers with Ruby” with Scott Bellware

This session was announced the same day as the conference started. I have been reading a little about Ruby, and find it very interesting. Scott Bellware jumped right to the scary parts. Creating new classes at runtime, filling it with members, instantiate it, adding more members to the new instance – yes, I guess this could scare both C# and other developers. It’s not easy to follow, but seen some of the things that you can do with it, makes it well worth I guess.

2nd day

“Understanding Test Driven Development” with Roy Osherove

This was the first of many TDD-related sessions for this day, and he started by defining TDD. For developers that want to start with TDD, but lacked experience with unit testing, he suggested to start out with writing tests for code that is already written, and then switching to tests first-development when you had gotten used to writing tests. Finally you will come to a stage where you write tests first, and let the tests drive the design, and you have reached Test Driven Design.

“Unit Testing Best Practices” with Roy Osherove

Now that we hade a fundamental knowledge of what TDD is, he went further and showed us some dos and don’ts. Only one assert in each test method. Only break one test at a time. Give good names to the test methods; He suggest a MethodName_StateUnderTest_ExpectedBehavior – naming convention.

“Beautiful Teams” with Roy Osherove

After a couple of TDD-sessions, Roy took a step aside to talk about ‘Beautiful Teams’. I chose to follow him, and got a interesting talk about what factors Roy thinks makes a good team. I was surprised how much he valued efficient use of tools. I was also surprised that he wanted the team to work together in the same room. I have always valued my four walls, and find it very distractive to work with others in the same room. Instead of helping each other out with problems, there will soon be talking about last nights game and things like that.

Everything could be summarized down to automation. You have to be on a continuous lookout for things stopping the productivity, and then look for ways of optimize that bottleneck away. Learn keyboard shortcuts. Stop using the mouse. Get a automated build-system. No long and wasteful design meetings.

“Test Driven Development: Using Mock Objects” with Roy Osherove

And then we where back to TDD, and this time Roy told us about mock objects and some mocking frameworks. He stressed the difference between a mock object and a stub. Both mocks and stubs are fake. What makes it a mock, is when you assert on it. If you test against a fake object, it’s a mock. If you just have a fake object to get rid of dependencies, it’s a stub.

“Mocking on the Edge – isolation at the System Level” with Roy Osherove

After looking at mocking in general, Roy presented how Typemock Isolator can help getting rid of dependencies. Isolator is using the .NET framework profiler API, and this makes it possible to stub out any method call at runtime. Every time a method get called, Isolator is notified and decides if the real method should get called, or if a mocked value should be returned. It makes it possible to unit test things like WPF and Silverlight. Quite impressive, actually.

3th day

“Good Test, Better Code Workshop” with Scott Bellware

This was a full-day workshop with Scott Bellware. Usually, he does this workshop with about 20 people, but this day he put a limit at 80 people. I guess there was about 100 people present when we started out by configuring our machines with the necessary tools: Visual Studio 2008, Subversion TortoiseSVN, ReSharper, TestDriven.NET and AutoHotKey.

He had created a userstory about discount rules in a salessystem, and showed how he directly converted the story into unittests, or ‘machine specifications’, and then wrote code to satisfy the specs. We used a framework called Machine.Specifications, or mSpec, witch has its origin in Rubys rSpecs and Bellwares own SpecUnit. The goal of this framework, is to make the code as readable as possible.

With the help from named delegates and anonymous functions, a specification like this:

[Description]
public class Transferring_between_from_account_and_to_account
{
  static Account fromAccount;
  static Account toAccount;

  Context before_each =()=>
  {
    fromAccount = new Account {Balance = 1m};
    toAccount = new Account {Balance = 1m};
  };

  When the_transfer_is_made =()=>
  {
    fromAccount.Transfer(1m, toAccount);
  };

  It should_debit_the_from_account_by_the_amount_transferred =()=>
  {
    fromAccount.Balance.ShouldEqual(0m);
  };

  It should_credit_the_to_account_by_the_amount_transferred =()=>
  {
    toAccount.Balance.ShouldEqual(2m);
  };
}

turns into this report when run:

------ Test started: Assembly: Machine.Specifications.Example.dll ------

Transferring between from account and to account
  When the transfer is made
    * It should debit the from account by the amount transferred
    * It should credit the to account by the amount transferred

It clearly shows that code can be readable, doesn’t it?

So that sums up my experiences from ndc09. It really was a exiting and inspiring experience, and I’m looking forward to next years happening.

Ownerdrawn menus in Delphi

Owner drawn popup menu Just when I was thinking that what this blog really needed was some content, an old article that I wrote for delphi.about.com was pulled out unto the frontpage again. It was written back when Office 2007 was still in beta, but the principles of owner drawn components should still be the same.

You can take a look at it on the delphi.about.com-page: How to Mimic Office 2007 Rich Menus using Owner Drawn TMenuItems

The famous first-post

What a journy this will be!And then I started blogging. Well, at least I intend to. I guess the future will tell if the first post should have been rewritten to something like ‘And then I blogged. Once.’

So why start blogging? I really don’t know for sure. I’ve been told, that every developer should have a blog, but I’m kind of glad that not everybody have. Well, think about it. Think about all the crazy hits you would get on google for instance.

So if I’m not sure why I should blog, do I know what I will blog about? At least I think I know. It has to be about what I do, and what I do most, is programming. I have been programming delphi for a living for about ten years, now, and I think I’m starting to get the hang of it. It’s not impossible that I would like to share some of what I have learned these years, and it’s quite likeable that I will share something about what I’m trying to learn, or what I would really like to learn in the future.

For instance, right now, I have a personal goal to become a certified .net developer before the summer. I’m not sure that this will work out for me yet, cause the spring is coming along, and there are a lot more to learn. But we will see. Hopefully I will come through knowing a lot more about .net then I used to either way.

And then there is reading. I love to read. I just need something to put my eyes on whenever it’s time for it. I’m a slow reader, though, so a book can last for weeks. I read mostly tech books, but from time to time I sneak in some fiction. What ever it is, maybe I will share my thoughts about it here when I’m finished?

And that leads to a significant reason for having a blog. Have you ever encountered a competition or price offering demanding a reference to your blog? ‘All you got to do, is mention us on your blog, and give us the link, and we will offer you great prices’. Expect to see a lot of mentioning in the feature…

So beside programming and books, there may be some other things that I want to share. It may be something oriented towards technology, or it may be something about one of my other hobbies, e.g. photography. Or my family. We will see.

{ TODO –oVegar : find a quote or something to close this post }