Hello,
I’m trying to see if there is shorthand syntax for initializing the members of an SDL_Rect struct, from a function. The approach I’m using now does work, but it would go a long way in organizing my project if I can figure this out. Currently, I’m doing this:
struct Example {
SDL_Rect bg_SRC;
} example;
void initExample()
{
example.bg_SRC.x = 100;
example.bg_SRC.y = 100;
example.bg_SRC.w = 100;
example.bg_SRC.h = 100;
}
This compiles and works fine, but is lengthy. The following will compile and run with no errors from ndk-build, but doesn’t initialize the members:
void initExample()
{
struct Example bg_SRC = { .x = 100, .y = 100, .w = 100, .h = 100 };
}
If anyone with experience with this could make a suggestion, that would awesome! Thank you so much.
Also, should probably mention this is a C project for Android, and follows the build systems provided by the SDL team.
Thanks!
obeah
August 5, 2021, 12:54am
2
struct SDL_Rect new_rect(int x, int y, int w, int h)
{
struct SDL_Rect r = {.x = x, .y = y, .w = w, .h = h};
return r;
}
maybe something like this?
С++
class AkkordPoint : public SDL_Point
{
public:
constexpr AkkordPoint() : SDL_Point() { x = y = 0; };
constexpr AkkordPoint(int X, int Y) : SDL_Point() { x = X; y = Y; };
constexpr AkkordPoint(const AkkordPoint& Point) : SDL_Point() { x = Point.x; y = Point.y; };
bool operator== (const AkkordPoint& Point) const { return Point.x == x && Point.y == y; };
bool operator!= (const AkkordPoint& Point) const { return !(Point == *this); };
AkkordPoint& SetX(int X) { x = X; return *this; }
AkkordPoint& SetY(int Y) { y = Y; return *this; }
int GetX() const { return x; }
int GetY() const { return y; }
};
class AkkordRect : public SDL_Rect
{
public:
constexpr AkkordRect() : SDL_Rect() { x = y = w = h = 0; };
constexpr AkkordRect(const AkkordRect& Rect) : SDL_Rect() { x = Rect.x; y = Rect.y; w = Rect.w; h = Rect.h; };
constexpr AkkordRect(int X, int Y, int W, int H) : SDL_Rect() { x = X; y = Y; w = W; h = H; };
constexpr AkkordRect(const AkkordPoint& Point1, const AkkordPoint& Point2) : SDL_Rect() { x = Point1.x; y = Point1.y; w = Point2.x; h = Point2.y; };
AkkordRect& SetW(int W) { w = W; return *this; };
AkkordRect& SetH(int H) { h = H; return *this; };
AkkordRect& SetX(int X) { x = X; return *this; };
AkkordRect& SetY(int Y) { y = Y; return *this; };
int GetW() const { return w; };
int GetH() const { return h; };
int GetX() const { return x; };
int GetY() const { return y; };
AkkordRect& SetPosition(const AkkordPoint& Position) { x = Position.GetX(); y = Position.GetY(); return *this; };
AkkordRect& SetPosition(int X, int Y) { x = X; y = Y; return *this; };
AkkordRect& SetSize(const AkkordPoint& Size) { w = Size.GetX(); h = Size.GetY(); return *this; };
AkkordRect& SetSize(int W, int H) { w = W; h = H; return *this; };
AkkordPoint GetPosition() const { return AkkordPoint(x, y); }
AkkordPoint GetSize() const { return AkkordPoint(w, h); }
bool operator== (const AkkordRect& Rect) const { return Rect.x == x && Rect.y == y && Rect.w == w && Rect.h == h; };
bool operator!= (const AkkordRect& Rect) const { return !(Rect == *this); };
};
1 Like