mirror of https://github.com/mgba-emu/mgba.git
Scripting: Add canvas internal scaling factor
This commit is contained in:
parent
428a29dae3
commit
55dd3e28db
|
@ -17,6 +17,7 @@ struct VideoBackend;
|
||||||
void mScriptContextAttachCanvas(struct mScriptContext* context);
|
void mScriptContextAttachCanvas(struct mScriptContext* context);
|
||||||
void mScriptCanvasUpdate(struct mScriptContext* context);
|
void mScriptCanvasUpdate(struct mScriptContext* context);
|
||||||
void mScriptCanvasUpdateBackend(struct mScriptContext* context, struct VideoBackend*);
|
void mScriptCanvasUpdateBackend(struct mScriptContext* context, struct VideoBackend*);
|
||||||
|
void mScriptCanvasSetInternalScale(struct mScriptContext* context, unsigned scale);
|
||||||
|
|
||||||
CXX_GUARD_END
|
CXX_GUARD_END
|
||||||
|
|
||||||
|
|
|
@ -1888,6 +1888,11 @@ void Window::setupOptions() {
|
||||||
videoScale->connect([this](const QVariant& value) {
|
videoScale->connect([this](const QVariant& value) {
|
||||||
if (m_display) {
|
if (m_display) {
|
||||||
m_display->setVideoScale(value.toInt());
|
m_display->setVideoScale(value.toInt());
|
||||||
|
#ifdef ENABLE_SCRIPTING
|
||||||
|
if (m_controller && m_scripting) {
|
||||||
|
m_scripting->updateVideoScale();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
|
|
@ -70,12 +70,16 @@ void ScriptingController::setController(std::shared_ptr<CoreController> controll
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
clearController();
|
clearController();
|
||||||
|
if (!controller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_controller = controller;
|
m_controller = controller;
|
||||||
CoreController::Interrupter interrupter(m_controller);
|
CoreController::Interrupter interrupter(m_controller);
|
||||||
m_controller->thread()->scriptContext = &m_scriptContext;
|
m_controller->thread()->scriptContext = &m_scriptContext;
|
||||||
if (m_controller->hasStarted()) {
|
if (m_controller->hasStarted()) {
|
||||||
mScriptContextAttachCore(&m_scriptContext, m_controller->thread()->core);
|
mScriptContextAttachCore(&m_scriptContext, m_controller->thread()->core);
|
||||||
}
|
}
|
||||||
|
updateVideoScale();
|
||||||
connect(m_controller.get(), &CoreController::stopping, this, &ScriptingController::clearController);
|
connect(m_controller.get(), &CoreController::stopping, this, &ScriptingController::clearController);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,6 +139,13 @@ void ScriptingController::clearController() {
|
||||||
m_controller.reset();
|
m_controller.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptingController::updateVideoScale() {
|
||||||
|
if (!m_controller) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mScriptCanvasSetInternalScale(&m_scriptContext, m_controller->videoScale());
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptingController::reset() {
|
void ScriptingController::reset() {
|
||||||
CoreController::Interrupter interrupter(m_controller);
|
CoreController::Interrupter interrupter(m_controller);
|
||||||
m_bufferModel->reset();
|
m_bufferModel->reset();
|
||||||
|
|
|
@ -56,6 +56,7 @@ signals:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void clearController();
|
void clearController();
|
||||||
|
void updateVideoScale();
|
||||||
void reset();
|
void reset();
|
||||||
void runCode(const QString& code);
|
void runCode(const QString& code);
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ struct mScriptCanvasLayer {
|
||||||
struct mImage* image;
|
struct mImage* image;
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
unsigned scale;
|
||||||
bool dirty;
|
bool dirty;
|
||||||
bool sizeDirty;
|
bool sizeDirty;
|
||||||
bool dimsDirty;
|
bool dimsDirty;
|
||||||
|
@ -27,6 +28,7 @@ struct mScriptCanvasContext {
|
||||||
struct VideoBackend* backend;
|
struct VideoBackend* backend;
|
||||||
struct mScriptContext* context;
|
struct mScriptContext* context;
|
||||||
uint32_t frameCbid;
|
uint32_t frameCbid;
|
||||||
|
unsigned scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
mSCRIPT_DECLARE_STRUCT(mScriptCanvasContext);
|
mSCRIPT_DECLARE_STRUCT(mScriptCanvasContext);
|
||||||
|
@ -77,6 +79,22 @@ void mScriptCanvasUpdateBackend(struct mScriptContext* context, struct VideoBack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mScriptCanvasSetInternalScale(struct mScriptContext* context, unsigned scale) {
|
||||||
|
struct mScriptValue* value = mScriptContextGetGlobal(context, "canvas");
|
||||||
|
if (!value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct mScriptCanvasContext* canvas = value->value.opaque;
|
||||||
|
if (scale < 1) {
|
||||||
|
scale = 1;
|
||||||
|
}
|
||||||
|
canvas->scale = scale;
|
||||||
|
size_t i;
|
||||||
|
for (i = 0; i < VIDEO_LAYER_OVERLAY_COUNT; ++i) {
|
||||||
|
canvas->overlays[i].scale = scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void _mScriptCanvasUpdate(struct mScriptCanvasContext* canvas) {
|
static void _mScriptCanvasUpdate(struct mScriptCanvasContext* canvas) {
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < VIDEO_LAYER_OVERLAY_COUNT; ++i) {
|
for (i = 0; i < VIDEO_LAYER_OVERLAY_COUNT; ++i) {
|
||||||
|
@ -90,7 +108,7 @@ static unsigned _mScriptCanvasWidth(struct mScriptCanvasContext* canvas) {
|
||||||
}
|
}
|
||||||
unsigned w, h;
|
unsigned w, h;
|
||||||
VideoBackendGetFrameSize(canvas->backend, &w, &h);
|
VideoBackendGetFrameSize(canvas->backend, &w, &h);
|
||||||
return w;
|
return w / canvas->scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned _mScriptCanvasHeight(struct mScriptCanvasContext* canvas) {
|
static unsigned _mScriptCanvasHeight(struct mScriptCanvasContext* canvas) {
|
||||||
|
@ -99,7 +117,7 @@ static unsigned _mScriptCanvasHeight(struct mScriptCanvasContext* canvas) {
|
||||||
}
|
}
|
||||||
unsigned w, h;
|
unsigned w, h;
|
||||||
VideoBackendGetFrameSize(canvas->backend, &w, &h);
|
VideoBackendGetFrameSize(canvas->backend, &w, &h);
|
||||||
return h;
|
return h / canvas->scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _mScriptCanvasScreenWidth(struct mScriptCanvasContext* canvas) {
|
static int _mScriptCanvasScreenWidth(struct mScriptCanvasContext* canvas) {
|
||||||
|
@ -108,7 +126,7 @@ static int _mScriptCanvasScreenWidth(struct mScriptCanvasContext* canvas) {
|
||||||
}
|
}
|
||||||
struct mRectangle dims;
|
struct mRectangle dims;
|
||||||
canvas->backend->layerDimensions(canvas->backend, VIDEO_LAYER_IMAGE, &dims);
|
canvas->backend->layerDimensions(canvas->backend, VIDEO_LAYER_IMAGE, &dims);
|
||||||
return dims.width;
|
return dims.width / canvas->scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _mScriptCanvasScreenHeight(struct mScriptCanvasContext* canvas) {
|
static int _mScriptCanvasScreenHeight(struct mScriptCanvasContext* canvas) {
|
||||||
|
@ -117,7 +135,7 @@ static int _mScriptCanvasScreenHeight(struct mScriptCanvasContext* canvas) {
|
||||||
}
|
}
|
||||||
struct mRectangle dims;
|
struct mRectangle dims;
|
||||||
canvas->backend->layerDimensions(canvas->backend, VIDEO_LAYER_IMAGE, &dims);
|
canvas->backend->layerDimensions(canvas->backend, VIDEO_LAYER_IMAGE, &dims);
|
||||||
return dims.height;
|
return dims.height / canvas->scale;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mScriptCanvasUpdate(struct mScriptContext* context) {
|
void mScriptCanvasUpdate(struct mScriptContext* context) {
|
||||||
|
@ -149,6 +167,7 @@ static struct mScriptValue* mScriptCanvasLayerCreate(struct mScriptCanvasContext
|
||||||
layer->dimsDirty = true;
|
layer->dimsDirty = true;
|
||||||
layer->sizeDirty = true;
|
layer->sizeDirty = true;
|
||||||
layer->contentsDirty = true;
|
layer->contentsDirty = true;
|
||||||
|
layer->scale = context->scale;
|
||||||
mScriptCanvasLayerUpdate(layer);
|
mScriptCanvasLayerUpdate(layer);
|
||||||
|
|
||||||
struct mScriptValue* value = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCanvasLayer));
|
struct mScriptValue* value = mScriptValueAlloc(mSCRIPT_TYPE_MS_S(mScriptCanvasLayer));
|
||||||
|
@ -180,10 +199,10 @@ static void mScriptCanvasLayerUpdate(struct mScriptCanvasLayer* layer) {
|
||||||
}
|
}
|
||||||
if (layer->dimsDirty) {
|
if (layer->dimsDirty) {
|
||||||
struct mRectangle frame = {
|
struct mRectangle frame = {
|
||||||
.x = layer->x,
|
.x = layer->x * layer->scale,
|
||||||
.y = layer->y,
|
.y = layer->y * layer->scale,
|
||||||
.width = layer->image->width,
|
.width = layer->image->width * layer->scale,
|
||||||
.height = layer->image->height,
|
.height = layer->image->height * layer->scale,
|
||||||
};
|
};
|
||||||
backend->setLayerDimensions(backend, layer->layer, &frame);
|
backend->setLayerDimensions(backend, layer->layer, &frame);
|
||||||
layer->dimsDirty = false;
|
layer->dimsDirty = false;
|
||||||
|
@ -210,6 +229,7 @@ static void mScriptCanvasLayerInvalidate(struct mScriptCanvasLayer* layer) {
|
||||||
|
|
||||||
void mScriptContextAttachCanvas(struct mScriptContext* context) {
|
void mScriptContextAttachCanvas(struct mScriptContext* context) {
|
||||||
struct mScriptCanvasContext* canvas = calloc(1, sizeof(*canvas));
|
struct mScriptCanvasContext* canvas = calloc(1, sizeof(*canvas));
|
||||||
|
canvas->scale = 1;
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < VIDEO_LAYER_OVERLAY_COUNT; ++i) {
|
for (i = 0; i < VIDEO_LAYER_OVERLAY_COUNT; ++i) {
|
||||||
canvas->overlays[i].layer = VIDEO_LAYER_OVERLAY0 + i;
|
canvas->overlays[i].layer = VIDEO_LAYER_OVERLAY0 + i;
|
||||||
|
|
Loading…
Reference in New Issue