SDL_Renderer

Hello all, I’ve been trying to implement a mutli-class system using SDL2
but I’ve run into a bit of a brick wall using SDL2’s renderer, window,
and texture structures and functions (using C++). What I’m trying to do is have two
seperate classes Entity and Window both share the renderer (as I’ve
assumed this is how the renderer is supposed to be used more or less).
I’ve tried setting the renderer globally and have them inherited to both
aEntity and Window and then use get functions for
these but I would end up with either multiple windows or entities. How
could/should I use renderer? (Sorry if my question is vague in detail or
if I sound like I should read a book on c++ and classes 101).

Example of a function of Entity:

int Entity::LoadSprite(const char *File)
{
opt = IMG_LoadTexture(`RENDERER NEEDED, File);

if (&opt == NULL) {
    std::cout << "Could not load sprite\n";
    std::cout << SDL_GetError() << "\n";
    return (-1);
}
else if (&opt != NULL) {
    std::cout << "Sprite " << File << " loaded\n";
    std::cout << SDL_GetError() << "\n";
}
return 0;

}

void Entity::Draw()
{
SDL_RenderPresent(RENDERER NEEDED);
}

I thought renderers were tied to the window (you need a separate
renderer for each window, since they’re indeed separate rendering
contexts).

Given the above, I’d say you should make the renderer part of the
window class itself (maybe calling it “display” or something like that
would be more appropriate?). Then entities rely on the window.

2013/11/8 Alberto Corona

Example of a function of Entity:

int Entity::LoadSprite(const char *File)
{
opt = IMG_LoadTexture(`RENDERER NEEDED, File);

if (&opt == NULL) {
    std::cout << "Could not load sprite\n";
    std::cout << SDL_GetError() << "\n";
    return (-1);
}
else if (&opt != NULL) {
    std::cout << "Sprite " << File << " loaded\n";
    std::cout << SDL_GetError() << "\n";
}
return 0;

}

void Entity::Draw()
{
SDL_RenderPresent(RENDERER NEEDED);
}

Can’t you just pass the renderer as a function parameter wherever
needed?

I’m not sure if I understand your problem, but I’ll try. You could just
pass the renderer as a parameter to any function that needs it or what I’ve
done before when I had different classes which need to draw stuff to the
same window is give each class a static pointer to SDL_Renderer and then I
initialize the class before using it. Something like this:

class CSprite
{
public:
CSprite(int Type);
~CSprite(void);

void SetPosition(int X, int Y);
void Draw();
void Draw(SDL_Rect * Rect);
static void InitSprites(SDL_Renderer * Renderer, char * FileName, int
TileSize);

protected:
SDL_Rect mRectSrc;
SDL_Rect mRectDest;

static SDL_Renderer * mRenderer;
static SDL_Texture * mSpriteSheet;
static int mSheetWidth;
static int mMaxSpriteNumber;

static int mTileSize;

};

void CSprite::InitSprites(SDL_Renderer * Renderer, char * FileName, int
TileSize)
{
mRenderer = Renderer;

mSpriteSheet = IMG_LoadTexture(Renderer, FileName);
SDL_QueryTexture(mSpriteSheet, NULL, NULL, &mSheetWidth, NULL);
mTileSize = TileSize;
mMaxSpriteNumber = (mSheetWidth * mSheetWidth) / (mTileSize * mTileSize);
}

void CSprite::Draw()
{
SDL_RenderCopy(mRenderer, mSpriteSheet, &mRectSrc, &mRectDest);
}

SDL_RenderPresent() isn’t called by this class since other object may also
need to draw themselves to the window before it’s presented.On Fri, Nov 8, 2013 at 4:19 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

I thought renderers were tied to the window (you need a separate
renderer for each window, since they’re indeed separate rendering
contexts).

Given the above, I’d say you should make the renderer part of the
window class itself (maybe calling it “display” or something like that
would be more appropriate?). Then entities rely on the window.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Message-ID: <20131108205452.GA2715 at gmail.com>
Content-Type: text/plain; charset=us-ascii

Hello all, I’ve been trying to implement a mutli-class system using SDL2
but I’ve run into a bit of a brick wall using SDL2’s renderer, window,
and texture structures and functions (using C++). What I’m trying to do is
have two
seperate classes Entity and Window both share the renderer (as I’ve
assumed this is how the renderer is supposed to be used more or less).
I’ve tried setting the renderer globally and have them inherited to both
aEntity and Window and then use get functions for
these but I would end up with either multiple windows or entities. How
could/should I use renderer? (Sorry if my question is vague in detail or
if I sound like I should read a book on c++ and classes 101).

Sounds like an OOD question more than a C++ / Classes 101 question.

Example of a function of Entity:

int Entity::LoadSprite(const char *File)
{
opt = IMG_LoadTexture(`RENDERER NEEDED, File);

if (&opt == NULL) {
    std::cout << "Could not load sprite\n";
    std::cout << SDL_GetError() << "\n";
    return (-1);
}
else if (&opt != NULL) {
    std::cout << "Sprite " << File << " loaded\n";
    std::cout << SDL_GetError() << "\n";
}
return 0;

}

void Entity::Draw()
{
SDL_RenderPresent(RENDERER NEEDED);
}

I’d suggest something more like this:

class Renderer;

class Entity
{
protected:
Renderer *rend;
SDL_texture *tex;
public:
Entity( Renderer *rend_, SDL_texture *tex_ ) : rend( rend_ ), tex( tex_ ) {};
};

class Renderer
{
protected:
Window *win;
SDL_Renderer *rend;
public:
Entity LoadSprite( const char *file )
{
SDL_texture *t = IMG_LoadTexture( rend, file );

if( t == NULL )
{
  std::cout << "Could not load sprite\n";
  std::cout << SDL_GetError() << "\n";

  throw( YourException() );
} else {
  std::cout << "Sprite " << File << " loaded\n";
  std::cout << SDL_GetError() << "\n";
}

return( Entity(  ) );

};
};

In essence, the guiding concept behind this version is “have the
things that are related to each other in the same place”. An
alternative to returning an Entity would be to return a texture
wrapper, since the graphics code wouldn’t need to know about anything
other than itself in that case.> Date: Fri, 8 Nov 2013 14:54:52 -0600

From: Alberto Corona
To: SDL at lists.libsdl.org
Subject: [SDL] SDL_Renderer

I’m not sure if I understand your problem, but I’ll try. You could just
pass the renderer as a parameter to any function that needs it or what I’ve
done before when I had different classes which need to draw stuff to the
same window is give each class a static pointer to SDL_Renderer and then I
initialize the class before using it. Something like this:

class CSprite
{
public:
CSprite(int Type);
~CSprite(void);

void SetPosition(int X, int Y);
void Draw();
void Draw(SDL_Rect * Rect);
static void InitSprites(SDL_Renderer * Renderer, char * FileName, int
TileSize);

protected:
SDL_Rect mRectSrc;
SDL_Rect mRectDest;

static SDL_Renderer * mRenderer;
static SDL_Texture * mSpriteSheet;
static int mSheetWidth;
static int mMaxSpriteNumber;

static int mTileSize;

};

void CSprite::InitSprites(SDL_Renderer * Renderer, char * FileName, int
TileSize)
{
mRenderer = Renderer;

mSpriteSheet = IMG_LoadTexture(Renderer, FileName);
SDL_QueryTexture(mSpriteSheet, NULL, NULL, &mSheetWidth, NULL);
mTileSize = TileSize;
mMaxSpriteNumber = (mSheetWidth * mSheetWidth) / (mTileSize * mTileSize);
}

void CSprite::Draw()
{
SDL_RenderCopy(mRenderer, mSpriteSheet, &mRectSrc, &mRectDest);
}

SDL_RenderPresent() isn’t called by this class since other object may also
need to draw themselves to the window before it’s presented.

I thought renderers were tied to the window (you need a separate
renderer for each window, since they’re indeed separate rendering
contexts).

Given the above, I’d say you should make the renderer part of the
window class itself (maybe calling it “display” or something like that
would be more appropriate?). Then entities rely on the window.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

What you described is exactly what I’m trying to do. I don’t understand
how the class you’ve written would inherit renderer from the window
class though, which is really the root of my problem.On Fri, Nov 08, 2013 at 04:50:29PM -0500, Mitch Crane wrote:

On Fri, Nov 8, 2013 at 4:19 PM, Sik the hedgehog <sik.the.hedgehog at gmail.com wrote:

Message-ID: <20131108205452.GA2715 at gmail.com>
Content-Type: text/plain; charset=us-ascii

Hello all, I’ve been trying to implement a mutli-class system using SDL2
but I’ve run into a bit of a brick wall using SDL2’s renderer, window,
and texture structures and functions (using C++). What I’m trying to do is
have two
seperate classes Entity and Window both share the renderer (as I’ve
assumed this is how the renderer is supposed to be used more or less).
I’ve tried setting the renderer globally and have them inherited to both
aEntity and Window and then use get functions for
these but I would end up with either multiple windows or entities. How
could/should I use renderer? (Sorry if my question is vague in detail or
if I sound like I should read a book on c++ and classes 101).

Sounds like an OOD question more than a C++ / Classes 101 question.

Example of a function of Entity:

int Entity::LoadSprite(const char *File)
{
opt = IMG_LoadTexture(`RENDERER NEEDED, File);

if (&opt == NULL) {
    std::cout << "Could not load sprite\n";
    std::cout << SDL_GetError() << "\n";
    return (-1);
}
else if (&opt != NULL) {
    std::cout << "Sprite " << File << " loaded\n";
    std::cout << SDL_GetError() << "\n";
}
return 0;

}

void Entity::Draw()
{
SDL_RenderPresent(RENDERER NEEDED);
}

I’d suggest something more like this:

class Renderer;

class Entity
{
protected:
Renderer *rend;
SDL_texture *tex;
public:
Entity( Renderer *rend_, SDL_texture *tex_ ) : rend( rend_ ), tex( tex_ ) {};
};

class Renderer
{
protected:
Window *win;
SDL_Renderer *rend;
public:
Entity LoadSprite( const char *file )
{
SDL_texture *t = IMG_LoadTexture( rend, file );

if( t == NULL )
{
  std::cout << "Could not load sprite\n";
  std::cout << SDL_GetError() << "\n";

  throw( YourException() );
} else {
  std::cout << "Sprite " << File << " loaded\n";
  std::cout << SDL_GetError() << "\n";
}

return( Entity(  ) );

};
};

In essence, the guiding concept behind this version is “have the
things that are related to each other in the same place”. An
alternative to returning an Entity would be to return a texture
wrapper, since the graphics code wouldn’t need to know about anything
other than itself in that case.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You’re right, it is a bit more of a OOD question. What you’re suggesting
though would be having both classes too interdependent. I’m trying to
keep them as seperate as possible with only the renderer being what they
share. A few others have made some other suggestions, having the
renderer as a parameter for a function. Which I’ve tried to do, but as I
said I had an issue setting the Renderer to private and sharing it with
Entity, which wouldn’t let me set the rendere as a parameter to a function.On Fri, Nov 08, 2013 at 09:32:31PM -0600, Jared Maddox wrote:

Date: Fri, 8 Nov 2013 14:54:52 -0600
From: Alberto Corona
To: SDL at lists.libsdl.org
Subject: [SDL] SDL_Renderer