-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
126 lines (111 loc) · 3.11 KB
/
mainwindow.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
#include "mainwindow.h"
#include <QDebug>
#include <QtMath>
#include <cmath>
#include <iostream>
#include "core/cpu.h"
#include "processorThread.h"
#include "qtimer.h"
#include "settingsdialog.h"
#include "ui_mainwindow.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_mixer.h>
#include <QAudioOutput>
#include <QBuffer>
#include <QFileDialog>
#include <QKeyEvent>
#define SAMPLE_RATE 44100
#define FREQ_CONST ((2.0 * M_PI) / SAMPLE_RATE)
void MainWindow::onUpdateGUIDisplay(QImage image)
{
QPixmap img = QPixmap::fromImage(image);
ui->display->setPixmap(img.scaled(ui->display->width(), ui->display->height(), Qt::KeepAspectRatio));
}
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
settings = new QSettings("../settings", QSettings::IniFormat, this);
processorThread = new ProcessorThread(this);
processorThread->syncSettings(settings);
connect(processorThread,
SIGNAL(updateGUIDisplay(QImage)),
SLOT(onUpdateGUIDisplay(QImage)));
}
MainWindow::~MainWindow()
{
delete ui;
delete settings;
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
int key = event->key();
processorThread->keyPressed(key);
}
void MainWindow::keyReleaseEvent(QKeyEvent* event)
{
int key = event->key();
processorThread->keyUp(key);
}
bool MainWindow::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent* key = static_cast<QKeyEvent*>(event);
if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return)) {
processorThread->keyPressed(Qt::Key_Return);
} else {
return QObject::eventFilter(obj, event);
}
return true;
} else {
return QObject::eventFilter(obj, event);
}
return false;
}
void MainWindow::on_actionOpen_ROM_triggered()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilter(tr("Gameboy ROM (*.gb)"));
dialog.setViewMode(QFileDialog::Detail);
if (!dialog.exec()) {
return;
}
QStringList fileName = dialog.selectedFiles();
std::cout << fileName[0].toStdString() << std::endl;
processorThread->stop();
processorThread->setRom(fileName[0].toStdString());
processorThread->start();
}
void MainWindow::on_actionKey_Mapping_triggered()
{
SettingsDialog* dialog = new SettingsDialog(this);
dialog->setSettings(settings);
connect(dialog, SIGNAL(accepted()), SLOT(syncSettings()));
dialog->open();
}
void MainWindow::syncSettings()
{
cout << "Syncing settings " << endl;
processorThread->syncSettings(settings);
}
short audiodata[4096];
volatile bool needsData = true; // poor mans semaphore
void callback(void* userdata, Uint8* stream, int len)
{
printf("%p,%d\n", stream, len);
while (needsData)
; // yay
short* snd = reinterpret_cast<short*>(stream);
len /= sizeof(*snd);
for (int i = 0; i < len; i++) {
snd[i] = audiodata[i];
}
needsData = true;
}
void MainWindow::on_actionSound_triggered()
{
SDL_AudioSpec desiredSpec;
}