// SimuLcd.c
#include <stdio.h>
#include "allegro.h"
#include "SimuLcd.h"

extern BITMAP *screen_buffer;
extern BITMAP *stretch_buffer;
int col_on;
int col_off;
extern Int16 LoWaterMark;
extern Int16 HiWaterMark;
extern UInt8 LcdCache [];

void SimuLcdPixel( UInt8 x, UInt8 y, UInt8 pixel ) {
  int validity = 1;
  if (x>=LCD_X) {
    printf ("SimuLcdPixel (%d,%d) X >= %d\n", x,y, LCD_X);
    validity = 0;
  }
  if (y>=LCD_Y) {
    printf ("SimuLcdPixel (%d,%d) Y >= %d\n", x,y,LCD_Y);
    validity = 0;
  }

  if (!validity) return;

  if (pixel == 0) // PIXEL_OFF
    putpixel (screen_buffer, x, y, col_off);
  else 
    putpixel (screen_buffer, x, y, col_on);

  //printf ("SimuLcdPixel (%d,%d)\n", x,y);
}

void SimuLcdUpdate (void) {
  UInt8 x,y;
  UInt16 j, k;
  // printf ("SimuLcdUpdate Lo:%d Hi:%d\n", LoWaterMark, HiWaterMark);

  for (k=LoWaterMark; k<=HiWaterMark;k++) {
    x = k % 84;
    y = k / 84;
    UInt8 val = LcdCache [k];
    for (j=0; j < 8;j++) {
      SimuLcdPixel (x,(y*8)+j, val & 0x01);
      val = val >> 1;
    }
  }

  stretch_blit (screen_buffer, stretch_buffer, 0, 0, LCD_X,  LCD_Y, 0, 0, STRETCH_X, STRETCH_Y);
  blit(stretch_buffer, screen, 0, 0, (320-STRETCH_X)/2, (200-STRETCH_Y)/2, STRETCH_X, STRETCH_Y); 
}

void SimuLcdInit (void) { 
  col_on = makecol (50, 100, 100);
  col_off = makecol (200, 160, 80);
  clear_to_color (screen_buffer, col_off);
  clear_to_color (screen, col_off);
  SimuLcdUpdate ();
}

// End of file
