-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextFactory.cpp
77 lines (63 loc) · 1.68 KB
/
ContextFactory.cpp
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
#include <getopt.h>
#include <stdexcept>
#include "ContextFactory.h"
#include "Displays/DisplayFactory.h"
#include "SceneFactory.h"
/* Program options */
static struct option long_options[] =
{
{"display", 1, 0, 'd'},
{"scene", 1, 0, 's'},
{"help", 0, 0, 'h'},
{NULL, 0, 0, 0}
};
void ContextFactory::help(void)
{
std::cout << "Littleray: small C++ raytracer."<< std::endl;
std::cout << "Options:"<< std::endl;
std::cout << "\t--scene <file> \t\tmandatory, set the xml description file to use"<< std::endl;
std::cout << "\t--display [none|sdl] \tdefines which kind of output the raytracer uses"<< std::endl;
}
Context* ContextFactory::makeContext(int argc, char **argv)
{
int c, optindex;
Display *display;
Scene *scene;
std::string *sceneName = 0,
*displayType = 0;
while ((c = getopt_long(argc,
argv, "ds",
long_options,
&optindex)) != -1) {
switch (c) {
case 's':
sceneName = new std::string(optarg);
break;
case 'd':
displayType = new std::string(optarg);
break;
case 'h':
this->help();
return 0;
break;
default:
std::cout << "Invalid option " << c << "." << std::endl;
return 0;
break;
}
}
if (displayType == 0) {
displayType = new std::string("sdl");
}
if (sceneName == 0) {
std::cout << "--scene file switch is mandatory. Use --help for usage." << std::endl;
throw std::runtime_error("Aborting argument parsing.");
}
SceneFactory sceneFactory;
DisplayFactory displayFactory;
scene = sceneFactory.makeScene(sceneName->c_str());
display = displayFactory.makeDisplay(scene, displayType);
delete sceneName;
delete displayType;
return new Context(scene, display);
}