-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (58 loc) · 2.16 KB
/
Copy pathmain.cpp
File metadata and controls
73 lines (58 loc) · 2.16 KB
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
#include <SFML/Window.hpp>
#include <fstream>
#include "util.hpp"
#include "parser.hpp"
#include "world.hpp"
int main(int argc, char **argv) {
SetupBinops();
getNextToken();
auto program = ParseProgram();
const char *filename = argc >= 2 ? argv[1] : "temp.krl";
std::ofstream ofs(filename, std::ios::out);
if (!ofs)
std::cerr << "Couldn't create temporary file" << std::endl;
program->codegen(ofs);
std::string instruction;
std::vector<std::string> instructions;
std::ifstream ifs(filename, std::ios::in);
if (!ifs)
std::cerr << "File named " << filename << "not found after adding "
<< "the genrated program to it" << std::endl;
while (!ifs.eof()) {
std::getline(ifs, instruction);
if (!(ifs.eof() && instruction.length() == 0))
instructions.push_back(instruction);
}
createDefaultWorld();
Karel karel(instructions);
sf::RenderWindow window(sf::VideoMode(WINDOWWIDTH, WINDOWHEIGHT),
"Emoji Karel");
window.setKeyRepeatEnabled(false);
bool finishedExecution = false;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Space) {
if (!finishedExecution) {
karel.executeUntilMovement();
if (karel.getState() == end) finishedExecution = true;
else if (karel.getState() == error) {
// TODO: enter error handling code here
finishedExecution = true;
}
} else {
// Finished execution, so close
// TODO: also show text showing that karel executed OK
window.close();
}
}
}
}
window.clear(sf::Color::Black);
renderWorld(window, karel);
window.display();
}
return 0;
}