#include "lc_global.h" #include #include "lc_application.h" #include "lc_colors.h" #include "library.h" #include "system.h" #include "console.h" #include "opengl.h" #include "project.h" #include "image.h" // ---------------------------------------------------------------------------- // Global functions. lcApplication* g_App; PiecesLibrary* lcGetPiecesLibrary() { LC_ASSERT(g_App, "g_App not initialized."); return g_App->GetPiecesLibrary(); } Project* lcGetActiveProject() { LC_ASSERT(g_App, "g_App not initialized."); return g_App->GetActiveProject(); } // ---------------------------------------------------------------------------- // lcApplication class. lcApplication::lcApplication() { m_ActiveProject = NULL; m_Library = NULL; } lcApplication::~lcApplication() { } void lcApplication::AddProject(Project* project) { m_Projects.Add(project); if (m_ActiveProject == NULL) m_ActiveProject = project; } bool lcApplication::LoadPiecesLibrary(const char* LibPath, const char* SysLibPath) { // Create an empty library. if (m_Library == NULL) m_Library = new PiecesLibrary(); else m_Library->Unload(); // Check if the user specified a library path in the command line. if (LibPath != NULL) if (m_Library->Load(LibPath)) return true; // Check for the LEOCAD_LIB environment variable. char* EnvPath = getenv("LEOCAD_LIB"); if (EnvPath != NULL) if (m_Library->Load(EnvPath)) return true; // Try the executable install path last. if (SysLibPath != NULL) if (m_Library->Load(SysLibPath)) return true; #ifdef LC_WINDOWS SystemDoMessageBox("Cannot load pieces library.\n" "Make sure that you have the PIECES.IDX file in the same " "folder where you installed the program.", LC_MB_OK|LC_MB_ICONERROR); #else printf("Error: Cannot load pieces library.\n"); #endif return false; } void lcApplication::ConvertPiecesLibrary(const char* SrcPath, const char* DstPath) { ObjArray FileList; PiecesLibrary Library; Library.SetPath(DstPath); if (!Library.DeleteAllPieces()) { printf("Error: Couldn't open library file for writing."); // TODO: replace printfs with a callback and progress dialog, then delete the duplicate code in libdlg.cpp return; } Sys_GetFileList(SrcPath, FileList); if (!FileList.GetSize()) { printf("Error: No files to import."); return; } char file1[LC_MAXPATH], file2[LC_MAXPATH]; lcDiskFile DiskIdx, DiskBin; strcpy(file1, Library.GetLibraryPath()); strcat(file1, "pieces.idx"); strcpy(file2, Library.GetLibraryPath()); strcat(file2, "pieces.bin"); if ((!DiskIdx.Open(file1, "rb")) || (!DiskBin.Open(file2, "rb"))) return; lcMemFile IdxFile1, IdxFile2, BinFile1, BinFile2; IdxFile1.CopyFrom(DiskIdx); BinFile1.CopyFrom(DiskBin); lcMemFile* NewIdx = &IdxFile1; lcMemFile* NewBin = &BinFile1; lcMemFile* OldIdx = &IdxFile2; lcMemFile* OldBin = &BinFile2; for (int i = 0; i < FileList.GetSize(); i++) { char* Name = FileList[i]; char* Slash = strrchr(Name, '\\'); if (Slash > Name) Name = Slash+1; Slash = strrchr(Name, '/'); if (Slash > Name) Name = Slash+1; printf("Importing %s\n", Name); lcMemFile* TmpFile; TmpFile = NewBin; NewBin = OldBin; OldBin = TmpFile; NewBin->SetLength(0); TmpFile = NewIdx; NewIdx = OldIdx; OldIdx = TmpFile; NewIdx->SetLength(0); lcGetPiecesLibrary()->ImportLDrawPiece(FileList[i], NewIdx, NewBin, OldIdx, OldBin); } if ((!DiskIdx.Open(file1, "wb")) || (!DiskBin.Open(file2, "wb"))) { printf("Error: Couldn't open library file for writing."); return; } DiskIdx.CopyFrom(*NewIdx); DiskBin.CopyFrom(*NewBin); } void lcApplication::ParseIntegerArgument(int* CurArg, int argc, char* argv[], int* Value) { if (argc > (*CurArg + 1)) { (*CurArg)++; int val; if ((sscanf(argv[(*CurArg)], "%d", &val) == 1) && (val > 0)) *Value = val; else console.PrintWarning("Invalid value specified for the %s argument.", argv[(*CurArg) - 1]); } else { console.PrintWarning("Not enough parameters for the %s argument.", argv[(*CurArg) - 1]); } } void lcApplication::ParseStringArgument(int* CurArg, int argc, char* argv[], char** Value) { if (argc > (*CurArg + 1)) { (*CurArg)++; *Value = argv[(*CurArg)]; } else { console.PrintWarning("No path specified after the %s argument.", argv[(*CurArg) - 1]); } } bool lcApplication::Initialize(int argc, char* argv[], const char* SysLibPath) { // System setup parameters. char* LibPath = NULL; char* GLPath = NULL; char* LDrawPath = NULL; // Image output options. bool SaveImage = false; bool ImageAnimation = false; bool ImageInstructions = false; bool ImageHighlight = false; int ImageWidth = Sys_ProfileLoadInt("Default", "Image Width", 640); int ImageHeight = Sys_ProfileLoadInt("Default", "Image Height", 480); int ImageStart = 0; int ImageEnd = 0; char* ImageName = NULL; lcLoadDefaultColors(); // File to open. char* ProjectName = NULL; // Parse the command line arguments. for (int i = 1; i < argc; i++) { char* Param = argv[i]; if (Param[0] == '-') { if (strcmp(Param, "--libgl") == 0) { ParseStringArgument(&i, argc, argv, &GLPath); } else if ((strcmp(Param, "-l") == 0) || (strcmp(Param, "--libpath") == 0)) { ParseStringArgument(&i, argc, argv, &LibPath); } else if (strcmp(Param, "--convert") == 0) { if (argc > i + 2) { ParseStringArgument(&i, argc, argv, &LDrawPath); ParseStringArgument(&i, argc, argv, &LibPath); ConvertPiecesLibrary(LDrawPath, LibPath); } else { printf("Not enough command line arguments.\n"); } return false; } else if ((strcmp(Param, "-i") == 0) || (strcmp(Param, "--image") == 0)) { SaveImage = true; if ((argc > (i+1)) && (argv[i+1][0] != '-')) { i++; ImageName = argv[i]; } } else if ((strcmp(Param, "-w") == 0) || (strcmp(Param, "--width") == 0)) { ParseIntegerArgument(&i, argc, argv, &ImageWidth); } else if ((strcmp(Param, "-h") == 0) || (strcmp(Param, "--height") == 0)) { ParseIntegerArgument(&i, argc, argv, &ImageHeight); } else if ((strcmp(Param, "-f") == 0) || (strcmp(Param, "--from") == 0)) { ParseIntegerArgument(&i, argc, argv, &ImageStart); } else if ((strcmp(Param, "-t") == 0) || (strcmp(Param, "--to") == 0)) { ParseIntegerArgument(&i, argc, argv, &ImageEnd); } else if (strcmp(Param, "--animation") == 0) ImageAnimation = true; else if (strcmp(Param, "--instructions") == 0) ImageInstructions = true; else if (strcmp(Param, "--highlight") == 0) ImageHighlight = true; else if ((strcmp(Param, "-v") == 0) || (strcmp(Param, "--version") == 0)) { printf("LeoCAD version " LC_VERSION_TEXT LC_VERSION_TAG " for "LC_VERSION_OSNAME"\n"); printf("Copyright (c) 1996-2006, BT Software\n"); printf("Compiled "__DATE__"\n"); #ifdef LC_HAVE_JPEGLIB printf("With JPEG support\n"); #else printf("Without JPEG support\n"); #endif #ifdef LC_HAVE_PNGLIB printf("With PNG support\n"); #else printf("Without PNG support\n"); #endif return false; } else if ((strcmp(Param, "-?") == 0) || (strcmp(Param, "--help") == 0)) { printf("Usage: leocad [options] [file]\n"); printf(" [options] can be:\n"); printf(" --libgl : Searches for OpenGL libraries in path.\n"); printf(" --libpath : Loads the Pieces Library from path.\n"); printf(" --convert : Generates Pieces Library from LDraw files.\n"); printf(" -i, --image : Saves a picture in the format specified by ext.\n"); printf(" -w, --width : Sets the picture width.\n"); printf(" -h, --height : Sets the picture height.\n"); printf(" -f, --from