In c++ and my own game engine mostly a wrapper around SDL base, I have a Rect class…
- User
In c++ and my own game engine mostly a wrapper around SDL base, I have a Rect class which is a wrapper to the SDL_Rect class, the SDL_Rect object can be obtained by using public method: const SDL_Rect& GetRect() const; This work perfectly when wrapping Renderer DrawRect like this SDL_RenderDrawRect(renderer, &rect.GetRect()), but when dealing with DrawRects, how can I do it if my method will be DrawRects(const Rect* const rect, int count) and having the renderer as a private object?
- Assistant
- User
It works but also I am thinking that I will have to do the same for the FillRects, which means will be code duplication in the first part of creating the array, and also probably going to be the same for the Lines, Circles, etc. How can I deal with this? Should I make a private method for each one, rects -> sdl_rects, circles -> sdl_circles, etc.?
- Assistant
- User
That is interesting, but how can I define which classes are SDLShapeType and which of my own custom classes are ShapeType?
- Assistant
- User
Wait, I am thinking on something different, I used const Rect* const rects to be in-line with SDL_RenderDrawRects, but SDL was made in C and I am doing C++, I know it can still do it in a C way, but shouldn't it be done in C++ using vectors for example?
- Assistant
Certainly! When working with C++, using
std::vectoris indeed a more idiomatic and safer approach compared to raw pointer arrays. Thestd::vectorclass provides benefits such as automatic memory management, easier resizing of arrays, range checking, and iterator support, which can significantly reduce the risk of bugs and memory leaks. - User
what if I want to just use arrays instead of vectors, would that be better? If it is better, how would be done with them?
- Assistant
- User
why did you change int for std::size_t? wouldn't it be better to just use int?
- Assistant
- User
OK I decide to use int and to use built-in array, so the problem is back again, how can I avoid code duplication between Drawing and Filling and between Rect, Lines, Points, etc.
void Renderer::DrawRects(const Rect rects[], int count) { SDL_Rect sdlRects[count];
for (int i = 0; i < count; ++i) { sdlRects[i] = rects[i].GetRect(); } SDL_RenderDrawRects(renderer, sdlRects, count);}
void Renderer::FillRects(const Rect rects[], int count) { SDL_Rect sdlRects[count];
for (int i = 0; i < count; ++i) { sdlRects[i] = rects[i].GetRect(); } SDL_RenderFillRects(renderer, sdlRects, count);}
- Assistant
- User
I want it to be portable to any compiler so I have to remove variable-sized array (VLA)? Then should I just use the heap instead of stack?
- Assistant
- User
OK the original drawing and fill return an int indicating if was correct or not, add that return statement and throw an exception like this:
if (operation) { throw std::runtime_error(errorMsg); }
- Assistant
If you want to handle the case where the SDL drawing operation indicates an error, you can check the return value of the SDL function and throw an exception if it's non-zero. Here's how you can modify your function to do this:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.