I am making a C++ SDL based game engine, currently in the process of switching my raw…
- User
I am making a C++ SDL based game engine, currently in the process of switching my raw pointers into smart pointers. I changed my Texture class and it is compiling, but I don't know if it was done in the correct way. Get me check if everything is correct.
- My original Texture class, received as raw pointers the my cont Rect pointer and const Transform pointers and assigned directly to my own variables with a simple member initialization. Rect and Transform and my own classes that represent the Rectangle and the transformations done to the Texture. But instead of directly member initialization I had to use reset with a const_cast, This compiles but don't know if is the correct way or if I should even use a const raw pointer as a parameter or just pass the raw pointer without const.
class Texture : public IDrawable { public: explicit Texture(const Renderer& renderer, const Surface& surface, const std::string& filename = "", const Rect* srcRect = nullptr, const Rect* dstRect = nullptr, const Transform* transform = nullptr); //...
private: SDL_Texture* GetTexture() const; void SetSDLTexture(SDL_Texture* sdlTexture); std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> texture; std::unique_ptr<Rect> sourceRect; std::unique_ptr<Rect> destinationRect; std::unique_ptr<Transform> transform;}
Texture::Texture(const Renderer& renderer, const Surface& surface, const std::string& filename, const Rect* srcRect, const Rect* dstRect, const Transform* transform) : texture(nullptr, &SDL_DestroyTexture), sourceRect(nullptr), destinationRect(nullptr), transform(nullptr) { LoadFromSurface(renderer, surface, filename);
sourceRect.reset(const_cast<Rect*>(srcRect)); destinationRect.reset(const_cast<Rect*>(dstRect)); this->transform.reset(const_cast<Transform*>(transform));}
void Texture::LoadFromSurface(const Renderer& renderer, const Surface& surface, const std::string& filename) { TextureAccess::CreateTextureFromSurface(this, renderer, surface);
if (GetTexture() == nullptr) { // throws error }}
void TextureAccess::CreateTextureFromSurface(Texture* texture, const Renderer& renderer, const Surface& surface) { SDL_Texture* tex = SDL_CreateTextureFromSurface(RendererAccess::GetRenderer(renderer), SurfaceAccess::GetSurface(surface)); texture->SetSDLTexture(tex); }
- Assistant
- User
In theory, my texture class just save the Rects and the Transform but I don't think they will reused, but maybe you are right by using a shared_ptr here, I am not quite sure since I haven't started using my engine actively to know that, the only part I used it is in the Text class:
void Text::Render(Renderer& renderer) const { Surface textSurface = CreateSurface();
Texture textTexture = Texture(); textTexture.LoadFromSurface(renderer, textSurface); Rect dstRect(position, textSurface.GetSize()); textTexture.SetDestRect(&dstRect); renderer.Render(textTexture);}
What do you think I should do my approach? How should I change the Texture class to be more efficient and better in this raw pointer -> smart pointer switch
- Assistant
- User
I think I prefer the first option, since I think the ownership is needed because I have many other methods that deal with these variables, like transformations on the rectangles, etc., so this is how I would use it if I choose the first option, right?
void Text::Render(Renderer& renderer) const { //... textTexture.SetDestRect(Rect(position, textSurface.GetSize())); renderer.Render(textTexture); }
Now that deals with the Set methods, but what about the constructor? How can I change it to avoid the const_cast you said is bad?
- Assistant
- User
Can you write the doxygen documentation block for the changed constructor and adding the usage example with that code?
- Assistant
- User
wait, I found out in this example:
std::unique_ptr srcRect = std::make_unique(args…); std::unique_ptr dstRect = std::make_unique(args…); std::unique_ptr transform = std::make_unique(args…);
Texture texture(renderer, surface, filename, std::move(srcRect), std::move(dstRect), std::move(transform));
You are calling the constructor with std::move and then you do the same when initilizing the constructors members:
Texture::Texture(...) : texture(nullptr, &SDL_DestroyTexture), srcRect(std::move(srcRect)), dstRect(std::move(dstRect)), transform(std::move(transform))
Why not just create the object without using std::move, something like this?
std::unique_ptr srcRect = std::make_unique(args…); std::unique_ptr dstRect = std::make_unique(args…); std::unique_ptr transform = std::make_unique(args…);
Texture texture(renderer, surface, filename,srcRect, dstRect,transform);
- Assistant
The reason we use in the example is due to how manages ownership. enforces unique ownership of the managed object, meaning that a resource managed by a instance can be owned by only one instance at a time. This unique ownership model is enforced by deleting the copy constructor and copy assignment operator of , making it impossible to accidentally or implicitly copy a .
- User
Ahh I see, I found it weird to use std::move twice but makes sense now, the caller use std::move to not copy it by value and the callee in this case, initialize it with std::move to also not receive it by value or why?
- Assistant
- User
OK got it, now I want you to write the doxygen documentation of my Texture class and its methods, avoid the constructor you already did:
class Texture : public IDrawable { public: explicit Texture(); void LoadFromSurface(const Renderer& renderer, const Surface& surface, const std::string& filename = ""); Point GetSize() const; BlendMode::Type GetBlendMode() const; void SetBlendMode(BlendMode::Type blendMode); Color GetColorMod() const; void SetColorMod(const Color& color); uint8_t GetAlphaMod() const; void SetAlphaMod(const uint8_t& a); void SetSrcRect(const Rect& srcRect); void SetDestRect(const Rect& dstRect); void SetTransform(const Transform& transform); bool IsInitialized() const;
- Assistant
- User
Perfect, now since we are at it, make the Transform class documentation too:
struct Transform { enum class Flip { None, Horizontal, Vertical, Both };
Transform(); FPoint scale; float rotation; Transform::Flip flip;};
Take into account that the default values are no scaling, no rotation and no flipping
- Assistant
- User
Great! Now the only documentation missing is the BlendMode, take care of it, please.
struct BlendMode { enum class Type { None, Blend, Add, Mod, Mul }; };
- Assistant
- User
Can you fix the parts with the Mod and Mul, to be written better, here is the calculation to help you define them without using the others definition:
SDL_BLENDMODE_MOD = 0x00000004, /< color modulate dstRGB = srcRGB * dstRGB dstA = dstA */ SDL_BLENDMODE_MUL = 0x00000008, /< color multiply dstRGB = (srcRGB * dstRGB) + (dstRGB * (1-srcA)) dstA = dstA */
- Assistant
- User
Perfect! Really liked it, now another one that was missing was the IDrawable, that Texture and many more inherited, you know the drill:
class IDrawable { public: virtual ~IDrawable() = default;
private: virtual void Render(Renderer& renderer) const = 0;};
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.