Browse Source

Platform: port GlfwApplication away from std::string.

Co-authored-by: Squareys <squareys@googlemail.com>
ktx1-detection
Vladimír Vondruš 4 years ago
parent
commit
b9459d3669
  1. 35
      src/Magnum/Platform/GlfwApplication.cpp
  2. 39
      src/Magnum/Platform/GlfwApplication.h
  3. 7
      src/Magnum/Platform/Test/GlfwApplicationTest.cpp

35
src/Magnum/Platform/GlfwApplication.cpp

@ -32,7 +32,6 @@
#include <Corrade/Containers/Array.h> #include <Corrade/Containers/Array.h>
#include <Corrade/Containers/StridedArrayView.h> #include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/Utility/Arguments.h> #include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/String.h>
#include <Corrade/Utility/Unicode.h> #include <Corrade/Utility/Unicode.h>
#include "Magnum/ImageView.h" #include "Magnum/ImageView.h"
@ -101,19 +100,19 @@ GlfwApplication::GlfwApplication(const Arguments& arguments, NoCreateT):
/* Save command-line arguments */ /* Save command-line arguments */
if(args.value("log") == "verbose") _verboseLog = true; if(args.value("log") == "verbose") _verboseLog = true;
const std::string dpiScaling = args.value("dpi-scaling"); const Containers::StringView dpiScaling = args.value<Containers::StringView>("dpi-scaling");
if(dpiScaling == "default") if(dpiScaling == "default"_s)
_commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Default; _commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Default;
#ifdef CORRADE_TARGET_APPLE #ifdef CORRADE_TARGET_APPLE
else if(dpiScaling == "framebuffer") else if(dpiScaling == "framebuffer"_s)
_commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Framebuffer; _commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Framebuffer;
#else #else
else if(dpiScaling == "virtual") else if(dpiScaling == "virtual"_s)
_commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Virtual; _commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Virtual;
else if(dpiScaling == "physical") else if(dpiScaling == "physical"_s)
_commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Physical; _commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Physical;
#endif #endif
else if(dpiScaling.find_first_of(" \t\n") != std::string::npos) else if(dpiScaling.containsAny(" \t\n"_s))
_commandLineDpiScaling = args.value<Vector2>("dpi-scaling"); _commandLineDpiScaling = args.value<Vector2>("dpi-scaling");
else else
_commandLineDpiScaling = Vector2{args.value<Float>("dpi-scaling")}; _commandLineDpiScaling = Vector2{args.value<Float>("dpi-scaling")};
@ -243,8 +242,8 @@ Vector2 GlfwApplication::dpiScaling(const Configuration& configuration) {
#endif #endif
} }
void GlfwApplication::setWindowTitle(const std::string& title) { void GlfwApplication::setWindowTitle(const Containers::StringView title) {
glfwSetWindowTitle(_window, title.data()); glfwSetWindowTitle(_window, Containers::String::nullTerminatedView(title).data());
} }
#if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302
@ -348,7 +347,8 @@ bool GlfwApplication::tryCreate(const Configuration& configuration) {
#endif #endif
/* Create the window */ /* Create the window */
_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().c_str(), monitor, nullptr); CORRADE_INTERNAL_ASSERT(configuration.title().flags() & Containers::StringViewFlag::NullTerminated);
_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().data(), monitor, nullptr);
if(!_window) { if(!_window) {
Error() << "Platform::GlfwApplication::tryCreate(): cannot create window"; Error() << "Platform::GlfwApplication::tryCreate(): cannot create window";
glfwTerminate(); glfwTerminate();
@ -505,7 +505,8 @@ bool GlfwApplication::tryCreate(const Configuration& configuration, const GLConf
glfwWindowHint(GLFW_VISIBLE, false); glfwWindowHint(GLFW_VISIBLE, false);
else if(_verboseLog) else if(_verboseLog)
Warning{} << "Platform::GlfwApplication: Wayland detected, GL context has to be created with the window visible and may cause flicker on startup"; Warning{} << "Platform::GlfwApplication: Wayland detected, GL context has to be created with the window visible and may cause flicker on startup";
if((_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().c_str(), monitor, nullptr))) CORRADE_INTERNAL_ASSERT(configuration.title().flags() & Containers::StringViewFlag::NullTerminated);
if((_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().data(), monitor, nullptr)))
glfwMakeContextCurrent(_window); glfwMakeContextCurrent(_window);
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
@ -544,7 +545,8 @@ bool GlfwApplication::tryCreate(const Configuration& configuration, const GLConf
just 2.1) and I assume on others as well. */ just 2.1) and I assume on others as well. */
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, false); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, false);
_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().c_str(), monitor, nullptr); CORRADE_INTERNAL_ASSERT(configuration.title().flags() & Containers::StringViewFlag::NullTerminated);
_window = glfwCreateWindow(scaledWindowSize.x(), scaledWindowSize.y(), configuration.title().data(), monitor, nullptr);
} }
#endif #endif
@ -901,7 +903,7 @@ GlfwApplication::GLConfiguration::~GLConfiguration() = default;
#endif #endif
GlfwApplication::Configuration::Configuration(): GlfwApplication::Configuration::Configuration():
_title{"Magnum GLFW Application"}, _title{Containers::String::nullTerminatedGlobalView("Magnum GLFW Application"_s)},
_size{800, 600}, _size{800, 600},
_windowFlags{WindowFlag::Focused}, _windowFlags{WindowFlag::Focused},
_dpiScalingPolicy{DpiScalingPolicy::Default} {} _dpiScalingPolicy{DpiScalingPolicy::Default} {}
@ -909,12 +911,11 @@ GlfwApplication::Configuration::Configuration():
GlfwApplication::Configuration::~Configuration() = default; GlfwApplication::Configuration::~Configuration() = default;
#if defined(DOXYGEN_GENERATING_OUTPUT) || GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 #if defined(DOXYGEN_GENERATING_OUTPUT) || GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302
std::string GlfwApplication::KeyEvent::keyName(const Key key) { Containers::StringView GlfwApplication::KeyEvent::keyName(const Key key) {
/* It can return null, so beware */ return glfwGetKeyName(int(key), 0);
return Utility::String::fromArray(glfwGetKeyName(int(key), 0));
} }
std::string GlfwApplication::KeyEvent::keyName() const { Containers::StringView GlfwApplication::KeyEvent::keyName() const {
return keyName(_key); return keyName(_key);
} }
#endif #endif

39
src/Magnum/Platform/GlfwApplication.h

@ -32,9 +32,9 @@
* @brief Class @ref Magnum::Platform::GlfwApplication, macro @ref MAGNUM_GLFWAPPLICATION_MAIN() * @brief Class @ref Magnum::Platform::GlfwApplication, macro @ref MAGNUM_GLFWAPPLICATION_MAIN()
*/ */
#include <string>
#include <Corrade/Containers/ArrayView.h> #include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h> /** @todo PIMPL Configuration instead? */
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/Tags.h" #include "Magnum/Tags.h"
@ -50,6 +50,11 @@
#endif #endif
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#ifdef MAGNUM_BUILD_DEPRECATED
/* Some APIs used to take or return a std::string before */
#include <Corrade/Containers/StringStl.h>
#endif
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
namespace Implementation { namespace Implementation {
@ -474,7 +479,7 @@ class GlfwApplication {
* *
* The @p title is expected to be encoded in UTF-8. * The @p title is expected to be encoded in UTF-8.
*/ */
void setWindowTitle(const std::string& title); void setWindowTitle(Containers::StringView title);
#if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 || defined(DOXYGEN_GENERATING_OUTPUT) #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 302 || defined(DOXYGEN_GENERATING_OUTPUT)
/** /**
@ -1218,8 +1223,14 @@ class GlfwApplication::Configuration {
/*implicit*/ Configuration(); /*implicit*/ Configuration();
~Configuration(); ~Configuration();
/** @brief Window title */ /**
std::string title() const { return _title; } * @brief Window title
*
* The returned view is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} and
* is valid until the next call to @ref setTitle().
*/
Containers::StringView title() const { return _title; }
/** /**
* @brief Set window title * @brief Set window title
@ -1227,8 +1238,8 @@ class GlfwApplication::Configuration {
* *
* Default is @cpp "Magnum GLFW Application" @ce. * Default is @cpp "Magnum GLFW Application" @ce.
*/ */
Configuration& setTitle(std::string title) { Configuration& setTitle(Containers::StringView title) {
_title = std::move(title); _title = Containers::String::nullTerminatedGlobalView(title);
return *this; return *this;
} }
@ -1368,7 +1379,7 @@ class GlfwApplication::Configuration {
#endif #endif
private: private:
std::string _title; Containers::String _title;
Vector2i _size; Vector2i _size;
WindowFlags _windowFlags; WindowFlags _windowFlags;
DpiScalingPolicy _dpiScalingPolicy; DpiScalingPolicy _dpiScalingPolicy;
@ -1814,11 +1825,14 @@ class GlfwApplication::KeyEvent: public GlfwApplication::InputEvent {
* *
* Human-readable localized UTF-8 name for given @p key, intended for * Human-readable localized UTF-8 name for given @p key, intended for
* displaying to the user in e.g. key binding configuration. If there * displaying to the user in e.g. key binding configuration. If there
* is no name for given key, empty string is returned. * is no name for given key, empty string is returned. The returned
* view is always @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}
* and is valid until the keyboard layout is changed or the application
* exits.
* @see @ref keyName(Key) * @see @ref keyName(Key)
* @note Supported since GLFW 3.2. * @note Supported since GLFW 3.2.
*/ */
static std::string keyName(Key key); static Containers::StringView keyName(Key key);
#endif #endif
/** @copydoc Sdl2Application::KeyEvent::key() */ /** @copydoc Sdl2Application::KeyEvent::key() */
@ -1831,11 +1845,14 @@ class GlfwApplication::KeyEvent: public GlfwApplication::InputEvent {
* Human-readable localized UTF-8 name for the key returned by * Human-readable localized UTF-8 name for the key returned by
* @ref key(), intended for displaying to the user in e.g. * @ref key(), intended for displaying to the user in e.g.
* key binding configuration. If there is no name for that key, empty * key binding configuration. If there is no name for that key, empty
* string is returned. * string is returned. The returned view is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} and
* is valid until the keyboard layout is changed or the application
* exits.
* @see @ref keyName(Key) * @see @ref keyName(Key)
* @note Supported since GLFW 3.2. * @note Supported since GLFW 3.2.
*/ */
std::string keyName() const; Containers::StringView keyName() const;
#endif #endif
/** @brief Modifiers */ /** @brief Modifiers */

7
src/Magnum/Platform/Test/GlfwApplicationTest.cpp

@ -37,6 +37,8 @@
namespace Magnum { namespace Platform { namespace Test { namespace { namespace Magnum { namespace Platform { namespace Test { namespace {
using namespace Containers::Literals;
struct GlfwApplicationTest: Platform::Application { struct GlfwApplicationTest: Platform::Application {
explicit GlfwApplicationTest(const Arguments& arguments); explicit GlfwApplicationTest(const Arguments& arguments);
@ -72,7 +74,7 @@ struct GlfwApplicationTest: Platform::Application {
stopTextInput(); stopTextInput();
} else if(event.key() == KeyEvent::Key::T) { } else if(event.key() == KeyEvent::Key::T) {
Debug{} << "setting window title"; Debug{} << "setting window title";
setWindowTitle("This is a UTF-8 Window Title™!"); setWindowTitle("This is a UTF-8 Window Title™ and it should have no exclamation mark!!"_s.exceptSuffix(2));
} else if(event.key() == KeyEvent::Key::S) { } else if(event.key() == KeyEvent::Key::S) {
Debug{} << "setting window size, which should trigger a viewport event"; Debug{} << "setting window size, which should trigger a viewport event";
setWindowSize(Vector2i{300, 200}); setWindowSize(Vector2i{300, 200});
@ -123,7 +125,8 @@ GlfwApplicationTest::GlfwApplicationTest(const Arguments& arguments): Platform::
} }
Configuration conf; Configuration conf;
conf.setWindowFlags(Configuration::WindowFlag::Resizable); conf.setTitle("Window title that should have no exclamation mark!!"_s.exceptSuffix(2))
.setWindowFlags(Configuration::WindowFlag::Resizable);
if(!args.value("dpi-scaling").empty()) if(!args.value("dpi-scaling").empty())
conf.setSize({800, 600}, args.value<Vector2>("dpi-scaling")); conf.setSize({800, 600}, args.value<Vector2>("dpi-scaling"));
if(args.isSet("borderless")) if(args.isSet("borderless"))

Loading…
Cancel
Save