[tr][td]
[/td][/tr]
^ preview image ^
The last couple of days and weeks I’ve been working for some kind of project just for having fun with coding and creating something which is somehow intresting.
It’s a resource loader, which can load image sprite sheets and provide cut-out Images after those are loaded. Additionally it allows specifying filters, operating in multiple layers when using filters, etc.
So an xml example would be this one:
<resources>
<images>
<image file="sheet.png">
<!-- Sorcerer -->
<animation name="sorcerer" delay="0.1">
<filters>
<filter name="replace(255, 255, 255, 255 with 1, 1, 1, 0 range 2)" />
<filter name="colorize_grayscale(0.4, 0.9, 0.3, 1)" />
<filter name="expand(4, 4)" />
<filter name="scale(4, 4)" />
<filter name="voxelize(4, 4 with 0.02, 0.02, 0.02, 1)" />
<filter name="scale(2, 2)" />
<filter name="outline(0, 0, 0, 1)" />
<branch merge="blend(bottom over top)" >
<filter name="colorize(0, 0, 0, 1)" />
<filter name="blur(8, 8, 16)" />
</branch>
</filters>
<frame bounds="0 0 16 16" />
<frame bounds="16 0 16 16" />
<frame bounds="32 0 16 16" />
<frame bounds="48 0 16 16" />
</animation>
</image>
</images>
</resources>
With an input from this file (scaled 8x):
[tr][td]
[/td][/tr]
the outcome of this xml is exactly the image you see above
So there are two intresting parts in this XML:
- The , and
- The and s
The and s:
What those do is pretty simple to explain:
In the tag come the s. Those frames specify the rectangles for each frame inside the “bounds” tag.
the “name” attribute from the animation tag is a name for the animation, which makes it possible to be referenced from the program.
I won’t explain those in great detail. They were the main part of the project at first, but now something else is much more intresting
The :
I will try to explain those with an example.
We have the source image from above using the same frames and animation as in the xml.
Now what we want is, we want to colorize the image. But only the parts which are grayscale. Additionally we remove the white, opaque pixels in the image and make them white, but transparent. We can do this using two filters:
<filters>
<filter name="replace(255, 255, 255, 255 with 1, 1, 1, 0 range 2)" />
<filter name="colorize_grayscale(0.4, 0.9, 0.3, 1)" />
</filters>
Now this animation would be generated (scaled 8x and my application cannot export gif with transparency…):
[tr][td]
[/td][/tr]
This is an image which one could use in a game already. But to show you even more advanced filter combinations, and play around a little, and finally also show you how to copy kevglass’s voxelization effect (see this thread ), I’ll use the “voxelization” filter / effect:
It subtracts the color in a specified grid by a specified ammount:
<filters>
<filter name="replace(255, 255, 255, 255 with 1, 1, 1, 0 range 2)" />
<filter name="colorize_grayscale(0.4, 0.9, 0.3, 1)" />
<filter name="scale(4, 4)" />
<filter name="voxelize(4, 4 with 0.02, 0.02, 0.02, 1)" />
</filters>
We also scale the image at first to make it possible to create borders around each pixel. Otherwise we couldn’t make 4x4 pixel voxels.
The result is this animation:
[tr][td]
[/td][/tr]
And since this is not enough. Not even to show all features, I want to ‘clone’ the Realm of the mad god sprite effects:
[tr][td]
http://hydramist.tv/wp-content/uploads/2012/04/realm-of-the-mad-god.jpg
[/td][/tr]
As you can see, realm of the mad god sprites have a black outline and a slight black blurred shadowed outline around the characters.
I can recreate this using filters.
First, let’s do the black outline. For that I use my ‘outline’ filter. It creates an outline, which is exactly one pixel wide. We also scale the image again twice, to make the outline a little more subtle:
<filters>
<filter name="replace(255, 255, 255, 255 with 1, 1, 1, 0 range 2)" />
<filter name="colorize_grayscale(0.4, 0.9, 0.3, 1)" />
<filter name="scale(4, 4)" />
<filter name="voxelize(4, 4 with 0.02, 0.02, 0.02, 1)" />
<filter name="scale(2, 2)" />
<filter name="outline(0, 0, 0, 1)" />
</filters>
This is the result (not scaled):
[tr][td]
[/td][/tr]
Now we have two problems: How do we add a blurred shadow, and how do we make the sprite bigger, so the shadow (and the outline, too) have place to exist around the sprite?
Well, the solution for the size: expand filter. It adds an empty border to the sprite. I might rename it into ‘border’ and add some more functionality…
(I won’t give an example image here, the result should be clear)
The solution for the shadow is a bit more complicated. For that I use something I call ‘branch’.
It works like that:
As soon as my parser encounters a tag, it saves the old image and proceeds operating on a copy of it. At the end those images are merged together by the specified branch merge operation.
In code this looks like this:
<filters>
<filter name="replace(255, 255, 255, 255 with 1, 1, 1, 0 range 2)" />
<filter name="colorize_grayscale(0.4, 0.9, 0.3, 1)" />
<filter name="expand(4, 4)" />
<filter name="scale(4, 4)" />
<filter name="voxelize(4, 4 with 0.02, 0.02, 0.02, 1)" />
<filter name="scale(2, 2)" />
<filter name="outline(0, 0, 0, 1)" />
<branch merge="blend(bottom over top)" >
<filter name="colorize(0, 0, 0, 1)" />
<filter name="blur(8, 8, 16)" />
</branch>
</filters>
This graph should make the stuff, which is happening clearer:
[tr][td]
[/td][/tr]
(scaled 8x)
|
is turned into this,
before it encouters
the tag:
|
[tr][td]
[/td][/tr]
|
Then branching
is going on:| / \ / \ / \ / \
‘bottom’ ‘top’/ \ / \
[tr][td]
[/td][/tr]
\ / \ / \ / \ / \ / \ / \ / |
merge
(standard blending:
left branch over
right branch)
|
[tr][td]
[/td][/tr]
So thats it with the demonstration.
I won’t release a demo / the source code yet, since it’s not finished. The blur filter is much too inefficient. Still have to optimize it.
I don’t think this might be too useful to you. This was more of a project just 4 fun, and probably for the use in some of my games, but it’s written in scala, so I guess nobody will want to read the project’s source, nor use it, since it’s not too nice to use from java