Move everything terminal related into hase.term.

This is to allow for seamless abstraction of the terminal and to make it easy to
share code between the different terminal types without cluttering the hase
namespace. And of course, the naming is now more consistently and we call
everything, be it a "surface provider" or a "term canvas", simply "terminal" no
matter if it's drawn using pixel operations, by sending ECMA-48 CSIs or just a
test terminal.

Everything in hase.term.internal is now only accessible from the hase.term
namespace, so that the name is clearly appropriate.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
rect: Add method to calculate intersection area.

This is needed in order to optimize our dirty rectangle blitter, so that we only
redraw all characters within the _current_ rectangle and avoid touching other
rectangles.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
rect: Add method to check if point is in area.

This comes in handy when we're going to blit single characters, so we only
redraw a fraction of the sprite. More or less just a convenience function.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
rect: Fix bug in contains() and test case.

Well, that's when you make an off-by-one error by yourself and actually provide
the wrong test case. This now took me quite a long time to figure out that the
bug was in contains(), which I believed it was working correctly.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
matrix: Add a constructor for repetitive matrices.

A simple create() constructor in case we need some base matrix that we can
change on later. Much more efficient that from_2d_array() or starting with an
empty matrix, adding rows and columns.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
surface: Refactor redraw_rect() and blitting.

We now no longer redraw everything in the universe and only the relevant parts
within the dirty range, like this:

  0123456789012345678
  1                    A (5,  2)
  2     ____           : (8,  4)
  3    |    |          B (12, 6)
  4    |  ..|....
  5    A__:c|   :      : x A (8,  4, 2, 1)
  6       :    _:__    : x B (12, 6, 2, 1)
  7       :...|c:  |
  8           |    |   c   -> common area
  9           |____B   :   -> rectangle to redraw
  0                    A/B -> sprites

This basically shows the dirty range in dots (the rectangle to redraw) and the
intersecting sprites A and B.

Our current implementation iterates through all registered sprites, throwing
away non-visible sprites and those out of range. Sprites that are in range are
then applied to a base matrix, ignoring alpha characters. Order of appliance is
based on the Z value of the corresponding sprites and we're drawing from bottom
to top.

Of course, the way of drawing is not very optimal, especially if there are a
whole lot of overlapping sprites. But for now this solution is the most accurate
and that's what we're testing for, we can still optimize later.

Speaking of testing, this commit fixes test_different_overlapping_frame_sizes()
as well, as - shame on me - I copied the result directly into the test output,
assuming it was correct.

I've noticed this bug, because sometimes there were drawing artifacts visible,
which only happened in the example when the framerate dropped.

Background of the bug is as follows:
  ___      ___        ___
 |   |    |   |      |   |
 | A |    | A |_     | A_|_
 |___|    |___| |    |_|   |
  ___  ->  _|_C | ->  _|_C |
 |   |    |   |_|    |   |_|
 | B |    | B |      | B |
 |___|    |___|      |___|

What happens is the following:

 1. A and B are in the scene, this is the starting point.
 2. C is added with a lower Z value and thus A and B get redrawn on top of it
    because they're *both* intersecting with C.
 3. Ugly things happen now as soon as we either update A or B, because they only
    intersect with C and won't trigger a redraw of the respective counterpart.

As explained in the beginning, this shouldn't happen anymore, as we only redraw
the characters intersecting with the dirty range, so A and B only redraw the
corner of C that overlaps with them, nothing more.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
surface: Use access control for most methods.

We don't really want people using HASE for their projects to fiddle around with
those methods, so let's make them private and explicitly set the methods which
are allowed to access them.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
surface: Clean up and optimize redraw_rect().

In the previous commit, we just tried to get the new rectangle redrawer running
and pass all the test. With this commit, it actually is now way faster (around
20% CPU usage on my system with curses, in comparison to 80% before) because we
don't iterate through the whole matrix, even if we only want to set a small
fraction on the base matrix.

And of course, this should also clean up the mess/ugly code we had until now.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>