U mad?
The theory and basic implementation of the view is fine and dandy - or you would think so. But like all great software, the initial, clean algorithm never takes more than 10% of the code. The other 90% is graceful spaghetti code in an attempt to fix bugs and impossible exceptions without breaking much else. We will now focus on this 90% of program.
Transparency
Let's use the last image to show what's wrong as of now with our display:
Namely, the pink has to go. It's background color, not useful at all; if anything, it blocks the different maps that are on the back.
We could adjust it so that every blank panel was not to be written, but that only solves half of the problem: pretty much every boxel that has some actual content in it also has some parts that have to have 0 alpha.
------------------------
EXTRA INFO: ALPHA CHANNEL.
We talk about alpha or about alpha channel when we refer to an images' opacity property; in other words, how transparent it is, or how much of the picture that's behind it can we see through. The following image makes the alpha fall from 1 to 0 as the y coordinate progresses:
-----------------------
We went through several approaches in order to solve this issue.
Sequential Pixel-by-Pixel Check
This one was by far the most obvious and the easiest to implement on that current structure. Only a pixel-by pixel check was required for the view to do; if it was that pink color, don't paint it - otherwise, go ahead.
This seems simple enough to program, and simple enough to understand, and it worked - No bugs whatsoever. However a tiny new problem arose that forced us to take a different view on the subject.
Such was the problem:
Threaded Pixel-by-Pixel Check
Same solution, but with a grid of Graphics2D, each one in an implemented Runnable for every boxel that came in.
------------------------
EXTRA INFO: THREADS
This one is interesting so take a look. A thread is an execution logic that allows us to make different operations at the same time while being independent of each other. Therefore, we could be painting a red pixel and adding 3 and 2 at the same time.
The threads are the units that allow us to do such parallel execution.
Normally one processor is not capable of calculating anything but sequentially; however the graphics' card, which consists of a bunch of little processors that can make the simplest operations (just enough to display the graphics) can BE used for threading.
------------------------
Since we only had to initialize every thread once and start it sequentially but without waiting for the earlier one to finish, this would have helped the performance of the View.
You might have noticed it's the same video as before. This is my sarcastic way of expressing the performance did not go as better as expected (in reality I was tearing my hair off at this point, along with David's if he ever came too close).
Pre-alpha loading
On the verge of destructing rage, I decided to pre-check every pixel of every texture for alpha and save it in a huge matrix (0 for opaque, 1 for invisible). This was done on the controller.
It took more time than it should, and while I was working on it I quickly realized this helped little with our problem; it was not the checking for transparency itself but the fact that we had to go pixel by pixel what affected the performance.
PNG alpha channel
As you may or may not know, PNG files do have a feature that allows storage of alpha, which works wonders on our issue.
So your totally justified question would be, "why was that not your first option?". Well, it was, and it failed. And then they suggested the same thing again. And I reluctantly retried it. And it worked. Java works in mysterious ways indeed.
Flickering
Of course, after one problem goes down, another one pops up, such is the way of the programmer. A really basic issue which I did not take into account was the flickering, or 2D clipping.
------------------------
EXTRA INFO: CLIPPING
This is a very basic problem that happens upon designing a graphics engine. Basically, it comes from these premises:
- You paint an object drawn on your screen. It can be anything: an image, a polygon, a circle, a line...
- You paint another object of the same nature on your screen. Such object happens to be in the same position, partially or fully, as the former.
- Computer: which one should I paint? Is one in front of the other? Are they truly in the same space? As I have no directions about this, I can't give a correct or stable solution...
Of course the bad outcome takes many forms; in our case, since we want to repaint the screen constantly to keep track every time of what's going on in the game, it will flicker.
To one that only wants to paint once, they will probably experience that the latter layer is painted in front of the earlier.
------------------------
At the moment of writing these lines no solution has been found yet for the flickering, however as stated it is a very common problem and we already have a couple approaches in mind that we'll no doubt explain in later entries.
It's time to turn in as that's as many information as I can give for now. We'll keep working with the engine and start practicing with the mobs and physics, and we'll surely get back to you once we've got more to tell you.
Until then, out and over!