#include #include "lc_application.h" #include "library.h" #include "system.h" #include "console.h" #include "config.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::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; // 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; // 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, "-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(" -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