-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (73 loc) · 2.64 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <array>
#include <thread>
#include <SFML/Graphics.hpp>
#include "boost/lockfree/queue.hpp"
//Function provided by object file output by hack_lifter
extern "C" void f(int16_t *);
/*
* Keyboard input buffer for the Hack code.
* Has to be a global variable (ack!) because the keyboard/screen update code needs access,
* and the Hack code needs a raw C-style function callback. Can't make it local and bind/lambda
* my way to a function callback the LLVM IR stage will accept.
*/
boost::lockfree::queue<int16_t> key_buffer{1024};
bool volatile hack_code_finished = false;
void handle_keyboard(int16_t *mem) {
int16_t val = 0;
key_buffer.pop(val);
mem[0x6000] = val;
}
void keyboard_and_screen_update(std::array<int16_t, 32768> *mem) {
// create the window
sf::RenderWindow window(sf::VideoMode(512, 256), "Hack Lifter Screen");
window.setFramerateLimit(10);
//Framebuffer that will be drawn to the screen
//RGBA byte order
auto *pixels = new sf::Uint8[512 * 256 * 4];
sf::Image img;
img.create(512, 256, sf::Color(255, 255, 255));
sf::Texture texture;
texture.loadFromImage(img);
sf::Sprite sprite(texture);
// run the program as long as the window is open
while (window.isOpen() && !hack_code_finished) {
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event{};
while (window.pollEvent(event)) {
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Escape) {
window.close();
} else {
key_buffer.push(event.key.code);
}
}
}
// clear the window with black color
window.clear(sf::Color::Black);
//TODO: Parse hack screen memory into an image
// draw everything here...
// window.draw(...);
window.draw(sprite);
// end the current frame
window.display();
}
}
int main() {
std::cout << "Starting code..." << std::endl;
std::array<int16_t, 32768> mem{};
mem[0] = 4;
mem[1] = 0;
std::thread updater(keyboard_and_screen_update, &mem);
//launch Hack code
f(mem.data());
std::cout << "Code complete" << std::endl;
std::cout << mem[0] << " + " << mem[1] << " = " << mem[2] << std::endl;
updater.join();
//std::cout << "Key buffer:\n";
//key_buffer.consume_all([](const int16_t v){ std::cout << v << ' ';});
return 0;
}