#include <me_defines.h>
#include <me_c_video.h>
#include <me_c_video_driver_interface.h>
#include <me_c_video_surface.h>
#include <string>
#include <iostream>
namespace SDL
{
#include <SDL.h>
#include <SDL_ttf.h>
}


SDL::TTF_Font *fnt;
bool FT_init( Uint16 size )
{
	if(SDL::TTF_Init()!=0)return false;
	fnt = SDL::TTF_OpenFont( "res/font.ttf", size );
	if(fnt==NULL)return false;
	SDL::TTF_SetFontStyle( fnt, TTF_STYLE_ITALIC );
	return true;
}
bool FT_openfont(Uint16 size)
{
	SDL::TTF_CloseFont(fnt);
	fnt = SDL::TTF_OpenFont( "res/font.ttf", size );
	if(fnt==NULL)return false;
	SDL::TTF_SetFontStyle( fnt, TTF_STYLE_ITALIC );
	return true;
}

ME_n_Video::ME_c_Video_Surface* FT_Render( std::string text, Uint32 color, std::string grp, std::string name )
{
	SDL::SDL_Color fg;
		fg.r = 0xff & color;
		fg.g = 0xff & ( color >> 8 );
		fg.b = 0xff & ( color >> 16 );

	SDL::SDL_Surface *srf = SDL::TTF_RenderText_Blended( fnt, text.c_str(), fg );
	ME_n_Video::ME_c_Video video;
	ME_n_Video::ME_c_Video_Surface* surface = &( *video )._create_surface( ( Sint16 ) srf->w, ( Sint16 ) srf->h, grp, false, name );
	Uint8 r, g, b, a,ga;
	ga=0xff&(color>>24);
	Uint32 color1;
	Uint32 sl = surface->_get_line_size();
	Uint8 *surfaced = ( Uint8* ) surface->_get_surface_data();
	Uint8 bpp = surface->_get_bpp();
	for ( Sint32 h = 0;h < srf->h;h++ )
	{
		for ( Sint32 w = 0;w < srf->w;w++ )
		{
			SDL::SDL_GetRGBA( *( ( Uint32* ) srf->pixels + srf->w * h + w ), srf->format, &r, &g, &b, &a );
			*( surfaced + (srf->h-h-1)*sl + w * bpp ) = r;
			*( surfaced + (srf->h-h-1)*sl + w * bpp + 1 ) = g;
			*( surfaced + (srf->h-h-1)*sl + w * bpp + 2 ) = b;
			*( surfaced + (srf->h-h-1)*sl + w * bpp + 3 ) = a&ga;
		}
	}
	SDL::SDL_FreeSurface( srf );
	return surface;
}
