#include <pspdisplay.h>
#include <pspdebug.h>
extern u8 msx[];
/////////////////////////////////////////////////////////////////////////////
// blit text
/////////////////////////////////////////////////////////////////////////////
int blit_string(int sx,int sy,const char *msg,int fg_col,int bg_col)
{
	int x,y,p;
	int offset;
	char code;
	unsigned char font;

	int pwidth, pheight, bufferwidth, pixelformat, unk, ret;
	unsigned int* vram32;
   	sceDisplay_driver_DEA197D4(&unk, &pwidth, &pheight);
   	sceDisplay_driver_E56B11BA((void*)&vram32, &bufferwidth, &pixelformat, &unk);

    if(bufferwidth == 0)
        return;
    if(pixelformat != 3)//this has problems when pixel format == 3 (in gta vcs anyway)
    {
        pspDebugScreenSetInit();//set init = 1;
        pspDebugScreenSetColorMode(pixelformat);
        pspDebugScreenSetOffset(0);
        pspDebugScreenSetBase(0x44000000);

        pspDebugScreenSetBackColor(bg_col);
        pspDebugScreenSetTextColor(fg_col);
        pspDebugScreenSetXY(sx,sy);
        return pspDebugScreenPuts(msg);
     }
     else
     {
        sy*=8;
    	for(x=0;msg[x] && x<(pwidth/8);x++)
    	{
    		code = msg[x] & 0x7f; // 7bit ANK
    		for(y=0;y<7;y++)
    		{
    			offset = (sy+y)*bufferwidth + (sx+x)*8;
    			font = msx[ code*8 + y ];
    			for(p=0;p<8;p++)
    			{
    				vram32[offset] = (font & 0x80) ? fg_col : bg_col;
    				font <<= 1;
    				offset++;
    			}
    		}
    	}
    	return x;
    }
}
