--
--	rastergraphics: raster graphics primitives
--	Copyright (C) 2007  David Wood <dcw@kent.ac.uk>
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation, either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library.  If not, see
--	<http://www.gnu.org/licenses/>.
--

--** @module rastergraphics

--* Draws a solid rectangle.
--
--  Clips as necessary.
--
--  @param x.0, y.0 Top left corner
--  @param w.0 Width
--  @param h.0 Height/depth
--  @param colour Colour
--  @param raster Raster

PROC fill.rectangle (VAL INT x.0, y.0, w.0, h.0, VAL INT colour, [][]INT raster)

  VAL INT W IS SIZE raster[0]:
  VAL INT H IS SIZE raster:

  -- Flatten raster to eliminate multiplications
  -- raster[y][x] -> raster[(y TIMES W) PLUS x]

  -- [Y][X]INT raster RETYPES raster:
  -- [W TIMES H]INT raster RESHAPES raster:

  IF
    (w.0 <= 0) OR (h.0 <= 0) OR
    ((x.0 PLUS w.0) < 0) OR (x.0 >= W) OR
    ((y.0 PLUS h.0) < 0) OR (y.0 >= H)
      SKIP					-- entirely outside raster
    TRUE
      INT x, y, w, h:
      SEQ
        IF					-- clip
          x.0 < 0
            x, w := 0, x.0 PLUS w.0
          TRUE
            x, w := x.0, w.0
        IF
          (x PLUS w) > W
            w := W MINUS x
          TRUE
            SKIP
        IF
          y.0 < 0
            y, h := 0, y.0 PLUS h.0
          TRUE
            y, h := y.0, h.0
        IF
          (y PLUS h) > H
            h := H MINUS y
          TRUE
            SKIP
        SEQ i = y FOR h
          SEQ j = x FOR w
            raster[i][j] := colour
:

--* Draw a solid square.

PROC fill.square (VAL INT x, y, s, VAL INT colour, [][]INT raster)
  fill.rectangle (x, y, s, s, colour, raster)
:
