Jump to content

  • Log In with Google      Sign In   
  • Create Account

D Bits



Putting D's Array Slices to Use

Posted by , in BorderWatch, D 08 March 2012 - - - - - - · 1,856 views

I've been working on BorderWatch a little bit every day. My focus has been on getting the ASCII engine, Arthur, into a state that will let me get a game up and running. With the few modules that I've implemented so far, I believe I'm there. One of the D features that has come into play in this process has been array slices. Read Steven Schvieghoffer's excellent article on slices, now hosted at dlang,org, for a good introduction if you don't know what they are.

Take a look at the BorderWatch main method (or view the whole module for context):

void main()
{
	initArthur(AppName, OrgName);
	scope(exit) termArthur();

	auto console = createConsole(ConsoleData(AppName));
	menuScreen(console);
}


The Console is the means of displaying the ASCII graphics to the user. Initially, there was only one kind, a "heavyweight" console that represents a window on the OS. But after realizing how annoyingly awkward it was to position things appropriately with my naive implementation of clipping, I came to the conculsion it would be much nicer to have another type, "virtual consoles", that maintain their own coordinate space.

At the heart of both types of Console is an array of Symbols (an ASCII character and RGBA values). A virtual console's buffer is a subrect of its parent's buffer. Every time you print to a console, it is marked as dirty. When you call the render method on a console, it first looks to see if any of its children are dirty. If they are, it copies the children's symbols into the proper region of its own buffer.

In C, this sort of operation would most likely be accomplished with a loop and a memcpy, copying entire rows at a time. My D implementation is done similarly, but instead of memcpy, I use array slices. Here's the (uncommented) code from console.d that does the work:


void render()
{
    foreach(c; _children)
    {
	    if(c._dirty)
	    {
		    uint dstStart = c.x + (c.y * columns);
		    uint dstEnd = dstStart + c.columns;
		    uint srcStart, srcEnd;
		    for(uint i=0; i<c.rows; ++i)
		    {
			    srcStart = i*c.columns;
			    srcEnd = srcStart + c.columns;
			   
			    // Here's the slicing...
			    // The symbols from one row of the child's buffer are
			    // copied to one row of the destination buffer.
			    _symBuffer[dstStart .. dstEnd] = c._symBuffer[srcStart .. srcEnd];

			    dstStart += columns;
			    dstEnd = dstStart + c.columns;
		    }
		    _dirty = true;
		    c._dirty = false;
	    }
    }
}


If you haven't yet read the article I linked above, a quick explanation. _symBuffer[dstStart .. dstEnd] takes a 'slice' of the _symBuffer array, starting from the index indicated by dstStart (inclusive) and ending at the index indicated by dstEnd (exclusive). That slice is then assigned all of the values contained in the slice of the child's buffer that is taken from srcStart to srcEnd. There's no need for pointer arithmetic, no chance of overwriting memory, no need to worry about allocations or deallocations... it's all safe and convenient.

Another use I had for array slices, in the same file, is in the following method:

void fill(ubyte c, ubyte r, ubyte g, ubyte b, ubyte a = 255)
{
    auto symbol = Symbol(c, r, g, b, a);

    // Here's the slice...
    _symBuffer[] = symbol;
}


Here, I'm taking a single symbol and using the slice syntax to assign it to the entire array.

These are seemingly farily trivial things, but I can tell you that it makes a big difference. I've been using C for many years and, though I've been frustrated from time to time, I've never actually hated it. But the more I use D, the more I miss the little things like this when I go back to my C codebase. It almost makes me not want to go back at all.


BorderWatch

Posted by , in BorderWatch, D 05 March 2012 - - - - - - · 1,617 views

I'm afraid I'm going to be waving goodbye to Dolce. It's been sitting, bit rotting, for a while now. Some time ago I encountered two major issues with my Allegro binding in Derelict 2. One, a random (and I mean random) crashing bug, I've been unable to solve. Another, regarding Allegro's interaction with Cocoa on Mac, I don't have the means to test (no Mac), though I do have an idea on how to solve it. However, I'm washing my hands of it all for now. I may come back to the Allegro binding after the Allegro 5.1 branch is complete, but Dolce I think is quite dead.

But I still haven't lost my urge to make a game. In fact, I did make one as a way to get back into the groove. I didn't mention it here because I actually coded it in C. Nothing to brag about, just a simple reimplementation of the A5teroids example that ships with Allegro 5. I wanted to get familiar with the API and get my chops back without the distractions my binding was causing. And that brings me to the topic of this post.

I really, really want to make this game that has been in my head forever. So, now I am. But without Allegro. Instead, I'm going with an ASCII-based approach using SDL2. This is an evolution of the text-based idea I was contemplating a while back. The big difference is that this time, I've got some code to show for it. Also, though it is still in the early stages, it's getting to the point where I'm ready to have a git repo to manage my changes. Rather than just working on a local repo, I decided to bite the bullet and just put everything on github as is. And so, I give you BorderWatch.

One of the reasons I wanted to put it up on github is to give myself a reference to talk about features of D on this blog. Dolce was supposed to fill that role, but I never really felt it was ready for the world. BorderWatch is different, though, and I hope it can serve as a starting point for anyone curious about using D for game development. I can seriously say that after working on even a simple little game like an asteroids clone in C (a language I've used and loved/hated for a very long time), putting one together in D is a much different, and better, world entirely.

Please read the project README before you bother with the code. I'm using my recently implemented SDL2 and SDL2_image bindings from Derelict 3, and no other external libraries. If you want to build the project, you'll have to get the SDL2 and SDL2_image source and compile it all yourself. Also, only DMD and Windows are currently supported.

I do hope to work on this over the coming months as often as I can. I have a lot of ideas for it and it is proving to be a lot of fun so far. It's licensed under the zlib license, so do what you want with it. There's not a whole lot there yet, but what is there allows you to open a 'console' window, print ASCII characters and text strings to the whole window area or specific regions, and there's an effect implemented that can display text strings as a slideshow. Nothing is optimized, nothing is stable. But it's a start.





September 2016 »

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526 27 282930 

Recent Entries



PARTNERS