-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginEditor.cpp
325 lines (269 loc) · 10.1 KB
/
PluginEditor.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "PluginEditor.h"
#include "PluginProcessor.h"
extern "C" {
#include "doomgeneric/doomgeneric/d_iwad.h"
#include "doomgeneric/doomgeneric/d_main.h"
#include "doomgeneric/doomgeneric/doomgeneric.h"
#include "doomgeneric/doomgeneric/doomkeys.h"
#include "doomgeneric/doomgeneric/m_argv.h"
}
#include <chrono>
#include <fstream>
#include <thread>
constexpr int key_offset = 62972;
DoomWindow* editor_ptr = nullptr;
static bool validate_IWAD(const std::string& path) {
char hopefully_this_says_iwad[4];
std::fstream file_for_validation(path);
if (!file_for_validation.is_open())
return false;
file_for_validation.read(hopefully_this_says_iwad, 4);
if (hopefully_this_says_iwad[0] == 'I'
&& hopefully_this_says_iwad[1] == 'W'
&& hopefully_this_says_iwad[2] == 'A'
&& hopefully_this_says_iwad[3] == 'D') {
// "Valid" file.
file_for_validation.close();
return true;
} else {
file_for_validation.close();
return false;
}
}
static std::atomic<bool> ready_to_quit = false;
static std::atomic<bool> finished_draw_loop = false;
static void run_doom_generic(int argc, char **argv) {
doomgeneric_Create(argc, argv);
while (!ready_to_quit) {
doomgeneric_Tick();
}
finished_draw_loop = true;
}
void DoomWindow::wad_file_found(const std::string& path) {
if (!validate_IWAD(path)) {
m_valid_file_selected = false;
return;
}
m_file_path = path;
m_valid_file_selected = true;
m_args = { (char*)m_who_cares.c_str(), (char*)m_iwad_flag.c_str(), (char*)m_file_path.c_str() };
m_doom_thread = std::make_unique<std::thread>(run_doom_generic, m_args.size(), m_args.data());
m_doom_thread->detach();
}
//==============================================================================
DoomWindow::DoomWindow(AudioPluginAudioProcessor& p)
: AudioProcessorEditor(&p)
, processorRef(p) {
juce::ignoreUnused(processorRef);
// Make sure that before the constructor has finished, you've set the
// editor's size to whatever you need it to be.
addKeyListener(this);
setSize(DOOMGENERIC_RESX, DOOMGENERIC_RESY);
editor_ptr = this;
setWantsKeyboardFocus(true);
setResizable(true, false);
setFixedAspectRatio(float(DOOMGENERIC_RESX) / float(DOOMGENERIC_RESY));
setConstrainer(this);
m_framebuffer = juce::Image(juce::Image::PixelFormat::ARGB, DOOMGENERIC_RESX, DOOMGENERIC_RESY, true);
for (int i = 0; i < 512; i++) {
keyboard_state[i] = false;
midi_state[i] = false;
}
m_file_chooser = std::make_unique<juce::FileChooser>("Choose WAD File");
auto flags = juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles;
m_file_chooser->launchAsync(flags, [this](const juce::FileChooser& chooser) {
juce::File wad_file(chooser.getResult());
wad_file_found(wad_file.getFullPathName().toStdString());
});
m_who_cares = "@";
m_iwad_flag = "-iwad";
}
DoomWindow::~DoomWindow() {
// Tell Doom we are done with the render loop.
ready_to_quit = 1;
// Wait for Doom to finish the last frame before quitting (just in case).
while (!finished_draw_loop) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void DoomWindow::handleAsyncUpdate() {
// Check for midi inputs
check_midi_input();
if (editor_ptr)
editor_ptr->repaint();
}
void DG_Init() { }
void DG_DrawFrame() {
// Draws the frame using juce::AsyncUpdater
if (editor_ptr)
editor_ptr->triggerAsyncUpdate();
}
void DG_SleepMs(uint32_t ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
uint32_t DG_GetTicksMs() {
static auto started = std::chrono::high_resolution_clock::now();
auto done = std::chrono::high_resolution_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(done - started).count();
return ms;
}
#define KEYQUEUE_SIZE 16
static unsigned short s_KeyQueue[KEYQUEUE_SIZE];
static unsigned int s_KeyQueueWriteIndex = 0;
static unsigned int s_KeyQueueReadIndex = 0;
#define KCTRL 0
#define KSHIFT 1
static unsigned char convertToDoomKey(int key) {
DBG("Key pressed: " << key);
// Explanation for the subtraction is in key_state_handler()
if (key == juce::KeyPress::leftKey - key_offset)
key = KEY_LEFTARROW;
else if (key == juce::KeyPress::rightKey - key_offset)
key = KEY_RIGHTARROW;
else if (key == juce::KeyPress::downKey - key_offset)
key = KEY_DOWNARROW;
else if (key == juce::KeyPress::upKey - key_offset)
key = KEY_UPARROW;
else if (key == juce::KeyPress::returnKey)
key = KEY_ENTER;
else if (key == juce::KeyPress::spaceKey)
key = KEY_USE;
else if (key == juce::KeyPress::escapeKey)
key = KEY_ESCAPE;
// Allow using the A key and Z key for Shift and Ctrl
// since it seems to drop the arrow key inputs on my MacBook.
else if (key == KSHIFT || key == 'a' || key == 'A')
key = KEY_RSHIFT;
else if (key == KCTRL || key == 'z' || key == 'Z')
key = KEY_FIRE;
else if (key == KEY_LALT || key == 'x' || key == 'X')
key = KEY_LALT;
key = tolower(key);
return key;
}
static void addKeyToQueue(int pressed, int keyCode) {
unsigned char key = convertToDoomKey(keyCode);
unsigned short keyData = (pressed << 8) | key;
s_KeyQueue[s_KeyQueueWriteIndex] = keyData;
s_KeyQueueWriteIndex++;
s_KeyQueueWriteIndex %= KEYQUEUE_SIZE;
}
bool DoomWindow::keyPressed(const juce::KeyPress& key, juce::Component* originatingComponent) {
return true;
}
void DoomWindow::modifierKeysChanged(const juce::ModifierKeys& modifiers) {
{
bool state = modifiers.isCtrlDown();
if (state != keyboard_state[KCTRL]) {
addKeyToQueue(state, KCTRL);
}
keyboard_state[KCTRL] = state;
}
{
bool state = modifiers.isShiftDown();
if (state != keyboard_state[KSHIFT]) {
addKeyToQueue(state, KSHIFT);
}
keyboard_state[KSHIFT] = state;
}
}
void DoomWindow::key_state_handler(int key_code) {
bool state = juce::KeyPress::isKeyCurrentlyDown(key_code);
// Subtracting 62972 is basically moving the value back into a reasonable range
// for an array that stores the keyboard state. JUCE uses values like 63234 to store
// the arrow keys.
if (key_code > 256) {
key_code -= key_offset;
}
if (state != keyboard_state[key_code]) {
addKeyToQueue(state, key_code);
}
keyboard_state[key_code] = state;
}
void DoomWindow::midi_state_handler(int key_code, bool state) {
if (key_code > 256) {
key_code -= key_offset;
}
if (state != midi_state[key_code]) {
addKeyToQueue(state, key_code);
}
midi_state[key_code] = state;
}
void DoomWindow::check_midi_input() {
using Keys = AudioPluginAudioProcessor::Keys;
static std::map<Keys, int> midi_to_key{
{Keys::leftKey, juce::KeyPress::leftKey},
{Keys::rightKey, juce::KeyPress::rightKey},
{Keys::upKey, juce::KeyPress::upKey},
{Keys::downKey, juce::KeyPress::downKey},
{Keys::strafeKey, 'x'},
{Keys::shootKey, 'z'},
{Keys::returnKey, juce::KeyPress::returnKey},
{Keys::spaceKey, juce::KeyPress::spaceKey},
{Keys::shiftKey, 'a'},
{Keys::escapeKey, juce::KeyPress::escapeKey}
};
auto& key_atomics = processorRef.m_key_atomics;
for (int i = 0; i < key_atomics.size(); i++) {
const bool state = key_atomics[i].load(std::memory_order_relaxed);
jassert(midi_to_key.count((Keys)i)); // Key is not in 'midi_to_key' map
midi_state_handler(midi_to_key[(Keys)i], state);
}
}
bool DoomWindow::keyStateChanged(bool isKeyDown, juce::Component* originatingComponent) {
key_state_handler(juce::KeyPress::leftKey);
key_state_handler(juce::KeyPress::rightKey);
key_state_handler(juce::KeyPress::upKey);
key_state_handler(juce::KeyPress::downKey);
key_state_handler(juce::KeyPress::returnKey);
key_state_handler(juce::KeyPress::spaceKey);
key_state_handler(juce::KeyPress::escapeKey);
for (int i = 65; i < 123; i++) {
key_state_handler(i);
}
return true;
}
int DG_GetKey(int* pressed, unsigned char* doomKey) {
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex) {
// key queue is empty
return 0;
} else {
unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex];
s_KeyQueueReadIndex++;
s_KeyQueueReadIndex %= KEYQUEUE_SIZE;
*pressed = keyData >> 8;
*doomKey = keyData & 0xFF;
return 1;
}
return 0;
}
void DG_SetWindowTitle(const char* title) {
(void)title;
}
//==============================================================================
void DoomWindow::paint(juce::Graphics& g) {
if (m_valid_file_selected) {
unsigned char img_test[DOOMGENERIC_RESX * DOOMGENERIC_RESY];
for (int i = 0; i < DOOMGENERIC_RESX * DOOMGENERIC_RESY; i++) {
img_test[i] = 255;
}
grabKeyboardFocus();
juce::Image::BitmapData bitmap(m_framebuffer, juce::Image::BitmapData::readWrite);
for (int i = 0; i < DOOMGENERIC_RESX; i++) {
for (int j = 0; j < DOOMGENERIC_RESY; j++) {
auto temp = DG_ScreenBuffer[j * DOOMGENERIC_RESX + i];
auto red = static_cast<uint8_t>((temp >> 16) & 0xff);
auto green = static_cast<uint8_t>((temp >> 8) & 0xff);
auto blue = static_cast<uint8_t>(temp & 0xff);
bitmap.setPixelColour(i, j, juce::Colour(red, green, blue, (uint8_t)255));
}
}
g.fillAll(juce::Colours::black);
g.drawImageWithin(m_framebuffer, 0, 0, getWidth(), getHeight(), juce::RectanglePlacement::stretchToFit);
} else {
g.setColour(juce::Colours::white);
g.drawText("You did not select a valid WAD file. Please close the Plug-In and select a valid WAD file.",
juce::Rectangle<int> { 0, 0, getWidth(), getHeight() }, juce::Justification::centred);
}
}
void DoomWindow::resized() { }