RobLoach / raylib-cpp

C++ Object Oriented Wrapper for raylib
https://robloach.github.io/raylib-cpp/
zlib License
584 stars 77 forks source link

Singleton pattern for raylib-cpp classes? #286

Closed akiriwas closed 3 months ago

akiriwas commented 4 months ago

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?

RobLoach commented 4 months ago

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.

kyomawolf commented 4 months ago

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

RobLoach commented 4 months ago

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 > raylib::TextSplit(const string&, char)’ defined but not used [-Wunused-function] 335 | RLCPPAPI std::vector 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 > raylib::LoadDroppedFiles()’ defined but not used [-Wunused-function]

kyomawolf commented 4 months ago

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.

RobLoach commented 4 months ago

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

RobLoach commented 3 months ago

Took the namespace approach here. Thanks!