File: mainloop.cpp

package info (click to toggle)
amoeba 1.1-26
  • links: PTS
  • area: contrib
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,256 kB
  • ctags: 1,561
  • sloc: cpp: 8,315; makefile: 173
file content (707 lines) | stat: -rw-r--r-- 17,515 bytes parent folder | download | duplicates (4)
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef __GNUC__
#include <unistd.h>
#endif
#include <expat.h>

#ifdef WIN32
#include <windows.h>
#include "main/win32-config/win32-config.h"
#else
#include <sys/time.h>
#include "main/linux-config/linux-config.h"
#endif

#include "main/mainloop.h"
#include "main/factory.h"
#include "main/demohandler.h"
#include "audio/musichandler.h"
#include "exception.h"
#include "demolib_prefs.h"

#include <GL/gl.h>

#if DEMOLIB_MAINLOOP

void start_ml(void *data, const char *el, const char **attr)
{
	MainLoop *ml = (MainLoop *)data;

	ml->process_element(el, attr);		
}
void end_ml(void *data, const char *el)
{
	MainLoop *ml = (MainLoop *)data;

	/* this is a brief hack, but it's OK */
	if (strcmp(el, "demo") == 0) {
		if (ml->events[0]->stop == -1.0f)
			ml->events[0]->stop = ml->events[ml->num_events - 1]->stop;
		return;
	}

	if (strcmp(el, "start") == 0 ||
	    strcmp(el, "point") == 0 ||
	    strcmp(el, "end") == 0 ||
	    strcmp(el, "demo") == 0 ||
	    strcmp(el, "music") == 0 ||
	    strcmp(el, "marker") == 0 ||
	    strcmp(el, "lpp") == 0)
		return;
	try {
		ml->curr_event->end_curvedata();
		ml->curr_event = NULL;
	} catch (FatalException *e) {
		char buf[256];
                sprintf(buf, "Error at line %d of demo script: %s", XML_GetCurrentLineNumber(ml->p), e->get_error());
                throw new FatalException(buf);
	}
}

MainLoop::MainLoop(int argc, char **argv)
{
	this->next_evnum = 0;

	this->events = (Event **)(malloc(sizeof(Event *) * 256));
	this->num_events = 0;

	this->factories = (Factory **)(malloc(sizeof(Factory *) * 256));
	this->num_factories = 0;

	this->markers = new Hashtable();
	this->timer = NULL;

	this->argc = argc;
	this->argv = argv;

	this->sound = true;
	this->precalc = NULL;
}

void MainLoop::parse(File *demoscript)
{
	this->p = XML_ParserCreate(NULL);
	XML_SetUserData(p, this);
	XML_SetElementHandler(p, start_ml, end_ml);

	bool status;
	
	try {
		status = XML_Parse(p, demoscript->get_data(), demoscript->data_length(), 1);
	} catch (ValueNotSpecifiedException *nve) {
		char buf[256];
		sprintf(buf, "Error at line %d of demo script: Parameter `%s=' missing", XML_GetCurrentLineNumber(p), nve->get_error());
		throw new FatalException(buf);
	}
		
	if (!status) {
		char buf[512];
		sprintf(buf, "Parse error at line %d of demo script: %s",
			XML_GetCurrentLineNumber(p),
			XML_ErrorString(XML_GetErrorCode(p)));
		throw new FatalException(buf);
	}

	XML_ParserFree(p);

	if (this->precalc != NULL)
		delete this->precalc;
}

MainLoop::~MainLoop()
{
	int i;
	for (i = 0; i < this->num_events; i++) {
		if (this->events[i] == NULL) continue;
#if !DEMOLIB_SILENT
		char *buf = strdup(this->events[i]->title);
#endif
		delete this->events[i];
#if !DEMOLIB_SILENT
		int err = glGetError();
		if (err != 0) {
			printf("Warning: Effect `%s' has OpenGL error 0x%x in exit code\n", buf, err);
		}
		free(buf);
#endif
		this->events[i] = NULL;
	}
	free(this->events);
	this->events = NULL;

	for (i = 0; i < this->num_factories; i++) {
		delete this->factories[i];
		this->factories[i] = NULL;
	}
	free(this->factories);
	this->factories = NULL;

	this->markers->destroy_values();
	delete this->markers;
	this->markers = NULL;
}

void MainLoop::add_handler(Factory *handler_factory)
{
	this->factories[num_factories++] = handler_factory;
}

void MainLoop::process_element(const char *el, const char **attr)
{
	char title[256] = "[No title]";

	/* check if this is a music marker */
	if (strcmp(el, "marker") == 0) {
		const char *name = NULL;
		float t = -1.0f;

		int i = 0;
		while (attr[i]) {
			if (strcmp(attr[i], "name") == 0) {
				name = attr[i + 1];
			}
			if (strcmp(attr[i], "time") == 0) {
				t = atof(attr[i + 1]);
			}
			i += 2;
		}

		if (name == NULL)
			throw new FatalException(el, "No name= attribute!");
		if (t == -1.0f)
			throw new FatalException(el, "No time= attribute!");

		/* sigh */
		float *tp = (float *)(malloc(sizeof(float)));
		if (tp == NULL)
			throw new FatalException("Out of memory!");
		
		*tp = t;
		this->markers->insert(name, (void *)tp);

		return;
	}

	/* check if this is an effect marker (start/end/point) */
	if (strcmp(el, "start") == 0 ||
	    strcmp(el, "point") == 0 ||
	    strcmp(el, "end") == 0) {
		if (this->curr_event == NULL) {
			throw new FatalException(el, "Time element outside effect elements!");
		} else {
			this->curr_event->add_curvepoint(this->markers, el, attr);
			return;
		}
	}

	/*
	 * Check if this is a loader progress point (lpp).
	 * This is used for two purposes:
	 *
	 * 1) Show a simple progress bar while loading. (For the
	 *    Underscore demo, this is replaced by a fancy Pi
	 *    display ;-) )
	 * 2) Timing loader parts, to be outputted and possibly
	 *    made into more exact p= values later. (This is
	 *    disabled by setting DEMOLIB_SILENT.)
	 */
	if (strcmp(el, "lpp") == 0) {
		float p = -1.0f;
		int i = 0;
#if !DEMOLIB_SILENT
		static bool init_timer = false;
#if __unix__
		static struct timeval first_lpp, now;
#else
		static DWORD first_lpp, now;
#endif
		
		if (!init_timer) {
#if __unix__
			gettimeofday(&first_lpp, NULL);
#else
			first_lpp = GetTickCount();
#endif
			init_timer = true;
		}
#if __unix__
		gettimeofday(&now, NULL);
		printf("LPP: [%6.3f]\n",
			(now.tv_sec - first_lpp.tv_sec) +
			(float)(now.tv_usec - first_lpp.tv_usec) * 0.000001f);
#else
		now = GetTickCount();
		printf("LPP: [%6.3f]\n",
			(float)(now - first_lpp) * 0.001f);
#endif

#endif /* !DEMOLIB_SILENT */
		
		while (attr[i]) {
			if (strcmp(attr[i], "p") == 0) {
				p = atof(attr[i + 1]);
				break;
			}
			i += 2;
		}
		if (p == -1.0f) return;
		
#if 0
		/* do we want this in a separate class? */
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
	        glOrtho(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f);
	        glDisable(GL_BLEND);
		glDisable(GL_LIGHTING);
		glColor3f(1.0f, 1.0f, 1.0f);

		glBegin(GL_QUADS);
		glVertex2f(0.0f, 0.9f);
		glVertex2f(p, 0.9f);
		glVertex2f(p, 1.0f);
		glVertex2f(0.0f, 1.0f);
		glEnd();

		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);

		this->win->flip();
#else
		if (this->precalc != NULL)
			this->precalc->update(p);		
#endif
		return;
	}

	/*
	 * If this is the music and we have -nosound, discard it.
	 */
#if WIN32
	if (strcmp(el, "music") == 0 &&
	    (!this->sound || Win32Config::instance()->get_sound() == (LPGUID)-1)) {
		return;
	}
#else
	if (strcmp(el, "music") == 0 && !this->sound) {
		return;
	}
#endif
	
	/*
	 * Find the title, and make a temporary hash table to hold all
	 * the attributes.
	 *
	 * C++ is very picky about const (which is good) -- we KNOW that the
	 * attribute strings will never be edited, but it's very hard to prove
	 * that to the computer programmatically :-)
	 */
	Hashtable *attr_hash = new Hashtable();
	int i = 0;
	while (attr[i]) {
		attr_hash->insert(attr[i], (char *)attr[i + 1]);
		if (strcmp(attr[i], "title") == 0) {
			strcpy(title, attr[i + 1]);
		}
		i += 2;
	}


	/* find a factory that can instantiate this type of event classes */
	Event *ev = NULL;
	for (i = 0; i < num_factories; i++) {
		Factory *f = this->factories[i];
		if (!f->can_handle(el)) continue;
		
		/* 
		 * If this is the first event, it's a DemoHandler, and we want
		 * to set window stuff etc. -- this isn't particularily clean,
		 * and should be rewritten someday.
		 */
		if (this->num_events == 0) {
#if __unix__
			/* attempt to use the colorful GTK+ interface first =) */
			try {
				/* 
				 * don't use the interface if there are command line
				 * options
				 */
				if (argc != 1) throw new NonFatalException("disabling because of command line options");
				
				LinuxConfig *lc = new LinuxConfig();
				lc->show(&argc, &argv, attr_hash);
				if (strcmp(attr_hash->get_str("sound"), "no") == 0) {
					this->sound = false;
				}
			} catch (NonFatalException *e) {
				fprintf(stderr,
					"Couldn't open GTK+ interface (%s), reverting to command line.\n",
					e->get_error());
				/* use the generic code, since GTK+ failed for some reason */
				this->parse_commandline(argc, argv, attr_hash);
			}
#else /* Win32 */
			if (argc != 1) {
				this->parse_commandline(argc, argv, attr_hash);
			} else {
				char buf[256];
				Win32Config *c = Win32Config::instance();
				c->dialog();
				if (c->get_fullscreen()) {
					attr_hash->insert("fullscreen", "yes");
				} else {
					attr_hash->insert("fullscreen", "no");
				}
				
				attr_hash->insert("xres", "0");
				attr_hash->insert("yres", "0");
				
				sprintf(buf, "%u", c->get_zbuffer());
				attr_hash->insert("zbuffer", buf);
				
				sprintf(buf, "%u", c->get_mode());
				attr_hash->insert("visual_id", buf);
			}
#endif
		}
		
		ev = f->instantiate(this, title, el, attr_hash);
#if !DEMOLIB_SILENT
	        int err = glGetError();
	        if (err != 0) {
	                printf("Warning: Initialization of `%s' has OpenGL error 0x%x\n", title, err);
	        }
#endif
				
		if (this->num_events == 0) {
			this->win = ((DemoHandler *)ev)->win;

			/* now that we have an OpenGL window, we can start the loader screen */
			if (!attr_hash->exists("noprecalcscreen"))
				this->precalc = new PiPrecalc(this->win);
		}
		break;
	}

	delete attr_hash;

	if (ev == NULL) {
		throw new FatalException(el, "No usable handler found!");
	}

	/*
	 * the only special tags ATM are <demo> and <music> -- they don't need end time
	 */
	if (strcmp(el, "demo") == 0 ||
	    strcmp(el, "music") == 0) {
		this->curr_event = NULL;
		ev->start = 0.0f;
		ev->stop = -1.0f;

		if (strcmp(el, "music") == 0) {
			/* music _can_ have a start time :-) */
			int i = 0;
			while (attr[i]) {
				if (strcmp(attr[i], "start") == 0) {
					ev->start = atof(attr[i + 1]);
					break;
				}
				i += 2;
			}
		}
	} else {
		if (this->curr_event != NULL) {
			throw new FatalException(el, "Nested effect elements aren't allowed.");
		}
		this->curr_event = ev;
	}


	/*
	 * ...and add the actual element (we could strip away start= etc., but we
	 * don't care -- it isn't worth it)
	 */
	this->events[this->num_events++] = ev;
}

void MainLoop::run()
{
#ifdef WIN32
	QueryPerformanceCounter(&this->tmstart);
	QueryPerformanceFrequency(&this->tmfreq);
#else
	gettimeofday(&tmstart, NULL);
#endif
	this->run(true);
}

#if !defined(__GNUC__) && !defined(__ICC__)
extern int layer_event_sort(const void *a, const void *b);
#else
int layer_event_sort(const void *a, const void *b)
{
	float la = (*((Event **)a))->layer;
	float lb = (*((Event **)b))->layer;

	if (la < lb) return -1;
	if (la > lb) return 1;
	return 0;
}
#endif

float MainLoop::get_time()
{
	if (this->timer == NULL) {
#if WIN32
		LARGE_INTEGER now;
		QueryPerformanceCounter(&now);
		return (float)(now.QuadPart - tmstart.QuadPart) /
			(float)(tmfreq.QuadPart);
#else
		struct timeval now;

		gettimeofday(&now, NULL);
		return (now.tv_sec - tmstart.tv_sec) +
			(float)(now.tv_usec - tmstart.tv_usec) * 0.000001f;
#endif
	} else {
		return this->timer->get_time() + 0.010;
	}
}

/*
 * This serves both as a main loop and a GLUT callback point. It has to work
 * this way because <demo> really is special -- most likely, <demo> will _never_
 * return (and that's good, because then we won't need to check which effects
 * have passed long time ago ;-) ) but will just keep on calling run() back...
 */
void MainLoop::run(bool infloop)
{
#if !DEMOLIB_SILENT
	int err = glGetError();
	if (err != 0) {
		printf("Warning: Some part of initialization has OpenGL error 0x%x\n", err);
	}
#endif
	do {
		this->num_play_events = 0;
		float timer = this->get_time();

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
	        glLoadIdentity();
		
		/*
		 * go through any active handlers and see if they should be ended
		 * (could perhaps be in reverse order? at least </music> and </demo>
		 * SHOULD come last)
		 */
		for (int i = 0; i < this->next_evnum; i++) {
			Event *e = this->events[i];
			if (e && e->active) {
				if (timer > e->stop && e->stop != -1.0f) {
#if !DEMOLIB_SILENT
					printf("Exiting: %s\n", e->title);
#endif

					e->end_effect();
				
#if !DEMOLIB_SILENT
					int err = glGetError();
					if (err != 0) {
						printf("Warning: Effect `%s' has OpenGL error 0x%x in destroy code\n", e->title, err);
					}
#endif
					
					if (i == 0 || i == 1) {
						e->active = false;
					} else {
#if !DEMOLIB_SILENT
						char *buf = strdup(e->title);
#endif
						delete e;
#if !DEMOLIB_SILENT
						int err = glGetError();
						if (err != 0) {
							printf("Warning: Effect `%s' has OpenGL error 0x%x in destroy code\n", buf, err);
						}
						free(buf);
#endif
						this->events[i] = NULL;
					}
#endif
				} else {
					evlist[num_play_events++] = e;
				}
			}
		}
		
		/* fire off any new events */
		while (this->next_evnum < this->num_events &&
		       timer >= this->events[this->next_evnum]->start) {
			Event *e = this->events[this->next_evnum];
			this->next_evnum++;

			if (timer > e->stop && e->stop != -1.0f) {
				/* we missed it completely! */
			} else {
#if !DEMOLIB_SILENT
				printf("Entering: %s\n", e->title);
#endif
				e->active = true;
				e->start_effect();
				evlist[num_play_events++] = e;
#if !DEMOLIB_SILENT
				int err = glGetError();
				if (err != 0 && this->next_evnum != 1) {
					printf("Warning: Effect `%s' has OpenGL error 0x%x in init code\n", e->title, err);
				}
#endif
			}
		}


		/* now sort the effects by layer (lower/negative ones come first) */
		if (num_play_events > 0) {
			qsort(evlist, num_play_events, sizeof(Event *), layer_event_sort);
			for (int i = 0; i < num_play_events; i++) {
				Event *e = evlist[i];
				e->draw_scene((timer - e->start) / (e->stop - e->start));
					
#if !DEMOLIB_SILENT
				int err = glGetError();
				if (err != 0 && i != 0) {
					printf("Warning: Effect `%s' has OpenGL error 0x%x\n", e->title, err);
				}
#endif
			}
			
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluPerspective(53.0f, DEMOLIB_XASPECT / DEMOLIB_YASPECT, 1.0f, 500.0f);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
		}
	} while (infloop && this->events[0] && this->events[0]->active);
}

void MainLoop::parse_commandline(int argc, char **argv, Hashtable *attr_hash)
{
	/* defaults :-) */
	attr_hash->insert("fullscreen", "yes");
	attr_hash->insert("xres", "640");
	attr_hash->insert("yres", "480");
	attr_hash->insert("depth", "32");
	attr_hash->insert("zbuffer", "16");
				
	for (int j = 1; j < argc; j++) {
		if (strcmp(argv[j], "-fullscreen") == 0) {
			attr_hash->insert("fullscreen", "yes");
			continue;
		}
		if (strcmp(argv[j], "-windowed") == 0) {
			attr_hash->insert("fullscreen", "no");
			continue;
		}
		if (strcmp(argv[j], "-noprecalc") == 0) {
			attr_hash->insert("noprecalcscreen", "yes");
			continue;
		}
		/* hidden modes, not very supported ;-) */
		if (strcmp(argv[j], "-320x200") == 0) {
			attr_hash->insert("xres", "320");
			attr_hash->insert("yres", "200");
			continue;
		}
		if (strcmp(argv[j], "-320x240") == 0) {
			attr_hash->insert("xres", "320");
			attr_hash->insert("yres", "240");
			continue;
		}
		if (strcmp(argv[j], "-512x384") == 0) {
			attr_hash->insert("xres", "512");
			attr_hash->insert("yres", "384");
			continue;
		}
		if (strcmp(argv[j], "-640x480") == 0) {
			attr_hash->insert("xres", "640");
			attr_hash->insert("yres", "480");
			continue;
		}
		if (strcmp(argv[j], "-800x600") == 0) {
			attr_hash->insert("xres", "800");
			attr_hash->insert("yres", "600");
			continue;
		}
		if (strcmp(argv[j], "-1024x768") == 0) {
			attr_hash->insert("xres", "1024");
			attr_hash->insert("yres", "768");
			continue;
		}
		if (strcmp(argv[j], "-1280x960") == 0) {
			attr_hash->insert("xres", "1280");
			attr_hash->insert("yres", "960");
			continue;
		}
		if (strcmp(argv[j], "-1280x1024") == 0) {
			attr_hash->insert("xres", "1280");
			attr_hash->insert("yres", "1024");
			continue;
		}
		if (strcmp(argv[j], "-1400x1050") == 0) {
			attr_hash->insert("xres", "1400");
			attr_hash->insert("yres", "1050");
			continue;
		}
		if (strcmp(argv[j], "-1600x1200") == 0) {
			attr_hash->insert("xres", "1600");
			attr_hash->insert("yres", "1200");
			continue;
		}
		if (strcmp(argv[j], "-16") == 0) {
			attr_hash->insert("depth", "16");
			continue;
		}
		if (strcmp(argv[j], "-32") == 0) {
			attr_hash->insert("depth", "32");
			continue;
		}
		if (strcmp(argv[j], "-z16") == 0) {
			attr_hash->insert("zbuffer", "16");
			continue;
		}
		if (strcmp(argv[j], "-z24") == 0) {
			attr_hash->insert("zbuffer", "24");
			continue;
		}
		if (strcmp(argv[j], "-z32") == 0) {
			attr_hash->insert("zbuffer", "32");
			continue;
		}
		if (strcmp(argv[j], "-nosound") == 0) {
			this->sound = false;
			continue;
		}
		throw new FatalException(
			"Allowed switches:\n\n"
			"-fullscreen\tRun in fullscreen (default)\n"
			"-windowed\tRun in a window (NOT SUPPORTED)\n"
			"-640x480\tRun in 640x480 (default)\n"
			"-800x600\tRun in 800x600\n"
			"-1024x768\tRun in 1024x768\n"
			"-1280x960\tRun in 1280x960\n"
			"-1280x1024\tRun in 1280x1024\n"
			"-1400x1050\tRun in 1400x1050 (yes, it exists)\n"
			"-1600x1200\tRun in 1600x1200 (good luck)\n"
			"-16\t\tRun in 16bpp (worse quality, but might work better)\n"
			"-32\t\tRun in 32bpp (default)\n"
			"-z16\t\t16bpp Z-buffer (default)\n"
			"-z24\t\t24bpp Z-buffer (better quality if it works)\n"
			"-z32\t\t32bpp Z-buffer (even better quality if it works)\n"
			"-nosound\tRun silently\n"
		);
	}
}