/
engine.cc
96 lines (87 loc) · 3.81 KB
/
engine.cc
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
#include "EasyImage.h"
#include "ini_configuration.hh"
#include "render.h"
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
img::EasyImage generate_image(const ini::Configuration &configuration) {
std::string type = configuration["General"]["type"].as_string_or_die();
Render render = Render();
if (type == std::string("2DLSystem")) {
return render.renderTwo2dLSystem(configuration);
}
if (type == std::string("Wireframe")) {
return render.renderWireFrame(configuration);
}
if (type == std::string("ZBufferedWireframe")) {
return render.renderWireFrameZBuffered(configuration);
}
if (type == std::string("ZBuffering")) {
return render.renderZBuffered(configuration);
}
return img::EasyImage(256, 256, img::Color(0, 0, 0));
}
int main(int argc, char const* argv[])
{
int retVal = 0;
try
{
for(int i = 1; i < argc; ++i)
{
ini::Configuration conf;
try
{
std::ifstream fin(argv[i]);
fin >> conf;
fin.close();
}
catch(ini::ParseException& ex)
{
std::cerr << "Error parsing file: " << argv[i] << ": " << ex.what() << std::endl;
retVal = 1;
continue;
}
img::EasyImage image = generate_image(conf);
if(image.get_height() > 0 && image.get_width() > 0)
{
std::string fileName(argv[i]);
std::string::size_type pos = fileName.rfind('.');
if(pos == std::string::npos)
{
//filename does not contain a '.' --> append a '.bmp' suffix
fileName += ".bmp";
}
else
{
fileName = fileName.substr(0,pos) + ".bmp";
}
try
{
std::ofstream f_out(fileName.c_str(),std::ios::trunc | std::ios::out | std::ios::binary);
f_out << image;
}
catch(std::exception& ex)
{
std::cerr << "Failed to write image to file: " << ex.what() << std::endl;
retVal = 1;
}
}
else
{
std::cout << "Could not generate image for " << argv[i] << std::endl;
}
}
}
catch(const std::bad_alloc &exception)
{
//When you run out of memory this exception is thrown. When this happens the return value of the program MUST be '100'.
//Basically this return value tells our automated test scripts to run your engine on a pc with more memory.
//(Unless of course you are already consuming the maximum allowed amount of memory)
//If your engine does NOT adhere to this requirement you risk losing points because then our scripts will
//mark the test as failed while in reality it just needed a bit more memory
std::cerr << "Error: insufficient memory" << std::endl;
retVal = 100;
}
return retVal;
}