From 525807c7290c8979dcdca3b210a14218c4edd8f1 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 12 Apr 2002 21:44:12 +0000 Subject: Moved Pieces Library functions to a new class. git-svn-id: http://svn.leocad.org/trunk@290 c7d43263-9d01-0410-8a33-9dba5d9f93d6 --- common/library.cpp | 243 ++++++++++++++++++++++++++++++++++++++++++++---- common/library.h | 35 +++++++ common/minifig.cpp | 9 +- common/pieceinf.cpp | 5 +- common/project.cpp | 259 +++++++++------------------------------------------- common/project.h | 20 +--- common/texfont.cpp | 3 +- common/texfont.h | 4 + common/texture.cpp | 3 +- 9 files changed, 323 insertions(+), 258 deletions(-) (limited to 'common') diff --git a/common/library.cpp b/common/library.cpp index 3aac729..d67da32 100755 --- a/common/library.cpp +++ b/common/library.cpp @@ -2,17 +2,216 @@ // Piece library management // +#include "library.h" +#include "file.h" +#include "pieceinf.h" +#include "texture.h" +#include "config.h" + +// ============================================================================= +// PiecesLibrary class +PiecesLibrary::PiecesLibrary () +{ + strcpy (m_LibraryPath, ""); + m_pMovedReference = NULL; + m_nMovedCount = 0; + m_pPieceIdx = NULL; + m_nPieceCount = 0; + m_pTextures = NULL; + m_nTextureCount = 0; +} + +PiecesLibrary::~PiecesLibrary () +{ + Unload (); +} + +void PiecesLibrary::Unload () +{ + strcpy (m_LibraryPath, ""); + + free (m_pMovedReference); + m_pMovedReference = NULL; + m_nMovedCount = 0; + + delete [] m_pPieceIdx; + m_pPieceIdx = NULL; + m_nPieceCount = 0; + + delete [] m_pTextures; + m_pTextures = NULL; + m_nTextureCount = 0; +} + +bool PiecesLibrary::Load (const char *libpath) +{ + FileDisk idx; + char filename[LC_MAXPATH]; + lcuint8 version; + lcuint16 count, movedcount; + lcuint32 binsize; + Texture* pTexture; + int i; + + strcpy (m_LibraryPath, libpath); + + // Make sure that the path ends with a '/' + i = strlen(m_LibraryPath)-1; + if ((m_LibraryPath[i] != '\\') && (m_LibraryPath[i] != '/')) + strcat(m_LibraryPath, "/"); + + // Read the piece library index. + strcpy (filename, m_LibraryPath); + strcat (filename, "pieces.idx"); + + if (!idx.Open (filename, "rb")) + return false; + + idx.Seek (-(long)(2*sizeof(count)+sizeof(binsize)), SEEK_END); + idx.ReadShort (&movedcount, 1); + idx.ReadLong (&binsize, 1); + idx.ReadShort (&count, 1); + idx.Seek (32, SEEK_SET); + idx.ReadByte (&version, 1); + + if ((version != 3) || (count == 0)) + { + idx.Close(); + return false; + } + idx.Seek (34, SEEK_SET); // skip update byte + + // TODO: check .bin file size. + + // Load piece indexes + delete [] m_pPieceIdx; + m_pPieceIdx = new PieceInfo[count]; + m_nPieceCount = count; + + // workaround for VC++ error C2538: new : cannot specify initializer for arrays + for (PieceInfo *pElements = m_pPieceIdx; count--; pElements++) + pElements->LoadIndex (idx); + + // Load moved files reference. + if (m_pMovedReference != NULL) + free(m_pMovedReference); + m_pMovedReference = (char*)malloc(18*movedcount); + memset (m_pMovedReference, 0, 18*movedcount); + m_nMovedCount = movedcount; + + for (i = 0; i < movedcount; i++) + { + idx.Read (&m_pMovedReference[i*18], 8); + idx.Read (&m_pMovedReference[i*18+9], 8); + } + + idx.Close(); + + // TODO: Load group configuration here + + // Read the texture index. + strcpy(filename, m_LibraryPath); + strcat(filename, "textures.idx"); + + if (m_pTextures != NULL) + { + delete [] m_pTextures; + m_pTextures = NULL; + m_nTextureCount = 0; + } + + if (!idx.Open(filename, "rb")) + return false; + + idx.Seek(-(long)(sizeof(count)+sizeof(binsize)), SEEK_END); + idx.ReadLong (&binsize, 1); + idx.ReadShort (&count, 1); + idx.Seek(32, SEEK_SET); + idx.ReadByte (&version, 1); + + if ((version != 1) || (count == 0)) + { + idx.Close(); + return false; + } + idx.Seek(34, SEEK_SET); // skip update byte + + // TODO: check .bin file size. + + m_pTextures = new Texture[count]; + m_nTextureCount = count; + memset(m_pTextures, 0, count * sizeof(Texture)); + + for (pTexture = m_pTextures; count--; pTexture++) + pTexture->LoadIndex(&idx); + + idx.Close(); + + return true; +} + +// Remeber to make 'name' uppercase. +PieceInfo* PiecesLibrary::FindPieceInfo (const char* name) const +{ + PieceInfo* pInfo; + int i; + + for (i = 0, pInfo = m_pPieceIdx; i < m_nPieceCount; i++, pInfo++) + if (!strcmp (name, pInfo->m_strName)) + return pInfo; + + for (i = 0; i < m_nMovedCount; i++) + { + if (!strcmp (&m_pMovedReference[i*18], name)) + { + char* tmp = &m_pMovedReference[i*18+9]; + + for (i = 0, pInfo = m_pPieceIdx; i < m_nPieceCount; i++, pInfo++) + if (!strcmp (tmp, pInfo->m_strName)) + return pInfo; + + break; // something went wrong. + } + } + + return NULL; +} + +PieceInfo* PiecesLibrary::GetPieceInfo (int index) const +{ + return &m_pPieceIdx[index]; +} + +int PiecesLibrary::GetPieceIndex (PieceInfo *pInfo) const +{ + return (((char*)pInfo - (char*)m_pPieceIdx) / sizeof (PieceInfo)); +} + +Texture* PiecesLibrary::FindTexture (const char* name) const +{ + for (int i = 0; i < m_nTextureCount; i++) + if (!strcmp (name, m_pTextures[i].m_strName)) + return &m_pTextures[i]; + + return NULL; +} + + + + + + + + + #include #include #include -#include "defines.h" #include "globals.h" #include "project.h" #include "matrix.h" #include "system.h" -#include "file.h" -#include "library.h" -#include "pieceinf.h" + // ============================================================================= // LibraryDialog class @@ -41,20 +240,21 @@ bool LibraryDialog::Initialize () { FileDisk idx; char filename[LC_MAXPATH]; + PiecesLibrary *pLib = project->GetPiecesLibrary (); // Read the piece library index. - strcpy(filename, project->GetLibraryPath()); + strcpy(filename, pLib->GetLibraryPath()); strcat(filename, "pieces.idx"); if (!idx.Open(filename, "rb")) return false; idx.Seek(34, SEEK_SET); // skip update byte - m_nPieces = project->GetPieceLibraryCount(); + m_nPieces = pLib->GetPieceCount(); m_pPieces = (LC_LIBDLG_PIECEINFO*) malloc (sizeof (LC_LIBDLG_PIECEINFO) * m_nPieces); for (int i = 0; i < m_nPieces; i++) { LC_LIBDLG_PIECEINFO* inf = &m_pPieces[i]; - inf->info = project->GetPieceInfo(i); + inf->info = pLib->GetPieceInfo(i); inf->current_groups = inf->info->m_nGroups; idx.Seek (85, SEEK_CUR); @@ -157,7 +357,7 @@ void LibraryDialog::HandleCommand (int id) if (ReadLDrawPiece(filename, &piece)) { - if (project->FindPieceInfo(piece.name) != NULL) + if (project->GetPiecesLibrary ()->FindPieceInfo(piece.name) != NULL) Sys_MessageBox ("Piece already exists in the library !"); if (SaveLDrawPiece(&piece)) @@ -1202,11 +1402,12 @@ bool SaveLDrawPiece(LC_LDRAW_PIECE* piece) unsigned long i, j, cs, binoff = 0, delta; void* membuf; short scale, sb[6]; + PiecesLibrary *pLib = project->GetPiecesLibrary (); - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-b.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.bin"); rename(file2, file1); @@ -1214,10 +1415,10 @@ bool SaveLDrawPiece(LC_LDRAW_PIECE* piece) (!newbin.Open(file2, "wb"))) return false; - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-i.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.idx"); rename(file2, file1); @@ -1584,11 +1785,12 @@ bool DeletePiece(char** names, int numpieces) char file1[LC_MAXPATH], file2[LC_MAXPATH], tmp[200]; unsigned short count, deleted = 0, j; void* membuf; + PiecesLibrary *pLib = project->GetPiecesLibrary (); - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-b.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.bin"); rename(file2, file1); @@ -1596,10 +1798,10 @@ bool DeletePiece(char** names, int numpieces) (!newbin.Open(file2, "wb"))) return false; - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-i.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.idx"); rename(file2, file1); @@ -1698,6 +1900,7 @@ bool LoadUpdate(const char* update) unsigned long cs, group, binoff; unsigned char bt; void* membuf; + PiecesLibrary *pLib = project->GetPiecesLibrary (); typedef struct { @@ -1707,10 +1910,10 @@ bool LoadUpdate(const char* update) } LC_UPDATE_INFO; LC_UPDATE_INFO* upinfo; - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-b.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.bin"); rename(file2, file1); @@ -1718,10 +1921,10 @@ bool LoadUpdate(const char* update) (!newbin.Open(file2, "wb"))) return false; - strcpy(file1, project->GetLibraryPath()); + strcpy(file1, pLib->GetLibraryPath()); strcat(file1, "pieces-i.old"); remove(file1); - strcpy(file2, project->GetLibraryPath()); + strcpy(file2, pLib->GetLibraryPath()); strcat(file2, "pieces.idx"); rename(file2, file1); diff --git a/common/library.h b/common/library.h index e58a3dd..bf550de 100755 --- a/common/library.h +++ b/common/library.h @@ -1,6 +1,41 @@ #ifndef _LIBRARY_H_ #define _LIBRARY_H_ +#include "defines.h" + +class Texture; +class PieceInfo; + +class PiecesLibrary +{ +public: + PiecesLibrary (); + ~PiecesLibrary (); + + const char* GetLibraryPath() const + { return m_LibraryPath; } + int GetPieceCount () const + { return m_nPieceCount; } + + bool Load (const char* libpath); + void Unload (); + + PieceInfo* FindPieceInfo (const char* name) const; + PieceInfo* GetPieceInfo (int index) const; + int GetPieceIndex (PieceInfo *pInfo) const; + Texture* FindTexture (const char* name) const; + +protected: + char m_LibraryPath[LC_MAXPATH]; // path to the library files + + int m_nMovedCount; // number of moved pieces + char* m_pMovedReference; // moved pieces list + int m_nPieceCount; // number of pieces + PieceInfo* m_pPieceIdx; // pieces array + int m_nTextureCount; // number of textures + Texture* m_pTextures; // textures array +}; + #include "basewnd.h" typedef enum { diff --git a/common/minifig.cpp b/common/minifig.cpp index caaff6d..ff8337d 100644 --- a/common/minifig.cpp +++ b/common/minifig.cpp @@ -12,6 +12,7 @@ #include "project.h" #include "system.h" #include "matrix.h" +#include "library.h" // ============================================================================= // Static variables @@ -168,7 +169,7 @@ MinifigWizard::MinifigWizard (GLWindow *share) m_Colors[i] = colors[i]; m_Angles[i] = 0; - m_Info[i] = project->FindPieceInfo (pieces[i]); + m_Info[i] = project->GetPiecesLibrary ()->FindPieceInfo (pieces[i]); if (m_Info[i] != NULL) m_Info[i]->AddRef(); } @@ -612,7 +613,7 @@ void MinifigWizard::GetDescriptions (int type, char ***names, int *count) { PieceInfo* piece_info; - piece_info = project->FindPieceInfo (mfw_pieceinfo[i].name); + piece_info = project->GetPiecesLibrary ()->FindPieceInfo (mfw_pieceinfo[i].name); if (piece_info == NULL) continue; @@ -695,7 +696,7 @@ void MinifigWizard::ChangePiece (int type, const char *desc) { if (strcmp (desc, mfw_pieceinfo[j].description) == 0) { - piece_info = project->FindPieceInfo (mfw_pieceinfo[j].name); + piece_info = project->GetPiecesLibrary ()->FindPieceInfo (mfw_pieceinfo[j].name); if (piece_info == NULL) continue; @@ -807,7 +808,7 @@ bool MinifigWizard::LoadMinifig (const char* name) endptr = strchr (ptr, ' '); *endptr = '\0'; - m_Info[j] = project->FindPieceInfo (ptr); + m_Info[j] = project->GetPiecesLibrary ()->FindPieceInfo (ptr); *endptr = ' '; ptr = endptr; diff --git a/common/pieceinf.cpp b/common/pieceinf.cpp index 7ef5a25..e6f53ca 100644 --- a/common/pieceinf.cpp +++ b/common/pieceinf.cpp @@ -14,6 +14,7 @@ #include "vector.h" #include "defines.h" #include "config.h" +#include "library.h" #define SIDES 16 static float sintbl[SIDES]; @@ -319,7 +320,7 @@ void PieceInfo::LoadInformation() glEndList (); // Open pieces.bin and buffer the information we need. - strcpy (filename, project->GetLibraryPath()); + strcpy (filename, project->GetPiecesLibrary ()->GetLibraryPath()); strcat (filename, "pieces.bin"); if (!bin.Open (filename, "rb")) return; @@ -380,7 +381,7 @@ void PieceInfo::LoadInformation() bytes++; strcpy(name, (char*)bytes); - tex->texture = project->FindTexture(name); + tex->texture = project->GetPiecesLibrary()->FindTexture(name); shorts = (lcint16*)(bytes + 8); for (i = 0; i < 4; i++) diff --git a/common/project.cpp b/common/project.cpp index cf1d53c..ec958e2 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -26,6 +26,7 @@ #include "curve.h" #include "mainwnd.h" #include "view.h" +#include "library.h" // FIXME: temporary function, replace the code !!! void SystemUpdateFocus (void* p, int i) @@ -74,19 +75,15 @@ static LC_VIEWPORT viewports[14] = { Project::Project() { + int i; + m_bModified = false; m_bTrackCancel = false; m_nTracking = LC_TRACK_NONE; - m_pPieceIdx = NULL; - m_nPieceCount = 0; - m_pTextures = NULL; - m_nTextureCount = 0; m_pPieces = NULL; m_pCameras = NULL; m_pLights = NULL; m_pGroups = NULL; - m_pMovedReference = NULL; - m_nMovedCount = 0; m_pUndoList = NULL; m_pRedoList = NULL; m_nGridList = 0; @@ -98,12 +95,11 @@ Project::Project() m_nMouse = Sys_ProfileLoadInt ("Default", "Mouse", 11); strcpy(m_strModelsPath, Sys_ProfileLoadString ("Default", "Projects", "")); - if (messenger == NULL) - messenger = new Messenger (); - messenger->AddRef (); + if (messenger == NULL) + messenger = new Messenger (); + messenger->AddRef (); - int i; - for (i = 0; i < LC_CONNECTIONS; i++) + for (i = 0; i < LC_CONNECTIONS; i++) { m_pConnections[i].entries = NULL; m_pConnections[i].numentries = 0; @@ -111,6 +107,8 @@ Project::Project() for (i = 0; i < 10; i++) m_pClipboard[i] = NULL; + + m_pLibrary = new PiecesLibrary (); } Project::~Project() @@ -124,25 +122,6 @@ Project::~Project() m_pTrackFile = NULL; } - if (m_pPieceIdx != NULL) - { - PieceInfo* pInfo; - for (pInfo = m_pPieceIdx; m_nPieceCount--; pInfo++) - pInfo->~PieceInfo(); - delete [] m_pPieceIdx; - } - - if (m_pTextures != NULL) - { - Texture* pTexture; - for (pTexture = m_pTextures; m_nTextureCount--; pTexture++) - pTexture->~Texture(); - delete [] m_pTextures; - } - - if (m_pMovedReference != NULL) - free(m_pMovedReference); - for (int i = 0; i < 10; i++) if (m_pClipboard[i] != NULL) delete m_pClipboard[i]; @@ -151,6 +130,7 @@ Project::~Project() delete m_pTerrain; delete m_pBackground; + delete m_pLibrary; } @@ -290,11 +270,11 @@ bool Project::Initialize(int argc, char *argv[], char* binpath, char* libpath) // if the user specified a library, try to load it first if (libpath != NULL) - loaded = LoadPieceLibrary (libpath); + loaded = m_pLibrary->Load (libpath); // if we couldn't find a library, try the executable path if (!loaded) - loaded = LoadPieceLibrary (binpath); + loaded = m_pLibrary->Load (binpath); if (!loaded) { @@ -430,118 +410,6 @@ bool Project::Initialize(int argc, char *argv[], char* binpath, char* libpath) return true; } -// Load the piece library -bool Project::LoadPieceLibrary (char *libpath) -{ - FileDisk idx; - char filename[LC_MAXPATH]; - lcuint8 version; - lcuint16 count, movedcount; - lcuint32 binsize; - Texture* pTexture; - int i; - - strcpy (m_LibraryPath, libpath); - - // Make sure that the path ends with a '/' - i = strlen(m_LibraryPath)-1; - if ((m_LibraryPath[i] != '\\') && (m_LibraryPath[i] != '/')) - strcat(m_LibraryPath, "/"); - - // Read the piece library index. - strcpy (filename, m_LibraryPath); - strcat (filename, "pieces.idx"); - - if (!idx.Open (filename, "rb")) - return false; - - idx.Seek (-(long)(2*sizeof(count)+sizeof(binsize)), SEEK_END); - idx.ReadShort (&movedcount, 1); - idx.ReadLong (&binsize, 1); - idx.ReadShort (&count, 1); - idx.Seek (32, SEEK_SET); - idx.ReadByte (&version, 1); - - if ((version != 3) || (count == 0)) - { - idx.Close(); - return false; - } - idx.Seek (34, SEEK_SET); // skip update byte - - // TODO: check .bin file size. - - // Load piece indexes - delete [] m_pPieceIdx; - m_pPieceIdx = new PieceInfo[count]; - m_nPieceCount = count; - - // workaround for VC++ error C2538: new : cannot specify initializer for arrays - for (PieceInfo *pElements = m_pPieceIdx; count--; pElements++) - pElements->LoadIndex (idx); - - // Load moved files reference. - if (m_pMovedReference != NULL) - free(m_pMovedReference); - m_pMovedReference = (char*)malloc(18*movedcount); - memset (m_pMovedReference, 0, 18*movedcount); - m_nMovedCount = movedcount; - - for (i = 0; i < movedcount; i++) - { - idx.Read (&m_pMovedReference[i*18], 8); - idx.Read (&m_pMovedReference[i*18+9], 8); - } - - idx.Close(); - - // TODO: Load group configuration here - - // Read the texture index. - strcpy(filename, m_LibraryPath); - strcat(filename, "textures.idx"); - - if (m_pTextures != NULL) - { - // call the destructors - // for (pTexture = m_pTextures; m_nTextureCount--; pTexture++) - // pTexture->~Texture(); - delete [] m_pTextures; - - m_pTextures = NULL; - m_nTextureCount = 0; - } - - if (!idx.Open(filename, "rb")) - return false; - - idx.Seek(-(long)(sizeof(count)+sizeof(binsize)), SEEK_END); - idx.ReadLong (&binsize, 1); - idx.ReadShort (&count, 1); - idx.Seek(32, SEEK_SET); - idx.ReadByte (&version, 1); - - if ((version != 1) || (count == 0)) - { - idx.Close(); - return false; - } - idx.Seek(34, SEEK_SET); // skip update byte - - // TODO: check .bin file size. - - m_pTextures = new Texture[count]; - m_nTextureCount = count; - memset(m_pTextures, 0, count * sizeof(Texture)); - - for (pTexture = m_pTextures; count--; pTexture++) - pTexture->LoadIndex(&idx); - - idx.Close(); - - return true; -} - void Project::SetTitle(const char* lpszTitle) { strcpy(m_strTitle, lpszTitle); @@ -839,7 +707,7 @@ bool Project::FileLoad(File* file, bool bUndo, bool bMerge) char name[9]; Piece* pPiece = new Piece(NULL); pPiece->FileLoad(*file, name); - PieceInfo* pInfo = FindPieceInfo(name); + PieceInfo* pInfo = m_pLibrary->FindPieceInfo(name); if (pInfo) { pPiece->SetPieceInfo(pInfo); @@ -878,7 +746,7 @@ bool Project::FileLoad(File* file, bool bUndo, bool bMerge) const unsigned char conv[20] = { 0,2,4,9,7,6,22,8,10,11,14,16,18,9,21,20,22,8,10,11 }; color = conv[color]; - PieceInfo* pInfo = FindPieceInfo(name); + PieceInfo* pInfo = m_pLibrary->FindPieceInfo(name); if (pInfo != NULL) { Piece* pPiece = new Piece(pInfo); @@ -1369,7 +1237,7 @@ void Project::FileReadLDraw(File* file, Matrix* prevmat, int* nOk, int DefColor, char name[9]; strcpy(name, tmp); - PieceInfo* pInfo = FindPieceInfo(name); + PieceInfo* pInfo = m_pLibrary->FindPieceInfo(name); if (pInfo != NULL) { float x, y, z, rot[4]; @@ -2055,7 +1923,7 @@ void Project::RenderScene(bool bShaded, bool bDrawViewports) m.SetTranslation(0,0,0); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - m_pTextures[0].MakeCurrent(); + m_ScreenFont.MakeCurrent(); glEnable(GL_TEXTURE_2D); glEnable(GL_ALPHA_TEST); @@ -2442,7 +2310,7 @@ void Project::RenderViewports(bool bBackground, bool bLines) if (!bBackground) glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - m_pTextures[0].MakeCurrent(); + m_ScreenFont.MakeCurrent(); glEnable(GL_ALPHA_TEST); glBegin(GL_QUADS); @@ -2597,7 +2465,7 @@ void Project::RenderInitialize() char filename[LC_MAXPATH]; FileDisk file; - strcpy (filename, m_LibraryPath); + strcpy (filename, m_pLibrary->GetLibraryPath ()); strcat (filename, "sysfont.txf"); if (file.Open (filename, "rb")) @@ -2994,12 +2862,12 @@ void Project::CreateHTMLPieceList(FILE* f, int nStep, bool bImages, char* ext) fputs("\n",f); PieceInfo* pInfo; - for (int j = 0; j < m_nPieceCount; j++) + for (int j = 0; j < m_pLibrary->GetPieceCount (); j++) { bool Add = false; int count[LC_MAXCOLORS]; memset (&count, 0, sizeof (count)); - pInfo = &m_pPieceIdx[j]; + pInfo = m_pLibrary->GetPieceInfo (j); for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) { @@ -3613,10 +3481,10 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) PieceInfo* pInfo; Piece* pPiece; FILE* f; - char *conv = (char*)malloc (9*m_nPieceCount); - char *flags = (char*)malloc (m_nPieceCount); - memset (conv, 0, 9*m_nPieceCount); - memset (flags, 0, m_nPieceCount); + char *conv = (char*)malloc (9*m_pLibrary->GetPieceCount ()); + char *flags = (char*)malloc (m_pLibrary->GetPieceCount ()); + memset (conv, 0, 9*m_pLibrary->GetPieceCount()); + memset (flags, 0, m_pLibrary->GetPieceCount()); // read LGEO conversion table if (strlen (opts.libpath)) @@ -3639,7 +3507,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) u = (((unsigned char)(bt[3])|((unsigned short)(bt[2]) << 8))| (((unsigned long)(bt[1])) << 16)) + bt[0] * 16581375; sprintf(tmp, "%d", (int)u); - pInfo = FindPieceInfo(tmp); + pInfo = m_pLibrary->FindPieceInfo(tmp); fread(&tmp, 9, 1, f); if (tmp[8] != 0) @@ -3647,9 +3515,9 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) if (pInfo != NULL) { - int idx = (((char*)pInfo - (char*)m_pPieceIdx)/sizeof(PieceInfo)); - memcpy (&conv[idx*9], &tmp[1], 9); - flags[idx] = tmp[0]; + int idx = m_pLibrary->GetPieceIndex (pInfo); + memcpy (&conv[idx*9], &tmp[1], 9); + flags[idx] = tmp[0]; } } fclose (f); @@ -3672,12 +3540,12 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) if ((tmp[u] == 0) && (u != 0)) { u = 0; - pInfo = FindPieceInfo(tmp); + pInfo = m_pLibrary->FindPieceInfo(tmp); fread(&tmp, 8, 1, f); if (pInfo != NULL) { - int idx = (((char*)pInfo - (char*)m_pPieceIdx)/sizeof(PieceInfo)); + int idx = m_pLibrary->GetPieceIndex (pInfo); memcpy(&conv[idx*9], tmp, 9); } } @@ -3709,7 +3577,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) if (pNext == pPiece) { - int idx = (((char*)pInfo - (char*)m_pPieceIdx)/sizeof(PieceInfo)); + int idx = m_pLibrary->GetPieceIndex (pInfo); char pat[] = "patterns/"; if (conv[idx*9+1] != 'p') strcpy(pat, ""); @@ -3752,7 +3620,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) { pInfo = pPiece->GetPieceInfo(); - int idx = (((char*)pInfo - (char*)m_pPieceIdx)/sizeof(PieceInfo)); + int idx = m_pLibrary->GetPieceIndex (pInfo); if (conv[idx*9] != 0) continue; @@ -3907,7 +3775,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) { float fl[12], pos[3], rot[4]; char name[20]; - int idx = (((char*)pPiece->GetPieceInfo() - (char*)m_pPieceIdx)/sizeof(PieceInfo)); + int idx = m_pLibrary->GetPieceIndex (pPiece->GetPieceInfo ()); if (conv[idx*9] == 0) { @@ -4058,16 +3926,16 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) strcpy(opts.strComments, m_strComments); opts.strFilename = m_strPathName; - opts.lines = m_nPieceCount; - opts.count = (unsigned short*)malloc(m_nPieceCount*LC_MAXCOLORS*sizeof(unsigned short)); - memset (opts.count, 0, m_nPieceCount*LC_MAXCOLORS*sizeof(unsigned short)); - opts.names = (char**)malloc(m_nPieceCount*sizeof(char*)); - for (int i = 0; i < m_nPieceCount; i++) - opts.names[i] = m_pPieceIdx[i].m_strDescription; + opts.lines = m_pLibrary->GetPieceCount(); + opts.count = (unsigned short*)malloc(m_pLibrary->GetPieceCount()*LC_MAXCOLORS*sizeof(unsigned short)); + memset (opts.count, 0, m_pLibrary->GetPieceCount()*LC_MAXCOLORS*sizeof(unsigned short)); + opts.names = (char**)malloc(m_pLibrary->GetPieceCount()*sizeof(char*)); + for (int i = 0; i < m_pLibrary->GetPieceCount(); i++) + opts.names[i] = m_pLibrary->GetPieceInfo (i)->m_strDescription; for (Piece* pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) { - int idx = (((char*)pPiece->GetPieceInfo() - (char*)m_pPieceIdx)/sizeof(PieceInfo)); + int idx = m_pLibrary->GetPieceIndex (pPiece->GetPieceInfo ()); opts.count[idx*LC_MAXCOLORS+pPiece->GetColor()]++; } @@ -4295,7 +4163,7 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) char name[9]; Piece* pPiece = new Piece(NULL); pPiece->FileLoad(*file, name); - PieceInfo* pInfo = FindPieceInfo(name); + PieceInfo* pInfo = m_pLibrary->FindPieceInfo(name); if (pInfo) { pPiece->SetPieceInfo(pInfo); @@ -5879,11 +5747,6 @@ void Project::SelectAndFocusNone(bool bFocusOnly) // AfxGetMainWnd()->PostMessage(WM_LC_UPDATE_INFO, NULL, OT_PIECE); } -PieceInfo* Project::GetPieceInfo(int index) -{ - return &m_pPieceIdx[index]; -} - Camera* Project::GetCamera(int i) { Camera* pCamera; @@ -5943,40 +5806,6 @@ void Project::GetFocusPosition(float* pos) pos[0] = pos[1] = pos[2] = 0.0f; } -Texture* Project::FindTexture (const char* name) -{ - for (int i = 0; i < m_nTextureCount; i++) - if (!strcmp (name, m_pTextures[i].m_strName)) - return &m_pTextures[i]; - - return NULL; -} - -// Remeber to make 'name' uppercase. -PieceInfo* Project::FindPieceInfo (const char* name) const -{ - PieceInfo* pInfo; - int i; - - for (i = 0, pInfo = m_pPieceIdx; i < m_nPieceCount; i++, pInfo++) - if (!strcmp (name, pInfo->m_strName)) - return pInfo; - - for (i = 0; i < m_nMovedCount; i++) - if (!strcmp(&m_pMovedReference[i*18], name)) - { - char* tmp = &m_pMovedReference[i*18+9]; - - for (i = 0, pInfo = m_pPieceIdx; i < m_nPieceCount; i++, pInfo++) - if (!strcmp (tmp, pInfo->m_strName)) - return pInfo; - - break; // something went wrong. - } - - return NULL; -} - void Project::FindObjectFromPoint(int x, int y, LC_CLICKLINE* pLine) { GLdouble px, py, pz, rx, ry, rz; @@ -6270,12 +6099,12 @@ void Project::SnapPoint (float *point, float *reminder) const if (m_nSnap & LC_DRAW_SNAP_Z) { - i = (int)(point[2]/0.4f); + i = (int)(point[2]/0.32f); if (reminder != NULL) - reminder[2] = point[2] - (0.4f * i); + reminder[2] = point[2] - (0.32f * i); - point[2] = 0.4f * i; + point[2] = 0.32f * i; } } diff --git a/common/project.h b/common/project.h index 95e26f3..e7ce472 100644 --- a/common/project.h +++ b/common/project.h @@ -24,6 +24,7 @@ class PieceInfo; class Matrix; class View; class Image; +class PiecesLibrary; // Undo support @@ -58,10 +59,6 @@ public: { return m_bAnimation; } unsigned short GetCurrentTime () { return m_bAnimation ? m_nCurFrame : m_nCurStep; } - int GetPieceLibraryCount() - { return m_nPieceCount; } - const char* GetLibraryPath() - { return m_LibraryPath; } void SetCurrentPiece(PieceInfo* pInfo) { m_pCurPiece = pInfo; } int GetCurrentColor () const @@ -71,7 +68,6 @@ public: float* GetBackgroundColor() { return m_fBackground; } Camera* GetCamera(int i); - PieceInfo* GetPieceInfo(int index); void GetTimeRange(int* from, int* to) { *from = m_bAnimation ? m_nCurFrame : m_nCurStep; @@ -87,6 +83,9 @@ public: *ppLight = m_pLights; } + PiecesLibrary* GetPiecesLibrary () const + { return m_pLibrary; } + void SetPathName (const char* lpszPathName, bool bAddToMRU); void SetTitle (const char* lpszTitle); @@ -97,8 +96,6 @@ public: void Render(bool bToMemory); void SetViewSize(int cx, int cy); - Texture* FindTexture (const char* name); - PieceInfo* FindPieceInfo (const char* name) const; void CheckAutoSave(); void GetFocusPosition(float* pos); Group* AddGroup (const char* name, Group* pParent, float x, float y, float z); @@ -121,16 +118,9 @@ protected: char m_strComments[256]; // Piece library - bool LoadPieceLibrary (char* libpath); - char m_LibraryPath[LC_MAXPATH]; // path to the library files char m_AppPath[LC_MAXPATH]; // path to the LeoCAD executable - int m_nPieceCount; // number of pieces - PieceInfo* m_pPieceIdx; // index - int m_nTextureCount; - Texture* m_pTextures; TexFont m_ScreenFont; - char* m_pMovedReference; - int m_nMovedCount; + PiecesLibrary* m_pLibrary; // Undo support LC_UNDOINFO* m_pUndoList; diff --git a/common/texfont.cpp b/common/texfont.cpp index 252b72b..fa3fe0f 100644 --- a/common/texfont.cpp +++ b/common/texfont.cpp @@ -6,6 +6,7 @@ #include "project.h" #include "texfont.h" #include "texture.h" +#include "library.h" #include "file.h" #define LC_TEXFONT_FILE_VERSION 1 // LeoCAD 0.74 @@ -55,7 +56,7 @@ bool TexFont::FileLoad (File& file) memset (buf, 0, 32); file.Read (buf, 8); - m_pTexture = project->FindTexture (buf); + m_pTexture = project->GetPiecesLibrary()->FindTexture (buf); if (m_pTexture == NULL) { console.PrintError ("Cannot find texture for font %s.\n", buf); diff --git a/common/texfont.h b/common/texfont.h index e3dc185..f5b190c 100644 --- a/common/texfont.h +++ b/common/texfont.h @@ -4,6 +4,8 @@ class File; class Texture; +#include "texture.h" + class TexFont { public: @@ -12,6 +14,8 @@ public: bool IsLoaded () const { return m_bLoaded; } + void MakeCurrent () + { if (m_bLoaded) m_pTexture->MakeCurrent (); } bool FileLoad (File& file); void PrintText (float left, float top, const char* text) const; diff --git a/common/texture.cpp b/common/texture.cpp index 0f31195..4957a0e 100644 --- a/common/texture.cpp +++ b/common/texture.cpp @@ -9,6 +9,7 @@ #include "project.h" #include "globals.h" #include "image.h" +#include "library.h" // ============================================================================= // Static functions @@ -130,7 +131,7 @@ void Texture::Load(bool bFilter) FileDisk bin; void* bits; - strcpy(filename, project->GetLibraryPath()); + strcpy(filename, project->GetPiecesLibrary ()->GetLibraryPath()); strcat(filename, "textures.bin"); if (!bin.Open(filename, "rb")) return; -- cgit v1.2.3