Saturday, June 19, 2010

Let's Roll Some Dice

I reimplemented my very simple dice rolling module. It is now a pair of functions instead of a struct, and it supports strings as input.

Couple examples:
auto roll = rollDice("1d4"); // results in a value between 1 and 4
auto roll2 = rollDice("2d6+2", 4); // results in a value between 8 and 18
auto roll3 = rollDice(1, 8, -2); // results in a value between -1 and 6
The parser doesn't support strings like "1d4 + 2d6". I have no plans to add that kind of support any way as it can be combined from smaller pieces.

Source code: here

Sunday, April 4, 2010

Undeath

This blog's been dead for about two years now, and I don't see much of chance that it will change in the near future.

I've started sharing some stuff in Google Reader from all the blogs I'm following, here:
http:

There's mostly AS3/Flash stuff, as that is the focus of my interests at the moment. If I have time I might write something about the PushButton Engine.

Monday, December 15, 2008

Composite Oriented Programming Part 2

I made a slightly more complicated version of the hello world example. This time I made two composite implementations of a Speaker interface.

public interface Speaker
{
public void setSpeech(char[] speech);

public char[] speak();
}

Then I changed how the data is handled as I didn't want two different templates, HelloData and ByeData. So I made a PropertiesMixin template which contains a simple way to set and get properties as char[]s.

public template PropertiesMixin()
{
private char[][char[]] properties;

public void set(char[] name, char[] value)
{
properties[name] = value;
}

public char[] get(char[] name, char[] defaultValue = null)
{
if (name in properties)
{
return properties[name];
}
return defaultValue;
}
}

This can be used to add properties support to a class. There is room for improvement, for example some how use the tango.text.Properties class to save and load the properties.

Then the real meat are the new HelloMixin and ByeMixin, which handles the heavy lifting.

public template HelloMixin()
{
private static const char[] HELLO = "hello";

mixin PropertiesMixin;

public void setSpeech(char[] speech)
{
set(HELLO, speech);
}

public char[] speak()
{
return get(HELLO);
}
}

public template ByeMixin()
{
private static const char[] BYE = "bye";

mixin PropertiesMixin;

public void setSpeech(char[] speech)
{
set(BYE, speech);
}

public char[] speak()
{
return get(BYE);
}
}

I've mixed in the PropertiesMixin for both of them, and then implemented the Speaker interface functions using properties. We could even have a default speech very easily, if needed.

The composite classes are very simple as they do not contain any functionality whatsoever.

public class HelloSpeaker : Speaker
{
mixin HelloMixin;
}

public class ByeSpeaker : Speaker
{
mixin ByeMixin;
}

I've put the setting of the speeches in the main function of the program, where the composites are created, to better demonstrate the usage of the Speaker interface.

public void main()
{
Speaker hello = new HelloSpeaker();
hello.setSpeech("Hello, World!");
Stdout(hello.speak()).newline;

Speaker bye = new ByeSpeaker();
bye.setSpeech("Bye, World!");
Stdout(bye.speak()).newline;
}

I'm pretty impressed about how easy it is to implement complex objects using mixins. Next I need to look into a more complicated version. Something like Fredrik Kalseth has done here.

Until then you can find the new code here.

Composite Oriented Programming

Ok, so I found a new buzzword; Composite Oriented Programming. I first heard it from Thomas Biskup. He wrote about it on his JADE-blog, here. From there I started reading about COP on qi4j.org. I haven't completely understood it, but so far it seems to be quite nice buzzword.

So I wrote a small hello world sample in D using templates. Got it working after a while with the help of downs and Bill Baxter, on #D and D.learn respectively.

You can find the code here.

I'll go over it here now.

First we have a template to hold out data, namely helloStr:

public template HelloData
{
private char[] helloStr;
}

Next we define the action or function for the whole composite. I don't remember what this is in COP-speak.

public interface HelloSpeech
{
public char[] sayHello();
}

Then we implement the above function in HelloSpeaker.

public template HelloSpeaker
{
mixin HelloData;

public char[] sayHello()
{
return helloStr;
}
}

So we mixin the data template, so we can return the helloStr from sayHello. Notice that we don't need to import the actual interface for sayHello.

Then we will create the actual composite.

public class HelloWorld : HelloSpeech
{
mixin HelloSpeaker;

public this()
{
helloStr = "Hello, World!";
}
}

Now we have a concrete class that implements HelloSpeech. If you look at the code here, you notice that we've had to import the HelloData module. This is as far as I can see the only downside so far.

We have now nicely removed the implementation for sayHello to their own modules. This is a very small example, but I can see it to be quite useful with bigger systems and with more interfaces.

Now just add an interface for setting the value for helloStr and creating an implementation for it, then this would be complete. The helloStr could be hidden completely behind a getter/setter-pair.

Monday, September 8, 2008

Small Update

It seems that I won't have much free time in the future either, Ive decided to completely stop even thinking on doing any of the old NeHe tutorials anymore. Besides anyone can get a pretty good idea of how to do them from those that I've already done and do the ones they want. It's good practice that way too. =)

I'll probably do any new tutorials for the new basecode, if ever there will be any.

My next D project or journey will be to familiarize myself with Hybrid and Dog. I've got a few ideas on what to do with them. But time will tell. I'll write anything worthwhile here.

Monday, June 30, 2008

NeHe Update

There's been some activity on the NeHe site recently. They have launched a wiki, and are planning on moving the old stuff from the website to the wiki. There is also new versions of the new lessons on the wiki. For some reason they rewrote the basecode and the first three lessons. The beta version wasn't that bad. On the plus side the code is much simpler, so they can extend the it later.

I've ported the new lessons to D. Why else write this post. =) I didn't do it 1:1, as in my opinion there are too much c++(bad design) in them. I kept them mostly as they were, but fixed most of the more glaring problems in it, at least in my opinion. I'm still thinking on making them more D'ish/generally better. I'll decide probably after more lessons are available, and we'll see where they are taking the basecode.

You can find the relevant information about the source code on the google code project page.

Friday, April 4, 2008

GLFW Derelictified

I started looking again at the GLFW library yesterday. I do really like the API. Somehow it's nicer than SDL's. The only thing missing is a better support for image loading.

I remembered using a D binding for GLFW once and tried to find it. In the end I found it in the Schooner project in dsource, but it hasn't been updated in a long time. Plus it uses Phobos. I thought about trying to update and convert it to Tango for about a second or two, and then decided to do Derelict bindings for it instead. They are easier to maintain, but the downside is that you need to lug around the shared library for it.

I used the latest release of GLFW (2.6), and only tested things with a simple test application on Windows XP.

So if you like the framework go grab the D bindings from here.

Windows users please read the readme file.