#include "draw.h"
#include "gray.h"
void draw_bmp(unsigned char* bmp, int x, int y, int width, int height, Buffer buffer, DrawMode drawmode)
{
char* screen;
if(buffer == LIGHT_BUFFER)
screen = gray_getScreen()->VRAM1;
else
screen = gray_getScreen()->VRAM2;
switch(drawmode) {
case OR: draw_bmp_or_cl(bmp, x, y, width, height, screen); break;
case AND: draw_bmp_and_cl(bmp, x, y, width, height, screen); break;
case AND_NOT: draw_bmp_andnot_cl(bmp, x, y, width, height, screen); break;
}
}
int draw_read_pix(int x, int y, char* light_buffer, char* dark_buffer)
{
char color;
if(x<0 || y<0 || x>127 || y>63) return 0;
color = (light_buffer[(y<<4)+(x>>3)]&128>>(x&7) ? 1 : 0);
color |= (dark_buffer[(y<<4)+(x>>3)]&128>>(x&7) ? 2 : 0);
return color;
}
void draw_write_pix(int x, int y, int color, char* light_buffer, char* dark_buffer)
{
if(x<0 || y<0 || x>127 || y>63) return;
if(color < 0) color = 0;
else if(color > 3) color = 3;
if(color & 1) light_buffer[(y<<4)+(x>>3)] |= 128>>(x&7);
else light_buffer[(y<<4)+(x>>3)] &= ~(unsigned char)(128>>(x&7));
if(color & 2) dark_buffer[(y<<4)+(x>>3)] |= 128>>(x&7);
else dark_buffer[(y<<4)+(x>>3)] &= ~(unsigned char)(128>>(x&7));
}
void draw_bmp_or_cl(unsigned char *bmp, int x, int y, int width, int height, char* buffer)
{
unsigned short line;
char shift, *screen, *p;
int i, j, real_width, begin_x, end_x, begin_y, end_y;
char bool1=1, bool2=1, bool3;
if(!bmp || x<1-width || x>127 || y<1-height || y>63 || height<1 || width<1) return;
p = (char*)&line;
real_width = (width-1>>3<<3)+8;
if(y < 0) begin_y = -y;
else begin_y = 0;
if(y+height > 64) end_y = 64-y;
else end_y = height;
shift = 8-(x&7);
if(x<0)
{
begin_x = -x>>3;
if(shift !=
bool1 = 0;
} else begin_x = 0;
if(x+real_width > 128) end_x = 15-(x>>3), bool2 = 0;
else end_x = real_width-1>>3;
bool3 = (end_x == real_width-1>>3);
screen = buffer+(y+begin_y<<4)+(x>>3);
for(i=begin_y ; i<end_y ; i++)
{
if(begin_x < end_x)
{
line = bmp[i*(real_width>>3)+begin_x] << shift;
if(bool1) screen[begin_x] |= *p;
if(shift!=8) screen[begin_x+1] |= *(p+1);
for(j=begin_x+1 ; j<end_x ; j++)
{
line = bmp[i*(real_width>>3)+j] << shift;
screen[j] |= *p;
if(shift!=8) screen[j+1] |= *(p+1);
}
}
line = bmp[i*(real_width>>3)+end_x];
if(bool3) line &= -1<<real_width-width;
line <<= shift;
if(begin_x < end_x || bool1) screen[end_x] |= *p;
if(bool2) screen[end_x+1] |= *(p+1);
screen += 16;
}
}
void draw_bmp_and_cl(unsigned char *bmp, int x, int y, int width, int height, char* buffer)
{
unsigned short line;
char shift, *screen, *p;
int i, j, real_width, begin_x, end_x, begin_y, end_y;
char bool1=1, bool2=1, bool3;
if(!bmp || x<1-width || x>127 || y<1-height || y>63 || height<1 || width<1) return;
p = (char*)&line;
real_width = (width-1>>3<<3)+8;
if(y < 0) begin_y = -y;
else begin_y = 0;
if(y+height > 64) end_y = 64-y;
else end_y = height;
shift = 8-(x&7);
if(x<0)
{
begin_x = -x>>3;
if(shift !=
bool1 = 0;
} else begin_x = 0;
if(x+real_width > 128) end_x = 15-(x>>3), bool2 = 0;
else end_x = real_width-1>>3;
bool3 = (end_x == real_width-1>>3);
screen = buffer+(y+begin_y<<4)+(x>>3);
for(i=begin_y ; i<end_y ; i++)
{
if(begin_x < end_x)
{
line = ~((unsigned char)~bmp[i*(real_width>>3)+begin_x]<<shift);
if(bool1) screen[begin_x] &= *p;
if(shift!=8) screen[begin_x+1] &= *(p+1);
for(j=begin_x+1 ; j<end_x ; j++)
{
line = ~((unsigned char)~bmp[i*(real_width>>3)+j]<<shift);
screen[j] &= *p;
if(shift!=8) screen[j+1] &= *(p+1);
}
}
line = (unsigned char)~bmp[i*(real_width>>3)+end_x];
if(bool3) line &= -1<<real_width-width;
line = ~(line << shift);
if(begin_x < end_x || bool1) screen[end_x] &= *p;
if(bool2) screen[end_x+1] &= *(p+1);
screen += 16;
}
}
void draw_bmp_andnot_cl(unsigned char *bmp, int x, int y, int width, int height, char* buffer)
{
unsigned short line;
char shift, *screen, *p;
int i, j, real_width, begin_x, end_x, begin_y, end_y;
char bool1=1, bool2=1, bool3;
if(!bmp || x<1-width || x>127 || y<1-height || y>63 || height<1 || width<1) return;
p = (char*)&line;
real_width = (width-1>>3<<3)+8;
if(y < 0) begin_y = -y;
else begin_y = 0;
if(y+height > 64) end_y = 64-y;
else end_y = height;
shift = 8-(x&7);
if(x<0)
{
begin_x = -x>>3;
if(shift !=
bool1 = 0;
} else begin_x = 0;
if(x+real_width > 128) end_x = 15-(x>>3), bool2 = 0;
else end_x = real_width-1>>3;
bool3 = (end_x == real_width-1>>3);
screen = buffer+(y+begin_y<<4)+(x>>3);
for(i=begin_y ; i<end_y ; i++)
{
if(begin_x < end_x)
{
line = ~(bmp[i*(real_width>>3)+begin_x]<<shift);
if(bool1) screen[begin_x] &= *p;
if(shift!=8) screen[begin_x+1] &= *(p+1);
for(j=begin_x+1 ; j<end_x ; j++)
{
line = ~(bmp[i*(real_width>>3)+j]<<shift);
screen[j] &= *p;
if(shift!=8) screen[j+1] &= *(p+1);
}
}
line = bmp[i*(real_width>>3)+end_x];
if(bool3) line &= -1<<real_width-width;
line = ~(line << shift);
if(begin_x < end_x || bool1) screen[end_x] &= *p;
if(bool2) screen[end_x+1] &= *(p+1);
screen += 16;
}
}