I am making a c++ sdl game engine, help me solve some problems I am having. I am trying…
- User
I am making a c++ sdl game engine, help me solve some problems I am having. I am trying to do a template function but it is giving me errors:
Renderer.cpp: In member function ‘void Renderer::DrawRect(const Rect&, const Color&)’: Renderer.cpp:155:24: error: expected primary-expression before ‘{’ token 155 | WithColor(color, & { | ^
template void Renderer::WithColor(const Color& color, DrawFunction draw) { Color oldColor = GetDrawColor(); SetDrawColor(color);
try { draw(); } catch (const std::runtime_error& e) { SetDrawColor(oldColor); throw; }
SetDrawColor(oldColor); }
void Renderer::DrawRect(const Rect& rect, const Color& color) { WithColor(color, & { // Error pointing here DrawRect(rect); }); }
- Assistant
It looks like the issue is with the syntax used when you are calling
WithColorfromDrawRect. In C++, lambda expressions are used to create inline functions, and it seems that you are trying to use a lambda as yourDrawFunctionin theWithColorcall.Here's how you can correct it:
void Renderer::DrawRect(const Rect& rect, const Color& color) { WithColor(color, [&]() { // use a lambda to capture local variables DrawRect(rect); }); }Instead of
& {you should use[&]() {to start a lambda expression. The square brackets is the capture clause, where you can specify which outside variables should be available inside the lambda. I have used to capture the local variables by reference, including this pointer implicitly, so that you can call member functions and variables directly inside the lambda. - User
It works now, and no it won't call it recursively since I have two methods, one with Color and one without, so the color one is calling the one without but not viceversa.
Now I have every drawing and filling methods with Color, should I add the Blending too as a parameter to change the blending on the fly or should I keep it the same way as the original SDL were you have to define it before drawing?
- Assistant
- User
I would opt for a flexible approach but that would mean adding 10+ new methods to what I already have, for example these are my rect drawing methods: void DrawRect(const Rect& rect); void DrawRect(const Rect& rect, const Color& color); void DrawRects(const std::vector& rects); void DrawRects(const std::vector& rects, const Color& color); void DrawRects(const Rect rects[], int count); void DrawRects(const Rect rects[], int count, const Color& color); This would mean for every single one with Color as extra, I have to add the Blend extra parameter so I will end up with 3 extra methods for every drawing, this repeats for Points, Lines, Circles, etc.
- Assistant
- User
I think I am leaning to the Stateful Renderer which is the way I already have, so I don't have to do anything, but I am thinking about adding the blending only to the fill methods, does the methods SDL_RenderDrawRect, SDL_RenderDrawPoint, SDL_RenderDrawLine, etc. use the blending modes or the fill methods only?
- Assistant
In SDL, blending modes affect both filled shapes and outline shapes (i.e., both
SDL_RenderFillRectandSDL_RenderDrawRect, as well as other draw and fill functions). Basically, any rendering operation that changes pixels on the rendering target will be affected by the current blending mode.The blending mode in SDL applies to texture rendering and shape rendering—it dictates how the rendered content will blend with the existing content of the rendering target (like a window, texture, or renderer). For example, when you draw lines, points, rectangles, or filled shapes, if the blending mode is set to , the colors will blend with what's already in the rendering target based on alpha values.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.