Skip to content

Commit 7e61d2d

Browse files
committed
OpenGL Shader configuration UI
1 parent 7a4d5ee commit 7e61d2d

13 files changed

+349
-16
lines changed

src/config.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,11 +1725,15 @@ load_gl3_shaders(void)
17251725
ini_section_t cat = ini_find_section(config, "GL3 Shaders");
17261726
char *p;
17271727
char temp[512];
1728-
int i = 0;
1728+
int i = 0, shaders = 0;
17291729
memset(temp, 0, sizeof(temp));
17301730
memset(gl3_shader_file, 0, sizeof(gl3_shader_file));
17311731

1732-
for (int i = 0; i < MAX_USER_SHADERS; i++) {
1732+
shaders = ini_section_get_int(cat, "shaders", 0);
1733+
if (shaders > MAX_USER_SHADERS)
1734+
shaders = MAX_USER_SHADERS;
1735+
1736+
for (int i = 0; i < shaders; i++) {
17331737
temp[0] = 0;
17341738
snprintf(temp, 512, "shader%d", i);
17351739
p = ini_section_get_string(cat, temp, "");
@@ -2670,8 +2674,12 @@ save_gl3_shaders(void)
26702674
int shaders = 0, i = 0;
26712675

26722676
for (i = 0; i < MAX_USER_SHADERS; i++) {
2673-
if (gl3_shader_file[i][0] == 0)
2677+
if (gl3_shader_file[i][0] == 0) {
2678+
temp[0] = 0;
2679+
snprintf(temp, 512, "shader%d", i);
2680+
ini_section_delete_var(cat, temp);
26742681
break;
2682+
}
26752683
shaders++;
26762684
}
26772685

src/include/86box/ini.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef void *ini_section_t;
3131

3232
extern ini_t ini_new(void);
3333
extern ini_t ini_read(const char *fn);
34+
extern void ini_strip_quotes(ini_t ini);
3435
extern void ini_write(ini_t ini, const char *fn);
3536
extern void ini_dump(ini_t ini);
3637
extern void ini_close(ini_t ini);
@@ -91,7 +92,7 @@ extern ini_section_t ini_find_or_create_section(ini_t ini, const char *name);
9192
extern void ini_rename_section(ini_section_t section, const char *name);
9293
extern void ini_delete_section_if_empty(ini_t ini, ini_section_t section);
9394

94-
static inline void *wx_config_load(const char *path) { return (void*) ini_read(path); }
95+
static inline void *wx_config_load(const char *path) { ini_t ini = ini_read(path); if (ini) ini_strip_quotes(ini); return (void*)ini; }
9596
static inline int wx_config_get_string(void *config, const char *name, char *dst, int size, const char *defVal) {
9697
int res = ini_has_entry(ini_find_or_create_section((ini_t)config, ""), name);
9798
char* str = ini_get_string((ini_t)config, "", name, (char*)defVal);

src/ini.c

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,8 @@ ini_delete_section_if_empty(ini_t ini, ini_section_t section)
254254
static section_t *
255255
create_section(list_t *head, const char *name)
256256
{
257-
section_t *ns = malloc(sizeof(section_t));
257+
section_t *ns = calloc(1, sizeof(section_t));
258258

259-
memset(ns, 0x00, sizeof(section_t));
260259
memcpy(ns->name, name, strlen(name) + 1);
261260
list_add(&ns->list, head);
262261

@@ -279,9 +278,8 @@ ini_find_or_create_section(ini_t ini, const char *name)
279278
static entry_t *
280279
create_entry(section_t *section, const char *name)
281280
{
282-
entry_t *ne = malloc(sizeof(entry_t));
281+
entry_t *ne = calloc(1, sizeof(entry_t));
283282

284-
memset(ne, 0x00, sizeof(entry_t));
285283
memcpy(ne->name, name, strlen(name) + 1);
286284
list_add(&ne->list, &section->entry_head);
287285

@@ -390,11 +388,8 @@ ini_read(const char *fn)
390388
if (fp == NULL)
391389
return NULL;
392390

393-
head = malloc(sizeof(list_t));
394-
memset(head, 0x00, sizeof(list_t));
395-
396-
sec = malloc(sizeof(section_t));
397-
memset(sec, 0x00, sizeof(section_t));
391+
head = calloc(1, sizeof(list_t));
392+
sec = calloc(1, sizeof(section_t));
398393

399394
list_add(&sec->list, head);
400395
if (bom)
@@ -475,7 +470,7 @@ ini_read(const char *fn)
475470
d = c;
476471

477472
/* Allocate a new variable entry.. */
478-
ne = malloc(sizeof(entry_t));
473+
ne = calloc(1, sizeof(entry_t));
479474
memset(ne, 0x00, sizeof(entry_t));
480475
memcpy(ne->name, ename, 128);
481476
wcsncpy(ne->wdata, &buff[d], sizeof_w(ne->wdata) - 1);
@@ -551,6 +546,43 @@ ini_write(ini_t ini, const char *fn)
551546
(void) fclose(fp);
552547
}
553548

549+
void
550+
ini_strip_quotes(ini_t ini)
551+
{
552+
list_t *list = (list_t *) ini;
553+
section_t *sec;
554+
555+
sec = (section_t *) list->next;
556+
557+
while (sec != NULL) {
558+
entry_t *ent;
559+
560+
ent = (entry_t *) sec->entry_head.next;
561+
while (ent != NULL) {
562+
if (ent->name[0] != '\0') {
563+
int i = 0;
564+
if (ent->wdata[0] == L'\"') {
565+
memmove(ent->wdata, &ent->wdata[1], sizeof(ent->wdata) - sizeof(wchar_t));
566+
}
567+
if (ent->wdata[wcslen(ent->wdata) - 1] == L'\"') {
568+
ent->wdata[wcslen(ent->wdata) - 1] = 0;
569+
}
570+
571+
if (ent->data[0] == '\"') {
572+
memmove(ent->data, &ent->data[1], sizeof(ent->data) - sizeof(char));
573+
}
574+
if (ent->data[strlen(ent->data) - 1] == '\"') {
575+
ent->data[strlen(ent->data) - 1] = 0;
576+
}
577+
}
578+
579+
ent = (entry_t *) ent->list.next;
580+
}
581+
582+
sec = (section_t *) sec->list.next;
583+
}
584+
}
585+
554586
ini_t
555587
ini_new(void)
556588
{

src/qt/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ add_library(ui STATIC
197197
qt_openglshadermanagerdialog.hpp
198198
qt_openglshadermanagerdialog.cpp
199199
qt_openglshadermanagerdialog.ui
200+
qt_openglshaderconfig.hpp qt_openglshaderconfig.cpp qt_openglshaderconfig.ui
200201
)
201202

202203
if(RTMIDI)

src/qt/qt_glsl_parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ glslp_t *glslp_parse(const char *f) {
255255
safe_strncpy(s, t + j, sublen);
256256
s[511 < sublen ? 511 : sublen] = 0;
257257

258+
if (s[strlen(s) - 1] == ';') s[strlen(s) - 1] = 0;
259+
258260
struct texture *tex = &glslp->textures[glslp->num_textures++];
259261

260262
strcpy(tex->name, s);

src/qt/qt_mainwindow.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,12 @@ MainWindow::changeEvent(QEvent *event)
19821982
}
19831983
}
19841984

1985+
void
1986+
MainWindow::reloadAllRenderers()
1987+
{
1988+
reload_renderers = true;
1989+
}
1990+
19851991
void
19861992
MainWindow::on_actionRenderer_options_triggered()
19871993
{
@@ -2000,6 +2006,16 @@ MainWindow::on_actionRenderer_options_triggered()
20002006
if (renderers[i] && renderers[i]->hasOptions())
20012007
renderers[i]->reloadOptions();
20022008
}
2009+
} else if (reload_renderers && ui->stackedWidget->reloadRendererOption()) {
2010+
reload_renderers = false;
2011+
ui->stackedWidget->switchRenderer(static_cast<RendererStack::Renderer>(vid_api));
2012+
if (show_second_monitors) {
2013+
for (int i = 1; i < MONITORS_NUM; i++) {
2014+
if (renderers[i] && renderers[i]->reloadRendererOption() && renderers[i]->hasOptions()) {
2015+
ui->stackedWidget->switchRenderer(static_cast<RendererStack::Renderer>(vid_api));
2016+
}
2017+
}
2018+
}
20032019
}
20042020
}
20052021
}

src/qt/qt_mainwindow.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MainWindow : public QMainWindow {
3232
QSize getRenderWidgetSize();
3333
void setSendKeyboardInput(bool enabled);
3434
void checkFullscreenHotkey();
35+
void reloadAllRenderers();
3536

3637
std::array<std::unique_ptr<RendererStack>, 8> renderers;
3738
signals:
@@ -173,6 +174,9 @@ private slots:
173174
bool fs_on_signal = false;
174175
bool fs_off_signal = false;
175176

177+
/* Reload the renderers after closing renderer options dialog. */
178+
bool reload_renderers = false;
179+
176180
friend class SpecifyDimensions;
177181
friend class ProgSettings;
178182
friend class RendererCommon;

src/qt/qt_openglshaderconfig.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include "qt_openglshaderconfig.hpp"
2+
#include "ui_qt_openglshaderconfig.h"
3+
4+
#include "qt_mainwindow.hpp"
5+
6+
extern MainWindow* main_window;
7+
8+
extern "C"
9+
{
10+
#include <86box/86box.h>
11+
#include <86box/plat.h>
12+
#include <86box/config.h>
13+
}
14+
15+
OpenGLShaderConfig::OpenGLShaderConfig(QWidget *parent, glslp_t* shader)
16+
: QDialog(parent)
17+
, ui(new Ui::OpenGLShaderConfig)
18+
{
19+
ui->setupUi(this);
20+
21+
currentShader = shader;
22+
23+
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
24+
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
25+
26+
glslp_read_shader_config(currentShader);
27+
28+
for (int i = 0; i < currentShader->num_parameters; i++) {
29+
auto spinBox = new QDoubleSpinBox;
30+
spinBox->setObjectName(currentShader->parameters[i].id);
31+
spinBox->setRange(currentShader->parameters[i].min, currentShader->parameters[i].max);
32+
spinBox->setValue(currentShader->parameters[i].value);
33+
spinBox->setSingleStep(currentShader->parameters[i].step);
34+
QFormLayout* layout = (QFormLayout*)ui->scrollAreaWidgetContents->layout();
35+
layout->addRow(currentShader->parameters[i].description, spinBox);
36+
}
37+
}
38+
39+
OpenGLShaderConfig::~OpenGLShaderConfig()
40+
{
41+
delete ui;
42+
}
43+
44+
void OpenGLShaderConfig::on_buttonBox_clicked(QAbstractButton *button)
45+
{
46+
if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole) {
47+
for (int i = 0; i < currentShader->num_parameters; i++) {
48+
QDoubleSpinBox* box = this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
49+
if (box) {
50+
box->setValue(currentShader->parameters[i].default_value);
51+
}
52+
}
53+
} else if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole) {
54+
startblit();
55+
for (int i = 0; i < currentShader->num_parameters; i++) {
56+
QDoubleSpinBox* box = this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
57+
if (box) {
58+
float val = (float)box->value();
59+
currentShader->parameters[i].value = val;
60+
}
61+
}
62+
glslp_write_shader_config(currentShader);
63+
config_save();
64+
endblit();
65+
main_window->reloadAllRenderers();
66+
}
67+
}
68+
69+
70+
void OpenGLShaderConfig::on_OpenGLShaderConfig_accepted()
71+
{
72+
startblit();
73+
for (int i = 0; i < currentShader->num_parameters; i++) {
74+
QDoubleSpinBox* box = (QDoubleSpinBox*)this->findChild<QDoubleSpinBox*>(QString(currentShader->parameters[i].id));
75+
if (box) {
76+
float val = (float)box->value();
77+
currentShader->parameters[i].value = val;
78+
}
79+
}
80+
glslp_write_shader_config(currentShader);
81+
config_save();
82+
endblit();
83+
main_window->reloadAllRenderers();
84+
}
85+

src/qt/qt_openglshaderconfig.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef QT_OPENGLSHADERCONFIG_HPP
2+
#define QT_OPENGLSHADERCONFIG_HPP
3+
4+
#include <QDialog>
5+
#include <QLabel>
6+
#include <QDoubleSpinBox>
7+
#include <QAbstractButton>
8+
9+
#include <map>
10+
#include <string>
11+
12+
extern "C"
13+
{
14+
#include <86box/qt-glslp-parser.h>
15+
}
16+
17+
namespace Ui {
18+
class OpenGLShaderConfig;
19+
}
20+
21+
class OpenGLShaderConfig : public QDialog {
22+
Q_OBJECT
23+
24+
public:
25+
explicit OpenGLShaderConfig(QWidget *parent = nullptr, glslp_t* shader = nullptr);
26+
~OpenGLShaderConfig();
27+
28+
private slots:
29+
void on_buttonBox_clicked(QAbstractButton *button);
30+
31+
void on_OpenGLShaderConfig_accepted();
32+
33+
private:
34+
Ui::OpenGLShaderConfig *ui;
35+
glslp_t* currentShader;
36+
37+
std::map<std::string, double> defaultValues;
38+
};
39+
40+
#endif // QT_OPENGLSHADERCONFIG_HPP

0 commit comments

Comments
 (0)