Array cleanup.

This commit is contained in:
Leonardo Zide 2019-07-04 17:06:26 -07:00
parent 3d9395e175
commit 4493c3f6f2
7 changed files with 59 additions and 58 deletions

View file

@ -3,7 +3,7 @@
#include "lc_file.h" #include "lc_file.h"
#include <float.h> #include <float.h>
lcArray<lcColor> gColorList; std::vector<lcColor> gColorList;
lcColorGroup gColorGroups[LC_NUM_COLORGROUPS]; lcColorGroup gColorGroups[LC_NUM_COLORGROUPS];
int gNumUserColors; int gNumUserColors;
int gEdgeColor; int gEdgeColor;
@ -184,13 +184,13 @@ int lcGetBrickLinkColor(int ColorIndex)
bool lcLoadColorFile(lcFile& File) bool lcLoadColorFile(lcFile& File)
{ {
char Line[1024], Token[1024]; char Line[1024], Token[1024];
lcArray<lcColor>& Colors = gColorList; std::vector<lcColor>& Colors = gColorList;
lcColor Color, MainColor, EdgeColor; lcColor Color, MainColor, EdgeColor;
Colors.RemoveAll(); Colors.clear();
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++)
gColorGroups[GroupIdx].Colors.RemoveAll(); gColorGroups[GroupIdx].Colors.clear();
gColorGroups[0].Name = QApplication::tr("Solid Colors", "Colors"); gColorGroups[0].Name = QApplication::tr("Solid Colors", "Colors");
gColorGroups[1].Name = QApplication::tr("Translucent Colors", "Colors"); gColorGroups[1].Name = QApplication::tr("Translucent Colors", "Colors");
@ -335,11 +335,11 @@ bool lcLoadColorFile(lcFile& File)
bool Duplicate = false; bool Duplicate = false;
for (int i = 0; i < Colors.GetSize(); i++) for (lcColor& ExistingColor : Colors)
{ {
if (Colors[i].Code == Color.Code) if (ExistingColor.Code == Color.Code)
{ {
Colors[i] = Color; ExistingColor = Color;
Duplicate = true; Duplicate = true;
break; break;
} }
@ -360,26 +360,26 @@ bool lcLoadColorFile(lcFile& File)
continue; continue;
} }
Colors.Add(Color); Colors.push_back(Color);
if (GroupSpecial) if (GroupSpecial)
gColorGroups[LC_COLORGROUP_SPECIAL].Colors.Add(Colors.GetSize() - 1); gColorGroups[LC_COLORGROUP_SPECIAL].Colors.push_back((int)Colors.size() - 1);
else if (GroupTranslucent) else if (GroupTranslucent)
gColorGroups[LC_COLORGROUP_TRANSLUCENT].Colors.Add(Colors.GetSize() - 1); gColorGroups[LC_COLORGROUP_TRANSLUCENT].Colors.push_back((int)Colors.size() - 1);
else else
gColorGroups[LC_COLORGROUP_SOLID].Colors.Add(Colors.GetSize() - 1); gColorGroups[LC_COLORGROUP_SOLID].Colors.push_back((int)Colors.size() - 1);
} }
gDefaultColor = Colors.GetSize(); gDefaultColor = (int)Colors.size();
Colors.Add(MainColor); Colors.push_back(MainColor);
gColorGroups[LC_COLORGROUP_SOLID].Colors.Add(gDefaultColor); gColorGroups[LC_COLORGROUP_SOLID].Colors.push_back(gDefaultColor);
gNumUserColors = Colors.GetSize(); gNumUserColors = (int)Colors.size();
gEdgeColor = Colors.GetSize(); gEdgeColor = (int)Colors.size();
Colors.Add(EdgeColor); Colors.push_back(EdgeColor);
return Colors.GetSize() > 2; return Colors.size() > 2;
} }
void lcLoadDefaultColors() void lcLoadDefaultColors()
@ -405,7 +405,7 @@ void lcLoadDefaultColors()
int lcGetColorIndex(quint32 ColorCode) int lcGetColorIndex(quint32 ColorCode)
{ {
for (int ColorIdx = 0; ColorIdx < gColorList.GetSize(); ColorIdx++) for (int ColorIdx = 0; ColorIdx < gColorList.size(); ColorIdx++)
if (gColorList[ColorIdx].Code == ColorCode) if (gColorList[ColorIdx].Code == ColorCode)
return ColorIdx; return ColorIdx;
@ -437,6 +437,6 @@ int lcGetColorIndex(quint32 ColorCode)
sprintf(Color.SafeName, "Color_%03d", ColorCode); sprintf(Color.SafeName, "Color_%03d", ColorCode);
} }
gColorList.Add(Color); gColorList.push_back(Color);
return gColorList.GetSize() - 1; return (int)gColorList.size() - 1;
} }

View file

@ -26,7 +26,7 @@ enum
struct lcColorGroup struct lcColorGroup
{ {
lcArray<int> Colors; std::vector<int> Colors;
QString Name; QString Name;
}; };
@ -44,7 +44,7 @@ enum lcInterfaceColor
}; };
extern lcVector4 gInterfaceColors[LC_NUM_INTERFACECOLORS]; extern lcVector4 gInterfaceColors[LC_NUM_INTERFACECOLORS];
extern lcArray<lcColor> gColorList; extern std::vector<lcColor> gColorList;
extern lcColorGroup gColorGroups[LC_NUM_COLORGROUPS]; extern lcColorGroup gColorGroups[LC_NUM_COLORGROUPS];
extern int gNumUserColors; extern int gNumUserColors;
extern int gEdgeColor; extern int gEdgeColor;

View file

@ -125,9 +125,9 @@ void lcPiecesLibrary::Unload()
delete PrimitiveIt.second; delete PrimitiveIt.second;
mPrimitives.clear(); mPrimitives.clear();
for (int TextureIdx = 0; TextureIdx < mTextures.GetSize(); TextureIdx++) for (lcTexture* Texture : mTextures)
delete mTextures[TextureIdx]; delete Texture;
mTextures.RemoveAll(); mTextures.clear();
mNumOfficialPieces = 0; mNumOfficialPieces = 0;
delete mZipFiles[LC_ZIPFILE_OFFICIAL]; delete mZipFiles[LC_ZIPFILE_OFFICIAL];
@ -266,9 +266,9 @@ PieceInfo* lcPiecesLibrary::FindPiece(const char* PieceName, Project* CurrentPro
lcTexture* lcPiecesLibrary::FindTexture(const char* TextureName, Project* CurrentProject, bool SearchProjectFolder) lcTexture* lcPiecesLibrary::FindTexture(const char* TextureName, Project* CurrentProject, bool SearchProjectFolder)
{ {
for (int TextureIdx = 0; TextureIdx < mTextures.GetSize(); TextureIdx++) for (lcTexture* Texture : mTextures)
if (!strcmp(TextureName, mTextures[TextureIdx]->mName)) if (!strcmp(TextureName, Texture->mName))
return mTextures[TextureIdx]; return Texture;
QString ProjectPath; QString ProjectPath;
if (SearchProjectFolder) if (SearchProjectFolder)
@ -289,7 +289,7 @@ lcTexture* lcPiecesLibrary::FindTexture(const char* TextureName, Project* Curren
if (Texture) if (Texture)
{ {
mTextures.Add(Texture); mTextures.push_back(Texture);
return Texture; return Texture;
} }
} }
@ -423,7 +423,7 @@ bool lcPiecesLibrary::OpenArchive(lcFile* File, const QString& FileName, lcZipFi
(ZipFileType == LC_ZIPFILE_UNOFFICIAL && !memcmp(Name, "PARTS/TEXTURES/", 15))) (ZipFileType == LC_ZIPFILE_UNOFFICIAL && !memcmp(Name, "PARTS/TEXTURES/", 15)))
{ {
lcTexture* Texture = new lcTexture(); lcTexture* Texture = new lcTexture();
mTextures.Add(Texture); mTextures.push_back(Texture);
*Dst = 0; *Dst = 0;
strncpy(Texture->mName, Name + (ZipFileType == LC_ZIPFILE_OFFICIAL ? 21 : 15), sizeof(Texture->mName)); strncpy(Texture->mName, Name + (ZipFileType == LC_ZIPFILE_OFFICIAL ? 21 : 15), sizeof(Texture->mName));
@ -623,7 +623,7 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress)
QDir Dir(LibraryDir.absoluteFilePath(QLatin1String("parts/textures/")), QLatin1String("*.png"), QDir::SortFlags(QDir::Name | QDir::IgnoreCase), QDir::Files | QDir::Hidden | QDir::Readable); QDir Dir(LibraryDir.absoluteFilePath(QLatin1String("parts/textures/")), QLatin1String("*.png"), QDir::SortFlags(QDir::Name | QDir::IgnoreCase), QDir::Files | QDir::Hidden | QDir::Readable);
QStringList FileList = Dir.entryList(); QStringList FileList = Dir.entryList();
mTextures.AllocGrow(FileList.size()); mTextures.reserve(FileList.size());
for (int FileIdx = 0; FileIdx < FileList.size(); FileIdx++) for (int FileIdx = 0; FileIdx < FileList.size(); FileIdx++)
{ {
@ -654,7 +654,7 @@ bool lcPiecesLibrary::OpenDirectory(const QDir& LibraryDir, bool ShowProgress)
*Dst = 0; *Dst = 0;
lcTexture* Texture = new lcTexture(); lcTexture* Texture = new lcTexture();
mTextures.Add(Texture); mTextures.push_back(Texture);
strncpy(Texture->mName, Name, sizeof(Texture->mName)); strncpy(Texture->mName, Name, sizeof(Texture->mName));
Texture->mName[sizeof(Texture->mName) - 1] = 0; Texture->mName[sizeof(Texture->mName) - 1] = 0;
@ -1884,7 +1884,9 @@ void lcPiecesLibrary::ReleaseTexture(lcTexture* Texture)
if (Texture->Release() == 0 && Texture->IsTemporary()) if (Texture->Release() == 0 && Texture->IsTemporary())
{ {
mTextures.Remove(Texture); std::vector<lcTexture*>::iterator TextureIt = std::find(mTextures.begin(), mTextures.end(), Texture);
if (TextureIt != mTextures.end())
mTextures.erase(TextureIt);
delete Texture; delete Texture;
} }
} }

View file

@ -248,7 +248,7 @@ public:
std::map<std::string, lcLibraryPrimitive*> mPrimitives; std::map<std::string, lcLibraryPrimitive*> mPrimitives;
int mNumOfficialPieces; int mNumOfficialPieces;
lcArray<lcTexture*> mTextures; std::vector<lcTexture*> mTextures;
QDir mLibraryDir; QDir mLibraryDir;

View file

@ -1506,13 +1506,13 @@ void lcModel::SaveCheckpoint(const QString& Description)
void lcModel::LoadCheckPoint(lcModelHistoryEntry* CheckPoint) void lcModel::LoadCheckPoint(lcModelHistoryEntry* CheckPoint)
{ {
lcPiecesLibrary* Library = lcGetPiecesLibrary(); lcPiecesLibrary* Library = lcGetPiecesLibrary();
lcArray<PieceInfo*> Infos; std::vector<PieceInfo*> LoadedInfos;
for (lcPiece* Piece : mPieces) for (lcPiece* Piece : mPieces)
{ {
PieceInfo* Info = Piece->mPieceInfo; PieceInfo* Info = Piece->mPieceInfo;
Library->LoadPieceInfo(Info, true, true); Library->LoadPieceInfo(Info, true, true);
Infos.Add(Info); LoadedInfos.push_back(Info);
} }
DeleteModel(); DeleteModel();
@ -1527,8 +1527,8 @@ void lcModel::LoadCheckPoint(lcModelHistoryEntry* CheckPoint)
gMainWindow->UpdateSelectedObjects(true); gMainWindow->UpdateSelectedObjects(true);
gMainWindow->UpdateAllViews(); gMainWindow->UpdateAllViews();
for (int InfoIdx = 0; InfoIdx < Infos.GetSize(); InfoIdx++) for (PieceInfo* Info : LoadedInfos)
Library->ReleasePieceInfo(Infos[InfoIdx]); Library->ReleasePieceInfo(Info);
} }
void lcModel::SetActive(bool Active) void lcModel::SetActive(bool Active)

View file

@ -713,7 +713,7 @@ void Project::Export3DStudio(const QString& FileName)
const int MaterialNameLength = 11; const int MaterialNameLength = 11;
char MaterialName[32]; char MaterialName[32];
for (int ColorIdx = 0; ColorIdx < gColorList.GetSize(); ColorIdx++) for (int ColorIdx = 0; ColorIdx < gColorList.size(); ColorIdx++)
{ {
lcColor* Color = &gColorList[ColorIdx]; lcColor* Color = &gColorList[ColorIdx];
@ -1685,7 +1685,7 @@ QImage Project::CreatePartsListImage(lcModel* Model, lcStep Step)
void Project::CreateHTMLPieceList(QTextStream& Stream, lcModel* Model, lcStep Step, bool Images) void Project::CreateHTMLPieceList(QTextStream& Stream, lcModel* Model, lcStep Step, bool Images)
{ {
std::vector<int> ColorsUsed(gColorList.GetSize(), 0); std::vector<int> ColorsUsed(gColorList.size(), 0);
int NumColors = 0; int NumColors = 0;
lcPartsList PartsList; lcPartsList PartsList;
@ -1701,7 +1701,7 @@ void Project::CreateHTMLPieceList(QTextStream& Stream, lcModel* Model, lcStep St
Stream << QLatin1String("<br><table border=1><tr><td><center>Piece</center></td>\r\n"); Stream << QLatin1String("<br><table border=1><tr><td><center>Piece</center></td>\r\n");
for (int ColorIdx = 0; ColorIdx < gColorList.GetSize(); ColorIdx++) for (int ColorIdx = 0; ColorIdx < gColorList.size(); ColorIdx++)
{ {
if (ColorsUsed[ColorIdx]) if (ColorsUsed[ColorIdx])
{ {
@ -2060,7 +2060,7 @@ bool Project::ExportPOVRay(const QString& FileName)
lcPiecesLibrary* Library = lcGetPiecesLibrary(); lcPiecesLibrary* Library = lcGetPiecesLibrary();
std::map<const PieceInfo*, std::pair<char[LC_PIECE_NAME_LEN], int>> PieceTable; std::map<const PieceInfo*, std::pair<char[LC_PIECE_NAME_LEN], int>> PieceTable;
int NumColors = gColorList.GetSize(); size_t NumColors = gColorList.size();
std::vector<std::array<char, LC_MAX_COLOR_NAME>> ColorTable(NumColors); std::vector<std::array<char, LC_MAX_COLOR_NAME>> ColorTable(NumColors);
enum enum
@ -2192,7 +2192,7 @@ bool Project::ExportPOVRay(const QString& FileName)
POVFile.WriteLine("\n"); POVFile.WriteLine("\n");
} }
for (int ColorIdx = 0; ColorIdx < gColorList.GetSize(); ColorIdx++) for (int ColorIdx = 0; ColorIdx < gColorList.size(); ColorIdx++)
{ {
lcColor* Color = &gColorList[ColorIdx]; lcColor* Color = &gColorList[ColorIdx];
@ -2391,13 +2391,12 @@ void Project::ExportWavefront(const QString& FileName)
} }
MaterialFile.WriteLine("# Colors used by LeoCAD\n\n"); MaterialFile.WriteLine("# Colors used by LeoCAD\n\n");
for (int ColorIdx = 0; ColorIdx < gColorList.GetSize(); ColorIdx++) for (const lcColor& Color : gColorList)
{ {
lcColor* Color = &gColorList[ColorIdx]; if (Color.Translucent)
if (Color->Translucent) sprintf(Line, "newmtl %s\nKd %.2f %.2f %.2f\nD %.2f\n\n", Color.SafeName, Color.Value[0], Color.Value[1], Color.Value[2], Color.Value[3]);
sprintf(Line, "newmtl %s\nKd %.2f %.2f %.2f\nD %.2f\n\n", Color->SafeName, Color->Value[0], Color->Value[1], Color->Value[2], Color->Value[3]);
else else
sprintf(Line, "newmtl %s\nKd %.2f %.2f %.2f\n\n", Color->SafeName, Color->Value[0], Color->Value[1], Color->Value[2]); sprintf(Line, "newmtl %s\nKd %.2f %.2f %.2f\n\n", Color.SafeName, Color.Value[0], Color.Value[1], Color.Value[2]);
MaterialFile.WriteLine(Line); MaterialFile.WriteLine(Line);
} }

View file

@ -18,10 +18,10 @@ lcQColorList::lcQColorList(QWidget *parent)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx]; lcColorGroup* Group = &gColorGroups[GroupIdx];
for (int ColorIdx = 0; ColorIdx < Group->Colors.GetSize(); ColorIdx++) for (int Color: Group->Colors)
mCellColors[mNumCells++] = Group->Colors[ColorIdx]; mCellColors[mNumCells++] = Color;
mRows += (Group->Colors.GetSize() + mColumns - 1) / mColumns; mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
} }
mWidth = 0; mWidth = 0;
@ -187,7 +187,7 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
for (CurGroup = 0; CurGroup < LC_NUM_COLORGROUPS; CurGroup++) for (CurGroup = 0; CurGroup < LC_NUM_COLORGROUPS; CurGroup++)
{ {
int NumColors = gColorGroups[CurGroup].Colors.GetSize(); int NumColors = (int)gColorGroups[CurGroup].Colors.size();
if (mCurCell < NumCells + NumColors) if (mCurCell < NumCells + NumColors)
break; break;
@ -204,7 +204,7 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
NewCell = mCurCell - mColumns; NewCell = mCurCell - mColumns;
else if (CurGroup > 0) else if (CurGroup > 0)
{ {
int NumColors = gColorGroups[CurGroup - 1].Colors.GetSize(); size_t NumColors = gColorGroups[CurGroup - 1].Colors.size();
int NumColumns = NumColors % mColumns; int NumColumns = NumColors % mColumns;
if (NumColumns <= Column + 1) if (NumColumns <= Column + 1)
@ -215,7 +215,7 @@ void lcQColorList::keyPressEvent(QKeyEvent *event)
} }
else if (event->key() == Qt::Key_Down) else if (event->key() == Qt::Key_Down)
{ {
int NumColors = gColorGroups[CurGroup].Colors.GetSize(); size_t NumColors = gColorGroups[CurGroup].Colors.size();
if (mCurCell + mColumns < NumCells + NumColors) if (mCurCell + mColumns < NumCells + NumColors)
NewCell = mCurCell + mColumns; NewCell = mCurCell + mColumns;
@ -274,7 +274,7 @@ void lcQColorList::resizeEvent(QResizeEvent *event)
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx]; lcColorGroup* Group = &gColorGroups[GroupIdx];
mRows += (Group->Colors.GetSize() + mColumns - 1) / mColumns; mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
} }
CellWidth = (float)(width() + 1) / (float)mColumns; CellWidth = (float)(width() + 1) / (float)mColumns;
@ -292,7 +292,7 @@ void lcQColorList::resizeEvent(QResizeEvent *event)
for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++) for (int GroupIdx = 0; GroupIdx < LC_NUM_COLORGROUPS; GroupIdx++)
{ {
lcColorGroup* Group = &gColorGroups[GroupIdx]; lcColorGroup* Group = &gColorGroups[GroupIdx];
mRows += (Group->Colors.GetSize() + mColumns - 1) / mColumns; mRows += ((int)Group->Colors.size() + mColumns - 1) / mColumns;
} }
CellWidth = (float)(width() + 1) / (float)mColumns; CellWidth = (float)(width() + 1) / (float)mColumns;
@ -315,7 +315,7 @@ void lcQColorList::resizeEvent(QResizeEvent *event)
mGroupRects[GroupIdx] = QRect(0, (int)GroupY, width(), mGroupRects[GroupIdx].height()); mGroupRects[GroupIdx] = QRect(0, (int)GroupY, width(), mGroupRects[GroupIdx].height());
GroupY += mGroupRects[GroupIdx].height(); GroupY += mGroupRects[GroupIdx].height();
for (int ColorIdx = 0; ColorIdx < Group->Colors.GetSize(); ColorIdx++) for (size_t ColorIdx = 0; ColorIdx < Group->Colors.size(); ColorIdx++)
{ {
const int Left = CellWidth * CurColumn - 1; const int Left = CellWidth * CurColumn - 1;
const int Right = (CurColumn + 1) * CellWidth - 1; const int Right = (CurColumn + 1) * CellWidth - 1;