RobLoach / raylib-cpp

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

Can't create class from raylib-cpp #198

Open TokyoSU opened 2 years ago

TokyoSU commented 2 years ago

Pretty all class of raylib-cpp have no constructor allowing that:

class ObjectName
{
public:
    ObjectName();
    ~ObjectName();

    void Load(const char* path);
    void Draw(raylib::Vector3 pos);

private:
    raylib::Model model;
};

So it's impossible to create class like this because a error is thrown saying Model have no default constructor !

RobLoach commented 2 years ago

A few options here...

  1. Use raylib::Model* model; instead... construct and delete at ObjectName destruction
  2. Pass const char* path in as part of the ObjectName constructor

I'm not sure how I feel about constructing the object without loading the associated data. Could run into Unload errors... Worth considering though!

TokyoSU commented 2 years ago
  1. Well creating a pointer need allocating via RL_MALLOC() im using mimalloc so its probably a good option.
  2. Can't, because i wont use pointer for model, its overkill :x im planning to do a sceneManager, ive changed the raylib-cpp model.hpp file with a default constructor and it work fine :) im just calling Load(path) manually since the ObjectName class wont be deleted until the game exit, or if i call it myself.
RobLoach commented 2 years ago

Mind submitting a Pull Request? Others may be interested in it too. I'd love to see what you're proposing.

TokyoSU commented 2 years ago

Okay, just need to test more about it, for now, ive not crashed when using this method :) The think is Texture use a default constructor that init a default texture, but i want to load it, i will technicaly have 2 texture for 1 define from a class xD

TokyoSU commented 2 years ago

For this one, it will need some time since i need to check each class and found a way to move some default constructor to function, also testing it to see if it work :)

RobLoach commented 2 years ago

Quick thought... Would an initializer list for this help here? https://docs.microsoft.com/en-us/cpp/cpp/constructors-cpp?view=msvc-170#member_init_list

class ObjectName
{
public:
    ObjectName(const std:string& modelFile);
    ~ObjectName();

    void Load(const char* path);
    void Draw(raylib::Vector3 pos);

private:
    raylib::Model model;
};

ObjectName(const std::string& modelFile) : model(modelFile) {
   // Do other things.
}

A child property will need to know how to construct itself if you're creating the parent.

TokyoSU commented 2 years ago

add a default one ObjectName() so it could be used within other class, else it would just throw a error about not being initialised properly with ObjectName(string), also load the model in the Load() function and use it in ObjectName(string) to avoid problem with it, since you can set the error check only in Load(), also the modder/coder with be able to use Load() manually in custom class. example:

ObjectName(const std:string& modelFile)
{
    Load(modelFile);
}
Load(path)
{
if (File.Exist(path))
    model.Load(path);
else
    Log("File not exist");
}

or something like that. (maybe the model class already check if file path exist so it would be weird to check 2 time for it)