summaryrefslogtreecommitdiff
path: root/common/lc_application.cpp
blob: ef57f5dd5bbf4d6a66ed5a2f6bae3f4a9588604b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdio.h>
#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 <path>: Searches for OpenGL libraries in path.\n");
				printf("  --libpath <path>: Loads the Pieces library from path.\n");
				printf("  -i, --image <outfile.ext>: Saves a picture in the format specified by ext.\n");
				printf("  -w, --width <width>: Sets the picture width.\n");
				printf("  -h, --height <height>: Sets the picture height.\n");
				printf("  -f, --from <time>: Sets the first frame or step to save pictures.\n");
				printf("  -t, --to <time>: Sets the last frame or step to save pictures.\n");
				printf("  --animation: Saves animations frames.\n");
				printf("  --instructions: Saves instructions steps.\n");
				printf("  --highlight: Highlight pieces in the steps they appear.\n");
				printf("  \n");
			}
			else
				console.PrintWarning("Unknown parameter: %s\n", Param);
		}
		else
		{
			ProjectName = Param;
		}
	}

	// Initialize other systems.
	if (!GL_Initialize(GLPath))
		return false;

	if (!LoadPiecesLibrary(LibPath, SysLibPath))
		return false;

	SystemInit();

	// Create a new project.
	Project* project = new Project();
	AddProject(project);

	// Load project.
	if (ProjectName && project->OpenProject(ProjectName))
	{
		if (!SaveImage)
			return true;

		// Check if there's a file name and it has an extension.
		bool NeedExt = true;
		String FileName;

		if (!ImageName)
		{
			FileName = ProjectName;

			int i = FileName.ReverseFind('.');
			if (i != -1)
				FileName[i] = 0;
		}
		else
		{
			FileName = ImageName;

			int i = FileName.ReverseFind('.');
			String Ext;

			if (i != -1)
			{
				Ext = FileName.Right(FileName.GetLength() - i);
				Ext.MakeLower();
			}

			if ((Ext == "bmp") || (Ext == "gif"))
				NeedExt = false;
#ifdef LC_HAVE_JPEGLIB
			else if ((Ext == "jpg") || (Ext == "jpeg"))
				NeedExt = false;
#endif
#ifdef LC_HAVE_PNGLIB
			else if (Ext == "png")
				NeedExt = false;
#endif
		}

		// Setup default options.
		LC_IMAGE_OPTS ImageOptions;
		unsigned long image = Sys_ProfileLoadInt  ("Default", "Image Options", 1|LC_IMAGE_TRANSPARENT);
		ImageOptions.quality = Sys_ProfileLoadInt ("Default", "JPEG Quality", 70);
		ImageOptions.interlaced = (image & LC_IMAGE_PROGRESSIVE) != 0;
		ImageOptions.transparent = (image & LC_IMAGE_TRANSPARENT) != 0;
		ImageOptions.truecolor = (image & LC_IMAGE_HIGHCOLOR) != 0;
		ImageOptions.format = image & ~(LC_IMAGE_MASK);
		ImageOptions.background[0] = (unsigned char)(project->GetBackgroundColor()[0]*255);
		ImageOptions.background[1] = (unsigned char)(project->GetBackgroundColor()[1]*255);
		ImageOptions.background[2] = (unsigned char)(project->GetBackgroundColor()[2]*255);

		// Append file extension if needed.
		if (NeedExt)
		{
			switch (ImageOptions.format)
			{
			default:
			case LC_IMAGE_BMP:
				FileName += ".bmp";
				break;
			case LC_IMAGE_GIF:
				FileName += ".gif";
				break;
			case LC_IMAGE_JPG:
				FileName += ".jpg";
				break;
			case LC_IMAGE_PNG:
				FileName += ".png";
				break;
			}
		}

		if (ImageInstructions)
			project->SetAnimation(false);
		else if (ImageAnimation)
			project->SetAnimation(true);

		if (ImageEnd < ImageStart)
			ImageEnd = ImageStart;
		else if (ImageStart > ImageEnd)
			ImageStart = ImageEnd;

		if ((ImageStart == 0) && (ImageEnd == 0))
		{
			ImageStart = ImageEnd = project->GetCurrentTime();
		}
		else if ((ImageStart == 0) && (ImageEnd != 0))
		{
			ImageStart = ImageEnd;
		}
		else if ((ImageStart != 0) && (ImageEnd == 0))
		{
			ImageEnd = ImageStart;
		}

		if (project->IsAnimation())
		{
			if (ImageStart > project->GetTotalFrames())
				ImageStart = project->GetTotalFrames();

			if (ImageEnd > project->GetTotalFrames())
				ImageEnd = project->GetTotalFrames();
		}
		else
		{
			if (ImageStart > 255)
				ImageStart = 255;

			if (ImageEnd > 255)
				ImageEnd = 255;
		}

		Image* images = new Image[ImageEnd - ImageStart + 1];
		project->CreateImages(images, ImageWidth, ImageHeight, ImageStart, ImageEnd, ImageHighlight);

		for (int i = 0; i <= ImageEnd - ImageStart; i++)
		{
			char idx[256];
			String Frame;

			if (ImageStart != ImageEnd)
			{
				sprintf(idx, "%02d", i+1);
				int Ext = FileName.ReverseFind('.');

				Frame = FileName.Left(Ext) + idx + FileName.Right(FileName.GetLength() - Ext);
			}
			else
				Frame = FileName;

			images[i].FileSave(Frame, &ImageOptions);
		}

		delete []images;

		return false;
	}
	else
	{
		if (SaveImage)
			return false;
		else
			project->OnNewDocument();
	}

	return true;
}

void lcApplication::Shutdown()
{
	for (int i = 0; i < m_Projects.GetSize(); i++)
	{
		Project* project = m_Projects[i];

		project->HandleNotify(LC_ACTIVATE, 0);
		delete project;
	}

	delete m_Library;
	m_Library = NULL;

	GL_Shutdown();
}