⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsfunc.cpp

📁 网络泡泡被.net管理
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}

GSFBUF CGsFunc::GSFBUF_Create(const char *szFile, const char *mode)
{
	GSFBUF	gsbuf;
	if((gsbuf.p_fstream	= fopen(szFile, mode))!=NULL)
	{
		gsbuf.pos_begin	= 0;
		fseek(gsbuf.p_fstream, 0, SEEK_END);
		gsbuf.file_size	= ftell(gsbuf.p_fstream);
		fseek(gsbuf.p_fstream, 0, SEEK_SET);
	}
	return gsbuf;
}

SIZE CGsFunc::FileGetJpegSize(GSFBUF* pfBuf)
{
	SIZE	size;
	size.cx=0;
	size.cy=0;
	// basic code from IJG Jpeg Code v6 example.c

	/* This struct contains the JPEG decompression parameters and pointers to
	* working space (which is allocated as needed by the JPEG library).
	*/
	struct jpeg_decompress_struct cinfo;
	/* We use our private extension JPEG error handler.
	* Note that this struct must live as long as the main JPEG parameter
	* struct, to avoid dangling-pointer problems.
	*/
	struct my_error_mgr jerr;
	/* More stuff */
//	char buf[250];

	/* In this example we want to open the input file before doing anything else,
	* so that the setjmp() error recovery below can assume the file is open.
	* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
	* requires it in order to read binary files.
	*/
	/* Step 1: allocate and initialize JPEG decompression object */

	/* We set up the normal JPEG error routines, then override error_exit. */
	cinfo.err = jpeg_std_error(&jerr.pub);
	jerr.pub.error_exit = my_error_exit;


	/* Establish the setjmp return context for my_error_exit to use. */
	if (setjmp(jerr.setjmp_buffer)) {
		/* If we get here, the JPEG code has signaled an error.
		 * We need to clean up the JPEG object, close the input file, and return.
		 */

		jpeg_destroy_decompress(&cinfo);

		return size;

	}

	/* Now we can initialize the JPEG decompression object. */
	jpeg_create_decompress(&cinfo);

	/* Step 2: specify data source (eg, a file) */
	fseek(pfBuf->p_fstream, pfBuf->pos_begin, SEEK_SET);
	jpeg_stdio_src(&cinfo, pfBuf->p_fstream);

	jpeg_stdio_src(&cinfo, pfBuf->p_fstream);

	/* Step 3: read file parameters with jpeg_read_header() */

	(void) jpeg_read_header(&cinfo, TRUE);
	/* We can ignore the return value from jpeg_read_header since
	*   (a) suspension is not possible with the stdio data source, and
	*   (b) we passed TRUE to reject a tables-only JPEG file as an error.
	* See libjpeg.doc for more info.
	*/


	// how big is this thing ?
	size.cx = cinfo.image_width;
	size.cy = cinfo.image_height;

	/* Step 8: Release JPEG decompression object */

	/* This is an important step since it will release a good deal of memory. */
	jpeg_destroy_decompress(&cinfo);

	/* After finish_decompress, we can close the input file.
	* Here we postpone it until after no more JPEG errors are possible,
	* so as to simplify the setjmp error logic above.  (Actually, I don't
	* think that jpeg_destroy can do an error exit, but why assume anything...)
	*/

	/* At this point you may want to check to see whether any corrupt-data
	* warnings occurred (test whether jerr.pub.num_warnings is nonzero).
	*/

	return size;
}

BOOL CGsFunc::FileCompareExt(const char *strFile, const char *strExt)
{
	char*	pExt	= strrchr(strFile, '.');
	char file_ext[20],dest_ext[20];
	if(pExt)
	{
		strcpy(file_ext, pExt+1);
		file_ext[strlen(strExt)]=0;
		_strlwr(file_ext);
		pExt	= strrchr(strExt, '.');
		if(pExt)
			strcpy(dest_ext, pExt+1);
		else
			strcpy(dest_ext, strExt);
		_strlwr(dest_ext);
		if(KEY(file_ext)==KEY(dest_ext))
			return TRUE;

	}

	return FALSE;
}

VOID CGsFunc::UI_SetEditMode(BOOL bEditMode)
{
	g_bEditUI	= bEditMode;
}

BOOL CGsFunc::File_CopyTo(const char *strFrom, const char *strTo)
{
	char	strDest[PATH_SIZE];
	Path_RectifyPath(strDest, strTo);
	char	strSrc[PATH_SIZE];
	Path_RectifyPath(strSrc, strFrom);
	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=g_pGsApp->GetMainWnd();
	lpFileOp.wFunc=FO_COPY;
	lpFileOp.pFrom=strSrc;
	lpFileOp.pTo=strDest;
	lpFileOp.fFlags=FOF_SILENT|FOF_NOCONFIRMATION;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	return (0==SHFileOperation(&lpFileOp));

}

BOOL CGsFunc::File_Rename(const char *strFrom, const char *strTo)
{
	char	strDest[PATH_SIZE];
	Path_RectifyPath(strDest, strTo);
	char	strSrc[PATH_SIZE];
	Path_RectifyPath(strSrc, strFrom);
	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=g_pGsApp->GetMainWnd();
	lpFileOp.wFunc=FO_RENAME;
	lpFileOp.pFrom=strSrc;
	lpFileOp.pTo=strDest;
	lpFileOp.fFlags=FOF_SILENT|FOF_NOCONFIRMATION;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	return (0==SHFileOperation(&lpFileOp));
}

BOOL CGsFunc::File_MoveTo(const char *strFrom, const char *strTo)
{
	char	strDest[PATH_SIZE];
	Path_RectifyPath(strDest, strTo);
	char	strSrc[PATH_SIZE];
	Path_RectifyPath(strSrc, strFrom);
	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=g_pGsApp->GetMainWnd();
	lpFileOp.wFunc=FO_MOVE;
	lpFileOp.pFrom=strSrc;
	lpFileOp.pTo=strDest;
	lpFileOp.fFlags=FOF_SILENT|FOF_NOCONFIRMATION;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	return (0==SHFileOperation(&lpFileOp));
}

BOOL CGsFunc::File_Delete(const char *str)
{
	char	strDesc[PATH_SIZE];
	Path_RectifyPath(strDesc, str);
	if(str[0]==0)
		return FALSE;
	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=g_pGsApp->GetMainWnd();
	lpFileOp.wFunc=FO_DELETE;
	lpFileOp.pFrom=strDesc;
	lpFileOp.pTo=NULL;
	lpFileOp.fFlags=FOF_SILENT|FOF_NOCONFIRMATION;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	return (0==SHFileOperation(&lpFileOp));
}

VOID CGsFunc::GSFBUF_Set(GSFBUF *pBuf, long pos)
{
	if(pBuf==NULL)
		return;
	fseek(pBuf->p_fstream, pBuf->pos_begin + pos, SEEK_SET);

}

int CGsFunc::GSFBUF_Seek(GSFBUF &gsbuf, long pos, int origin)
{
	return fseek(gsbuf.p_fstream, gsbuf.pos_begin + pos, origin);

}

const char pExt[][5]
			=
		{
			"", //GSF_UNKNOWN	= 0,
			"", //GSF_FOLDER,
			"ini", //GSF_INI,
			"bmp", //GSF_BMP,
			"jpg", //GSF_JPG,
			"tga", //GSF_TGA,
			"wav", //GSF_WAV,
			"gif", //GSF_PGM,
			"pgg", //GSF_PGG,
			"tex", //GSF_TEX,
			"txg", //GSF_TXG,
			"txt", //GSF_TXT
			"avi",  //GSF_AVI
			"mpg",  //GSF_MPG
			"mpeg",  //GSF_MPEG
			"wmv",  //GSF_WMV
			"mp3",  //GSF_MP3
			"mid",  //GSF_MIDI
			"wma",  //GSF_WMA

			"map",

			"fnt",	//GSF_FNT,
			"fon",	//GSF_FON,
			"ttf",	//GSF_TTF,
			"fot",	//GSF_FOT,
			"dds",
			"gss",  //GSF_GSS
		};

const char* CGsFunc::File_GetExt(FFT fft)
{
	return pExt[fft];
}

FFT CGsFunc::File_GetFFT(const char* strFile)
{
//	char pExt[][5]
//		=
//	{
//		"", //GSF_UNKNOWN	= 0,
//		"", //GSF_FOLDER,
//		"ini", //GSF_INI,
//		"bmp", //GSF_BMP,
//		"jpg", //GSF_JPG,
//		"tga", //GSF_TGA,
//		"wav", //GSF_WAV,
//		"gif", //GSF_PGM,
//		"pgg", //GSF_PGG,
//		"tex", //GSF_TEX,
//		"txg", //GSF_TXG,
//		"txt", //GSF_TXT
//		"avi",  //GSF_AVI
//		"mpg",  //GSF_MPG
//		"mpeg",  //GSF_MPEG
//		"wmv",  //GSF_WMV
//		"mp3",  //GSF_MP3
//		"mid",  //GSF_MIDI
//		"wma",  //GSF_WMA
//		"map",
//		"fnt",
//		"dds",
//		"gss",  //GSF_GSS
//	};
	for(int i=GSF_INI; i<=GSF_GSS; i++)
	{
		if(FileCompareExt(strFile, pExt[i]))
		{
			return (FFT)i;
		}
	}
	return GSF_UNKNOWN;
}

VOID CGsFunc::File_UpdateInfo(GSFILE_INFO *pfi)
{
	if(NULL==pfi)
		return;
	struct _stat stat;
	_stat(pfi->strFile, &stat);
	pfi->file_size = stat.st_size;
	SIZE size;
	GSFBUF gsbuf = CGsFunc::GSFBUF_Create(pfi->strFile,"rb");
	switch(pfi->fft) 
	{
	case GSF_JPG:
		size = CGsFunc::FileGetJpegSize(&gsbuf);
		pfi->info.width = size.cx;
		pfi->info.height = size.cy;
		break;
	case GSF_TGA:
		break;
	default:
		break;
	}
	fclose(gsbuf.p_fstream);
	
}

size_t CGsFunc::GSFBUF_Write(const void *buffer, size_t size, size_t count, GSFBUF &gsbuf)
{
	return fwrite(buffer, size, count, gsbuf.p_fstream);
}

size_t CGsFunc::GSFBUF_Read(void *buffer, size_t size, size_t count, GSFBUF &gsbuf)
{
	return fread(buffer, size, count, gsbuf.p_fstream);
}

long CGsFunc::GSFBUF_Tell(GSFBUF &gsbuf)
{
	return ftell(gsbuf.p_fstream);
}

int CGsFunc::GSFBUF_Close(GSFBUF &gsbuf)
{
	return fclose(gsbuf.p_fstream);
}

int CGsFunc::Font_AddResource(const char *strFont)
{
	int nRet;
 	nRet = AddFontResource(strFont);
//	if(nRet!=0)
//		SendMessage(HWND_BROADCAST , WM_FONTCHANGE, 0,0);
	return nRet;
}

BOOL CGsFunc::Font_RemoveResource(const char *strFont)
{
	BOOL bRet;
 	bRet = RemoveFontResource(strFont);
//	if(bRet)
//		SendMessage(HWND_BROADCAST , WM_FONTCHANGE, 0,0);
	return bRet;
}

const char* CGsFunc::Font_GetName(const char *strKey, CGsSource *pSrc)
{
	if(pSrc!=NULL)
	{
		GSFILE_INFO*	pfi	= pSrc->FindSource(strKey);
		if(pfi && pfi->font.strName[0]!=0)
		{
			return pfi->font.strName;
		}
	}
	return strKey;

}


VOID CGsFunc::ColorToString(char *strDest, D3DCOLOR color)
{
	if(strDest==NULL)
		return;
	sprintf(strDest, "%d,%d,%d,%d", RGBA_GETRED(color), RGBA_GETGREEN(color), RGBA_GETBLUE(color), RGBA_GETALPHA(color));
}

D3DCOLOR CGsFunc::StringToColor(const char *strSrc)
{
	if(strSrc==NULL || strSrc[0]==0)
		return 0;
	char* sz = (char*)strSrc;
	int r=0,g=0,b=0,a=0;
    r		= _tcstol(sz,&sz,10);
	if(*(sz+1)!=0)
		g		= _tcstol(sz+1,&sz,10);
	if(*(sz+1)!=0)
		b	= _tcstol(sz+1,&sz,10);
	if(*(sz+1)!=0)
		a	= _tcstol(sz+1,&sz,10);

	return RGBA_MAKE(r,g,b,a);
}

FLOAT CGsFunc::Math_GetPointToAngle(FLOAT x, FLOAT y)
{
	float	f_angle	;
	if(x==0)
	{
		if(y==0)
		{
			return 0;
		}
		else if(y>0)
			f_angle	= const_PI_DIV_2;
		else if(y<0)
			f_angle	= -const_PI_DIV_2;
	}
	else
	{
		f_angle	= atan(y/x);
	}

	if(x<0)
	{
		f_angle += const_PI;
	}
	else if(f_angle<0)
	{
		f_angle += const_2_PI;
	}
	return f_angle;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -