/*
Copyright (c) 2007-2013, John Ryland
*/
#include <screen.h>
#include <oslayer.h>

#define GET_SCREENINFO			0x4600

Image openScreen()
{
	struct {
		unsigned width;
		unsigned height;
		unsigned misc1[4];
		unsigned bpp;
		unsigned misc2[33];
	} screenInfo;

	Image data;
	data.fd = fsOpen("/dev/fb0", O_RDWR);

	sysIoctl(data.fd, GET_SCREENINFO, &screenInfo);

	data.width = screenInfo.width; //320;
	data.height = screenInfo.height; //240;
	data.bytes_per_pixel = screenInfo.bpp >> 3; // 2;
	//data.pixel_data = (unsigned char *)fsMmap(0, 240*320*2, PROT_READ | PROT_WRITE, MAP_SHARED, data.fd, 0);
	data.pixel_data = (unsigned char *)fsMmap(0, data.width*data.height*data.bytes_per_pixel, PROT_READ | PROT_WRITE, MAP_SHARED, data.fd, 0);
	return data;
}

void closeScreen(Image screen)
{
	fsClose(screen.fd);
}

void fillImage(Image image, unsigned short col)
{
	int x, y, i = 0;
	unsigned col2 = col << 16 | col;
	unsigned *fbptr = (unsigned *)image.pixel_data;
	for (y = 0; y < image.height; y++) {
		for (x = 0; x < (image.width>>1); x+=4) {
			fbptr[i+0] = col2;
			fbptr[i+1] = col2;
			fbptr[i+2] = col2;
			fbptr[i+3] = col2;
			i += 4;
		}
	}
}

void blitScreen(unsigned char *fbptr, short *buf)
{
//	memCopy((char*)fbptr, (char*)buf, 320*240*2);

	int i;
	for (i = 0; i < 240; i++)
		memCopy((char*)fbptr + i*1280*2 + 1280-320 + 1280*20, (char*)buf + i*320*2, 320*2);

}


