-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
160 lines (140 loc) · 3.16 KB
/
Copy pathmain.cpp
File metadata and controls
160 lines (140 loc) · 3.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
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
//
// Title: ZX Spectrum 48K emulator
// Description: ZX Spectrum 48K emulator
// Author: Dean Belfield
// Created: 22/05/2026
// Last Updated: 02/08/2026
//
// Modinfo:
// 02/08/2026: Added Emscripten support
#include <iostream>
#include <memory>
#include <vector>
#include <fstream>
#include <filesystem>
#include <chrono>
#include <thread>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
#endif
#include "emulator.h"
using namespace std::chrono;
using namespace std::literals::chrono_literals;
// Global variables
//
static Emulator* emulator = nullptr;
static bool quit = false;
// Function prototypes
//
void loop();
// Functions callable from JavaScript
//
#ifdef __EMSCRIPTEN__
// Load a file into the emulator
//
void open(string filename) {
cout << "Opening emulator file " << filename << endl;
if (!emulator->open(filename)) {
cout << "Error opening file" << endl;
}
}
// The function bindings
//
EMSCRIPTEN_BINDINGS(web) {
emscripten::function("open", &open);
}
#endif
// Set the emulation speed
//
#ifdef __EMSCRIPTEN__
void setSpeed(int speed) {
emscripten_cancel_main_loop();
emscripten_set_main_loop(loop, speed, 1);
}
#else
static auto speed = 20000us;
#endif
// The loop
// Parameters:
// - arg: void pointer to an Emulator object
//
void loop() {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_QUIT: {
quit = true;
} break;
case SDL_KEYDOWN: {
switch (e.key.keysym.sym) {
#ifdef __EMSCRIPTEN__
case SDLK_F1: setSpeed(50); break;
case SDLK_F2: setSpeed(100); break;
case SDLK_F3: setSpeed(200); break;
case SDLK_F4: setSpeed(400); break;
#else
case SDLK_F1: speed = 20000us; break;
case SDLK_F2: speed = 10000us; break;
case SDLK_F3: speed = 5000us; break;
case SDLK_F4: speed = 2500us; break;
#endif
}
} break;
}
emulator->handleEvents(e);
}
try {
emulator->run();
}
catch(const runtime_error& e) {
cout << "Error: " << e.what() << endl;
}
}
// The main function
// Parameters:
// - argc: argument count
// - argv: argument array
//
int main(int argc, char* argv[])
{
string filename;
int scale = 1;
// Handle any command-line parameters
//
vector<string> arguments(argv + 1, argv + argc);
for(string argument : arguments) {
size_t delimiter = argument.find("=");
string token(argument.substr(0, delimiter));
string parameter(delimiter == string::npos ? "" : argument.substr(delimiter + 1));
if (token == "s" || token =="scale") {
scale = stoi(parameter);
continue;
}
if (token == "l" || token == "load") {
filename = parameter;
continue;
}
}
try {
emulator = new Emulator(scale, filename);
}
catch(const runtime_error& e) {
cout << "Error: " << e.what() << endl;
delete emulator;
return 0;
}
// The main loop
//
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 50, 1);
#else
while (!quit) {
auto nextTick = steady_clock::now() + speed;
loop();
this_thread::sleep_until(nextTick); // Wait until the next 1/50th of a second
nextTick = steady_clock::now() + speed; // Set the next tick to be from now
}
#endif
return 0;
}