raylib-cpp icon indicating copy to clipboard operation
raylib-cpp copied to clipboard

Singleton pattern for raylib-cpp classes?

Open akiriwas opened this issue 1 year ago • 5 comments

I've been implementing my project using raylib-cpp because I am a big fan of raylib and using C++ for this project. As I've explored raylib-cpp, I'm starting to wonder if a Singleton pattern was considered for development? Several classes, such as Window, Keyboard or AudioStream are only ever intended to be instantiated once, given that they then pass on calls. Other ones wouldn't make sense, such as Gamepad since it can support multiple Gamepads (and thus multiple object instantiations).

Am I missing something?

akiriwas avatar Feb 11 '24 23:02 akiriwas

A singleton would make sense for those systems that can only be initialized once, for sure. If you're willing to put together a singleton wrapper around them, I'd be happy to go thorugh some testing on it.

RobLoach avatar Feb 11 '24 23:02 RobLoach

I would actually suggest to move the Keyboard and Mouse functions into a namespace instead of the class, as they only have static member functions and it only functions as a name seperator

kyomawolf avatar Feb 16 '24 17:02 kyomawolf

Speaking of global namespaces, curious how we could avoid two problems...

  1. Declaration vs implementation... Does having the straight functions (both declaration and implementation) in a .hpp file cause issues? Within a class, I understand they end up being inlineed, so it's not an issue.
  2. With Functions.hpp, we constantly see this warning. Does this cause problems?

In file included from /home/runner/work/raylib-cpp/raylib-cpp/include/raylib-cpp.hpp:42,
                 from /home/runner/work/raylib-cpp/raylib-cpp/examples/audio/audio_sound_loading.cpp:12:
/home/runner/work/raylib-cpp/raylib-cpp/include/./Functions.hpp: At global scope:
/home/runner/work/raylib-cpp/raylib-cpp/include/./Functions.hpp:335:35: warning: ‘std::vector<std::__cxx11::basic_string<char> > raylib::TextSplit(const string&, char)’ defined but not used [-Wunused-function]
  335 | RLCPPAPI std::vector<std::string> TextSplit(const std::string& text, char delimiter) {
      |                                   ^~~~~~~~~
/home/runner/work/raylib-cpp/raylib-cpp/include/./Functions.hpp:322:22: warning: ‘std::string raylib::TextInsert(const string&, const string&, int)’ defined but not used [-Wunused-function]
  322 | RLCPPAPI std::string TextInsert(const std::string& text, const std::string& insert, int position) {
      |                      ^~~~~~~~~~
/home/runner/work/raylib-cpp/raylib-cpp/include/./Functions.hpp:308:22: warning: ‘std::string raylib::TextReplace(const string&, const string&, const string&)’ defined but not used [-Wunused-function]
  308 | RLCPPAPI std::string TextReplace(const std::string& text, const std::string& replace, const std::string& by) {
      |                      ^~~~~~~~~~~
/home/runner/work/raylib-cpp/raylib-cpp/include/./Functions.hpp:170:35: warning: ‘std::vector<std::__cxx11::basic_string<char> > raylib::LoadDroppedFiles()’ defined but not used [-Wunused-function]

RobLoach avatar Feb 16 '24 17:02 RobLoach

Shouldn't cause issues, as it is a simple wrapper API. Its rather annoying if you have other warnings/error that are getting drowned by this warning spam. you can just simple disable it with a [[maybe_unused]] attribute. I will make a PR for fixing the compiler warnings.

kyomawolf avatar Feb 16 '24 19:02 kyomawolf

Thanks. I'd much prefer static/global functions over singleton :+1:

RobLoach avatar Feb 16 '24 19:02 RobLoach

Took the namespace approach here. Thanks!

RobLoach avatar Mar 09 '24 21:03 RobLoach