SBSPSS/Utils/MapEdit/utils.cpp

418 lines
9.7 KiB
C++
Raw Normal View History

2000-09-25 17:43:52 +02:00
/*************/
/*** Utils ***/
/*************/
#include "stdafx.h"
2001-02-01 23:48:22 +01:00
#include "MapEdit.h"
#include "MapEditDoc.h"
2000-11-21 22:27:55 +01:00
2000-12-04 17:47:34 +01:00
#include <Vector3.h>
2000-09-25 17:43:52 +02:00
#include <gl\gl.h>
#include <gl\glu.h>
#include "GLEnabledView.h"
2001-02-01 23:48:22 +01:00
#include <Misc.hpp>
#include <GFName.hpp>
2001-03-31 03:30:53 +02:00
#include <malloc.h>
2001-02-01 23:48:22 +01:00
2000-09-25 17:43:52 +02:00
#include "Utils.H"
/**************************************************************************************/
void DbgMsg(const char * pszFmt,...)
{
char szBuf[256];
if (pszFmt)
{
va_list args;
va_start(args,pszFmt);
vsprintf(szBuf,pszFmt,args);
OutputDebugString(szBuf);
va_end(args);
}
}
2001-03-31 03:30:53 +02:00
/**************************************************************************************/
2001-05-05 23:18:12 +02:00
const int TexAlignTable[]={1,2,4,8,16,32,64,128,256,512,1024};
2001-03-31 03:30:53 +02:00
const int TexAlignTableSize=sizeof(TexAlignTable)/sizeof(int);
int AlignSize(int Size)
{
for (int i=0;i<TexAlignTableSize-1; i++)
{
if (Size>TexAlignTable[i] && Size<TexAlignTable[i+1]) return(TexAlignTable[i+1]);
}
return(Size);
}
2000-09-25 17:43:52 +02:00
/**************************************************************************************/
/**************************************************************************************/
void BuildGLBox(float XMin,float XMax,float YMin,float YMax,float ZMin,float ZMax)
{
// Bottom Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMax, YMin, ZMin);
glVertex3f( XMax, YMin, ZMax);
glVertex3f( XMin, YMin, ZMax);
// Front Face
glVertex3f( XMin, YMin, ZMax);
glVertex3f( XMax, YMin, ZMax);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMin, YMax, ZMax);
// Back Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMin, YMax, ZMin);
glVertex3f( XMax, YMax, ZMin);
glVertex3f( XMax, YMin, ZMin);
// Right face
glVertex3f( XMax, YMin, ZMin);
glVertex3f( XMax, YMax, ZMin);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMax, YMin, ZMax);
// Left Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMin, YMin, ZMax);
glVertex3f( XMin, YMax, ZMax);
glVertex3f( XMin, YMax, ZMin);
// Top Face
glVertex3f( XMin, YMax, ZMin);
glVertex3f( XMin, YMax, ZMax);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMax, YMax, ZMin);
}
/**************************************************************************************/
void BuildGLBoxNoNormals(float XMin,float XMax,float YMin,float YMax,float ZMin,float ZMax)
{
// Bottom Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMax, YMin, ZMin);
glVertex3f( XMax, YMin, ZMax);
glVertex3f( XMin, YMin, ZMax);
// Front Face
glVertex3f( XMin, YMin, ZMax);
glVertex3f( XMax, YMin, ZMax);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMin, YMax, ZMax);
// Back Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMin, YMax, ZMin);
glVertex3f( XMax, YMax, ZMin);
glVertex3f( XMax, YMin, ZMin);
// Right face
glVertex3f( XMax, YMin, ZMin);
glVertex3f( XMax, YMax, ZMin);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMax, YMin, ZMax);
// Left Face
glVertex3f( XMin, YMin, ZMin);
glVertex3f( XMin, YMin, ZMax);
glVertex3f( XMin, YMax, ZMax);
glVertex3f( XMin, YMax, ZMin);
// Top Face
glVertex3f( XMin, YMax, ZMin);
glVertex3f( XMin, YMax, ZMax);
glVertex3f( XMax, YMax, ZMax);
glVertex3f( XMax, YMax, ZMin);
}
2000-10-25 23:00:54 +02:00
2000-09-25 17:43:52 +02:00
/**************************************************************************************/
2000-10-25 23:00:54 +02:00
void BuildGLQuad(float XMin,float XMax,float YMin,float YMax,float Z)
{
glVertex3f( XMin, YMin, Z);
glVertex3f( XMax, YMin, Z);
glVertex3f( XMax, YMax, Z);
glVertex3f( XMin, YMax, Z);
}
2000-09-25 17:43:52 +02:00
/**************************************************************************************/
/**************************************************************************************/
2000-11-17 22:36:13 +01:00
/**************************************************************************************/
CPoint IDToPoint(int ID,int Width)
{
CPoint XY;
XY.x=ID%Width;
XY.y=ID/Width;
return(XY);
}
/**************************************************************************************/
int PointToID(CPoint &Pnt,int Width)
{
int ID;
ID=(Pnt.y*Width)+Pnt.x;
return(ID);
}
2000-10-27 02:06:19 +02:00
/**************************************************************************************/
/**************************************************************************************/
2000-11-21 22:27:55 +01:00
/**************************************************************************************/
struct sTgaHdr
{
char id; // 0
char colmaptype; // 1
char imagetype; // 2
char fei[2]; // 3
char cml[2]; // 5
char cmes; // 7
short xorig; // 8
short yorig; // 10
short width; // 12
short height; // 14
char depth; // 15
char imagedesc; // 16
};
2001-04-05 16:20:56 +02:00
void SaveTGA(const char *Filename,int W,int H,u8 *Data,bool IsBGR)
2000-11-21 22:27:55 +01:00
{
FILE *File;
sTgaHdr FileHdr;
File=fopen(Filename,"wb");
memset(&FileHdr,0 ,sizeof(sTgaHdr));
FileHdr.imagetype= 2; //imagetype
2000-11-22 23:08:47 +01:00
FileHdr.width = W;
FileHdr.height= H;
2000-11-21 22:27:55 +01:00
FileHdr.depth=24;
fwrite(&FileHdr,sizeof(sTgaHdr),1,File);
2001-04-05 16:20:56 +02:00
if (!IsBGR)
{
fwrite(Data,W*H*3,1,File);
}
else
{
int Size=W*H;
for (int i=0; i<Size;i++)
{
fwrite(&Data[2],1,1,File);
fwrite(&Data[1],1,1,File);
fwrite(&Data[0],1,1,File);
Data+=3;
2000-11-21 22:27:55 +01:00
2001-04-05 16:20:56 +02:00
}
}
2000-11-21 22:27:55 +01:00
fclose(File);
}
2000-11-22 23:08:47 +01:00
/**************************************************************************************/
2000-11-24 23:34:20 +01:00
void BGR2RGB(int W,int H,u8 *Data)
2000-11-22 23:08:47 +01:00
{
for (int Y=0; Y<H; Y++)
{
for (int X=0; X<W; X++)
{
2000-11-24 23:34:20 +01:00
u8 c0,c1;
2000-11-22 23:08:47 +01:00
c0=Data[0];
c1=Data[2];
Data[0]=c1;
Data[2]=c0;
Data+=3;
}
}
2000-11-24 23:34:20 +01:00
}
/**************************************************************************************/
void SaveBmp(char *Filename,int Width,int Height,RGBQUAD *Pal,u8 *Image)
{
FILE *File;
BITMAPFILEHEADER FileHdr;
BITMAPINFOHEADER ImageHdr;
int PaletteSize,ImageSize;
File=fopen(Filename,"wb");
if (!Pal)
{
PaletteSize=0;
ImageSize=Width*Height*3;
ImageHdr.biBitCount=24;
}
else
{
PaletteSize=256*sizeof(RGBQUAD);
ImageSize=Width*Height;
ImageHdr.biBitCount=8;
}
FileHdr.bfType=19778;
FileHdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+PaletteSize+ImageSize;
FileHdr.bfReserved1=0;
FileHdr.bfReserved2=0;
FileHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+PaletteSize;
ImageHdr.biSize=sizeof(BITMAPINFOHEADER);
ImageHdr.biWidth=Width;
ImageHdr.biHeight=Height;
ImageHdr.biPlanes=1;
// Set Above ImageHdr.biBitCount=8; // 24
ImageHdr.biCompression=BI_RGB;
ImageHdr.biSizeImage=0;
ImageHdr.biXPelsPerMeter=0;
ImageHdr.biYPelsPerMeter=0;
ImageHdr.biClrUsed=0;
ImageHdr.biClrImportant=0;
fwrite(&FileHdr,sizeof(BITMAPFILEHEADER),1,File);
fwrite(&ImageHdr,sizeof(BITMAPINFOHEADER),1,File);
if (Pal) fwrite(Pal,sizeof(RGBQUAD),256,File);
fwrite(Image,ImageSize,1,File);
fclose(File);
}
2000-11-28 00:07:07 +01:00
/**************************************************************************************/
2001-04-07 23:05:33 +02:00
void SetFileExt(const char *InName,char *OutName,char *Ext)
2000-11-28 00:07:07 +01:00
{
char Drive[_MAX_DRIVE];
char Path[_MAX_DIR];
char Name[_MAX_FNAME];
_splitpath(InName,Drive,Path,Name,0);
sprintf(OutName,"%s%s%s.%s",Drive,Path,Name,Ext);
}
2000-11-28 22:16:00 +01:00
2001-02-01 23:48:22 +01:00
/**************************************************************************************/
2001-02-14 23:35:47 +01:00
/***
2001-02-01 23:48:22 +01:00
void MakeFullFilename(const char* In,GString &Out)
{
GFName FName=theApp.GetCurrent()->GetPathName();
GString Path;
Path=FName.Drive();
Path+=FName.Dir();
Path.Append('\\');
Out=Path;
Out+=In;
}
2001-02-14 23:35:47 +01:00
*/
2001-02-01 23:48:22 +01:00
/**************************************************************************************/
2001-02-14 23:35:47 +01:00
/*!!!
2001-02-01 23:48:22 +01:00
void MakePathRel2App(const char* In,char *Out)
{
GString RootPath=theApp.GetCurrent()->GetPathName();
GFName::makerelative(RootPath,In,Out);
}
2001-02-14 23:35:47 +01:00
*/
2001-02-06 23:25:39 +01:00
/**************************************************************************************/
GString GetWorkingPath()
{
GFName FullPath=theApp.GetCurrent()->GetPathName();
GString Path;
Path=FullPath.Drive();
Path+=FullPath.Dir();
Path.Append('\\');
return(Path);
}
2001-02-07 19:46:59 +01:00
/*****************************************************************************/
void CheckFilename(GString &Filename)
{
FILE *File;
CString mexstr;
GFName FName=Filename;
GString Name;
// Check File exists
File=fopen(Filename,"r");
if (File)
{
fclose(File);
return;
}
Name=FName.File();
Name.Append('.');
Name+=FName.Ext();
mexstr.Format("%s Not Found.\n\nPlease Locate\n", Filename);
AfxMessageBox(mexstr,MB_OK | MB_ICONEXCLAMATION);
char BASED_CODE Filter[]= "All Files (*.*)|*.*||";
CFileDialog Dlg(true,NULL,Name,OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,Filter);
if (Dlg.DoModal()!=IDOK) return;
Filename=Dlg.GetPathName();
}
2001-02-14 23:35:47 +01:00
/*****************************************************************************/
2001-03-05 21:49:46 +01:00
int round(float f)
{
if (f<0)
{
return (int)(f-0.5f);
}
else
{
return (int)(f+0.5f);
}
}
/*****************************************************************************/
2001-03-31 03:30:53 +02:00
void *_MemAlloc(size_t Size)
{
CheckMem();
void *Ptr=malloc(Size);
CheckMem();
return(Ptr);
}
/*****************************************************************************/
void _MemFree(void *Ptr)
{
CheckMem();
free(Ptr);
CheckMem();
}
/*****************************************************************************/
void CheckMem()
{
int Status = _heapchk();
switch( Status)
{
case _HEAPOK:
// TRACE0(" OK - heap is fine\n" );
break;
case _HEAPEMPTY:
// TRACE0(" OK - heap is empty\n" );
break;
case _HEAPBADBEGIN:
TRACE0( "ERROR - bad start of heap\n" );
break;
case _HEAPBADNODE:
TRACE0( "ERROR - bad node in heap\n" );
break;
}
}
/*****************************************************************************/
void GetExecPath(GString &Path)
{
#ifndef _DEBUG
// Get application path
char ExeFilename[2048];
GFName Exe;
GetModuleFileName(GetModuleHandle(NULL),ExeFilename,2048);
Exe=ExeFilename;
Exe.File(0);
Exe.Ext(0);
Path=Exe.FullName();
#else
Path="\\SpongeBob\\tools\\MapEdit\\";
#endif
}
2001-04-07 23:05:33 +02:00