From b838d0de542b1208e5362c3f84c632bccd03f2d0 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 30 Sep 2017 10:39:24 -0300 Subject: [PATCH] Simplified an internal Variant constructor to be more intuitive to use. --HG-- branch : minor --- src/common/Variant.cpp | 8 ++-- src/modules/event/sdl/Event.cpp | 63 +++++++++++--------------- src/modules/graphics/wrap_Graphics.cpp | 5 +- src/modules/thread/LuaThread.cpp | 6 +-- 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/common/Variant.cpp b/src/common/Variant.cpp index 11c34a8e2..df5ac0a17 100644 --- a/src/common/Variant.cpp +++ b/src/common/Variant.cpp @@ -85,10 +85,10 @@ Variant::Variant(love::Type *udatatype, void *userdata) { if (udatatype != nullptr) { - Proxy *p = (Proxy *) userdata; - data.userdata = p->object; - if (p->object) - p->object->retain(); + love::Object *o = (love::Object *) userdata; + data.userdata = o; + if (o != nullptr) + o->retain(); } else data.userdata = userdata; diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index 9b40ddaea..83902668b 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -383,12 +383,10 @@ Message *Event::convert(const SDL_Event &e) } else { - Proxy proxy; - proxy.object = new love::filesystem::DroppedFile(e.drop.file); - proxy.type = &love::filesystem::DroppedFile::type; - vargs.emplace_back(proxy.type, &proxy); + auto *file = new love::filesystem::DroppedFile(e.drop.file); + vargs.emplace_back(&love::filesystem::DroppedFile::type, file); msg = new Message("filedropped", vargs); - proxy.object->release(); + file->release(); } } SDL_free(e.drop.file); @@ -418,7 +416,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const std::vector vargs; vargs.reserve(4); - Proxy proxy; + love::Type *joysticktype = &love::joystick::Joystick::type; + love::joystick::Joystick *stick = nullptr; love::joystick::Joystick::Hat hat; love::joystick::Joystick::GamepadButton padbutton; love::joystick::Joystick::GamepadAxis padaxis; @@ -428,12 +427,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const { case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: - proxy.type = &love::joystick::Joystick::type; - proxy.object = joymodule->getJoystickFromID(e.jbutton.which); - if (!proxy.object) + stick = joymodule->getJoystickFromID(e.jbutton.which); + if (!stick) break; - vargs.emplace_back(proxy.type, (void *) &proxy); + vargs.emplace_back(joysticktype, stick); vargs.emplace_back((double)(e.jbutton.button+1)); msg = new Message((e.type == SDL_JOYBUTTONDOWN) ? "joystickpressed" : "joystickreleased", @@ -441,12 +439,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; case SDL_JOYAXISMOTION: { - proxy.type = &love::joystick::Joystick::type; - proxy.object = joymodule->getJoystickFromID(e.jaxis.which); - if (!proxy.object) + stick = joymodule->getJoystickFromID(e.jaxis.which); + if (!stick) break; - vargs.emplace_back(proxy.type, (void *) &proxy); + vargs.emplace_back(joysticktype, stick); vargs.emplace_back((double)(e.jaxis.axis+1)); float value = joystick::Joystick::clampval(e.jaxis.value / 32768.0f); vargs.emplace_back((double) value); @@ -457,12 +454,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const if (!joystick::sdl::Joystick::getConstant(e.jhat.value, hat) || !joystick::Joystick::getConstant(hat, txt)) break; - proxy.type = &love::joystick::Joystick::type; - proxy.object = joymodule->getJoystickFromID(e.jhat.which); - if (!proxy.object) + stick = joymodule->getJoystickFromID(e.jhat.which); + if (!stick) break; - vargs.emplace_back(proxy.type, (void *) &proxy); + vargs.emplace_back(joysticktype, stick); vargs.emplace_back((double)(e.jhat.hat+1)); vargs.emplace_back(txt, strlen(txt)); msg = new Message("joystickhat", vargs); @@ -475,12 +471,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const if (!joystick::Joystick::getConstant(padbutton, txt)) break; - proxy.type = &love::joystick::Joystick::type; - proxy.object = joymodule->getJoystickFromID(e.cbutton.which); - if (!proxy.object) + stick = joymodule->getJoystickFromID(e.cbutton.which); + if (!stick) break; - vargs.emplace_back(proxy.type, (void *) &proxy); + vargs.emplace_back(joysticktype, stick); vargs.emplace_back(txt, strlen(txt)); msg = new Message(e.type == SDL_CONTROLLERBUTTONDOWN ? "gamepadpressed" : "gamepadreleased", vargs); @@ -491,13 +486,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const if (!joystick::Joystick::getConstant(padaxis, txt)) break; - proxy.type = &love::joystick::Joystick::type; - proxy.object = joymodule->getJoystickFromID(e.caxis.which); - if (!proxy.object) + stick = joymodule->getJoystickFromID(e.caxis.which); + if (!stick) break; - vargs.emplace_back(proxy.type, (void *) &proxy); - + vargs.emplace_back(joysticktype, stick); vargs.emplace_back(txt, strlen(txt)); float value = joystick::Joystick::clampval(e.caxis.value / 32768.0f); vargs.emplace_back((double) value); @@ -506,22 +499,20 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; case SDL_JOYDEVICEADDED: // jdevice.which is the joystick device index. - proxy.object = joymodule->addJoystick(e.jdevice.which); - proxy.type = &love::joystick::Joystick::type; - if (proxy.object) + stick = joymodule->addJoystick(e.jdevice.which); + if (stick) { - vargs.emplace_back(proxy.type, (void *) &proxy); + vargs.emplace_back(joysticktype, stick); msg = new Message("joystickadded", vargs); } break; case SDL_JOYDEVICEREMOVED: // jdevice.which is the joystick instance ID now. - proxy.object = joymodule->getJoystickFromID(e.jdevice.which); - proxy.type = &love::joystick::Joystick::type; - if (proxy.object) + stick = joymodule->getJoystickFromID(e.jdevice.which); + if (stick) { - joymodule->removeJoystick((joystick::Joystick *) proxy.object); - vargs.emplace_back(proxy.type, (void *) &proxy); + joymodule->removeJoystick(stick); + vargs.emplace_back(joysticktype, stick); msg = new Message("joystickremoved", vargs); } break; diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index 2980f0d23..e96a3c792 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -487,11 +487,8 @@ static void screenshotChannelCallback(const Graphics::ScreenshotInfo *info, love if (channel != nullptr) { - Proxy p; - p.type = &love::image::ImageData::type; - p.object = i; if (i != nullptr) - channel->push(Variant(p.type, &p)); + channel->push(Variant(&love::image::ImageData::type, i)); channel->release(); } diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 479aaa363..d8577cfc6 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -110,12 +110,8 @@ void LuaThread::onError() if (!eventmodule) return; - Proxy p; - p.type = &LuaThread::type; - p.object = this; - std::vector vargs = { - Variant(p.type, &p), + Variant(&LuaThread::type, this), Variant(error.c_str(), error.length()) };