Help me, is this C++ SDL based game engine code good or could it be improved? void…
- User
Help me, is this C++ SDL based game engine code good or could it be improved?
void Renderer::Render(const IDrawable& drawable, const Color& color) { Color oldColor = GetDrawColor();
if (oldColor != color) { SetDrawColor(color); } drawable.Render(*this); Present(); if (oldColor != color) { SetDrawColor(oldColor); }}
- Assistant
- User
What about this now:
Color drawColor;
void Renderer::Render(const IDrawable& drawable) { SetDrawColor(drawColor); drawable.Render(*this); Present(); }
void Renderer::Render(const IDrawable& drawable, const Color& color) { Color oldColor = GetDrawColor();
SetDrawColor(color); drawable.Render(*this); Present(); SetDrawColor(oldColor);}
void Renderer::SetDrawColor(const Color& color) { if (drawColor != color) { drawColor(color.R, color.G, color.B, color.A); SDL_SetRenderDrawColor(renderer, color.R, color.G, color.B, color.A); } }
- Assistant
- User
so you telling me creating a whole new class is more effective than the way i am doing it with 3 lines of code
- Assistant
The suggestion to use a
ScopedColorGuardclass isn't necessarily about making the code more succinct, but about making it safer and more maintainable. The original code you provided works, but the idea behind using something like a is to leverage C++'s RAII (Resource Acquisition Is Initialization) idiom to ensure that changes to the state are correctly reverted even in the presence of exceptions or early returns. - User
should my struct representing colors have const in its constructor if so do I have to make the variables const too? I expect them to change during runtime
struct Color { Color(const Uint8& red, const Uint8& green, const Uint8& blue, const Uint8& alpha); bool operator==(const Color& other) const; bool operator!=(const Color& other) const; bool Equals(const Color& other) const;
Uint8 R; Uint8 G; Uint8 B; Uint8 A;};
- Assistant
When you define a constructor for a
struct(or class) in C++, usingconstin the parameter list is a way to promise not to change those arguments within the constructor body. It does not have an effect on the actual member variables of the struct.If you expect the
Colorvalues to change during runtime, you would not make your member variables . The in the parameter list is unrelated to whether the members can change after construction. - User
Ok I am getting a weird error here:
void Renderer::SetDrawColor(const Color& color) { if (drawColor != color) { drawColor(color.R, color.G, color.B, color.A); // error is in this line: error: no match for call to '(Color) (const Uint8&,const Uint8&,const Uint8&,const Uint8&) SDL_SetRenderDrawColor(renderer, color.R, color.G, color.B, color.A); } }
- Assistant
The error you're seeing:
error: no match for call to '(Color) (const Uint8&, const Uint8&, const Uint8&, const Uint8&)indicates that you're attempting to call the
ColorobjectdrawColoras if it were a function. AsdrawColoris an instance of theColorstruct and not a function, you cannot call it like that.What you probably wanted to do is to assign the new color values to the
drawColorobject. You can do this by creating a newColorinstance or by setting its individualR,G, , and fields: - User
- Assistant
The error message you're getting is due to an attempt to default-construct a
Colorobject, but yourColorstruct doesn't have a default constructor (a constructor that takes no arguments). Your constructor requires fourUint8arguments, but yourRendererclass is trying to create a object without providing any arguments.