#include <stdio.h>
#include "textgraph.h"

char buffer[HEIGHT][WIDTH];

void clear (char c)
{
  for (int y = 0; y < HEIGHT; y++)
    for (int x = 0; x < WIDTH; x++)
      buffer[y][x] = c;
}

static int check_coordinates (int x, int y, char *function)
{
  if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
    return 1;
  else
    {
      fprintf (stderr, "coordinates (%d,%d) out of range in %s\n", x, y, function);
      return 0;
    }
}

void put_point (int x, int y, char c)
{
  if (check_coordinates (x, y, "put_point"))
    buffer[y][x] = c;
}

char get_point (int x, int y)
{
  if (check_coordinates (x, y, "get_point"))
    return buffer[y][x];
  else
    return 0;
}

void display (void)
{
  for (int y = 0; y < HEIGHT; y++)
    {
      for (int x = 0; x < WIDTH; x++)
        printf ("%c", buffer[y][x]);
      printf ("\n");
    }
}